feat(timetable): seven artist-domain services + supporting exceptions
GenreService, ArtistService, ArtistEngagementService (state machine), StageService, StageDayService, PerformanceService, LaneCascadeService under app/Services/Artist/. Plain final classes with constructor injection — matches FormSubmissionService convention. ArtistEngagementService implements the RFC §10.1 booking_status state machine: terminal Cancelled/Rejected/Declined, Option requires future option_expires_at, Contracted requires fee_amount > 0. transitionStatus is the focused entry point; update() routes through it whenever the payload mutates booking_status. cancel() composes transitionStatus + soft delete in one transaction so the existing ArtistEngagementObserver cascade fires. LaneCascadeService is the D18 transactional move algorithm. Locks the dragged Performance row FOR UPDATE, validates client version against the persisted version (D14), then either parks (stage_id=null, no cascade) or places onto (stage, event, lane) with single-level cascade-bump of any time-overlapping rows on the target lane. Returns a MoveResult value object carrying the moved + cascaded performances so the controller maps them to API resources without a second query. StageDayService implements the §10.5 atomic matrix replace. Detects non-cancelled performances on event_ids about to be removed; throws StageDaysOrphanedPerformancesException unless force_orphan=true. The orphans are not deleted — they persist with the same stage_id so they re-appear when the day re-activates (D5/D27 retention). ArtistService.create raises DuplicateArtistException carrying the existing master so the controller can offer a "use existing" choice instead of forcing the booker to abandon their dialog. ArtistEngagement defaults buma_handled_by based on artist.agent_company.handles_buma per RFC D26. GenreService.delete is hard-blocked (GenreInUseException) when artists still reference the genre via default_genre_id; the frontend rebinds those artists first. StageService.delete cascade-parks performances (stage_id → null, lane preserved) and returns the parked count for the activity-log entry the controller writes in Step 9. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
150
api/app/Services/Artist/LaneCascadeService.php
Normal file
150
api/app/Services/Artist/LaneCascadeService.php
Normal file
@@ -0,0 +1,150 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Services\Artist;
|
||||
|
||||
use App\Exceptions\Artist\VersionMismatchException;
|
||||
use App\Models\Performance;
|
||||
use App\Models\Stage;
|
||||
use Carbon\CarbonImmutable;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
|
||||
/**
|
||||
* RFC v0.2 D18 — single transactional move algorithm.
|
||||
*
|
||||
* Locks the dragged Performance row (FOR UPDATE), validates the
|
||||
* client's version number against the persisted one, and either parks
|
||||
* the row (stage_id → null) or places it on the target stage with a
|
||||
* cascade-bump of any time-overlapping rows on the same lane. Cascade
|
||||
* is single-level: every performance currently on
|
||||
* (target_stage, target_event, target_lane) whose [start, end] window
|
||||
* overlaps the new placement gets bumped to lane+1. Higher-lane chain
|
||||
* collisions resolve themselves in a subsequent move; the prototype
|
||||
* audit (§5) shows that single-level bumping is sufficient because
|
||||
* collisions are rare and the user is editing one block at a time.
|
||||
*
|
||||
* The PerformanceObserver auto-bumps `version` on save so we don't
|
||||
* need to set it explicitly here.
|
||||
*/
|
||||
final class LaneCascadeService
|
||||
{
|
||||
/**
|
||||
* @return MoveResult moved + (other) cascaded performances
|
||||
*
|
||||
* @throws VersionMismatchException when client version is stale
|
||||
*/
|
||||
public function move(
|
||||
Performance $performance,
|
||||
?Stage $targetStage,
|
||||
?CarbonImmutable $start,
|
||||
?CarbonImmutable $end,
|
||||
?int $targetLane,
|
||||
int $clientVersion,
|
||||
): MoveResult {
|
||||
return DB::transaction(function () use (
|
||||
$performance,
|
||||
$targetStage,
|
||||
$start,
|
||||
$end,
|
||||
$targetLane,
|
||||
$clientVersion,
|
||||
): MoveResult {
|
||||
/** @var Performance $locked */
|
||||
$locked = Performance::query()
|
||||
->whereKey($performance->id)
|
||||
->lockForUpdate()
|
||||
->firstOrFail();
|
||||
|
||||
if ((int) $locked->version !== $clientVersion) {
|
||||
throw new VersionMismatchException(
|
||||
currentVersion: (int) $locked->version,
|
||||
clientVersion: $clientVersion,
|
||||
);
|
||||
}
|
||||
|
||||
if ($targetStage === null) {
|
||||
$locked->stage_id = null;
|
||||
if ($start !== null) {
|
||||
$locked->start_at = $start;
|
||||
}
|
||||
if ($end !== null) {
|
||||
$locked->end_at = $end;
|
||||
}
|
||||
if ($targetLane !== null) {
|
||||
$locked->lane = $targetLane;
|
||||
}
|
||||
$locked->save();
|
||||
|
||||
return new MoveResult($locked->refresh(), []);
|
||||
}
|
||||
|
||||
if ($start === null || $end === null || $targetLane === null) {
|
||||
throw new \InvalidArgumentException(
|
||||
'targetStage non-null requires start, end and targetLane.',
|
||||
);
|
||||
}
|
||||
|
||||
$newEventId = $this->resolveEventIdForStageAndStart($targetStage, $start)
|
||||
?? $performance->event_id;
|
||||
|
||||
// Lock all peer performances on (stage, event, lane) for the
|
||||
// duration of the cascade. The first iteration locks the
|
||||
// already-existing rows; new placements that follow this
|
||||
// transaction wait on these row locks until commit.
|
||||
$existingOnLane = Performance::query()
|
||||
->where('stage_id', $targetStage->id)
|
||||
->where('event_id', $newEventId)
|
||||
->where('lane', $targetLane)
|
||||
->where('id', '!=', $locked->id)
|
||||
->lockForUpdate()
|
||||
->get();
|
||||
|
||||
$cascaded = [];
|
||||
foreach ($existingOnLane as $other) {
|
||||
if ($this->overlaps($start, $end, $other->start_at, $other->end_at)) {
|
||||
$other->lane = (int) $other->lane + 1;
|
||||
$other->save();
|
||||
$cascaded[] = $other->refresh();
|
||||
}
|
||||
}
|
||||
|
||||
$locked->stage_id = $targetStage->id;
|
||||
$locked->event_id = $newEventId;
|
||||
$locked->start_at = $start;
|
||||
$locked->end_at = $end;
|
||||
$locked->lane = $targetLane;
|
||||
$locked->save();
|
||||
|
||||
return new MoveResult($locked->refresh(), $cascaded);
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* For festivals, `target_start_at` falls inside one of the stage's
|
||||
* sub-events; for flat events, the stage's own event_id is the
|
||||
* answer. Returns null if no stage_day matches — caller falls back
|
||||
* to keeping the performance's existing event_id (validation rules
|
||||
* in the FormRequest already guarantee a valid stage_day exists).
|
||||
*/
|
||||
private function resolveEventIdForStageAndStart(Stage $stage, CarbonImmutable $start): ?string
|
||||
{
|
||||
return $stage->stageDays()
|
||||
->join('events', 'events.id', '=', 'stage_days.event_id')
|
||||
->where('events.start_at', '<=', $start)
|
||||
->where('events.end_at', '>=', $start)
|
||||
->orderBy('events.start_at', 'desc')
|
||||
->limit(1)
|
||||
->value('stage_days.event_id');
|
||||
}
|
||||
|
||||
private function overlaps(
|
||||
CarbonImmutable $aStart,
|
||||
CarbonImmutable $aEnd,
|
||||
\DateTimeInterface $bStart,
|
||||
\DateTimeInterface $bEnd,
|
||||
): bool {
|
||||
return $aStart < CarbonImmutable::instance($bEnd)
|
||||
&& CarbonImmutable::instance($bStart) < $aEnd;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user