Remove email/in-app paths and enforce sms action on create/update so price alerts match the product requirement for SMS-only delivery. Co-authored-by: Cursor <cursoragent@cursor.com>
83 lines
2.3 KiB
PHP
83 lines
2.3 KiB
PHP
<?php
|
|
namespace App\Http\Controllers\Customer;
|
|
|
|
use App\Http\Controllers\SuperController;
|
|
use App\Models\PriceTrigger;
|
|
use App\Models\Product;
|
|
use Illuminate\Http\Request;
|
|
use Symfony\Component\HttpKernel\Exception\UnprocessableEntityHttpException;
|
|
|
|
class PriceTriggerController extends SuperController
|
|
{
|
|
public function __construct(Request $request)
|
|
{
|
|
$request->request->add(['customer_id'=>auth('customer')->id()]);
|
|
$model = PriceTrigger::class;
|
|
$this->with = ['customer'];
|
|
$this->select = ['*'];
|
|
$this->search = ['*'];
|
|
parent::__construct($model, $this->with, $this->select);
|
|
}
|
|
|
|
/**
|
|
* Display a listing of the resource.
|
|
*
|
|
* @return \Illuminate\Http\JsonResponse
|
|
*/
|
|
public function index()
|
|
{
|
|
$query = PriceTrigger::where("customer_id", auth()->id());
|
|
if (PriceTrigger::hasProductIdColumn()) {
|
|
$query->with(['product:id,title']);
|
|
}
|
|
|
|
return response()->json($query->get());
|
|
}
|
|
|
|
public function store(Request $request)
|
|
{
|
|
$this->sanitizeProductId($request, true);
|
|
$request->merge(['action' => 'sms']);
|
|
|
|
return parent::store($request);
|
|
}
|
|
|
|
public function update(Request $request, $id)
|
|
{
|
|
$this->sanitizeProductId($request, false);
|
|
$request->merge(['action' => 'sms']);
|
|
|
|
return parent::update($request, $id);
|
|
}
|
|
|
|
private function sanitizeProductId(Request $request, bool $required): void
|
|
{
|
|
if (! PriceTrigger::hasProductIdColumn()) {
|
|
$request->request->remove('product_id');
|
|
|
|
return;
|
|
}
|
|
|
|
$productId = $request->input('product_id');
|
|
if ($productId === null || $productId === '') {
|
|
if ($required) {
|
|
throw new UnprocessableEntityHttpException('محصول را انتخاب کنید');
|
|
}
|
|
$request->request->remove('product_id');
|
|
|
|
return;
|
|
}
|
|
|
|
$exists = Product::query()
|
|
->where('id', (int) $productId)
|
|
->where('active', 1)
|
|
->exists();
|
|
|
|
if (! $exists) {
|
|
throw new UnprocessableEntityHttpException('محصول نامعتبر است');
|
|
}
|
|
|
|
$request->merge(['product_id' => (int) $productId]);
|
|
}
|
|
}
|