feat: festival/series model with sub-events, cross-event sections, tab navigation, SectionsShiftsPanel extraction
Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
@@ -12,6 +12,7 @@ use Illuminate\Database\Eloquent\Relations\BelongsTo;
|
||||
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
|
||||
use Illuminate\Database\Eloquent\Relations\HasMany;
|
||||
use Illuminate\Database\Eloquent\SoftDeletes;
|
||||
use Illuminate\Support\Collection;
|
||||
|
||||
final class Event extends Model
|
||||
{
|
||||
@@ -19,6 +20,34 @@ final class Event extends Model
|
||||
use HasUlids;
|
||||
use SoftDeletes;
|
||||
|
||||
/** @var array<string, list<string>> Allowed status transitions */
|
||||
public const STATUS_TRANSITIONS = [
|
||||
'draft' => ['published'],
|
||||
'published' => ['registration_open', 'draft'],
|
||||
'registration_open' => ['buildup', 'published'],
|
||||
'buildup' => ['showday'],
|
||||
'showday' => ['teardown'],
|
||||
'teardown' => ['closed'],
|
||||
'closed' => [],
|
||||
];
|
||||
|
||||
/**
|
||||
* Statuses that cascade from a festival parent to its children.
|
||||
* When the parent reaches one of these, children in an earlier status follow.
|
||||
*/
|
||||
private const CASCADE_STATUSES = ['showday', 'teardown', 'closed'];
|
||||
|
||||
/** Ordered list used to determine "earlier" status. */
|
||||
private const STATUS_ORDER = [
|
||||
'draft' => 0,
|
||||
'published' => 1,
|
||||
'registration_open' => 2,
|
||||
'buildup' => 3,
|
||||
'showday' => 4,
|
||||
'teardown' => 5,
|
||||
'closed' => 6,
|
||||
];
|
||||
|
||||
protected $fillable = [
|
||||
'organisation_id',
|
||||
'parent_event_id',
|
||||
@@ -101,6 +130,80 @@ final class Event extends Model
|
||||
->orderBy('name');
|
||||
}
|
||||
|
||||
// ----- Status State Machine -----
|
||||
|
||||
public function canTransitionTo(string $newStatus): bool
|
||||
{
|
||||
$allowed = self::STATUS_TRANSITIONS[$this->status] ?? [];
|
||||
|
||||
return in_array($newStatus, $allowed, true);
|
||||
}
|
||||
|
||||
/** @return list<string> Missing prerequisites (empty = OK) */
|
||||
public function getTransitionPrerequisites(string $newStatus): array
|
||||
{
|
||||
$missing = [];
|
||||
|
||||
switch ($newStatus) {
|
||||
case 'published':
|
||||
if (! $this->name || ! $this->start_date || ! $this->end_date) {
|
||||
$missing[] = 'Event must have a name, start date, and end date.';
|
||||
}
|
||||
break;
|
||||
case 'registration_open':
|
||||
if ($this->timeSlots()->count() === 0) {
|
||||
$missing[] = 'At least one time slot must exist before opening registration.';
|
||||
}
|
||||
if ($this->festivalSections()->count() === 0) {
|
||||
$missing[] = 'At least one section must exist before opening registration.';
|
||||
}
|
||||
break;
|
||||
}
|
||||
|
||||
return $missing;
|
||||
}
|
||||
|
||||
/** @return array{errors: list<string>} */
|
||||
public function canTransitionToWithPrerequisites(string $newStatus): array
|
||||
{
|
||||
if (! $this->canTransitionTo($newStatus)) {
|
||||
return ['errors' => ["Status transition from '{$this->status}' to '{$newStatus}' is not allowed."]];
|
||||
}
|
||||
|
||||
return ['errors' => $this->getTransitionPrerequisites($newStatus)];
|
||||
}
|
||||
|
||||
public function transitionTo(string $newStatus): void
|
||||
{
|
||||
if (! $this->canTransitionTo($newStatus)) {
|
||||
throw new \InvalidArgumentException(
|
||||
"Cannot transition from '{$this->status}' to '{$newStatus}'."
|
||||
);
|
||||
}
|
||||
|
||||
$this->update(['status' => $newStatus]);
|
||||
|
||||
if ($this->isFestival() && in_array($newStatus, self::CASCADE_STATUSES, true)) {
|
||||
$this->cascadeStatusToChildren($newStatus);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Cascade a status to all children that are in an earlier lifecycle stage.
|
||||
*/
|
||||
private function cascadeStatusToChildren(string $newStatus): void
|
||||
{
|
||||
$targetOrder = self::STATUS_ORDER[$newStatus];
|
||||
|
||||
$earlierStatuses = array_keys(
|
||||
array_filter(self::STATUS_ORDER, fn (int $order) => $order < $targetOrder)
|
||||
);
|
||||
|
||||
$this->children()
|
||||
->whereIn('status', $earlierStatuses)
|
||||
->update(['status' => $newStatus]);
|
||||
}
|
||||
|
||||
// ----- Scopes -----
|
||||
|
||||
public function scopeTopLevel(Builder $query): Builder
|
||||
@@ -165,4 +268,45 @@ final class Event extends Model
|
||||
{
|
||||
return $query->whereIn('status', ['showday', 'buildup', 'teardown']);
|
||||
}
|
||||
|
||||
/**
|
||||
* Eager-load the event's own time slots plus, for sub-events,
|
||||
* time slots from parent cross_event sections.
|
||||
*/
|
||||
public function scopeWithOperationalContext(Builder $query): Builder
|
||||
{
|
||||
return $query->with(['timeSlots', 'festivalSections', 'parent.timeSlots', 'parent.festivalSections']);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get all time slots relevant for shift planning:
|
||||
* - Flat event: own time slots
|
||||
* - Festival parent: own time slots + all children's time slots
|
||||
* - Sub-event: own time slots + parent's time slots
|
||||
*/
|
||||
public function getAllRelevantTimeSlots(): Collection
|
||||
{
|
||||
$ownSlots = $this->timeSlots()->orderBy('date')->orderBy('start_time')->get();
|
||||
|
||||
if ($this->isFestival()) {
|
||||
$childIds = $this->children()->pluck('id');
|
||||
$childSlots = TimeSlot::whereIn('event_id', $childIds)
|
||||
->orderBy('date')
|
||||
->orderBy('start_time')
|
||||
->get();
|
||||
|
||||
return $ownSlots->merge($childSlots)->sortBy(['date', 'start_time'])->values();
|
||||
}
|
||||
|
||||
if ($this->isSubEvent() && $this->parent_event_id) {
|
||||
$parentSlots = TimeSlot::where('event_id', $this->parent_event_id)
|
||||
->orderBy('date')
|
||||
->orderBy('start_time')
|
||||
->get();
|
||||
|
||||
return $ownSlots->merge($parentSlots)->sortBy(['date', 'start_time'])->values();
|
||||
}
|
||||
|
||||
return $ownSlots;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -21,6 +21,8 @@ final class FestivalSection extends Model
|
||||
protected $fillable = [
|
||||
'event_id',
|
||||
'name',
|
||||
'category',
|
||||
'icon',
|
||||
'type',
|
||||
'sort_order',
|
||||
'crew_need',
|
||||
|
||||
@@ -57,4 +57,9 @@ final class Organisation extends Model
|
||||
{
|
||||
return $this->hasMany(Company::class);
|
||||
}
|
||||
|
||||
public function personTags(): HasMany
|
||||
{
|
||||
return $this->hasMany(PersonTag::class);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -45,6 +45,7 @@ final class Shift extends Model
|
||||
return [
|
||||
'is_lead_role' => 'boolean',
|
||||
'allow_overlap' => 'boolean',
|
||||
'end_date' => 'date',
|
||||
'events_during_shift' => 'array',
|
||||
'slots_total' => 'integer',
|
||||
'slots_open_for_claiming' => 'integer',
|
||||
@@ -93,7 +94,13 @@ final class Shift extends Model
|
||||
|
||||
protected function filledSlots(): Attribute
|
||||
{
|
||||
return Attribute::get(fn () => $this->shiftAssignments()->where('status', 'approved')->count());
|
||||
return Attribute::get(function () {
|
||||
if (array_key_exists('filled_slots', $this->attributes)) {
|
||||
return (int) $this->attributes['filled_slots'];
|
||||
}
|
||||
|
||||
return $this->shiftAssignments()->where('status', 'approved')->count();
|
||||
});
|
||||
}
|
||||
|
||||
protected function fillRate(): Attribute
|
||||
|
||||
Reference in New Issue
Block a user