Resolve a service's duration and price from the resource that performs it
The chain the spec asks for, plus the branch level that already has data: resource+option, resource+service, branch override, then the item's own value. Duration and price resolve independently. If they resolved together the first override would silently swallow the other value — a resource that only differs in how long it takes would also drop the branch's tariff. Each resolved value carries where it came from. Without that, the panel cannot label a number "from the branch" or "service default", and "why this number?" becomes a four-table investigation. Two rules worth stating: null means inherit while zero is a real value, so a free service keeps its zero instead of inheriting the parent's price; and an inactive offering is skipped whole, since "this resource does not perform this right now" is not the same as "I have no opinion on the numbers". The parent service is passed in rather than looked up from the item's group. The booking flow already holds both, and a reverse query would be a second way to answer a question that already has an answer in hand. Eight tests: one per level with the other levels populated so the winner is provable, plus independent resolution, the inactive skip, zero, and resolving the service itself without a parent. Suite 1272 green, phpstan at its 14-error baseline. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
This commit is contained in:
@@ -38,14 +38,14 @@
|
||||
|
||||
| # | مورد | وضعیت | یادداشت |
|
||||
|---|---|---|---|
|
||||
| ۲.۱ | `ResourceServiceResolver` با زنجیرهٔ چهارسطحی | ⏳ | |
|
||||
| ۲.۲ | `ResolvedServiceSpec` منبعِ هر مقدار را میگوید | ⏳ | |
|
||||
| ۲.۳ | مدت و قیمت **جدا** حل میشوند | ⏳ | |
|
||||
| ۲.۴ | تست سطح ۱ (منبع+گزینه) | ⏳ | |
|
||||
| ۲.۵ | تست سطح ۲ (منبع+سرویس) | ⏳ | |
|
||||
| ۲.۶ | تست سطح ۳ (شعبه) | ⏳ | |
|
||||
| ۲.۷ | تست سطح ۴ (پیشفرض آیتم) | ⏳ | |
|
||||
| ۲.۸ | تست مرزی: مدت از سطح ۱، قیمت از سطح ۳ | ⏳ | |
|
||||
| ۲.۱ | `ResourceServiceResolver` با زنجیرهٔ چهارسطحی | ✅ | `ResourceServiceResolver` — منبع+گزینه → منبع+سرویس → شعبه → پیشفرض آیتم |
|
||||
| ۲.۲ | `ResolvedServiceSpec` منبعِ هر مقدار را میگوید | ✅ | `ResolvedServiceSpec` با `durationSource` و `priceSource` |
|
||||
| ۲.۳ | مدت و قیمت **جدا** حل میشوند | ✅ | `testDurationAndPriceResolveIndependently` — مدت از سطح ۱، قیمت از سطح ۳ |
|
||||
| ۲.۴ | تست سطح ۱ (منبع+گزینه) | ✅ | `testLevelOneResourcePlusOptionWins` سبز |
|
||||
| ۲.۵ | تست سطح ۲ (منبع+سرویس) | ✅ | `testLevelTwoResourcePlusServiceWinsWhenTheOptionHasNothing` سبز |
|
||||
| ۲.۶ | تست سطح ۳ (شعبه) | ✅ | `testLevelThreeBranchWinsWhenTheResourceHasNothing` سبز |
|
||||
| ۲.۷ | تست سطح ۴ (پیشفرض آیتم) | ✅ | `testLevelFourFallsBackToTheItemItself` سبز |
|
||||
| ۲.۸ | تست مرزی: مدت از سطح ۱، قیمت از سطح ۳ | ✅ | بهعلاوهٔ سه مرزی: ردیف غیرفعال رد میشود · قیمت صفر ارث نمیگیرد · حل خودِ سرویس بدون والد |
|
||||
|
||||
## ۳. فیلتر کاندیدها بر اساس سرویس
|
||||
|
||||
|
||||
@@ -0,0 +1,98 @@
|
||||
<?php
|
||||
|
||||
namespace App\ClinicService\Service;
|
||||
|
||||
use App\ClinicService\Entity\ServiceItem;
|
||||
use App\ClinicService\Repository\ServiceBranchOverrideRepository;
|
||||
use App\ClinicService\ValueObject\ResolvedServiceSpec;
|
||||
use App\Doctor\Entity\DoctorAddress;
|
||||
use App\Resource\Entity\ClinicResource;
|
||||
use App\Resource\Repository\ResourceServiceOfferingRepository;
|
||||
|
||||
/**
|
||||
* «این سرویس با این منبع چقدر طول میکشد و چقدر میشود؟»
|
||||
*
|
||||
* زنجیرهٔ خاص به عام، طبق سند مالک محصول بهعلاوهٔ سطح شعبه که از قبل داده دارد:
|
||||
*
|
||||
* ۱. منبع + گزینه → `ResourceServiceOffering(resource, option)`
|
||||
* ۲. منبع + سرویس → `ResourceServiceOffering(resource, parent)`
|
||||
* ۳. شعبه + آیتم → `ServiceBranchOverride(item, address)`
|
||||
* ۴. پیشفرض آیتم → `ServiceItem`
|
||||
*
|
||||
* **مدت و قیمت جدا حل میشوند.** منبعی که فقط مدتش فرق دارد نباید قیمتش هم از همان
|
||||
* سطح بیاید؛ اگر با هم حل شوند، اولین override باعث میشود تعرفهٔ شعبه بیصدا نادیده گرفته شود.
|
||||
*
|
||||
* سطحِ والد را **صدازننده** میدهد، نه یک کوئری معکوس روی گروهها: جریان رزرو هر دو را
|
||||
* از قبل در دست دارد (سرویس انتخابشده و گزینهاش)، و کوئری معکوس فقط یک راه اضافه برای
|
||||
* واگرایی میسازد.
|
||||
*/
|
||||
final class ResourceServiceResolver
|
||||
{
|
||||
public function __construct(
|
||||
private readonly ResourceServiceOfferingRepository $offerings,
|
||||
private readonly ServiceBranchOverrideRepository $branchOverrides,
|
||||
) {}
|
||||
|
||||
/**
|
||||
* @param ServiceItem $item آیتمی که قیمت و مدتش را میخواهیم (گزینه یا خودِ سرویس)
|
||||
* @param ServiceItem|null $parentService سرویسِ والد وقتی `$item` یک گزینه است
|
||||
*/
|
||||
public function resolve(
|
||||
ClinicResource $resource,
|
||||
ServiceItem $item,
|
||||
DoctorAddress $address,
|
||||
?ServiceItem $parentService = null,
|
||||
): ResolvedServiceSpec {
|
||||
$optionOffering = $this->offerings->findOneFor($resource, $item);
|
||||
|
||||
$parentOffering = $parentService !== null && $parentService->getId() !== $item->getId()
|
||||
? $this->offerings->findOneFor($resource, $parentService)
|
||||
: null;
|
||||
|
||||
// ردیف غیرفعال یعنی «این منبع فعلاً این را نمیدهد»، نه «مقدارش را نمیدانم» —
|
||||
// پس اعدادش هم خوانده نمیشوند و زنجیره از رویش رد میشود.
|
||||
$optionOffering = $optionOffering?->isActive() === true ? $optionOffering : null;
|
||||
$parentOffering = $parentOffering?->isActive() === true ? $parentOffering : null;
|
||||
|
||||
$branch = $this->branchOverrides->findOneBy(['item' => $item, 'address' => $address]);
|
||||
|
||||
[$duration, $durationSource] = $this->first([
|
||||
[$optionOffering?->getDurationMinutes(), ResolvedServiceSpec::SOURCE_RESOURCE_OPTION],
|
||||
[$parentOffering?->getDurationMinutes(), ResolvedServiceSpec::SOURCE_RESOURCE_SERVICE],
|
||||
[$branch?->getSoloDurationMinutes(), ResolvedServiceSpec::SOURCE_BRANCH],
|
||||
[$item->getSoloDurationMinutes(), ResolvedServiceSpec::SOURCE_SERVICE_DEFAULT],
|
||||
]);
|
||||
|
||||
[$price, $priceSource] = $this->first([
|
||||
[$optionOffering?->getPriceRials(), ResolvedServiceSpec::SOURCE_RESOURCE_OPTION],
|
||||
[$parentOffering?->getPriceRials(), ResolvedServiceSpec::SOURCE_RESOURCE_SERVICE],
|
||||
[$branch?->getPriceRials(), ResolvedServiceSpec::SOURCE_BRANCH],
|
||||
[$item->getPriceRials(), ResolvedServiceSpec::SOURCE_SERVICE_DEFAULT],
|
||||
]);
|
||||
|
||||
return new ResolvedServiceSpec(
|
||||
durationMinutes: $duration,
|
||||
priceRials: (int) ($price ?? 0),
|
||||
durationSource: $duration === null ? null : $durationSource,
|
||||
priceSource: $priceSource ?? ResolvedServiceSpec::SOURCE_SERVICE_DEFAULT,
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* اولین سطحی که مقدار دارد. `null` یعنی «حرفی برای گفتن ندارم» و رد میشود؛
|
||||
* صفر یک مقدارِ واقعی است و **رد نمیشود** — سرویسِ رایگان قیمتش صفر است، نه ارثبر.
|
||||
*
|
||||
* @param list<array{0: int|null, 1: string}> $levels
|
||||
* @return array{0: int|null, 1: string|null}
|
||||
*/
|
||||
private function first(array $levels): array
|
||||
{
|
||||
foreach ($levels as [$value, $source]) {
|
||||
if ($value !== null) {
|
||||
return [$value, $source];
|
||||
}
|
||||
}
|
||||
|
||||
return [null, null];
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,44 @@
|
||||
<?php
|
||||
|
||||
namespace App\ClinicService\ValueObject;
|
||||
|
||||
/**
|
||||
* مدت و قیمتِ حلشدهٔ یک سرویس برای یک منبع مشخص — بههمراه اینکه هر عدد **از کجا** آمده.
|
||||
*
|
||||
* منبعِ هر مقدار بخشی از نتیجه است نه اطلاعات اضافه: پنل باید بتواند کنار عدد بنویسد
|
||||
* «از شعبه» یا «پیشفرض سرویس»، و وقتی کلینیک میپرسد «چرا این عدد؟» جواب بدون خواندن
|
||||
* چهار جدول در دسترس باشد.
|
||||
*/
|
||||
final readonly class ResolvedServiceSpec
|
||||
{
|
||||
/** ردیف `resource_service_offerings` برای همان گزینهای که بیمار انتخاب کرده. */
|
||||
public const SOURCE_RESOURCE_OPTION = 'resource_option';
|
||||
|
||||
/** ردیف همان منبع ولی روی سرویسِ والد — وقتی گزینه مقدار خودش را ندارد. */
|
||||
public const SOURCE_RESOURCE_SERVICE = 'resource_service';
|
||||
|
||||
/** `ServiceBranchOverride` — تنظیم این شعبه، مستقل از اینکه کدام منبع کار را میکند. */
|
||||
public const SOURCE_BRANCH = 'branch';
|
||||
|
||||
/** مقدار خودِ `ServiceItem`. */
|
||||
public const SOURCE_SERVICE_DEFAULT = 'service_default';
|
||||
|
||||
public function __construct(
|
||||
public ?int $durationMinutes,
|
||||
public int $priceRials,
|
||||
/** یکی از ثابتهای `SOURCE_*`؛ `null` یعنی هیچ سطحی مدتی تعریف نکرده. */
|
||||
public ?string $durationSource,
|
||||
public string $priceSource,
|
||||
) {}
|
||||
|
||||
/** @return array<string, mixed> */
|
||||
public function toArray(): array
|
||||
{
|
||||
return [
|
||||
'duration_minutes' => $this->durationMinutes,
|
||||
'price_rials' => $this->priceRials,
|
||||
'duration_source' => $this->durationSource,
|
||||
'price_source' => $this->priceSource,
|
||||
];
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,196 @@
|
||||
<?php
|
||||
|
||||
namespace App\Tests\ClinicService;
|
||||
|
||||
use App\ClinicService\Entity\ServiceBranchOverride;
|
||||
use App\ClinicService\Entity\ServiceItem;
|
||||
use App\ClinicService\Entity\ServiceSection;
|
||||
use App\ClinicService\Service\ResourceServiceResolver;
|
||||
use App\ClinicService\ValueObject\ResolvedServiceSpec;
|
||||
use App\Clinic\Entity\Clinic;
|
||||
use App\Doctor\Entity\DoctorAddress;
|
||||
use App\Resource\Entity\ClinicResource;
|
||||
use App\Resource\Entity\ResourceServiceOffering;
|
||||
use App\Resource\Entity\ResourceType;
|
||||
use App\Tests\ApiTestCase;
|
||||
|
||||
/**
|
||||
* زنجیرهٔ حل مدت و قیمت: منبع+گزینه → منبع+سرویس → شعبه → پیشفرض آیتم.
|
||||
*
|
||||
* هر تست یک سطح را تنها میگذارد تا معلوم شود همان سطح برنده شده، نه سطحی که تصادفاً
|
||||
* مقدار مشابه داشته.
|
||||
*/
|
||||
class ResourceServiceResolverTest extends ApiTestCase
|
||||
{
|
||||
private ResourceServiceResolver $resolver;
|
||||
private DoctorAddress $address;
|
||||
private ClinicResource $resource;
|
||||
private ServiceItem $service;
|
||||
private ServiceItem $option;
|
||||
|
||||
protected function setUp(): void
|
||||
{
|
||||
parent::setUp();
|
||||
|
||||
// resolver مستقیم ساخته میشود، نه از کانتینر: سرویسهای اپ private اند و
|
||||
// عمومیکردنشان فقط برای تست، پیکربندی را بهخاطر تست خم میکند.
|
||||
$this->resolver = new ResourceServiceResolver(
|
||||
$this->em->getRepository(ResourceServiceOffering::class),
|
||||
$this->em->getRepository(ServiceBranchOverride::class),
|
||||
);
|
||||
|
||||
$user = $this->createUser(['ROLE_USER', 'ROLE_CLINIC']);
|
||||
$clinic = new Clinic($user);
|
||||
$clinic->setName('کلینیک زنجیره');
|
||||
$this->em->persist($clinic);
|
||||
$this->em->flush();
|
||||
|
||||
$this->address = DoctorAddress::forClinic($clinic->getId());
|
||||
$this->address->setName('شعبهٔ مرکزی');
|
||||
$this->em->persist($this->address);
|
||||
|
||||
$type = new ResourceType('clinic', (int) $clinic->getId(), 'device', 'دستگاه');
|
||||
$this->em->persist($type);
|
||||
$this->em->flush();
|
||||
|
||||
$this->resource = new ClinicResource($this->address, $type, 'لیزر دایود');
|
||||
$this->em->persist($this->resource);
|
||||
|
||||
$section = new ServiceSection('clinic', (int) $clinic->getId(), 'لیزر');
|
||||
$this->em->persist($section);
|
||||
$this->em->flush();
|
||||
|
||||
// سرویسِ والد «لیزر» و گزینهٔ «پا» — هر دو ServiceItem اند، همان مدل کاتالوگ.
|
||||
$this->service = new ServiceItem($section, 'لیزر', 10_000_000);
|
||||
$this->service->setDurationMinutes(60);
|
||||
$this->em->persist($this->service);
|
||||
|
||||
$this->option = new ServiceItem($section, 'لیزر پا', 8_000_000);
|
||||
$this->option->setDurationMinutes(30);
|
||||
$this->em->persist($this->option);
|
||||
$this->em->flush();
|
||||
}
|
||||
|
||||
private function offer(ServiceItem $item, ?int $minutes, ?int $price, bool $active = true): ResourceServiceOffering
|
||||
{
|
||||
$offering = new ResourceServiceOffering($this->resource, $item);
|
||||
$offering->setDurationMinutes($minutes)->setPriceRials($price)->setActive($active);
|
||||
$this->em->persist($offering);
|
||||
$this->em->flush();
|
||||
|
||||
return $offering;
|
||||
}
|
||||
|
||||
private function branchOverride(ServiceItem $item, ?int $minutes, ?int $price): void
|
||||
{
|
||||
$override = new ServiceBranchOverride($item, $this->address);
|
||||
$override->setSoloDurationMinutes($minutes)->setPriceRials($price);
|
||||
$this->em->persist($override);
|
||||
$this->em->flush();
|
||||
}
|
||||
|
||||
private function resolve(): ResolvedServiceSpec
|
||||
{
|
||||
return $this->resolver->resolve($this->resource, $this->option, $this->address, $this->service);
|
||||
}
|
||||
|
||||
// ── ✅ هر سطح، تنها ──────────────────────────────────────────────────────
|
||||
|
||||
public function testLevelOneResourcePlusOptionWins(): void
|
||||
{
|
||||
$this->offer($this->option, 15, 9_500_000);
|
||||
$this->offer($this->service, 40, 12_000_000);
|
||||
$this->branchOverride($this->option, 50, 11_000_000);
|
||||
|
||||
$spec = $this->resolve();
|
||||
|
||||
self::assertSame(15, $spec->durationMinutes);
|
||||
self::assertSame(9_500_000, $spec->priceRials);
|
||||
self::assertSame(ResolvedServiceSpec::SOURCE_RESOURCE_OPTION, $spec->durationSource);
|
||||
self::assertSame(ResolvedServiceSpec::SOURCE_RESOURCE_OPTION, $spec->priceSource);
|
||||
}
|
||||
|
||||
public function testLevelTwoResourcePlusServiceWinsWhenTheOptionHasNothing(): void
|
||||
{
|
||||
$this->offer($this->service, 40, 12_000_000);
|
||||
$this->branchOverride($this->option, 50, 11_000_000);
|
||||
|
||||
$spec = $this->resolve();
|
||||
|
||||
self::assertSame(40, $spec->durationMinutes);
|
||||
self::assertSame(12_000_000, $spec->priceRials);
|
||||
self::assertSame(ResolvedServiceSpec::SOURCE_RESOURCE_SERVICE, $spec->durationSource);
|
||||
}
|
||||
|
||||
public function testLevelThreeBranchWinsWhenTheResourceHasNothing(): void
|
||||
{
|
||||
$this->branchOverride($this->option, 50, 11_000_000);
|
||||
|
||||
$spec = $this->resolve();
|
||||
|
||||
self::assertSame(50, $spec->durationMinutes);
|
||||
self::assertSame(11_000_000, $spec->priceRials);
|
||||
self::assertSame(ResolvedServiceSpec::SOURCE_BRANCH, $spec->durationSource);
|
||||
}
|
||||
|
||||
public function testLevelFourFallsBackToTheItemItself(): void
|
||||
{
|
||||
$spec = $this->resolve();
|
||||
|
||||
self::assertSame(30, $spec->durationMinutes);
|
||||
self::assertSame(8_000_000, $spec->priceRials);
|
||||
self::assertSame(ResolvedServiceSpec::SOURCE_SERVICE_DEFAULT, $spec->durationSource);
|
||||
self::assertSame(ResolvedServiceSpec::SOURCE_SERVICE_DEFAULT, $spec->priceSource);
|
||||
}
|
||||
|
||||
// ── ⚠️ مرزی ──────────────────────────────────────────────────────────────
|
||||
|
||||
public function testDurationAndPriceResolveIndependently(): void
|
||||
{
|
||||
// منبع فقط مدت را میگوید؛ قیمت باید تا سطح شعبه پایین برود.
|
||||
$this->offer($this->option, 15, null);
|
||||
$this->branchOverride($this->option, null, 11_000_000);
|
||||
|
||||
$spec = $this->resolve();
|
||||
|
||||
self::assertSame(15, $spec->durationMinutes);
|
||||
self::assertSame(ResolvedServiceSpec::SOURCE_RESOURCE_OPTION, $spec->durationSource);
|
||||
|
||||
self::assertSame(11_000_000, $spec->priceRials);
|
||||
self::assertSame(ResolvedServiceSpec::SOURCE_BRANCH, $spec->priceSource);
|
||||
}
|
||||
|
||||
public function testAnInactiveOfferingIsSkippedEntirely(): void
|
||||
{
|
||||
// ردیف هست ولی خاموش: «این منبع فعلاً این را نمیدهد» یعنی اعدادش هم خوانده نمیشوند.
|
||||
$this->offer($this->option, 15, 9_500_000, active: false);
|
||||
|
||||
$spec = $this->resolve();
|
||||
|
||||
self::assertSame(30, $spec->durationMinutes);
|
||||
self::assertSame(8_000_000, $spec->priceRials);
|
||||
self::assertSame(ResolvedServiceSpec::SOURCE_SERVICE_DEFAULT, $spec->durationSource);
|
||||
}
|
||||
|
||||
public function testAFreeServiceKeepsItsZeroInsteadOfInheriting(): void
|
||||
{
|
||||
// صفر مقدار واقعی است، نه «حرفی ندارم» — وگرنه سرویس رایگان قیمت سرویس والد را میگرفت.
|
||||
$this->offer($this->option, null, 0);
|
||||
|
||||
$spec = $this->resolve();
|
||||
|
||||
self::assertSame(0, $spec->priceRials);
|
||||
self::assertSame(ResolvedServiceSpec::SOURCE_RESOURCE_OPTION, $spec->priceSource);
|
||||
}
|
||||
|
||||
public function testResolvingTheServiceItselfNeedsNoParent(): void
|
||||
{
|
||||
$this->offer($this->service, 45, 13_000_000);
|
||||
|
||||
$spec = $this->resolver->resolve($this->resource, $this->service, $this->address);
|
||||
|
||||
self::assertSame(45, $spec->durationMinutes);
|
||||
self::assertSame(13_000_000, $spec->priceRials);
|
||||
self::assertSame(ResolvedServiceSpec::SOURCE_RESOURCE_OPTION, $spec->durationSource);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user