76 lines
3.0 KiB
PHP
76 lines
3.0 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
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
|
|
{
|
|
public function toArray(Request $request): array
|
|
{
|
|
return [
|
|
'id' => $this->id,
|
|
'festival_section_id' => $this->festival_section_id,
|
|
'time_slot_id' => $this->time_slot_id,
|
|
'location_id' => $this->location_id,
|
|
'title' => $this->title,
|
|
'description' => $this->description,
|
|
'instructions' => $this->instructions,
|
|
'coordinator_notes' => $this->when(
|
|
$this->shouldShowCoordinatorNotes($request),
|
|
$this->coordinator_notes,
|
|
),
|
|
'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 ? 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 ? 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')),
|
|
'festival_section' => new FestivalSectionResource($this->whenLoaded('festivalSection')),
|
|
];
|
|
}
|
|
|
|
private function shouldShowCoordinatorNotes(Request $request): bool
|
|
{
|
|
$user = $request->user();
|
|
if (! $user) {
|
|
return false;
|
|
}
|
|
|
|
if ($user->hasRole('super_admin')) {
|
|
return true;
|
|
}
|
|
|
|
$shift = $this->resource;
|
|
$event = $shift->festivalSection?->event;
|
|
if (! $event) {
|
|
return false;
|
|
}
|
|
|
|
return $event->organisation->users()
|
|
->where('user_id', $user->id)
|
|
->wherePivot('role', 'org_admin')
|
|
->exists()
|
|
|| $event->users()
|
|
->where('user_id', $user->id)
|
|
->wherePivot('role', 'event_manager')
|
|
->exists();
|
|
}
|
|
}
|