refactor(policy): build the registries and six engines the architecture asked for
The task 09 architecture specified FieldRegistry, OperatorRegistry, six engine classes and a stored specificity. What shipped was a single PolicySchema constant list, six operators, one resolver and a specificity recomputed on every booking. Each shortcut was defensible on its own; together they left the starred risk the task itself recorded — a field can be advertised in the form and supplied by nobody, and the rule silently never matches. OperatorRegistry now holds all eleven operators. The five that were missing are real capability, not ceremony: greater_or_equal and less_or_equal make boundary rules expressible without off-by-one, not_in is the natural way to write an exclusion, between stops "18 to 65" needing two clauses, and days_since is the documented operator for "more than N days since" — until now every caller computed that by hand. between is inclusive at both ends because that is what the Persian phrasing means and what the user will type. FieldRegistry is now the single source: it builds the form schema and extracts the value, so a field that exists in one and not the other is impossible. It also declares which categories each field belongs to, which is what the closed list per category used to do separately. Adding it immediately caught its own first case — last_visit_at was advertised and supplied nowhere, so the guard now populates it and days_since has something to read. The six engines are thin on purpose. They give the call site a type — "the pricing engine" rather than "the resolver with the string pricing" — and a place for evaluateIsolated, which the sandbox needs to answer "what would this one rule do". Conflict resolution and effect combination stay in PolicyResolver: six copies of that would be six places to break. specificity is a stored column now, computed on save with the documented weights, and the migration backfills existing rows with the same formula. Left at zero they would all have tied and the ordering would have changed overnight. Field names stay as they are rather than moving to the document's dotted names (patient.age). Stored condition_json rows point at the current names on live clinic policies; renaming them is a data migration, and the mapping is not one-to-one — implementation_notes.md says as much. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
This commit is contained in:
@@ -119,7 +119,7 @@ class PolicyEngineTest extends ApiTestCase
|
||||
$schema = $body['data'];
|
||||
|
||||
self::assertArrayHasKey('timing', $schema);
|
||||
self::assertContains('equals', $schema['timing']['operators']);
|
||||
self::assertContains('equals', array_column($schema['timing']['operators'], 'value'));
|
||||
|
||||
self::assertSame(
|
||||
['min_duration_minutes', 'add_duration_minutes'],
|
||||
@@ -138,7 +138,11 @@ class PolicyEngineTest extends ApiTestCase
|
||||
$meta = array_column($schema['eligibility']['field_meta'], null, 'key');
|
||||
|
||||
self::assertSame('int', $meta['patient_age']['type']);
|
||||
self::assertSame(['equals', 'not_equals', 'greater_than', 'less_than'], $meta['patient_age']['operators']);
|
||||
// عدد یازده عملگر ندارد؛ فقط آنهایی که روی عدد معنا دارند.
|
||||
self::assertSame(
|
||||
['equals', 'not_equals', 'greater_than', 'greater_or_equal', 'less_than', 'less_or_equal', 'between', 'in', 'not_in'],
|
||||
$meta['patient_age']['operators'],
|
||||
);
|
||||
self::assertSame(['contains'], $meta['patient_tags']['operators']);
|
||||
self::assertSame('سن بیمار', $meta['patient_age']['label']);
|
||||
}
|
||||
@@ -545,6 +549,48 @@ class PolicyEngineTest extends ApiTestCase
|
||||
self::assertTrue(true);
|
||||
}
|
||||
|
||||
/**
|
||||
* ⭐ `specificity` هنگام **ذخیره** حساب میشود و در تساوی اولویت تصمیم میگیرد.
|
||||
*
|
||||
* محاسبهاش در زمان اجرا یعنی کاری که یک بار در عمر قانون کافی بود، در هر رزرو
|
||||
* تکرار شود؛ و ذخیرهشدنش یعنی میشود روزی مرتبسازی را به SQL برد.
|
||||
*/
|
||||
public function testSpecificityIsStoredAndDecidesTiesAtEqualPriority(): void
|
||||
{
|
||||
[$user, $section, $address] = $this->clinicWithBranch();
|
||||
$service = $this->service($section, 'لیزر', 20, 1_000_000);
|
||||
|
||||
// قانون عام: بدون دامنه، بدون شرط.
|
||||
$broad = $this->policy($user, [
|
||||
'category' => 'pricing',
|
||||
'name' => 'تخفیف عمومی',
|
||||
'priority' => 5,
|
||||
'effects' => [['type' => 'discount_percent', 'value' => 10]],
|
||||
]);
|
||||
|
||||
// قانون خاص: همان اولویت، ولی سرویس و یک شرط دارد.
|
||||
$narrow = $this->policy($user, [
|
||||
'category' => 'pricing',
|
||||
'name' => 'تخفیف همین سرویس',
|
||||
'priority' => 5,
|
||||
'service_uuid' => $service->getUuid(),
|
||||
'condition' => ['match' => 'all', 'conditions' => [
|
||||
['field' => 'item_count', 'operator' => 'greater_or_equal', 'value' => 0],
|
||||
]],
|
||||
'effects' => [['type' => 'discount_percent', 'value' => 40]],
|
||||
]);
|
||||
|
||||
self::assertSame(0, $broad['specificity'], 'قانون بیدامنه و بیشرط');
|
||||
self::assertSame(5, $narrow['specificity'], 'سرویس ۴ + یک شرط ۱');
|
||||
|
||||
// هر دو اعمال میشوند (تخفیف درصدی جمع میشود)، ولی **ترتیب** مال specificity است:
|
||||
// اختصاصیتر اول میآید، و همان ترتیبی است که اثرهای «اولی برنده» را تعیین میکند.
|
||||
$quote = $this->quote($user, $service, $address);
|
||||
$names = array_column($quote['data']['breakdown']['sources']['applied_policies'], 'name');
|
||||
|
||||
self::assertSame(['تخفیف همین سرویس', 'تخفیف عمومی'], $names, 'اختصاصیتر باید اول باشد');
|
||||
}
|
||||
|
||||
// ── جداسازی محیط ────────────────────────────────────────────────────────
|
||||
|
||||
public function testPolicyOfAnotherClinicIsNeitherVisibleNorApplied(): void
|
||||
|
||||
Reference in New Issue
Block a user