feat(policy): six-category policy engine wired into the booking flow

Rules become data instead of code: a clinic can say "laser under 18 requires
parental consent" without a deploy.

Engine
- Policy / PolicyVersionLog entities, closed field/operator/effect lists per
  category (PolicySchema), condition validation at write time
- PolicyResolver: priority -> specificity -> age, combining effects by
  veto / max / sum / union
- A missing fact fails its clause instead of silently passing it
- Policies are drafts until activated, and are versioned rather than edited

Wiring
- selection -> ServiceSelectionValidator
- eligibility + spacing -> BookingPolicyGuard, at hold time not confirm time
- resource + timing -> AppointmentPlanBuilder, including template-less services
- pricing -> PricingEngine, alongside (not replacing) the manual discount

The condition column is named condition_json: `condition` is a MariaDB keyword
and broke every INSERT.

Tests: 17 in tests/Policy including NoPolicyRegressionTest, which pins that a
clinic with no policies sees byte-identical output to task 08.
Docs: docs/api/policy.md (real captured JSON) + docs/architecture/policy-engine.md.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
This commit is contained in:
hamed
2026-07-31 10:19:19 +03:30
co-authored by Claude Opus 5
parent 281420ab4d
commit 584ea4067f
26 changed files with 2870 additions and 86 deletions
+43
View File
@@ -0,0 +1,43 @@
<?php
namespace App\Policy\ValueObject;
/**
* نتیجهٔ اعمال یک دسته قانون: اثرهای ترکیب‌شده + ردِ قانون‌هایی که اعمال شدند.
*
* `appliedPolicies` شناسه **و نسخه** را نگه می‌دارد. فقط شناسه کافی نیست: قانون فردا
* نسخهٔ ۲ می‌گیرد و آن‌وقت «چرا این نوبت این قیمت را گرفت؟» جواب اشتباه می‌دهد.
*/
final readonly class PolicyOutcome
{
/**
* @param array<string, mixed> $effects نوعِ اثر => مقدار ترکیب‌شده
* @param list<array<string, mixed>> $appliedPolicies
* @param list<string> $forbidReasons پیام‌های انسانیِ ممنوعیت
*/
public function __construct(
public array $effects = [],
public array $appliedPolicies = [],
public array $forbidReasons = [],
) {}
public function isForbidden(): bool
{
return $this->forbidReasons !== [];
}
public function effect(string $type, mixed $default = null): mixed
{
return $this->effects[$type] ?? $default;
}
public function toArray(): array
{
return [
'effects' => (object) $this->effects,
'applied_policies' => $this->appliedPolicies,
'forbidden' => $this->isForbidden(),
'forbid_reasons' => $this->forbidReasons,
];
}
}