feat(booking): multi-resource holds and confirmation with a database-level guarantee
Section 11 and the third closing rule of the design document: preventing a double booking is the database's job, not the code's. Any "is it free?" check in PHP has a race window between the read and the write — two concurrent requests both see free and both write. MariaDB has no range EXCLUDE constraint, so every occupied interval is broken into fixed five-minute buckets under UNIQUE(resource_id, bucket_at, seat). The code only INSERTs; a rejection from the database *is* the answer. `seat` carries capacity: a three-bed room has seats 0..2, allocation walks upward on each collision, and the fourth concurrent hold finds nowhere to sit. Counting capacity in PHP would have rebuilt the very race this removes. Buckets are written through DBAL rather than the ORM on purpose: a unique violation raised inside flush() closes the EntityManager, and the next seat attempt would then fail with "EntityManager is closed", hiding the real outcome. Occupancy is one row per (segment × resource). The reference test asserts the payoff directly: for a 55-minute appointment of numbing / waiting / laser, the room gets three rows and the operator only two — the operator holds nothing during the wait and stays bookable for someone else. A partial hold never survives. If the second resource has no room, the first is released and the hold itself removed; otherwise a resource stays locked for an appointment that will never exist. Confirming does not re-reserve anything — the seats were taken at hold time and only the label changes. Re-reserving on confirm would reopen the race the hold closed. Cancelling marks rows `released` instead of deleting them, because the history of which resource was busy when is the input to the utilisation reports; the uniqueness buckets *are* deleted, or that interval would stay locked forever. Expired holds are released by the existing scheduler rather than a new one. That exposed a bug in my own change: the flush guard used $count, which now includes released holds, so reset([]) could pass false to save(). It is guarded on $expired. The appointment itself is still built with the existing constructor, so active_slot_key, events and the payment path behave exactly as before — the multi-resource occupancy sits beside them, not instead of them. 12 tests. Two matter most: the second hold on the same resource and interval getting 409, and a test that writes a duplicate bucket row over a *separate connection* and expects the unique-key violation — if that one ever passes silently, the guarantee had moved back into the code. 1208 tests / 3495 assertions. phpstan at its 14-error baseline. Frozen slot contract green. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
This commit is contained in:
@@ -33,7 +33,18 @@ class ResourceOccupancy
|
||||
/** رزرو موقت تا پایان مهلت — تسک ۰۷ آن را مصرف میکند. */
|
||||
public const STATUS_HOLD = 'hold';
|
||||
|
||||
public const STATUSES = [self::STATUS_BOOKED, self::STATUS_HOLD];
|
||||
/**
|
||||
* لغو یا منقضی — ردیف **حذف فیزیکی نمیشود**.
|
||||
*
|
||||
* تاریخچهٔ اینکه چه منبعی کِی گرفته شده بود، ورودی گزارش بهرهوری است و حذفش یعنی
|
||||
* پاک کردن همان چیزی که قرار است اندازه بگیریم.
|
||||
*/
|
||||
public const STATUS_RELEASED = 'released';
|
||||
|
||||
public const STATUSES = [self::STATUS_BOOKED, self::STATUS_HOLD, self::STATUS_RELEASED];
|
||||
|
||||
/** وضعیتهایی که واقعاً منبع را میگیرند. */
|
||||
public const BLOCKING_STATUSES = [self::STATUS_BOOKED, self::STATUS_HOLD];
|
||||
|
||||
#[ORM\Id]
|
||||
#[ORM\GeneratedValue]
|
||||
@@ -63,6 +74,10 @@ class ResourceOccupancy
|
||||
#[ORM\Column(name: 'segment_name', type: 'string', length: 150, nullable: true)]
|
||||
private ?string $segmentName = null;
|
||||
|
||||
/** رزرو موقتی که این اشغال از آن آمده؛ بعد از ثبت نهایی هم نگه داشته میشود. */
|
||||
#[ORM\Column(name: 'hold_id', type: 'integer', nullable: true)]
|
||||
private ?int $holdId = null;
|
||||
|
||||
#[ORM\Column(name: 'created_at', type: 'integer')]
|
||||
private int $createdAt;
|
||||
|
||||
@@ -95,7 +110,13 @@ class ResourceOccupancy
|
||||
public function getStatus(): string { return $this->status; }
|
||||
public function getSegmentName(): ?string { return $this->segmentName; }
|
||||
|
||||
public function getHoldId(): ?int { return $this->holdId; }
|
||||
|
||||
public function setAppointmentId(?int $v): self { $this->appointmentId = $v; return $this; }
|
||||
public function setHoldId(?int $v): self { $this->holdId = $v; return $this; }
|
||||
|
||||
public function markBooked(): self { $this->status = self::STATUS_BOOKED; return $this; }
|
||||
public function markReleased(): self { $this->status = self::STATUS_RELEASED; return $this; }
|
||||
public function setSegmentName(?string $v): self { $this->segmentName = $v; return $this; }
|
||||
|
||||
public function toArray(): array
|
||||
|
||||
@@ -34,6 +34,10 @@ class ResourceOccupancyRepository extends ServiceEntityRepository
|
||||
->where('o.resource IN (:ids)')
|
||||
->andWhere('o.startsAt < :to')
|
||||
->andWhere('o.endsAt > :from')
|
||||
// ردیف آزادشده تاریخچه است، نه اشغال؛ اگر شمرده شود، زمانِ لغوشده هرگز
|
||||
// دوباره پیشنهاد نمیشود.
|
||||
->andWhere('o.status IN (:blocking)')
|
||||
->setParameter('blocking', ResourceOccupancy::BLOCKING_STATUSES)
|
||||
->setParameter('ids', $resourceIds)
|
||||
->setParameter('from', $from)
|
||||
->setParameter('to', $to)
|
||||
|
||||
Reference in New Issue
Block a user