Files
clinicpro/src/Resource/Controller/ResourceCalendarController.php
T
hamedandClaude Opus 5 1fdfdf9e48 feat(resource): resource calendars, exceptions and national holidays
Section 9 of the design document builds free time by subtracting seven layers.
Four existed and all of them hung off the doctor. This adds the missing ones and
puts them on the resource:

  branch hours ∩ resource shifts − national holidays − resource exceptions

Booked appointments and holds are deliberately NOT subtracted here — those are
tasks 06/07, as is intersecting several resources. The method is called
rawAvailability() so nobody mistakes the output for bookable time. Nothing in this
change calls SlotCalculatorService; the existing slot path stays frozen.

Four types of exception (leave, absence, maintenance, ad-hoc closure) share one
table because all four are "an interval subtracted from a resource's calendar";
splitting them would mean four queries per availability lookup instead of one.
Holiday overrides work in both directions: a clinic that opens on a public holiday,
and a clinic that closes on an ordinary day.

Every empty day carries a reason (national_holiday, no_shift, branch_closed,
outside_branch_hours, exception, …). Without it an empty response is
indistinguishable from a bug and the first person debugging has to read four tables
by hand.

Three real defects found on the way:

JalaliDateService.gregorianToJalali() was wrong — it returned [3006, 7, 3] for
2026-07-30 instead of [1405, 5, 8], roughly 1601 years off. jalaliYear(),
jalaliMonth(), jalaliMonthRange() and jalaliYearRange() all inherit that, so the
representation reports built on them have been filtering by nonsense ranges. The
class's own formatDateTime() was already correct because it used IntlDateFormatter,
so both conversions now go through the same mechanism, and JalaliDateServiceTest
pins Nowruz and the 6/31→7/1 boundary. There were no tests before, which is why
nobody noticed.

TimeInterval added a seconds-based midnight to a minutes-based interval, turning an
eight-hour shift into eight seconds. The conversion is now an explicitly named
minutesToAbsolute() so the unit change cannot happen silently again.

HolidayService.upsertNational() persisted but left flushing to the caller. Every
HTTP request reboots the kernel, so the caller often held a different
EntityManager: persist landed on one, flush on the other, and nothing was written
with no error at all. The write is now self-contained.

119 tests across tests/Resource, tests/Branch and tests/Representation. phpstan
clean on both touched domains.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
2026-07-30 18:14:23 +03:30

193 lines
7.9 KiB
PHP

<?php
namespace App\Resource\Controller;
use App\Auth\Entity\User;
use App\Resource\Entity\ResourceException;
use App\Resource\Repository\ResourceExceptionRepository;
use App\Resource\Service\ResourceAvailabilityService;
use App\Resource\Service\ResourceCalendarService;
use App\Resource\Service\ResourceContext;
use App\Resource\ValueObject\DayAvailability;
use App\Shared\Constant\ErrorCodes;
use App\Shared\Controller\BaseController;
use App\Shared\Exception\AppException;
use App\Shared\Tenant\TenantOwnershipChecker;
use OpenApi\Attributes as OA;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Routing\Attribute\Route;
use Symfony\Component\Security\Http\Attribute\CurrentUser;
use Symfony\Component\Security\Http\Attribute\IsGranted;
#[OA\Tag(name: 'Resource')]
#[IsGranted('IS_AUTHENTICATED_FULLY')]
class ResourceCalendarController extends BaseController
{
use ResourcePermissionTrait;
public function __construct(
private readonly ResourceContext $context,
private readonly ResourceCalendarService $calendar,
private readonly ResourceAvailabilityService $availability,
private readonly ResourceExceptionRepository $exceptions,
private readonly TenantOwnershipChecker $ownership,
) {}
#[Route('/api/v1/resource/{uuid}/calendar', name: 'resource_calendar_show', methods: ['GET'])]
public function show(#[CurrentUser] User $user, string $uuid): JsonResponse
{
$this->denyUnlessGranted($user, 'view');
$resource = $this->context->resource($user, $uuid);
return $this->success([
'resource_uuid' => $resource->getUuid(),
'timezone' => $resource->getAddress()->getTimezone(),
'defined' => $this->calendar->isDefined($resource),
'days' => (object) $this->calendar->read($resource),
]);
}
/** جایگزینی کامل هفت روز؛ آرایهٔ خالی یعنی منبع هیچ شیفتی ندارد. */
#[Route('/api/v1/resource/{uuid}/calendar', name: 'resource_calendar_replace', methods: ['PUT'])]
public function replace(#[CurrentUser] User $user, string $uuid, Request $request): JsonResponse
{
$this->denyUnlessGranted($user, 'update');
$data = json_decode($request->getContent(), true);
if (!is_array($data) || !is_array($data['days'] ?? null)) {
return $this->error(ErrorCodes::ERR_VALIDATION_002, 'فیلد days الزامی است', 422, 'days');
}
$resource = $this->context->resource($user, $uuid);
$days = $this->calendar->replace($resource, $data['days']);
return $this->success([
'resource_uuid' => $resource->getUuid(),
'timezone' => $resource->getAddress()->getTimezone(),
'defined' => $days !== array_fill_keys(ResourceCalendarService::DAYS, []),
'days' => (object) $days,
]);
}
#[Route('/api/v1/resource/{uuid}/exceptions', name: 'resource_exceptions_list', methods: ['GET'])]
public function listExceptions(#[CurrentUser] User $user, string $uuid, Request $request): JsonResponse
{
$this->denyUnlessGranted($user, 'view');
$resource = $this->context->resource($user, $uuid);
$from = $request->query->get('from');
$to = $request->query->get('to');
// بدون بازه، همهٔ استثناها از ابتدای زمان تا انتهای آن — عمداً محدود نمی‌شود
// چون فهرست مرخصیِ یک منبع کوچک است و صفحه‌بندی‌اش سود ندارد.
$rows = $this->exceptions->findOverlapping(
$resource,
is_numeric($from) ? (int) $from : 0,
is_numeric($to) ? (int) $to : PHP_INT_MAX,
);
return $this->success(array_map(
static fn (ResourceException $e): array => $e->toArray(),
$rows,
));
}
#[Route('/api/v1/resource/{uuid}/exception', name: 'resource_exception_create', methods: ['POST'])]
public function createException(#[CurrentUser] User $user, string $uuid, Request $request): JsonResponse
{
$this->denyUnlessGranted($user, 'update');
$data = json_decode($request->getContent(), true);
if (!is_array($data)) {
return $this->error(ErrorCodes::ERR_VALIDATION_001, 'بدنهٔ درخواست نامعتبر است', 422);
}
$resource = $this->context->resource($user, $uuid);
$exception = $this->calendar->createException($resource, $data);
return $this->success($exception->toArray(), 201);
}
#[Route('/api/v1/resource-exception/{uuid}', name: 'resource_exception_update', methods: ['PATCH'])]
public function updateException(#[CurrentUser] User $user, string $uuid, Request $request): JsonResponse
{
$this->denyUnlessGranted($user, 'update');
$data = json_decode($request->getContent(), true);
if (!is_array($data)) {
return $this->error(ErrorCodes::ERR_VALIDATION_001, 'بدنهٔ درخواست نامعتبر است', 422);
}
return $this->success($this->calendar->updateException($this->requireException($user, $uuid), $data)->toArray());
}
#[Route('/api/v1/resource-exception/{uuid}', name: 'resource_exception_delete', methods: ['DELETE'])]
public function deleteException(#[CurrentUser] User $user, string $uuid): JsonResponse
{
$this->denyUnlessGranted($user, 'update');
$this->calendar->deleteException($this->requireException($user, $uuid));
return $this->success(null);
}
/**
* ساعت آزاد **خام**: ساعت شعبه ∩ شیفت منبع − تعطیلات − استثناها.
* نوبت‌های ثبت‌شده اینجا کسر نمی‌شوند — آن کارِ تسک ۰۶/۰۷ است.
*/
#[Route('/api/v1/resource/{uuid}/availability', name: 'resource_availability', methods: ['GET'])]
public function availabilityFor(#[CurrentUser] User $user, string $uuid, Request $request): JsonResponse
{
$this->denyUnlessGranted($user, 'view');
$resource = $this->context->resource($user, $uuid);
$from = $request->query->get('from');
$to = $request->query->get('to');
if (!is_numeric($from) || !is_numeric($to)) {
return $this->error(ErrorCodes::ERR_VALIDATION_002, 'پارامترهای from و to الزامی‌اند', 422, 'from');
}
if ((int) $to < (int) $from) {
return $this->error(ErrorCodes::ERR_VALIDATION_001, 'to باید بعد از from باشد', 422, 'to');
}
$days = intdiv((int) $to - (int) $from, ResourceAvailabilityService::DAY_SECONDS) + 1;
if ($days > ResourceAvailabilityService::MAX_DAYS) {
return $this->error(
ErrorCodes::ERR_VALIDATION_001,
sprintf('بازهٔ درخواستی حداکثر %d روز است', ResourceAvailabilityService::MAX_DAYS),
422,
'to',
);
}
return $this->success([
'resource_uuid' => $resource->getUuid(),
'timezone' => $resource->getAddress()->getTimezone(),
'days' => array_map(
static fn (DayAvailability $d): array => $d->toArray(),
$this->availability->rawAvailability($resource, (int) $from, (int) $to),
),
]);
}
private function requireException(User $user, string $uuid): ResourceException
{
$exception = $this->exceptions->findByUuid($uuid);
[$entityType, $entityId] = $this->context->pair($user);
if ($exception === null || !$this->ownership->belongsToPair($entityType, $entityId, $exception)) {
throw new AppException(ErrorCodes::ERR_NOT_FOUND_001, 'استثنا یافت نشد', 404);
}
return $exception;
}
}