Files
clinicpro/tests/Appointment/ResourceBlockTest.php
T
hamedandClaude Opus 5 4049daf071 feat: close the last four domain events, and the panel paths they describe
Every one of the fourteen named events now has an emit point. The four that
were missing all sat on paths owned by earlier tasks:

- AppointmentCompleted fires from both status-change routes, after the row is
  saved. A rejected transition or a version conflict leaves no event; otherwise
  the completed count runs ahead of the appointments themselves.
- AppointmentRescheduled is a third event, not a replacement. A rebook is a
  confirm plus a cancel, and a consumer that only hears the cancel messages a
  patient who still has an appointment.
- ResourceBlocked / ResourceReleased are a pair. Capacity coming back has to be
  as audible as capacity going away, or the resource reads as permanently taken.

Publishing is now on the scheduler rather than an unregistered command: the
logic moved out of PublishDomainEventsCommand into OutboxPublisher so the
recurring message and the manual command share it, and the existing
worker-scheduler container consumes it. The scheduler message carries no data
on purpose — what to publish is read from the table, so an event recorded
between two ticks is not skipped. DomainEventMessage routes to async, since a
slow consumer was otherwise slowing the drain itself and its failure marked a
row failed that had in fact been delivered.

Panel work that these paths made reachable:

- Cancelling from the appointment page now goes through the policy-aware
  endpoint and shows the penalty preview before the confirm, so the operator
  does not discover the patient's penalty after the fact. The cancellation
  service writes the timeline entry itself and accepts a reason, which that
  path previously dropped on the floor.
- Rescheduling reuses the booking page under ?rebook=<uuid> — the search and
  hold steps are identical and only the final step differs. The doctor picker
  is hidden there: a reschedule is not an invitation to change doctors.
- A new GET /appointment/{uuid}/segments exposes the recorded plan. An empty
  list is not an error, it means the appointment is slot-based, and that is
  exactly what gates the resource-mode reschedule button.

AppointmentInvoiceCard no longer crashes the whole detail page when an older
invoice has no discount breakdown.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
2026-07-31 21:27:55 +03:30

203 lines
7.2 KiB
PHP
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
<?php
namespace App\Tests\Appointment;
use App\Appointment\Availability\Entity\ResourceOccupancy;
use App\Auth\Entity\User;
use App\Clinic\Entity\Clinic;
use App\Doctor\Entity\DoctorAddress;
use App\Resource\Entity\ClinicResource;
use App\Resource\Entity\ResourceType;
use App\Shared\Event\DomainEvents;
use App\Shared\Event\Entity\DomainEventLog;
use App\Tests\ApiTestCase;
use Doctrine\ORM\EntityManagerInterface;
/**
* مسدودسازی موردی منبع — «این بعدازظهر دستگاه سرویس دارد».
*
* با استثنای تقویم فرق دارد: آن الگوی کاری را عوض می‌کند، این یک بازهٔ مشخص را
* می‌بندد. مرز بینشان همان چیزی است که این تست‌ها نگه می‌دارند.
*/
class ResourceBlockTest extends ApiTestCase
{
/** @return array{0: User, 1: ClinicResource} */
private function clinicWithResource(): array
{
$user = $this->createUser(['ROLE_USER', 'ROLE_CLINIC']);
$clinic = new Clinic($user);
$clinic->setName('کلینیک مسدودسازی');
$this->em->persist($clinic);
$this->em->flush();
$address = DoctorAddress::forClinic($clinic->getId());
$address->setName('شعبه');
$this->em->persist($address);
$type = new ResourceType('clinic', (int) $clinic->getId(), 'device', 'دستگاه');
$this->em->persist($type);
$this->em->flush();
$resource = new ClinicResource($address, $type, 'لیزر ۱');
$this->em->persist($resource);
$this->em->flush();
return [$user, $resource];
}
public function testBlockingAFreeRangeSucceedsAndIsListed(): void
{
[$user, $resource] = $this->clinicWithResource();
$start = time() + 86400;
$body = $this->authJson('POST', "/api/v1/resource/{$resource->getUuid()}/blocks", $user, [
'starts_at' => $start,
'ends_at' => $start + 4 * 3600,
'reason' => 'سرویس دوره‌ای دستگاه',
]);
self::assertSame(201, $this->responseCode(), json_encode($body, JSON_UNESCAPED_UNICODE));
self::assertSame('سرویس دوره‌ای دستگاه', $body['data']['segment_name']);
$list = $this->authJson(
'GET',
sprintf('/api/v1/resource/%s/blocks?from=%d&to=%d', $resource->getUuid(), $start - 3600, $start + 86400),
$user,
);
self::assertCount(1, $list['data']);
}
public function testAnInvertedRangeIsRejected(): void
{
[$user, $resource] = $this->clinicWithResource();
$start = time() + 86400;
$this->authJson('POST', "/api/v1/resource/{$resource->getUuid()}/blocks", $user, [
'starts_at' => $start,
'ends_at' => $start - 3600,
]);
self::assertSame(422, $this->responseCode());
}
/** ⭐ مسدودسازی روی بازه‌ای که نوبت دارد، ظرفیت را پس نمی‌گیرد. */
public function testBlockingOverABookedRangeIsRejected(): void
{
[$user, $resource] = $this->clinicWithResource();
$start = time() + 2 * 86400;
$em = static::getContainer()->get(EntityManagerInterface::class);
$reloaded = $em->getRepository(ClinicResource::class)->find($resource->getId());
$booked = new ResourceOccupancy($reloaded, $start, $start + 3600);
$booked->setAppointmentId(4242);
$em->persist($booked);
$em->flush();
$this->authJson('POST', "/api/v1/resource/{$resource->getUuid()}/blocks", $user, [
'starts_at' => $start,
'ends_at' => $start + 7200,
]);
self::assertSame(409, $this->responseCode());
}
/** ⭐ اشغالِ یک نوبت واقعی از این مسیر حذف نمی‌شود. */
public function testAnAppointmentOccupancyCannotBeDeletedAsABlock(): void
{
[$user, $resource] = $this->clinicWithResource();
$start = time() + 3 * 86400;
$em = static::getContainer()->get(EntityManagerInterface::class);
$reloaded = $em->getRepository(ClinicResource::class)->find($resource->getId());
$booked = new ResourceOccupancy($reloaded, $start, $start + 3600);
$booked->setAppointmentId(777);
$em->persist($booked);
$em->flush();
$this->authJson('DELETE', "/api/v1/resource-block/{$booked->getUuid()}", $user);
self::assertSame(422, $this->responseCode());
}
public function testAManualBlockCanBeDeleted(): void
{
[$user, $resource] = $this->clinicWithResource();
$start = time() + 86400;
$created = $this->authJson('POST', "/api/v1/resource/{$resource->getUuid()}/blocks", $user, [
'starts_at' => $start,
'ends_at' => $start + 3600,
]);
$this->authJson('DELETE', "/api/v1/resource-block/{$created['data']['uuid']}", $user);
self::assertSame(200, $this->responseCode());
$list = $this->authJson(
'GET',
sprintf('/api/v1/resource/%s/blocks?from=%d&to=%d', $resource->getUuid(), $start - 3600, $start + 86400),
$user,
);
self::assertCount(0, $list['data']);
}
public function testAnotherClinicCannotBlockTheResource(): void
{
[, $resource] = $this->clinicWithResource();
[$other] = $this->clinicWithResource();
$start = time() + 86400;
$this->authJson('POST', "/api/v1/resource/{$resource->getUuid()}/blocks", $other, [
'starts_at' => $start,
'ends_at' => $start + 3600,
]);
self::assertSame(404, $this->responseCode());
}
/**
* ظرفیتی که برمی‌گردد باید همان‌قدر شنیده شود که ظرفیتی که می‌رود: مصرف‌کننده‌ای که
* فقط مسدودسازی را بشنود، منبع را برای همیشه اشغال می‌بیند.
*/
public function testBlockingAndReleasingEachRecordADomainEvent(): void
{
[$user, $resource] = $this->clinicWithResource();
$start = time() + 86400;
$created = $this->authJson('POST', "/api/v1/resource/{$resource->getUuid()}/blocks", $user, [
'starts_at' => $start,
'ends_at' => $start + 3600,
]);
self::assertSame(201, $this->responseCode());
$blocked = $this->latestEvent(DomainEvents::RESOURCE_BLOCKED);
self::assertNotNull($blocked);
self::assertSame($resource->getUuid(), $blocked->getPayload()['resource_uuid']);
self::assertSame($start, $blocked->getPayload()['starts_at']);
$this->authJson('DELETE', "/api/v1/resource-block/{$created['data']['uuid']}", $user);
self::assertSame(200, $this->responseCode());
$released = $this->latestEvent(DomainEvents::RESOURCE_RELEASED);
self::assertNotNull($released);
self::assertSame($created['data']['uuid'], $released->getPayload()['block_uuid']);
}
private function latestEvent(string $name): ?DomainEventLog
{
return $this->em->getRepository(DomainEventLog::class)
->findOneBy(['name' => $name], ['id' => 'DESC']);
}
}