Files
clinicpro/src/Doctor/Service/AddressResolver.php
T
hamedandClaude Opus 5 dd284ec622 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>
2026-08-02 15:25:32 +03:30

107 lines
4.1 KiB
PHP

<?php
namespace App\Doctor\Service;
use App\Auth\Entity\User;
use App\Doctor\Entity\DoctorAddress;
use App\Doctor\Repository\DoctorAddressRepository;
use App\Secretary\Security\SecretaryAccessChecker;
use App\Shared\Constant\ErrorCodes;
use App\Shared\Context\EntityContextResolver;
use App\Shared\Exception\AppException;
use Symfony\Component\HttpFoundation\RequestStack;
/**
* تک‌نقطهٔ تبدیل «uuid محل نوبت‌دهی در request» به یک {@see DoctorAddress} از محیط جاری.
*
* لازم است چون doctor_addresses جفت (entity_type, entity_id) ندارد و در
* GlobalTables::ENTITIES سراسری اعلام شده، پس TenantFilter رویش کار نمی‌کند:
* findOneBy(['uuid' => …]) آدرس کلینیک دیگری را هم برمی‌گرداند. هر کنترلری که
* address_uuid می‌گیرد از اینجا رد می‌شود تا این بررسی جایی جا نیفتد.
*
* قبلاً `App\Branch\Service\BranchResolver` بود. با حذف مفهوم «شعبه» از محصول، اسم
* و خانه‌اش عوض شد ولی خودش نمی‌توانست حذف شود: موتور رزرو، دسترس‌پذیری، قیمت و
* کاتالوگ همگی از همین رد می‌شوند.
*/
final class AddressResolver
{
public function __construct(
private readonly DoctorAddressRepository $addresses,
private readonly EntityContextResolver $contexts,
private readonly SecretaryAccessChecker $secretaryAccess,
private readonly RequestStack $requestStack,
) {}
/**
* جفت محیطِ این درخواست.
*
* منشی جدا حساب می‌شود چون EntityContextResolver او را مالک هیچ محیطی نمی‌شناسد —
* همان استثنایی که ClinicServiceController::resolveEntity() هم دارد. مجوزش جداگانه
* با denyUnlessGranted سنجیده می‌شود، اینجا فقط «کدام محیط» است.
*
* @return array{0: string, 1: int}
* @throws AppException وقتی محیطی حل نشود
*/
public function pair(User $user): array
{
[$type, $id] = $user->hasRole('ROLE_SECRETARY')
? $this->secretaryAccess->resolveOwnerEntity($user)
: $this->contexts->resolve($user, $this->requestedClinicUuid())->toEntityPair();
if ($id === null) {
throw new AppException(ErrorCodes::ERR_FORBIDDEN_001, 'محیط کاری انتخاب نشده است', 403);
}
return [$type, (int) $id];
}
/**
* ۴۰۴ می‌دهد نه ۴۰۳ — همان رفتار TenantFilter: وجودِ دادهٔ محیط دیگر لو نمی‌رود.
*
* @throws AppException
*/
public function resolve(User $user, string $addressUuid): DoctorAddress
{
[$entityType, $entityId] = $this->pair($user);
$address = $this->addresses->findByUuidForEntityPair($addressUuid, $entityType, $entityId);
if ($address === null) {
throw new AppException(ErrorCodes::ERR_NOT_FOUND_001, 'محل نوبت‌دهی یافت نشد', 404);
}
return $address;
}
/** @return DoctorAddress[] محل‌های نوبت‌دهی محیط جاری */
public function listForContext(User $user): array
{
[$entityType, $entityId] = $this->pair($user);
return $this->addresses->findForEntityPair($entityType, $entityId);
}
private function requestedClinicUuid(): ?string
{
$request = $this->requestStack->getCurrentRequest();
if ($request === null) {
return null;
}
$fromQuery = $request->query->get('clinic_uuid');
if (is_string($fromQuery) && $fromQuery !== '') {
return $fromQuery;
}
if (!in_array($request->getMethod(), ['POST', 'PATCH', 'PUT'], true)) {
return null;
}
$body = json_decode($request->getContent(), true);
return is_array($body) && is_string($body['clinic_uuid'] ?? null) && $body['clinic_uuid'] !== ''
? $body['clinic_uuid']
: null;
}
}