clqms-be/app/Models/Rule/RuleDefModel.php
mahdahar 88be3f3809 feat: add rules engine API and order-created hook
- Add /api/rules CRUD, nested actions, and expr validation

- Add rules migration, models, and RuleEngine/Expression services

- Run ORDER_CREATED rules after order create (non-blocking) and refresh tests

- Update OpenAPI tags/schemas/paths and bundled docs
2026-03-12 06:34:56 +07:00

62 lines
1.5 KiB
PHP

<?php
namespace App\Models\Rule;
use App\Models\BaseModel;
class RuleDefModel extends BaseModel
{
protected $table = 'ruledef';
protected $primaryKey = 'RuleID';
protected $allowedFields = [
'Name',
'Description',
'EventCode',
'ScopeType',
'TestSiteID',
'ConditionExpr',
'Priority',
'Active',
'CreateDate',
'StartDate',
'EndDate',
];
protected $useTimestamps = true;
protected $createdField = 'CreateDate';
protected $updatedField = 'StartDate';
protected $useSoftDeletes = true;
protected $deletedField = 'EndDate';
/**
* Fetch active rules for an event, optionally scoped.
*
* Scope behavior:
* - Always returns GLOBAL rules
* - If $testSiteID provided, also returns TESTSITE rules matching TestSiteID
*/
public function getActiveByEvent(string $eventCode, ?int $testSiteID = null): array
{
$builder = $this->where('EventCode', $eventCode)
->where('EndDate IS NULL')
->where('Active', 1)
->groupStart()
->where('ScopeType', 'GLOBAL');
if ($testSiteID !== null) {
$builder->orGroupStart()
->where('ScopeType', 'TESTSITE')
->where('TestSiteID', $testSiteID)
->groupEnd();
}
$builder->groupEnd();
return $builder
->orderBy('Priority', 'ASC')
->orderBy('RuleID', 'ASC')
->findAll();
}
}