2026-07-06 19:06:06 +03:30
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
namespace App\Http\Controllers\Admin;
|
|
|
|
|
|
|
|
|
|
use App\DB\Te\TableEngine;
|
|
|
|
|
use App\Http\Controllers\Controller;
|
|
|
|
|
use App\Models\PriceTrigger;
|
|
|
|
|
use Illuminate\Http\Request;
|
|
|
|
|
use Symfony\Component\HttpKernel\Exception\UnprocessableEntityHttpException;
|
|
|
|
|
|
|
|
|
|
class PriceTriggerController extends Controller
|
|
|
|
|
{
|
|
|
|
|
public function table(Request $request)
|
|
|
|
|
{
|
|
|
|
|
$query = PriceTrigger::query()->with('customer');
|
2026-08-18 22:23:20 +03:30
|
|
|
if (PriceTrigger::hasProductIdColumn()) {
|
|
|
|
|
$query->with('product:id,title');
|
|
|
|
|
}
|
2026-07-06 19:06:06 +03:30
|
|
|
|
|
|
|
|
TableEngine::create($query, $request->all())
|
|
|
|
|
->initSearch(['customer_id', 'target_price', 'action', 'price_condition'])
|
|
|
|
|
->select(['*'])
|
|
|
|
|
->export($query);
|
|
|
|
|
|
|
|
|
|
return $query;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function store(Request $request)
|
|
|
|
|
{
|
|
|
|
|
$data = $this->validatedPayload($request);
|
|
|
|
|
|
|
|
|
|
$trigger = PriceTrigger::create([
|
|
|
|
|
...$data,
|
|
|
|
|
'is_active' => $data['is_active'] ?? true,
|
|
|
|
|
]);
|
|
|
|
|
|
2026-08-18 22:23:20 +03:30
|
|
|
$relations = ['customer'];
|
|
|
|
|
if (PriceTrigger::hasProductIdColumn()) {
|
|
|
|
|
$relations[] = 'product:id,title';
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return response()->json($trigger->load($relations), 201);
|
2026-07-06 19:06:06 +03:30
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function update(Request $request, int $id)
|
|
|
|
|
{
|
|
|
|
|
$trigger = PriceTrigger::findOrFail($id);
|
|
|
|
|
$data = $this->validatedPayload($request, true);
|
|
|
|
|
|
|
|
|
|
$trigger->update($data);
|
|
|
|
|
|
|
|
|
|
return response()->json(true);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function toggleActive(int $id)
|
|
|
|
|
{
|
|
|
|
|
$trigger = PriceTrigger::findOrFail($id);
|
|
|
|
|
$trigger->is_active = !$trigger->is_active;
|
|
|
|
|
|
|
|
|
|
if ($trigger->is_active) {
|
|
|
|
|
$trigger->triggered_at = null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$trigger->save();
|
|
|
|
|
|
|
|
|
|
return response()->json([
|
|
|
|
|
'is_active' => (bool) $trigger->is_active,
|
|
|
|
|
]);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function destroy(int $id)
|
|
|
|
|
{
|
|
|
|
|
PriceTrigger::findOrFail($id)->delete();
|
|
|
|
|
|
|
|
|
|
return response()->json(true);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private function validatedPayload(Request $request, bool $partial = false): array
|
|
|
|
|
{
|
|
|
|
|
$rules = [
|
|
|
|
|
'customer_id' => ($partial ? 'sometimes|' : '') . 'required|integer|exists:customers,id',
|
2026-08-18 22:23:20 +03:30
|
|
|
'product_id' => ($partial ? 'sometimes|' : '') . 'required|integer|exists:products,id',
|
2026-07-06 19:06:06 +03:30
|
|
|
'target_price' => ($partial ? 'sometimes|' : '') . 'required|numeric|min:1',
|
|
|
|
|
'price_condition' => ($partial ? 'sometimes|' : '') . 'required|in:>,<',
|
|
|
|
|
'action' => ($partial ? 'sometimes|' : '') . 'required|in:sms,email',
|
|
|
|
|
'is_active' => 'sometimes|boolean',
|
|
|
|
|
];
|
|
|
|
|
|
2026-08-18 22:23:20 +03:30
|
|
|
if (! PriceTrigger::hasProductIdColumn()) {
|
|
|
|
|
unset($rules['product_id']);
|
|
|
|
|
}
|
|
|
|
|
|
2026-07-06 19:06:06 +03:30
|
|
|
$data = $request->validate($rules);
|
|
|
|
|
|
|
|
|
|
if (isset($data['price_condition']) && !in_array($data['price_condition'], ['>', '<'], true)) {
|
|
|
|
|
throw new UnprocessableEntityHttpException('شرط قیمت نامعتبر است');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return $data;
|
|
|
|
|
}
|
|
|
|
|
}
|