Files
boursecu_server/app/Http/Controllers/Customer/PriceTriggerController.php
T
mohmdsdgandCursor b0296c6e94 feat(server): evaluate price alerts per product and fix gold discharge rounding
Alerts now bind to a product_id and share one evaluator; displayed gold within a rounding step discharges the full wallet.

Co-authored-by: Cursor <cursoragent@cursor.com>
2026-08-18 22:23:20 +03:30

81 lines
2.2 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);
return parent::store($request);
}
public function update(Request $request, $id)
{
$this->sanitizeProductId($request, false);
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]);
}
}