feat(resource): resource types, resources, skills and pools
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>
This commit is contained in:
@@ -0,0 +1,105 @@
|
||||
<?php
|
||||
|
||||
namespace App\Resource\Service;
|
||||
|
||||
use App\Auth\Entity\User;
|
||||
use App\Resource\Entity\ClinicResource;
|
||||
use App\Resource\Entity\ResourceSkill;
|
||||
use App\Resource\Entity\Skill;
|
||||
use App\Resource\Repository\ResourceSkillRepository;
|
||||
use App\Shared\Constant\ErrorCodes;
|
||||
use App\Shared\Exception\AppException;
|
||||
use Doctrine\ORM\EntityManagerInterface;
|
||||
|
||||
final class SkillAssignmentService
|
||||
{
|
||||
public function __construct(
|
||||
private readonly ResourceSkillRepository $resourceSkills,
|
||||
private readonly ResourceContext $context,
|
||||
private readonly EntityManagerInterface $em,
|
||||
) {}
|
||||
|
||||
/**
|
||||
* جایگزینی **کامل** مهارتهای یک منبع: مهارتی که در بدنه نیست، برداشته میشود.
|
||||
*
|
||||
* مثل ساعت کاری شعبه، اعتبارسنجی همهٔ ورودی پیش از هر حذفی انجام میشود تا یک
|
||||
* ورودی نامعتبر در انتهای فهرست، مهارتهای درستِ قبلی را پاک نکند و بعد ۴۲۲ بدهد.
|
||||
*
|
||||
* @param array<int, mixed> $rows
|
||||
* @return ResourceSkill[]
|
||||
*/
|
||||
public function replace(User $user, ClinicResource $resource, array $rows): array
|
||||
{
|
||||
$validated = $this->validate($user, $rows);
|
||||
|
||||
$this->resourceSkills->deleteForResource($resource);
|
||||
$resource->getSkills()->clear();
|
||||
|
||||
$created = [];
|
||||
foreach ($validated as $row) {
|
||||
$assignment = new ResourceSkill($resource, $row['skill'], $row['level']);
|
||||
$this->em->persist($assignment);
|
||||
$resource->getSkills()->add($assignment);
|
||||
$created[] = $assignment;
|
||||
}
|
||||
|
||||
$this->em->flush();
|
||||
|
||||
return $created;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array<int, mixed> $rows
|
||||
* @return list<array{skill: Skill, level: int}>
|
||||
*/
|
||||
private function validate(User $user, array $rows): array
|
||||
{
|
||||
$seen = [];
|
||||
$out = [];
|
||||
|
||||
foreach ($rows as $row) {
|
||||
if (!is_array($row) || !is_string($row['skill_uuid'] ?? null)) {
|
||||
throw new AppException(ErrorCodes::ERR_VALIDATION_002, 'فیلد skill_uuid الزامی است', 422, 'skill_uuid');
|
||||
}
|
||||
|
||||
$uuid = $row['skill_uuid'];
|
||||
|
||||
if (isset($seen[$uuid])) {
|
||||
throw new AppException(ErrorCodes::ERR_VALIDATION_001, 'یک مهارت دو بار فرستاده شده است', 422, 'skill_uuid');
|
||||
}
|
||||
$seen[$uuid] = true;
|
||||
|
||||
// مهارت محیط دیگر → ۴۰۴، پیش از هر تغییری در دیتابیس.
|
||||
$skill = $this->context->skill($user, $uuid);
|
||||
$level = $row['level'] ?? ResourceSkill::MIN_LEVEL;
|
||||
|
||||
if (!is_numeric($level) || (int) $level < ResourceSkill::MIN_LEVEL || (int) $level > ResourceSkill::MAX_LEVEL) {
|
||||
throw new AppException(ErrorCodes::ERR_VALIDATION_001, 'سطح مهارت باید بین ۱ تا ۵ باشد', 422, 'level');
|
||||
}
|
||||
|
||||
$out[] = ['skill' => $skill, 'level' => (int) $level];
|
||||
}
|
||||
|
||||
return $out;
|
||||
}
|
||||
|
||||
/**
|
||||
* مهارتی که روی منبعی نشسته حذف نمیشود — اول باید از منابع برداشته شود، وگرنه
|
||||
* `ON DELETE RESTRICT` خطای خام دیتابیس میداد.
|
||||
*/
|
||||
public function deleteSkill(Skill $skill): void
|
||||
{
|
||||
$inUse = $this->resourceSkills->countForSkill($skill);
|
||||
|
||||
if ($inUse > 0) {
|
||||
throw new AppException(
|
||||
ErrorCodes::ERR_VALIDATION_001,
|
||||
sprintf('این مهارت به %d منبع داده شده است؛ اول از آنها برداشته شود', $inUse),
|
||||
422,
|
||||
);
|
||||
}
|
||||
|
||||
$this->em->remove($skill);
|
||||
$this->em->flush();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user