|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace App\Http\Controllers\API; |
| 4 | + |
| 5 | +use App\Http\Controllers\Controller; |
| 6 | +use App\Http\Resources\Party as PartyResource; |
| 7 | +use App\Models\Group; |
| 8 | +use App\Models\Party; |
| 9 | +use Carbon\Carbon; |
| 10 | +use Illuminate\Database\Eloquent\Builder; |
| 11 | +use Illuminate\Http\JsonResponse; |
| 12 | +use Illuminate\Http\Request; |
| 13 | + |
| 14 | +class PublicEventController extends Controller |
| 15 | +{ |
| 16 | + public function listEvents(Request $request): JsonResponse |
| 17 | + { |
| 18 | + return $this->listWithFilters($request); |
| 19 | + } |
| 20 | + |
| 21 | + public function showEvent(Request $request, int $id): JsonResponse |
| 22 | + { |
| 23 | + $query = $this->buildBaseEventQuery(); |
| 24 | + $this->applyClientRestrictions($query, $request); |
| 25 | + |
| 26 | + $event = $query |
| 27 | + ->where('events.idevents', $id) |
| 28 | + ->firstOrFail(); |
| 29 | + |
| 30 | + return response()->json([ |
| 31 | + 'data' => $this->toPublicEventArray($event), |
| 32 | + ]); |
| 33 | + } |
| 34 | + |
| 35 | + public function listGroupEvents(Request $request, int $id): JsonResponse |
| 36 | + { |
| 37 | + Group::findOrFail($id); |
| 38 | + |
| 39 | + return $this->listWithFilters($request, function (Builder $query) use ($id) { |
| 40 | + $query->where('events.group', $id); |
| 41 | + }); |
| 42 | + } |
| 43 | + |
| 44 | + private function listWithFilters(Request $request, ?callable $filter = null): JsonResponse |
| 45 | + { |
| 46 | + $validated = $request->validate([ |
| 47 | + 'start' => ['nullable', 'date'], |
| 48 | + 'end' => ['nullable', 'date'], |
| 49 | + 'updated_start' => ['nullable', 'date'], |
| 50 | + 'updated_end' => ['nullable', 'date'], |
| 51 | + 'page' => ['nullable', 'integer', 'min:1'], |
| 52 | + 'per_page' => ['nullable', 'integer', 'min:1', 'max:100'], |
| 53 | + ]); |
| 54 | + |
| 55 | + $query = $this->buildBaseEventQuery(); |
| 56 | + $this->applyClientRestrictions($query, $request); |
| 57 | + |
| 58 | + if ($filter) { |
| 59 | + $filter($query); |
| 60 | + } |
| 61 | + |
| 62 | + $this->applyDateFilters($query, $validated); |
| 63 | + |
| 64 | + $maxUpdatedAt = (clone $query)->max('events.updated_at'); |
| 65 | + |
| 66 | + $perPage = (int) ($validated['per_page'] ?? 50); |
| 67 | + $paginator = $query->paginate($perPage); |
| 68 | + |
| 69 | + return response()->json([ |
| 70 | + 'data' => $paginator->getCollection()->map(function (Party $event) { |
| 71 | + return $this->toPublicEventArray($event); |
| 72 | + })->values(), |
| 73 | + 'meta' => [ |
| 74 | + 'page' => $paginator->currentPage(), |
| 75 | + 'per_page' => $paginator->perPage(), |
| 76 | + 'total' => $paginator->total(), |
| 77 | + 'last_page' => $paginator->lastPage(), |
| 78 | + ], |
| 79 | + 'sync' => [ |
| 80 | + 'generated_at' => Carbon::now()->toIso8601String(), |
| 81 | + 'max_updated_at' => $maxUpdatedAt ? Carbon::parse($maxUpdatedAt)->toIso8601String() : null, |
| 82 | + ], |
| 83 | + ]); |
| 84 | + } |
| 85 | + |
| 86 | + private function toPublicEventArray(Party $event): array |
| 87 | + { |
| 88 | + $data = PartyResource::make($event)->resolve(); |
| 89 | + |
| 90 | + // Keep payload lightweight for third-party display use-cases. |
| 91 | + unset($data['stats'], $data['network_data']); |
| 92 | + |
| 93 | + if (isset($data['group']) && is_array($data['group'])) { |
| 94 | + unset($data['group']['networks']); |
| 95 | + } |
| 96 | + |
| 97 | + return $data; |
| 98 | + } |
| 99 | + |
| 100 | + private function buildBaseEventQuery(): Builder |
| 101 | + { |
| 102 | + return Party::query() |
| 103 | + ->join('groups', 'groups.idgroups', '=', 'events.group') |
| 104 | + ->whereNull('events.deleted_at') |
| 105 | + ->whereNull('groups.deleted_at') |
| 106 | + ->where('events.approved', true) |
| 107 | + ->where('groups.approved', true) |
| 108 | + ->distinct() |
| 109 | + ->select('events.*') |
| 110 | + ->orderBy('events.event_start_utc', 'asc'); |
| 111 | + } |
| 112 | + |
| 113 | + private function applyClientRestrictions(Builder $query, Request $request): void |
| 114 | + { |
| 115 | + $client = $request->attributes->get('apiClient'); |
| 116 | + $allowedNetworkIds = $client?->allowed_network_ids ?: []; |
| 117 | + |
| 118 | + if (!empty($allowedNetworkIds)) { |
| 119 | + $query->join('group_network as permitted_network', 'permitted_network.group_id', '=', 'groups.idgroups') |
| 120 | + ->whereIn('permitted_network.network_id', $allowedNetworkIds); |
| 121 | + } |
| 122 | + } |
| 123 | + |
| 124 | + private function applyDateFilters(Builder $query, array $validated): void |
| 125 | + { |
| 126 | + if (!empty($validated['start'])) { |
| 127 | + $start = Carbon::parse($validated['start'])->setTimezone('UTC')->toIso8601String(); |
| 128 | + $query->where('events.event_start_utc', '>=', $start); |
| 129 | + } else { |
| 130 | + $query->where('events.event_end_utc', '>=', Carbon::now()->setTimezone('UTC')->toIso8601String()); |
| 131 | + } |
| 132 | + |
| 133 | + if (!empty($validated['end'])) { |
| 134 | + $end = Carbon::parse($validated['end'])->setTimezone('UTC')->toIso8601String(); |
| 135 | + $query->where('events.event_end_utc', '<=', $end); |
| 136 | + } |
| 137 | + |
| 138 | + if (!empty($validated['updated_start'])) { |
| 139 | + $updatedStart = Carbon::parse($validated['updated_start'])->setTimezone('UTC')->toDateTimeString(); |
| 140 | + $query->where('events.updated_at', '>=', $updatedStart); |
| 141 | + } |
| 142 | + |
| 143 | + if (!empty($validated['updated_end'])) { |
| 144 | + $updatedEnd = Carbon::parse($validated['updated_end'])->setTimezone('UTC')->toDateTimeString(); |
| 145 | + $query->where('events.updated_at', '<=', $updatedEnd); |
| 146 | + } |
| 147 | + } |
| 148 | + |
| 149 | +} |
0 commit comments