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>
177 lines
7.8 KiB
PHP
177 lines
7.8 KiB
PHP
<?php
|
|
|
|
namespace App\Tests\Resource;
|
|
|
|
class SkillAssignmentTest extends ResourceTestCase
|
|
{
|
|
public function testSkillsAreReplacedWholesale(): void
|
|
{
|
|
[$user, , $address] = $this->clinicWithAddress();
|
|
$resource = $this->createResource($user, $address, $this->resourceType($address));
|
|
$laser = $this->createSkill($user, 'لیزر آلکساندرایت');
|
|
$botox = $this->createSkill($user, 'بوتاکس');
|
|
|
|
$withBoth = $this->authJson('PUT', "/api/v1/resource/{$resource['data']['uuid']}/skills", $user, [
|
|
'skills' => [
|
|
['skill_uuid' => $laser['data']['uuid'], 'level' => 4],
|
|
['skill_uuid' => $botox['data']['uuid']],
|
|
],
|
|
]);
|
|
|
|
self::assertSame(200, $this->responseCode(), json_encode($withBoth, JSON_UNESCAPED_UNICODE));
|
|
self::assertCount(2, $withBoth['data']['skills']);
|
|
self::assertSame(1, $withBoth['data']['skills'][1]['level'], 'سطح پیشفرض ۱ است');
|
|
|
|
// PUT جایگزینی کامل است: مهارتی که نیامده، برداشته میشود.
|
|
$onlyLaser = $this->authJson('PUT', "/api/v1/resource/{$resource['data']['uuid']}/skills", $user, [
|
|
'skills' => [['skill_uuid' => $laser['data']['uuid'], 'level' => 5]],
|
|
]);
|
|
|
|
self::assertCount(1, $onlyLaser['data']['skills']);
|
|
self::assertSame($laser['data']['uuid'], $onlyLaser['data']['skills'][0]['skill_uuid']);
|
|
self::assertSame(5, $onlyLaser['data']['skills'][0]['level']);
|
|
}
|
|
|
|
public function testEmptyListClearsAllSkills(): void
|
|
{
|
|
[$user, , $address] = $this->clinicWithAddress();
|
|
$resource = $this->createResource($user, $address, $this->resourceType($address));
|
|
$skill = $this->createSkill($user);
|
|
|
|
$this->authJson('PUT', "/api/v1/resource/{$resource['data']['uuid']}/skills", $user, [
|
|
'skills' => [['skill_uuid' => $skill['data']['uuid']]],
|
|
]);
|
|
|
|
$body = $this->authJson('PUT', "/api/v1/resource/{$resource['data']['uuid']}/skills", $user, ['skills' => []]);
|
|
|
|
self::assertSame(200, $this->responseCode());
|
|
self::assertSame([], $body['data']['skills']);
|
|
}
|
|
|
|
public function testLevelOutsideOneToFiveIsRejected(): void
|
|
{
|
|
[$user, , $address] = $this->clinicWithAddress();
|
|
$resource = $this->createResource($user, $address, $this->resourceType($address));
|
|
$skill = $this->createSkill($user);
|
|
|
|
foreach ([0, 6] as $level) {
|
|
$body = $this->authJson('PUT', "/api/v1/resource/{$resource['data']['uuid']}/skills", $user, [
|
|
'skills' => [['skill_uuid' => $skill['data']['uuid'], 'level' => $level]],
|
|
]);
|
|
|
|
self::assertSame(422, $this->responseCode(), "level=$level باید رد شود");
|
|
self::assertSame('level', $body['errors'][0]['field']);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* اعتبارسنجی پیش از حذف: ورودی نامعتبر در انتهای فهرست نباید مهارتهای درستِ
|
|
* قبلی را پاک کند و بعد ۴۲۲ برگرداند.
|
|
*/
|
|
public function testInvalidRowLeavesTheStoredSkillsUntouched(): void
|
|
{
|
|
[$user, , $address] = $this->clinicWithAddress();
|
|
$resource = $this->createResource($user, $address, $this->resourceType($address));
|
|
$laser = $this->createSkill($user, 'لیزر');
|
|
$botox = $this->createSkill($user, 'بوتاکس');
|
|
|
|
$this->authJson('PUT', "/api/v1/resource/{$resource['data']['uuid']}/skills", $user, [
|
|
'skills' => [['skill_uuid' => $laser['data']['uuid'], 'level' => 3]],
|
|
]);
|
|
|
|
$this->authJson('PUT', "/api/v1/resource/{$resource['data']['uuid']}/skills", $user, [
|
|
'skills' => [
|
|
['skill_uuid' => $botox['data']['uuid']],
|
|
['skill_uuid' => $laser['data']['uuid'], 'level' => 99],
|
|
],
|
|
]);
|
|
self::assertSame(422, $this->responseCode());
|
|
|
|
$read = $this->authJson('GET', "/api/v1/resource/{$resource['data']['uuid']}", $user);
|
|
self::assertCount(1, $read['data']['skills']);
|
|
self::assertSame(3, $read['data']['skills'][0]['level']);
|
|
}
|
|
|
|
public function testDuplicateSkillInOnePayloadIsRejected(): void
|
|
{
|
|
[$user, , $address] = $this->clinicWithAddress();
|
|
$resource = $this->createResource($user, $address, $this->resourceType($address));
|
|
$skill = $this->createSkill($user);
|
|
|
|
$this->authJson('PUT', "/api/v1/resource/{$resource['data']['uuid']}/skills", $user, [
|
|
'skills' => [
|
|
['skill_uuid' => $skill['data']['uuid']],
|
|
['skill_uuid' => $skill['data']['uuid'], 'level' => 2],
|
|
],
|
|
]);
|
|
|
|
self::assertSame(422, $this->responseCode());
|
|
}
|
|
|
|
public function testForeignSkillIsNotFound(): void
|
|
{
|
|
[$user, , $address] = $this->clinicWithAddress();
|
|
$resource = $this->createResource($user, $address, $this->resourceType($address));
|
|
|
|
[$otherUser] = $this->clinicWithAddress('شعبهٔ دیگر');
|
|
$foreignSkill = $this->createSkill($otherUser, 'مهارت بیگانه');
|
|
|
|
$this->authJson('PUT', "/api/v1/resource/{$resource['data']['uuid']}/skills", $user, [
|
|
'skills' => [['skill_uuid' => $foreignSkill['data']['uuid']]],
|
|
]);
|
|
|
|
self::assertSame(404, $this->responseCode());
|
|
}
|
|
|
|
/** مهارتی که روی منبعی نشسته حذف نمیشود؛ وگرنه FK خطای خام دیتابیس میداد. */
|
|
public function testSkillInUseCannotBeDeleted(): void
|
|
{
|
|
[$user, , $address] = $this->clinicWithAddress();
|
|
$resource = $this->createResource($user, $address, $this->resourceType($address));
|
|
$skill = $this->createSkill($user);
|
|
|
|
$this->authJson('PUT', "/api/v1/resource/{$resource['data']['uuid']}/skills", $user, [
|
|
'skills' => [['skill_uuid' => $skill['data']['uuid']]],
|
|
]);
|
|
|
|
$body = $this->authJson('DELETE', "/api/v1/skill/{$skill['data']['uuid']}", $user);
|
|
self::assertSame(422, $this->responseCode());
|
|
// رقم لاتین عمدی است: قرارداد پیامهای درونریزیِ بکاند همین است
|
|
// (BackfillServiceDurationCommand، WorkingHoursService و …) و قالببندی فارسی
|
|
// کارِ نمایش در کلاینت است.
|
|
self::assertStringContainsString('1 منبع', $body['errors'][0]['message']);
|
|
|
|
// بعد از برداشتن از منبع، حذف مجاز است.
|
|
$this->authJson('PUT', "/api/v1/resource/{$resource['data']['uuid']}/skills", $user, ['skills' => []]);
|
|
$this->authJson('DELETE', "/api/v1/skill/{$skill['data']['uuid']}", $user);
|
|
self::assertSame(200, $this->responseCode());
|
|
}
|
|
|
|
public function testSkillListReportsUsageCount(): void
|
|
{
|
|
[$user, , $address] = $this->clinicWithAddress();
|
|
$resource = $this->createResource($user, $address, $this->resourceType($address));
|
|
$skill = $this->createSkill($user);
|
|
|
|
$before = $this->authJson('GET', '/api/v1/skills', $user);
|
|
self::assertSame(0, $before['data'][0]['resources_count']);
|
|
|
|
$this->authJson('PUT', "/api/v1/resource/{$resource['data']['uuid']}/skills", $user, [
|
|
'skills' => [['skill_uuid' => $skill['data']['uuid']]],
|
|
]);
|
|
|
|
$after = $this->authJson('GET', '/api/v1/skills', $user);
|
|
self::assertSame(1, $after['data'][0]['resources_count']);
|
|
}
|
|
|
|
public function testBlankSkillNameIsRejected(): void
|
|
{
|
|
[$user] = $this->clinicWithAddress();
|
|
|
|
$body = $this->authJson('POST', '/api/v1/skills', $user, ['name' => ' ']);
|
|
|
|
self::assertSame(422, $this->responseCode());
|
|
self::assertSame('name', $body['errors'][0]['field']);
|
|
}
|
|
}
|