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>
This commit is contained in:
@@ -2,8 +2,10 @@
|
||||
namespace App\Http\Controllers\Customer;
|
||||
|
||||
use App\Http\Controllers\SuperController;
|
||||
use App\Models\PriceTrigger;
|
||||
use App\Models\Product;
|
||||
use Illuminate\Http\Request;
|
||||
use App\Models\PriceTrigger; // اطمینان حاصل کنید که این مدل به درستی وارد شده است
|
||||
use Symfony\Component\HttpKernel\Exception\UnprocessableEntityHttpException;
|
||||
|
||||
class PriceTriggerController extends SuperController
|
||||
{
|
||||
@@ -24,10 +26,55 @@ class PriceTriggerController extends SuperController
|
||||
*/
|
||||
public function index()
|
||||
{
|
||||
$data=PriceTrigger::where("customer_id",auth()->id())->get();
|
||||
return response()->json($data);
|
||||
$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]);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user