The document's first golden rule is "the calendar belongs to the resource, not to the doctor". Today the only thing that can be occupied is a doctor, and ClinicStaff is a label on services and appointments with no calendar, capacity or skills. This adds the layer underneath: anything that can be busy — doctor, operator, assistant, device, room, bed, chair. Two corrections to the planned schema: - `address_id` → doctor_addresses, not `branch_id` → a new branches table. The branch already exists and is the address (task 01). - UNIQUE is (doctor_id, address_id), not (doctor_id). A WeeklySchedule is per (doctor, clinic) but every session inside it carries its own location_id, so one doctor already works at several addresses within one environment. Keying on the doctor alone would have made that unrepresentable — and task 03 gives each resource its own calendar, which is exactly per-location. Design points worth keeping: - Resources bridge to Doctor/ClinicStaff/Room rather than absorbing them; those three have live consumers (appointments.doctor_id, service_item_staff, the public site) and subclassing would mean migrating all of them at once. At most one bridge column is non-null, enforced in the entity because MariaDB will not reliably enforce a multi-column CHECK. - Capacity is concurrency: a three-bed injection room is one resource with capacity 3, not three resources, so occupancy in task 06 stays a COUNT against a limit instead of a merge of three calendars. A person resource is refused capacity > 1. - Skills are a table, not rules. With 50 operators and 200 services, expressing "who may operate what" as policy would mean 10,000 rules. - findEligible() uses HAVING COUNT(DISTINCT …) because "skills A and B" means both; a plain IN would have matched a resource holding only one. - setup/cleanup minutes occupy the resource without being part of the patient's appointment, and are per-resource — distinct from the existing per-doctor WeeklySchedule.meta.buffer_minutes, which stays untouched. Two real bugs found by running the backfill against real data rather than fixtures: ResourceLinker::systemType() persisted a type without flushing, so the next lookup missed it and created a second — the run died on "Duplicate entry 'doctor-1-staff' for key uniq_rt_tenant_code". It now keeps an identity map for the unit of work. The command looped over every WeeklySchedule once per environment, which is quadratic and never finished on real data. Doctors are now a single pass keyed by the schedule's own environment. It also flushes per environment and accepts --pair=clinic:12, so one bad row cannot close the EntityManager and abort a fleet-wide run, and operators can re-run for a single clinic. Staff are the one case that cannot be derived: nothing records which branch they work at. Rather than guessing the first one and seating them in the wrong building, multi-branch environments are skipped and reported. 88 tests, 230 assertions across tests/Resource and tests/Branch. phpstan clean on src/Resource. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
157 lines
6.9 KiB
PHP
157 lines
6.9 KiB
PHP
<?php
|
||
|
||
namespace App\Tests\Resource;
|
||
|
||
use App\Resource\Entity\ClinicResource;
|
||
use App\Resource\Entity\Skill;
|
||
use App\Resource\Repository\ClinicResourceRepository;
|
||
|
||
/**
|
||
* `findEligible()` — پرسوجوی داغِ تسک ۰۶. جدا از HTTP تست میشود چون قرارداد
|
||
* «همهٔ مهارتها، نه یکی» یک تصمیم SQL است و باید مستقل از کنترلر قفل بماند.
|
||
*/
|
||
class ResourceEligibilityTest extends ResourceTestCase
|
||
{
|
||
private function repo(): ClinicResourceRepository
|
||
{
|
||
return static::getContainer()->get(ClinicResourceRepository::class);
|
||
}
|
||
|
||
private function skillIdOf(string $uuid): int
|
||
{
|
||
return (int) $this->em->getRepository(Skill::class)->findOneBy(['uuid' => $uuid])->getId();
|
||
}
|
||
|
||
private function assign(\App\Auth\Entity\User $user, string $resourceUuid, array $skillUuids): void
|
||
{
|
||
$this->authJson('PUT', "/api/v1/resource/$resourceUuid/skills", $user, [
|
||
'skills' => array_map(static fn (string $u): array => ['skill_uuid' => $u], $skillUuids),
|
||
]);
|
||
}
|
||
|
||
/**
|
||
* نیازمندی «مهارت الف و ب» یعنی هر دو. با یک `IN` ساده، منبعی که فقط یکی را
|
||
* دارد هم برمیگشت — به همین دلیل `HAVING COUNT(DISTINCT …)` هست.
|
||
*/
|
||
public function testAllSkillsAreRequiredNotAny(): void
|
||
{
|
||
[$user, , $address] = $this->clinicWithAddress();
|
||
$type = $this->resourceType($address, 'operator', 'اپراتور');
|
||
|
||
$laser = $this->createSkill($user, 'لیزر آلکساندرایت');
|
||
$botox = $this->createSkill($user, 'بوتاکس');
|
||
|
||
$both = $this->createResource($user, $address, $type, ['name' => 'اپراتور کامل']);
|
||
$one = $this->createResource($user, $address, $type, ['name' => 'اپراتور نصفه']);
|
||
|
||
$this->assign($user, $both['data']['uuid'], [$laser['data']['uuid'], $botox['data']['uuid']]);
|
||
$this->assign($user, $one['data']['uuid'], [$laser['data']['uuid']]);
|
||
|
||
$this->em->clear();
|
||
|
||
$eligible = $this->repo()->findEligible(
|
||
$this->em->getRepository(\App\Doctor\Entity\DoctorAddress::class)->find($address->getId()),
|
||
$this->em->getRepository(\App\Resource\Entity\ResourceType::class)->find($type->getId()),
|
||
[$this->skillIdOf($laser['data']['uuid']), $this->skillIdOf($botox['data']['uuid'])],
|
||
);
|
||
|
||
self::assertCount(1, $eligible);
|
||
self::assertSame('اپراتور کامل', $eligible[0]->getName());
|
||
}
|
||
|
||
public function testSingleSkillMatchesEveryResourceThatHasIt(): void
|
||
{
|
||
[$user, , $address] = $this->clinicWithAddress();
|
||
$type = $this->resourceType($address, 'operator', 'اپراتور');
|
||
$laser = $this->createSkill($user, 'لیزر');
|
||
|
||
foreach (['اپراتور ۱', 'اپراتور ۲'] as $name) {
|
||
$r = $this->createResource($user, $address, $type, ['name' => $name]);
|
||
$this->assign($user, $r['data']['uuid'], [$laser['data']['uuid']]);
|
||
}
|
||
$this->createResource($user, $address, $type, ['name' => 'اپراتور بیمهارت']);
|
||
|
||
$this->em->clear();
|
||
|
||
$eligible = $this->repo()->findEligible(
|
||
$this->em->getRepository(\App\Doctor\Entity\DoctorAddress::class)->find($address->getId()),
|
||
$this->em->getRepository(\App\Resource\Entity\ResourceType::class)->find($type->getId()),
|
||
[$this->skillIdOf($laser['data']['uuid'])],
|
||
);
|
||
|
||
self::assertCount(2, $eligible);
|
||
}
|
||
|
||
public function testInactiveResourceIsNeverEligible(): void
|
||
{
|
||
[$user, , $address] = $this->clinicWithAddress();
|
||
$type = $this->resourceType($address, 'operator', 'اپراتور');
|
||
$laser = $this->createSkill($user, 'لیزر');
|
||
|
||
$resource = $this->createResource($user, $address, $type, ['name' => 'اپراتور خاموش']);
|
||
$this->assign($user, $resource['data']['uuid'], [$laser['data']['uuid']]);
|
||
$this->authJson('PATCH', "/api/v1/resource/{$resource['data']['uuid']}", $user, ['active' => false]);
|
||
|
||
$this->em->clear();
|
||
|
||
$eligible = $this->repo()->findEligible(
|
||
$this->em->getRepository(\App\Doctor\Entity\DoctorAddress::class)->find($address->getId()),
|
||
$this->em->getRepository(\App\Resource\Entity\ResourceType::class)->find($type->getId()),
|
||
[$this->skillIdOf($laser['data']['uuid'])],
|
||
);
|
||
|
||
self::assertSame([], $eligible);
|
||
}
|
||
|
||
/** بدون شرط مهارت، همهٔ منابع فعالِ آن شعبه و نوع برمیگردند. */
|
||
public function testNoSkillFilterReturnsEveryActiveResourceOfThatType(): void
|
||
{
|
||
[$user, $clinic, $address] = $this->clinicWithAddress();
|
||
$second = $this->extraAddress($clinic);
|
||
$type = $this->resourceType($address, 'operator', 'اپراتور');
|
||
|
||
$this->createResource($user, $address, $type, ['name' => 'اینجا ۱']);
|
||
$this->createResource($user, $address, $type, ['name' => 'اینجا ۲']);
|
||
$this->createResource($user, $second, $type, ['name' => 'شعبهٔ دیگر']);
|
||
|
||
$this->em->clear();
|
||
|
||
$eligible = $this->repo()->findEligible(
|
||
$this->em->getRepository(\App\Doctor\Entity\DoctorAddress::class)->find($address->getId()),
|
||
$this->em->getRepository(\App\Resource\Entity\ResourceType::class)->find($type->getId()),
|
||
);
|
||
|
||
self::assertCount(2, $eligible);
|
||
}
|
||
|
||
/** فیلتر مهارت روی endpoint فهرست هم همان معنا را میدهد. */
|
||
public function testListEndpointFiltersBySkill(): void
|
||
{
|
||
[$user, , $address] = $this->clinicWithAddress();
|
||
$type = $this->resourceType($address, 'operator', 'اپراتور');
|
||
$laser = $this->createSkill($user, 'لیزر');
|
||
|
||
$withSkill = $this->createResource($user, $address, $type, ['name' => 'با مهارت']);
|
||
$this->assign($user, $withSkill['data']['uuid'], [$laser['data']['uuid']]);
|
||
$this->createResource($user, $address, $type, ['name' => 'بدون مهارت']);
|
||
|
||
$body = $this->authJson('GET', "/api/v1/resources?skill_uuid={$laser['data']['uuid']}", $user);
|
||
|
||
self::assertSame(200, $this->responseCode());
|
||
self::assertCount(1, $body['data']);
|
||
self::assertSame('با مهارت', $body['data'][0]['name']);
|
||
}
|
||
|
||
/** مهارت محیط دیگر نباید بیصدا «هیچ نتیجه» بدهد. */
|
||
public function testListEndpointRejectsAForeignSkillFilter(): void
|
||
{
|
||
[$user] = $this->clinicWithAddress();
|
||
[$otherUser] = $this->clinicWithAddress('شعبهٔ دیگر');
|
||
$foreign = $this->createSkill($otherUser, 'مهارت بیگانه');
|
||
|
||
$this->authJson('GET', "/api/v1/resources?skill_uuid={$foreign['data']['uuid']}", $user);
|
||
|
||
self::assertSame(404, $this->responseCode());
|
||
}
|
||
}
|