|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace He4rt\Events\Http\Controllers\Mobile; |
| 6 | + |
| 7 | +use App\Http\Controllers\Controller; |
| 8 | +use He4rt\Events\CheckIn\Actions\NumericCodeCheckInAction; |
| 9 | +use He4rt\Events\CheckIn\DTOs\NumericCodeCheckInDTO; |
| 10 | +use He4rt\Events\CheckIn\Exceptions\CheckInException; |
| 11 | +use He4rt\Events\CheckIn\Rules\CheckInCodeRule; |
| 12 | +use He4rt\Events\Enrollment\Actions\EnrollUserAction; |
| 13 | +use He4rt\Events\Enrollment\DTOs\EnrollUserDTO; |
| 14 | +use He4rt\Events\Enrollment\Enums\EnrollmentStatus; |
| 15 | +use He4rt\Events\Enrollment\Exceptions\EnrollmentException; |
| 16 | +use He4rt\Events\Enrollment\Models\Enrollment; |
| 17 | +use He4rt\Events\Event\Models\Event; |
| 18 | +use He4rt\Events\Http\Resources\EventDetailResource; |
| 19 | +use He4rt\Events\Http\Resources\EventResource; |
| 20 | +use He4rt\Identity\User\Models\User; |
| 21 | +use Illuminate\Database\Eloquent\Relations\Relation; |
| 22 | +use Illuminate\Http\JsonResponse; |
| 23 | +use Illuminate\Http\Request; |
| 24 | +use Illuminate\Http\Resources\Json\AnonymousResourceCollection; |
| 25 | +use Illuminate\Http\Response; |
| 26 | +use Illuminate\Support\Facades\Date; |
| 27 | +use Illuminate\Support\Facades\RateLimiter; |
| 28 | +use Symfony\Component\HttpKernel\Exception\NotFoundHttpException; |
| 29 | + |
| 30 | +final class MobileEventController extends Controller |
| 31 | +{ |
| 32 | + /** |
| 33 | + * Listar eventos |
| 34 | + * |
| 35 | + * Retorna os eventos visíveis a participantes (publicados, concluídos |
| 36 | + * ou cancelados), mais próximos primeiro, com o status de inscrição |
| 37 | + * do usuário autenticado quando houver. |
| 38 | + */ |
| 39 | + public function index(Request $request): AnonymousResourceCollection |
| 40 | + { |
| 41 | + /** @var User $user */ |
| 42 | + $user = $request->user(); |
| 43 | + |
| 44 | + $events = Event::query() |
| 45 | + ->with(['enrollments' => fn (Relation $query): Relation => $query->where('user_id', $user->id)]) |
| 46 | + ->viewableByParticipant() |
| 47 | + ->orderBy('starts_at') |
| 48 | + ->simplePaginate(20); |
| 49 | + |
| 50 | + return EventResource::collection($events); |
| 51 | + } |
| 52 | + |
| 53 | + /** |
| 54 | + * Detalhe do evento |
| 55 | + * |
| 56 | + * Inclui a inscrição do usuário (se houver) e os mesmos booleans que |
| 57 | + * o painel web usa pra decidir a ação disponível (can_apply, |
| 58 | + * can_confirm_presence, is_event_full). |
| 59 | + */ |
| 60 | + public function show(Request $request, Event $event): EventDetailResource |
| 61 | + { |
| 62 | + throw_unless($event->status->isViewableByParticipant(), NotFoundHttpException::class); |
| 63 | + |
| 64 | + $event->load('enrollmentPolicy'); |
| 65 | + |
| 66 | + /** @var User $user */ |
| 67 | + $user = $request->user(); |
| 68 | + |
| 69 | + $enrollment = Enrollment::query() |
| 70 | + ->where('event_id', $event->id) |
| 71 | + ->where('user_id', $user->id) |
| 72 | + ->first(); |
| 73 | + |
| 74 | + return new EventDetailResource($event, $enrollment); |
| 75 | + } |
| 76 | + |
| 77 | + /** |
| 78 | + * Inscrever-se no evento |
| 79 | + * |
| 80 | + * Cobre RSVP e candidatura (application) — a mesma Action decide o |
| 81 | + * status inicial (confirmed, waitlisted ou pending) de acordo com a |
| 82 | + * política de inscrição do evento. |
| 83 | + */ |
| 84 | + public function enroll(Request $request, Event $event, EnrollUserAction $enrollUser): JsonResponse |
| 85 | + { |
| 86 | + throw_unless($event->status->isViewableByParticipant(), NotFoundHttpException::class); |
| 87 | + |
| 88 | + $request->validate([ |
| 89 | + 'application_data' => ['sometimes', 'array'], |
| 90 | + ]); |
| 91 | + |
| 92 | + /** @var array<string, mixed>|null $applicationData */ |
| 93 | + $applicationData = $request->input('application_data'); |
| 94 | + |
| 95 | + /** @var User $user */ |
| 96 | + $user = $request->user(); |
| 97 | + |
| 98 | + try { |
| 99 | + $enrollment = $enrollUser->handle(new EnrollUserDTO( |
| 100 | + eventId: $event->id, |
| 101 | + userId: $user->id, |
| 102 | + applicationData: $applicationData, |
| 103 | + )); |
| 104 | + } catch (EnrollmentException $enrollmentException) { |
| 105 | + return response()->json(['message' => $enrollmentException->getMessage()], $enrollmentException->getCode()); |
| 106 | + } |
| 107 | + |
| 108 | + return response()->json([ |
| 109 | + 'status' => $enrollment->status->value, |
| 110 | + 'waitlist_position' => $enrollment->waitlist_position, |
| 111 | + ], Response::HTTP_CREATED); |
| 112 | + } |
| 113 | + |
| 114 | + /** |
| 115 | + * Check-in por código numérico |
| 116 | + * |
| 117 | + * Único método de check-in self-service disponível hoje (QR fica pra |
| 118 | + * uma iteração futura — ver docs/plans/2026-09-22-api-mobile-jwt.md). |
| 119 | + */ |
| 120 | + public function checkIn(Request $request, Event $event, NumericCodeCheckInAction $checkIn): JsonResponse |
| 121 | + { |
| 122 | + throw_unless($event->status->isViewableByParticipant(), NotFoundHttpException::class); |
| 123 | + |
| 124 | + $request->validate([ |
| 125 | + 'code' => ['required', 'string', new CheckInCodeRule], |
| 126 | + ]); |
| 127 | + |
| 128 | + $code = $request->string('code')->toString(); |
| 129 | + |
| 130 | + /** @var User $user */ |
| 131 | + $user = $request->user(); |
| 132 | + |
| 133 | + $enrollment = Enrollment::query() |
| 134 | + ->where('event_id', $event->id) |
| 135 | + ->where('user_id', $user->id) |
| 136 | + ->first(); |
| 137 | + |
| 138 | + if (!$enrollment instanceof Enrollment || !in_array($enrollment->status, [EnrollmentStatus::Confirmed, EnrollmentStatus::CheckedIn], strict: true)) { |
| 139 | + return response()->json(['message' => 'Not enrolled in this event.'], Response::HTTP_UNPROCESSABLE_ENTITY); |
| 140 | + } |
| 141 | + |
| 142 | + $rateLimitKey = $this->checkInRateLimitKey($user->id, $event->id, $request->ip()); |
| 143 | + |
| 144 | + if (RateLimiter::tooManyAttempts($rateLimitKey, 5)) { |
| 145 | + return response()->json([ |
| 146 | + 'message' => CheckInException::checkInCodeRateLimited(RateLimiter::availableIn($rateLimitKey))->getMessage(), |
| 147 | + ], Response::HTTP_TOO_MANY_REQUESTS); |
| 148 | + } |
| 149 | + |
| 150 | + RateLimiter::hit($rateLimitKey, 60); |
| 151 | + |
| 152 | + try { |
| 153 | + $checkInRecord = $checkIn->handle(new NumericCodeCheckInDTO( |
| 154 | + enrollment: $enrollment, |
| 155 | + code: $code, |
| 156 | + eventDate: Date::today(), |
| 157 | + )); |
| 158 | + } catch (CheckInException $checkInException) { |
| 159 | + return response()->json(['message' => $checkInException->getMessage()], $checkInException->getCode()); |
| 160 | + } |
| 161 | + |
| 162 | + RateLimiter::clear($rateLimitKey); |
| 163 | + |
| 164 | + return response()->json([ |
| 165 | + 'event_date' => $checkInRecord->event_date->toDateString(), |
| 166 | + 'checked_in_at' => $checkInRecord->checked_in_at?->toIso8601String(), |
| 167 | + ], Response::HTTP_CREATED); |
| 168 | + } |
| 169 | + |
| 170 | + private function checkInRateLimitKey(string $userId, string $eventId, ?string $ip): string |
| 171 | + { |
| 172 | + return sprintf('numeric-code-check-in:%s:%s:%s', $userId, $eventId, md5($ip ?? 'unknown')); |
| 173 | + } |
| 174 | +} |
0 commit comments