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:
2026-04-10 11:15:19 +02:00
parent 11b9f1d399
commit 10bd55b8ae
40 changed files with 3087 additions and 1080 deletions

View File

@@ -63,7 +63,7 @@ final class EventController extends Controller
}
if (!isset($data['event_type'])) {
$data['event_type'] = empty($data['parent_event_id']) ? 'event' : 'event';
$data['event_type'] = 'event';
}
$event = $organisation->events()->create($data);
@@ -80,6 +80,39 @@ final class EventController extends Controller
return $this->success(new EventResource($event->fresh()));
}
public function destroy(Organisation $organisation, Event $event): JsonResponse
{
Gate::authorize('delete', [$event, $organisation]);
$event->delete();
return $this->success(null, 'Event deleted');
}
public function transition(Request $request, Organisation $organisation, Event $event): JsonResponse
{
Gate::authorize('update', [$event, $organisation]);
$request->validate(['status' => 'required|string']);
$newStatus = $request->status;
$result = $event->canTransitionToWithPrerequisites($newStatus);
if (! empty($result['errors'])) {
return response()->json([
'message' => 'Status transition not possible.',
'errors' => $result['errors'],
'current_status' => $event->status,
'requested_status' => $newStatus,
'allowed_transitions' => Event::STATUS_TRANSITIONS[$event->status] ?? [],
], 422);
}
$event->transitionTo($newStatus);
return $this->success(new EventResource($event->fresh()));
}
public function children(Organisation $organisation, Event $event): AnonymousResourceCollection
{
Gate::authorize('view', [$event, $organisation]);

View File

@@ -23,6 +23,17 @@ final class FestivalSectionController extends Controller
$sections = $event->festivalSections()->ordered()->get();
// For sub-events, also include cross_event sections from the parent festival
if ($event->isSubEvent()) {
$parentCrossEventSections = $event->parent
->festivalSections()
->where('type', 'cross_event')
->ordered()
->get();
$sections = $parentCrossEventSections->merge($sections)->sortBy('sort_order')->values();
}
return FestivalSectionResource::collection($sections);
}
@@ -30,9 +41,39 @@ final class FestivalSectionController extends Controller
{
Gate::authorize('create', [FestivalSection::class, $event]);
$section = $event->festivalSections()->create($request->validated());
$data = $request->validated();
$redirectedToParent = false;
return $this->created(new FestivalSectionResource($section));
if (($data['type'] ?? 'standard') === 'cross_event') {
if ($event->isFlatEvent()) {
return $this->error(
'Overkoepelende secties kunnen alleen worden aangemaakt bij festivals met programmaonderdelen.',
422,
);
}
if ($event->isSubEvent()) {
$event = $event->parent;
Gate::authorize('create', [FestivalSection::class, $event]);
$redirectedToParent = true;
}
}
$section = $event->festivalSections()->create($data);
$response = $this->created(new FestivalSectionResource($section));
if ($redirectedToParent) {
$original = $response->getData(true);
$original['meta'] = [
'redirected_to_parent' => true,
'parent_event_name' => $event->name,
];
return response()->json($original, 201);
}
return $response;
}
public function update(UpdateFestivalSectionRequest $request, Event $event, FestivalSection $section): JsonResponse
@@ -57,10 +98,10 @@ final class FestivalSectionController extends Controller
{
Gate::authorize('reorder', [FestivalSection::class, $event]);
foreach ($request->validated('sections') as $item) {
foreach ($request->validated('sections') as $index => $id) {
$event->festivalSections()
->where('id', $item['id'])
->update(['sort_order' => $item['sort_order']]);
->where('id', $id)
->update(['sort_order' => $index]);
}
$sections = $event->festivalSections()->ordered()->get();

View File

@@ -26,6 +26,7 @@ final class ShiftController extends Controller
$shifts = $section->shifts()
->with(['timeSlot', 'location'])
->withCount(['shiftAssignments as filled_slots' => fn ($q) => $q->where('status', 'approved')])
->get();
return ShiftResource::collection($shifts);
@@ -84,13 +85,11 @@ final class ShiftController extends Controller
}
}
$autoApprove = $section->crew_auto_accepts;
$assignment = $shift->shiftAssignments()->create([
'person_id' => $personId,
'time_slot_id' => $shift->time_slot_id,
'status' => $autoApprove ? 'approved' : 'approved',
'auto_approved' => $autoApprove,
'status' => 'approved',
'auto_approved' => false,
'assigned_by' => $request->user()->id,
'assigned_at' => now(),
'approved_at' => now(),

View File

@@ -17,9 +17,8 @@ final class ReorderFestivalSectionsRequest extends FormRequest
public function rules(): array
{
return [
'sections' => ['required', 'array'],
'sections.*.id' => ['required', 'ulid'],
'sections.*.sort_order' => ['required', 'integer', 'min:0'],
'sections' => ['required', 'array', 'min:1'],
'sections.*' => ['required', 'ulid'],
];
}
}

View File

@@ -4,6 +4,7 @@ declare(strict_types=1);
namespace App\Http\Requests\Api\V1;
use App\Models\Event;
use Illuminate\Foundation\Http\FormRequest;
final class StoreFestivalSectionRequest extends FormRequest
@@ -18,10 +19,26 @@ final class StoreFestivalSectionRequest extends FormRequest
{
return [
'name' => ['required', 'string', 'max:255'],
'category' => ['nullable', 'string', 'max:50'],
'icon' => ['nullable', 'string', 'max:50'],
'sort_order' => ['nullable', 'integer', 'min:0'],
'type' => ['nullable', 'in:standard,cross_event'],
'crew_need' => ['nullable', 'integer', 'min:0'],
'crew_auto_accepts' => ['nullable', 'boolean'],
'crew_invited_to_events' => ['nullable', 'boolean'],
'added_to_timeline' => ['nullable', 'boolean'],
'responder_self_checkin' => ['nullable', 'boolean'],
'crew_accreditation_level' => ['nullable', 'string', 'max:50'],
'public_form_accreditation_level' => ['nullable', 'string', 'max:50'],
'timed_accreditations' => ['nullable', 'boolean'],
];
}
/** @return array<string, string> */
public function messages(): array
{
return [
'type.in' => 'Type moet standard of cross_event zijn.',
];
}
}

View File

@@ -17,12 +17,12 @@ final class UpdateEventRequest extends FormRequest
public function rules(): array
{
return [
// Status changes must go through POST /events/{event}/transition
'name' => ['sometimes', 'string', 'max:255'],
'slug' => ['sometimes', 'string', 'max:255', 'regex:/^[a-z0-9-]+$/'],
'start_date' => ['sometimes', 'date'],
'end_date' => ['sometimes', 'date', 'after_or_equal:start_date'],
'timezone' => ['sometimes', 'string', 'max:50'],
'status' => ['sometimes', 'string', 'in:draft,published,registration_open,buildup,showday,teardown,closed'],
'parent_event_id' => ['nullable', 'ulid', 'exists:events,id'],
'event_type' => ['sometimes', 'in:event,festival,series'],
'event_type_label' => ['nullable', 'string', 'max:50'],

View File

@@ -18,6 +18,8 @@ final class UpdateFestivalSectionRequest extends FormRequest
{
return [
'name' => ['sometimes', 'string', 'max:255'],
'category' => ['sometimes', 'nullable', 'string', 'max:50'],
'icon' => ['sometimes', 'nullable', 'string', 'max:50'],
'sort_order' => ['sometimes', 'integer', 'min:0'],
'type' => ['sometimes', 'in:standard,cross_event'],
'crew_auto_accepts' => ['sometimes', 'boolean'],

View File

@@ -4,6 +4,7 @@ declare(strict_types=1);
namespace App\Http\Resources\Api\V1;
use App\Models\Event;
use Illuminate\Http\Request;
use Illuminate\Http\Resources\Json\JsonResource;
@@ -21,6 +22,7 @@ final class EventResource extends JsonResource
'end_date' => $this->end_date->toDateString(),
'timezone' => $this->timezone,
'status' => $this->status,
'allowed_transitions' => Event::STATUS_TRANSITIONS[$this->status] ?? [],
'event_type' => $this->event_type,
'event_type_label' => $this->event_type_label,
'sub_event_label' => $this->sub_event_label,

View File

@@ -15,13 +15,18 @@ final class FestivalSectionResource extends JsonResource
'id' => $this->id,
'event_id' => $this->event_id,
'name' => $this->name,
'category' => $this->category,
'icon' => $this->icon,
'type' => $this->type,
'sort_order' => $this->sort_order,
'crew_need' => $this->crew_need,
'crew_auto_accepts' => $this->crew_auto_accepts,
'responder_self_checkin' => $this->responder_self_checkin,
'crew_invited_to_events' => $this->crew_invited_to_events,
'added_to_timeline' => $this->added_to_timeline,
'responder_self_checkin' => $this->responder_self_checkin,
'crew_accreditation_level' => $this->crew_accreditation_level,
'public_form_accreditation_level' => $this->public_form_accreditation_level,
'timed_accreditations' => $this->timed_accreditations,
'created_at' => $this->created_at->toIso8601String(),
'shifts_count' => $this->whenCounted('shifts'),
];

View File

@@ -6,6 +6,7 @@ namespace App\Http\Resources\Api\V1;
use Illuminate\Http\Request;
use Illuminate\Http\Resources\Json\JsonResource;
use Illuminate\Support\Carbon;
final class ShiftResource extends JsonResource
{
@@ -26,15 +27,18 @@ final class ShiftResource extends JsonResource
'slots_total' => $this->slots_total,
'slots_open_for_claiming' => $this->slots_open_for_claiming,
'is_lead_role' => $this->is_lead_role,
'report_time' => $this->report_time,
'actual_start_time' => $this->actual_start_time,
'actual_end_time' => $this->actual_end_time,
'report_time' => $this->report_time ? Carbon::parse($this->report_time)->format('H:i') : null,
'actual_start_time' => $this->actual_start_time ? Carbon::parse($this->actual_start_time)->format('H:i') : null,
'actual_end_time' => $this->actual_end_time ? Carbon::parse($this->actual_end_time)->format('H:i') : null,
'end_date' => $this->end_date?->toDateString(),
'allow_overlap' => $this->allow_overlap,
'events_during_shift' => $this->events_during_shift,
'assigned_crew_id' => $this->assigned_crew_id,
'status' => $this->status,
'filled_slots' => $this->filled_slots,
'fill_rate' => $this->fill_rate,
'effective_start_time' => $this->effective_start_time,
'effective_end_time' => $this->effective_end_time,
'effective_start_time' => $this->effective_start_time ? Carbon::parse($this->effective_start_time)->format('H:i') : null,
'effective_end_time' => $this->effective_end_time ? Carbon::parse($this->effective_end_time)->format('H:i') : null,
'created_at' => $this->created_at->toIso8601String(),
'time_slot' => new TimeSlotResource($this->whenLoaded('timeSlot')),
'location' => new LocationResource($this->whenLoaded('location')),

View File

@@ -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;
}
}

View File

@@ -21,6 +21,8 @@ final class FestivalSection extends Model
protected $fillable = [
'event_id',
'name',
'category',
'icon',
'type',
'sort_order',
'crew_need',

View File

@@ -57,4 +57,9 @@ final class Organisation extends Model
{
return $this->hasMany(Company::class);
}
public function personTags(): HasMany
{
return $this->hasMany(PersonTag::class);
}
}

View File

@@ -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

View File

@@ -64,4 +64,31 @@ final class EventPolicy
->wherePivot('role', 'event_manager')
->exists();
}
public function delete(User $user, Event $event, ?Organisation $organisation = null): bool
{
if ($organisation && $event->organisation_id !== $organisation->id) {
return false;
}
if ($user->hasRole('super_admin')) {
return true;
}
// org_admin at organisation level
$isOrgAdmin = $event->organisation->users()
->where('user_id', $user->id)
->wherePivot('role', 'org_admin')
->exists();
if ($isOrgAdmin) {
return true;
}
// event_manager at event level
return $event->users()
->where('user_id', $user->id)
->wherePivot('role', 'event_manager')
->exists();
}
}