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>
182 lines
8.1 KiB
PHP
182 lines
8.1 KiB
PHP
<?php
|
||
|
||
namespace App\Tests\Resource;
|
||
|
||
class ResourcePoolTest extends ResourceTestCase
|
||
{
|
||
public function testPoolIsCreatedAndMembersAreReplacedWholesale(): void
|
||
{
|
||
[$user, , $address] = $this->clinicWithAddress();
|
||
$type = $this->resourceType($address, 'laser', 'دستگاه لیزر');
|
||
|
||
$one = $this->createResource($user, $address, $type, ['name' => 'لیزر ۱']);
|
||
$two = $this->createResource($user, $address, $type, ['name' => 'لیزر ۲']);
|
||
$three = $this->createResource($user, $address, $type, ['name' => 'لیزر ۳']);
|
||
|
||
$pool = $this->authJson('POST', '/api/v1/resource-pools', $user, [
|
||
'address_uuid' => $address->getUuid(),
|
||
'type_uuid' => $type->getUuid(),
|
||
'name' => 'لیزرهای آلکساندرایت',
|
||
]);
|
||
self::assertSame(201, $this->responseCode(), json_encode($pool, JSON_UNESCAPED_UNICODE));
|
||
self::assertSame([], $pool['data']['members'], 'استخر تازه هیچ عضوی ندارد و این معتبر است');
|
||
|
||
$filled = $this->authJson('PUT', "/api/v1/resource-pool/{$pool['data']['uuid']}/members", $user, [
|
||
'members' => [
|
||
['resource_uuid' => $one['data']['uuid'], 'priority' => 0],
|
||
['resource_uuid' => $two['data']['uuid'], 'priority' => 1],
|
||
['resource_uuid' => $three['data']['uuid'], 'priority' => 2],
|
||
],
|
||
]);
|
||
|
||
self::assertSame(200, $this->responseCode());
|
||
self::assertCount(3, $filled['data']['members']);
|
||
self::assertSame($address->getUuid(), $filled['data']['address_uuid']);
|
||
|
||
// جایگزینی کامل: عضوی که نیامده، برداشته میشود.
|
||
$shrunk = $this->authJson('PUT', "/api/v1/resource-pool/{$pool['data']['uuid']}/members", $user, [
|
||
'members' => [['resource_uuid' => $one['data']['uuid']]],
|
||
]);
|
||
self::assertCount(1, $shrunk['data']['members']);
|
||
}
|
||
|
||
public function testMemberFromAnotherBranchIsRejected(): void
|
||
{
|
||
[$user, $clinic, $address] = $this->clinicWithAddress();
|
||
$second = $this->extraAddress($clinic);
|
||
$type = $this->resourceType($address, 'laser', 'دستگاه لیزر');
|
||
|
||
$elsewhere = $this->createResource($user, $second, $type, ['name' => 'لیزر شعبهٔ دو']);
|
||
|
||
$pool = $this->authJson('POST', '/api/v1/resource-pools', $user, [
|
||
'address_uuid' => $address->getUuid(),
|
||
'type_uuid' => $type->getUuid(),
|
||
'name' => 'استخر شعبهٔ یک',
|
||
]);
|
||
|
||
$body = $this->authJson('PUT', "/api/v1/resource-pool/{$pool['data']['uuid']}/members", $user, [
|
||
'members' => [['resource_uuid' => $elsewhere['data']['uuid']]],
|
||
]);
|
||
|
||
self::assertSame(422, $this->responseCode());
|
||
self::assertStringContainsString('یک شعبه', $body['errors'][0]['message']);
|
||
}
|
||
|
||
public function testMemberOfAnotherTypeIsRejected(): void
|
||
{
|
||
[$user, , $address] = $this->clinicWithAddress();
|
||
$laser = $this->resourceType($address, 'laser', 'دستگاه لیزر');
|
||
$chair = $this->resourceType($address, 'chair', 'صندلی');
|
||
|
||
$wrongType = $this->createResource($user, $address, $chair, ['name' => 'صندلی ۱']);
|
||
|
||
$pool = $this->authJson('POST', '/api/v1/resource-pools', $user, [
|
||
'address_uuid' => $address->getUuid(),
|
||
'type_uuid' => $laser->getUuid(),
|
||
'name' => 'استخر لیزر',
|
||
]);
|
||
|
||
$body = $this->authJson('PUT', "/api/v1/resource-pool/{$pool['data']['uuid']}/members", $user, [
|
||
'members' => [['resource_uuid' => $wrongType['data']['uuid']]],
|
||
]);
|
||
|
||
self::assertSame(422, $this->responseCode());
|
||
self::assertStringContainsString('یک نوع', $body['errors'][0]['message']);
|
||
}
|
||
|
||
/** عضو نامعتبر نباید اعضای درستِ قبلی را پاک کند. */
|
||
public function testInvalidMemberLeavesTheStoredMembersUntouched(): void
|
||
{
|
||
[$user, $clinic, $address] = $this->clinicWithAddress();
|
||
$second = $this->extraAddress($clinic);
|
||
$type = $this->resourceType($address, 'laser', 'دستگاه لیزر');
|
||
|
||
$good = $this->createResource($user, $address, $type, ['name' => 'لیزر خوب']);
|
||
$elsewhere = $this->createResource($user, $second, $type, ['name' => 'لیزر شعبهٔ دو']);
|
||
|
||
$pool = $this->authJson('POST', '/api/v1/resource-pools', $user, [
|
||
'address_uuid' => $address->getUuid(),
|
||
'type_uuid' => $type->getUuid(),
|
||
'name' => 'استخر',
|
||
]);
|
||
|
||
$this->authJson('PUT', "/api/v1/resource-pool/{$pool['data']['uuid']}/members", $user, [
|
||
'members' => [['resource_uuid' => $good['data']['uuid']]],
|
||
]);
|
||
|
||
$this->authJson('PUT', "/api/v1/resource-pool/{$pool['data']['uuid']}/members", $user, [
|
||
'members' => [
|
||
['resource_uuid' => $good['data']['uuid']],
|
||
['resource_uuid' => $elsewhere['data']['uuid']],
|
||
],
|
||
]);
|
||
self::assertSame(422, $this->responseCode());
|
||
|
||
$read = $this->authJson('GET', "/api/v1/resource-pool/{$pool['data']['uuid']}", $user);
|
||
self::assertCount(1, $read['data']['members']);
|
||
}
|
||
|
||
public function testForeignPoolIsNotFound(): void
|
||
{
|
||
[$user, , $address] = $this->clinicWithAddress();
|
||
$pool = $this->authJson('POST', '/api/v1/resource-pools', $user, [
|
||
'address_uuid' => $address->getUuid(),
|
||
'type_uuid' => $this->resourceType($address)->getUuid(),
|
||
'name' => 'استخر',
|
||
]);
|
||
|
||
[$otherUser] = $this->clinicWithAddress('شعبهٔ دیگر');
|
||
|
||
$this->authJson('GET', "/api/v1/resource-pool/{$pool['data']['uuid']}", $otherUser);
|
||
self::assertSame(404, $this->responseCode());
|
||
|
||
$this->authJson('PUT', "/api/v1/resource-pool/{$pool['data']['uuid']}/members", $otherUser, ['members' => []]);
|
||
self::assertSame(404, $this->responseCode());
|
||
}
|
||
|
||
/** حذف استخر اعضا را میبرد ولی خودِ منابع باید سرِ جایشان بمانند. */
|
||
public function testDeletingAPoolKeepsItsResources(): void
|
||
{
|
||
[$user, , $address] = $this->clinicWithAddress();
|
||
$type = $this->resourceType($address, 'laser', 'لیزر');
|
||
$resource = $this->createResource($user, $address, $type, ['name' => 'لیزر ۱']);
|
||
|
||
$pool = $this->authJson('POST', '/api/v1/resource-pools', $user, [
|
||
'address_uuid' => $address->getUuid(),
|
||
'type_uuid' => $type->getUuid(),
|
||
'name' => 'استخر',
|
||
]);
|
||
$this->authJson('PUT', "/api/v1/resource-pool/{$pool['data']['uuid']}/members", $user, [
|
||
'members' => [['resource_uuid' => $resource['data']['uuid']]],
|
||
]);
|
||
|
||
$this->authJson('DELETE', "/api/v1/resource-pool/{$pool['data']['uuid']}", $user);
|
||
self::assertSame(200, $this->responseCode());
|
||
|
||
$this->authJson('GET', "/api/v1/resource/{$resource['data']['uuid']}", $user);
|
||
self::assertSame(200, $this->responseCode(), 'منبع نباید با استخر حذف شود');
|
||
}
|
||
|
||
public function testDuplicateMemberIsRejected(): void
|
||
{
|
||
[$user, , $address] = $this->clinicWithAddress();
|
||
$type = $this->resourceType($address, 'laser', 'لیزر');
|
||
$resource = $this->createResource($user, $address, $type, ['name' => 'لیزر ۱']);
|
||
|
||
$pool = $this->authJson('POST', '/api/v1/resource-pools', $user, [
|
||
'address_uuid' => $address->getUuid(),
|
||
'type_uuid' => $type->getUuid(),
|
||
'name' => 'استخر',
|
||
]);
|
||
|
||
$this->authJson('PUT', "/api/v1/resource-pool/{$pool['data']['uuid']}/members", $user, [
|
||
'members' => [
|
||
['resource_uuid' => $resource['data']['uuid']],
|
||
['resource_uuid' => $resource['data']['uuid']],
|
||
],
|
||
]);
|
||
|
||
self::assertSame(422, $this->responseCode());
|
||
}
|
||
}
|