refactor(branch): remove the branch domain, keep the address

Branches and rooms are not part of the resource-first product: a room is a
resource like any other, and the only thing the branch pages still managed —
opening hours — duplicated the resource's own shift.

What could not go is the address. Every appointment carries address_id (75 of
75 rows), the public booking site reads /clinic-pro/doctor-address/{id}, and a
resource derives its tenant pair from the address it belongs to. So
DoctorAddress stays as an invisible anchor with no page and no menu entry, and
GET /api/v1/addresses replaces GET /api/v1/branches for the forms that still
need to say "where".

BranchResolver was likewise not a branch feature. doctor_addresses is a global
table, so TenantFilter does not cover it and eight callers across booking,
availability, pricing and the catalog went through this resolver to avoid
leaking another clinic's address. It moved to Doctor\Service\AddressResolver
rather than dying with the domain.

The availability engine loses one layer: a resource's real hours were the
branch hours intersected with its shift, and are now the shift alone. That is
the single behavioural change, and the three tests that asserted the old
contract are replaced by one that states the new one.

Rooms already had a resource row each; the migration drops only the bridge
back to `rooms`, and drops it before the table — that foreign key is ON DELETE
CASCADE and the other order would take the resources, and their appointments,
with it.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
This commit is contained in:
hamed
2026-08-02 15:25:32 +03:30
co-authored by Claude Opus 5
parent 1c4f2a2451
commit dd284ec622
59 changed files with 566 additions and 3128 deletions
@@ -2,7 +2,6 @@
namespace App\Resource\Service;
use App\Branch\Repository\BranchWorkingHoursRepository;
use App\Resource\Entity\ClinicResource;
use App\Resource\Entity\ResourceException;
use App\Resource\Repository\NationalHolidayRepository;
@@ -34,7 +33,6 @@ final class ResourceAvailabilityService
public function __construct(
private readonly ResourceCalendarRepository $calendars,
private readonly ResourceExceptionRepository $exceptions,
private readonly BranchWorkingHoursRepository $branchHours,
private readonly NationalHolidayRepository $holidays,
private readonly TenantHolidayOverrideRepository $overrides,
) {}
@@ -52,7 +50,6 @@ final class ResourceAvailabilityService
// شیفت‌ها و ساعت شعبه یک بار خوانده می‌شوند، نه per روز.
$shiftsByDay = $this->shiftsByDay($resource);
$branchByDay = $this->branchHoursByDay($resource);
$holidayMap = $this->holidays->mapForRange($startDay, $endDay);
$overrideMap = $this->overrides->mapForRange(
@@ -76,7 +73,6 @@ final class ResourceAvailabilityService
$day,
$timezone,
$shiftsByDay,
$branchByDay,
$holidayMap,
$overrideMap,
$exceptions,
@@ -115,7 +111,6 @@ final class ResourceAvailabilityService
$endDay,
);
$branchByDay = $this->branchHoursByDay($first);
$ids = array_map(static fn (ClinicResource $r): int => (int) $r->getId(), $resources);
$shiftsById = $this->calendars->findForResources($ids);
@@ -142,7 +137,6 @@ final class ResourceAvailabilityService
$day,
$timezone,
$shiftsByDay,
$branchByDay,
$holidayMap,
$overrideMap,
$exceptionsById[$id] ?? [],
@@ -157,7 +151,6 @@ final class ResourceAvailabilityService
/**
* @param array<int, list<TimeInterval>> $shiftsByDay
* @param array<int, list<TimeInterval>>|null $branchByDay
* @param array<int, \App\Resource\Entity\NationalHoliday> $holidayMap
* @param array<int, \App\Resource\Entity\TenantHolidayOverride> $overrideMap
* @param ResourceException[] $exceptions
@@ -167,7 +160,6 @@ final class ResourceAvailabilityService
int $midnight,
\DateTimeZone $timezone,
array $shiftsByDay,
?array $branchByDay,
array $holidayMap,
array $overrideMap,
array $exceptions,
@@ -180,7 +172,7 @@ final class ResourceAvailabilityService
}
if (!$resource->getAddress()->isActive()) {
return new DayAvailability($midnight, $dayOfWeek, [], ['branch_inactive']);
return new DayAvailability($midnight, $dayOfWeek, [], ['address_inactive']);
}
$override = $overrideMap[$midnight] ?? null;
@@ -201,26 +193,6 @@ final class ResourceAvailabilityService
return new DayAvailability($midnight, $dayOfWeek, [], ['no_shift']);
}
// شعبهٔ بدون ساعت کاری = «تعریف‌نشده»، نه «بسته»: شیفت منبع بی‌قید اعمال
// می‌شود تا دادهٔ موجود دقیقاً مثل امروز کار کند (قرارداد تسک ۰۱).
if ($branchByDay !== null) {
$branchWindows = $branchByDay[$dayOfWeek] ?? [];
if ($branchWindows === []) {
return new DayAvailability($midnight, $dayOfWeek, [], ['branch_closed']);
}
$intersected = TimeInterval::intersectAll($shifts, $branchWindows);
// شیفت هست ولی تقاطعش با ساعت شعبه خالی شد — این با «شیفتی نیست» فرق دارد
// و بدون دلیل صریح، پاسخِ خالی از یک باگ قابل تشخیص نیست.
if ($intersected === []) {
$reasons[] = 'outside_branch_hours';
}
$shifts = $intersected;
}
$absolute = array_map(
static fn (TimeInterval $i): TimeInterval => $i->minutesToAbsolute($midnight),
$shifts,
@@ -263,31 +235,6 @@ final class ResourceAvailabilityService
return array_map(TimeInterval::mergeAll(...), $byDay);
}
/**
* `null` یعنی این شعبه اصلاً ساعت کاری تعریف‌شده ندارد — که با «همهٔ روزها بسته»
* فرق دارد و نباید با آن یکی گرفته شود.
*
* @return array<int, list<TimeInterval>>|null
*/
private function branchHoursByDay(ClinicResource $resource): ?array
{
$rows = $this->branchHours->findForAddress($resource->getAddress());
if ($rows === []) {
return null;
}
$byDay = [];
foreach ($rows as $row) {
if (!$row->isActive()) {
continue;
}
$byDay[$row->getDayOfWeek()][] = new TimeInterval($row->getStartMinute(), $row->getEndMinute());
}
return array_map(TimeInterval::mergeAll(...), $byDay);
}
/** ۰=شنبه … ۶=جمعه — همان قرارداد بقیهٔ سامانه، نه `w` استاندارد PHP. */
public function dayOfWeek(int $timestamp, \DateTimeZone $timezone): int