feat(events): domain event outbox and the two reports that close the loop

Tasks 07 through 13 each changed something the rest of the system might want
to know about, with no contract for saying so. And task 05 shipped a powerful
segment editor with no feedback on whether a clinic defined its segments right.

Events
- A closed list of names, because a consumer branches on the string and a
  one-letter typo would produce an event nobody hears and no error either
- Payloads carry uuids and scalars only; non-scalars are dropped, not
  serialised, so a consumer always fetches fresh rather than reading a stale
  detached entity
- record() deliberately does not flush: the event row commits with the change
  it describes, so a rolled-back transaction leaves no event behind. A test
  pins exactly that
- app:events:publish drains the outbox; five failed attempts park a row with
  its error rather than deleting it, because a silently dropped event is a
  loss with no trace. app:events:prune only ever removes published rows

Reports
- Resource utilisation separates available, occupied and active minutes.
  The gap between occupied and active is what exposes a bad segment
  definition, and available is multiplied by capacity so a three-chair room
  does not read as permanently over 100%
- A resource with no calendar reports utilization: null, not zero — dividing
  by zero means something different from being idle
- Plan accuracy compares planned against actual duration per service and
  flags both directions: running short wastes capacity that could have been
  sold. Its row links straight to editing that service's segments, because a
  report with no route to a fix does not get read

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
This commit is contained in:
hamed
2026-07-31 12:27:54 +03:30
co-authored by Claude Opus 5
parent a379111606
commit 3c43955800
32 changed files with 2313 additions and 73 deletions
@@ -10,6 +10,8 @@ use App\Package\Service\CreditLedgerService;
use App\Course\Service\CourseSessionLinker;
use App\Package\Service\PackageConsumptionService;
use App\Shared\Constant\ErrorCodes;
use App\Shared\Event\DomainEventPublisher;
use App\Shared\Event\DomainEvents;
use App\Shared\Exception\AppException;
use Doctrine\ORM\EntityManagerInterface;
@@ -27,6 +29,7 @@ final class BookingService
private readonly PackageConsumptionService $packages,
private readonly CreditLedgerService $credits,
private readonly CourseSessionLinker $courseSessions,
private readonly DomainEventPublisher $events,
private readonly EntityManagerInterface $em,
) {}
@@ -58,6 +61,16 @@ final class BookingService
$this->writeSegments($hold, $appointment);
$hold->markConfirmed($now);
// رویداد در همان flushِ ثبت نوبت می‌رود؛ اگر این تراکنش برگردد، رویدادی هم
// نمی‌ماند که کسی به آن واکنش نشان دهد.
$this->events->record(
$appointment->getEntityType(),
$appointment->getEntityId(),
DomainEvents::APPOINTMENT_BOOKED,
['appointment_uuid' => $appointment->getUuid(), 'hold_uuid' => $hold->getUuid()],
$now,
);
$this->em->flush();
// مصرف اعتبار **اینجا**ست نه در پیش‌نمایش قیمت: تنها لحظه‌ای که نوبت واقعاً
@@ -112,6 +125,13 @@ final class BookingService
// جلسهٔ دوره به `planned` برمی‌گردد؛ بقیهٔ جلسات دست‌نخورده می‌مانند.
$this->courseSessions->unlink($appointment);
$this->events->recordAndFlush(
$appointment->getEntityType(),
$appointment->getEntityId(),
DomainEvents::APPOINTMENT_CANCELLED,
['appointment_uuid' => $appointment->getUuid(), 'released_resources' => count($occupancies)],
);
return count($occupancies);
}
@@ -9,6 +9,8 @@ use App\Appointment\Plan\ValueObject\AppointmentPlan;
use App\Auth\Entity\User;
use App\Resource\Entity\ClinicResource;
use App\Shared\Constant\ErrorCodes;
use App\Shared\Event\DomainEventPublisher;
use App\Shared\Event\DomainEvents;
use App\Shared\Exception\AppException;
use Doctrine\DBAL\Exception\UniqueConstraintViolationException;
use Doctrine\ORM\EntityManagerInterface;
@@ -34,6 +36,7 @@ use Doctrine\ORM\EntityManagerInterface;
final class HoldService
{
public function __construct(
private readonly DomainEventPublisher $events,
private readonly EntityManagerInterface $em,
) {}
@@ -95,6 +98,16 @@ final class HoldService
throw $e;
}
// بعد از اینکه **همهٔ** منابع گرفته شدند، نه پیش از آن: رزروی که وسط کار
// شکسته، رویدادی هم ندارد.
$this->events->recordAndFlush(
$entityType,
$entityId,
DomainEvents::HOLD_CREATED,
['hold_uuid' => $hold->getUuid(), 'starts_at' => $startsAt, 'resources' => count($taken)],
$now,
);
return $hold;
}