Files
clinicpro/migrations/Version20260801122211.php
hamedandClaude Opus 5 f2600f9922 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>
2026-08-01 15:56:50 +03:30

42 lines
1.4 KiB
PHP

<?php
declare(strict_types=1);
namespace DoctrineMigrations;
use Doctrine\DBAL\Schema\Schema;
use Doctrine\Migrations\AbstractMigration;
/**
* Auto-generated Migration: Please modify to your needs!
*/
final class Version20260801122211 extends AbstractMigration
{
public function getDescription(): string
{
return 'Persist policy specificity instead of recomputing it on every booking.';
}
public function up(Schema $schema): void
{
$this->addSql('ALTER TABLE policies ADD specificity SMALLINT DEFAULT 0 NOT NULL');
// قانون‌های موجود باید همان عددی را بگیرند که تا امروز در زمان اجرا حساب می‌شد،
// وگرنه همه با صفر می‌مانند و ترتیب حل تناقض یک‌شبه عوض می‌شود.
$this->addSql(<<<'SQL'
UPDATE policies
SET specificity =
(CASE WHEN address_id IS NOT NULL THEN 8 ELSE 0 END)
+ (CASE WHEN service_item_id IS NOT NULL THEN 4 ELSE 0 END)
+ (CASE WHEN catalog_category_id IS NOT NULL THEN 2 ELSE 0 END)
+ COALESCE(JSON_LENGTH(condition_json, '$.conditions'), 0)
SQL);
}
public function down(Schema $schema): void
{
// this down() migration is auto-generated, please modify it to your needs
$this->addSql('ALTER TABLE policies DROP specificity');
}
}