From c98a1002d617036cef62666f0bb44726845c5664 Mon Sep 17 00:00:00 2001 From: Davi Castello Branco Tavares de Oliveira Date: Fri, 22 May 2026 20:05:01 -0400 Subject: [PATCH 1/7] feat(events): add manual participant check-in Add CheckInAction, after-commit domain event, audit transition records, per-day duplicate protection, Admin panel single/bulk check-in actions, and feature coverage for manual and multi-day check-ins. --- .../database/factories/CheckInFactory.php | 1 + ...hecked_in_at_to_events_check_ins_table.php | 24 +++ app-modules/events/lang/en/exceptions.php | 4 + app-modules/events/lang/pt_BR/exceptions.php | 4 + .../src/CheckIn/Actions/CheckInAction.php | 136 ++++++++++++++ .../CheckIn/Events/ParticipantCheckedIn.php | 22 +++ .../events/src/CheckIn/Models/CheckIn.php | 3 + .../Exceptions/EnrollmentException.php | 32 ++++ .../tests/Feature/CheckInActionTest.php | 167 ++++++++++++++++++ .../tests/Feature/EventResourceTest.php | 88 +++++++++ .../EnrollmentsRelationManager.php | 105 +++++++++++ 11 files changed, 586 insertions(+) create mode 100644 app-modules/events/database/migrations/2026_05_22_000001_add_checked_in_at_to_events_check_ins_table.php create mode 100644 app-modules/events/src/CheckIn/Actions/CheckInAction.php create mode 100644 app-modules/events/src/CheckIn/Events/ParticipantCheckedIn.php create mode 100644 app-modules/events/tests/Feature/CheckInActionTest.php diff --git a/app-modules/events/database/factories/CheckInFactory.php b/app-modules/events/database/factories/CheckInFactory.php index fa621af9f..2392e1fec 100644 --- a/app-modules/events/database/factories/CheckInFactory.php +++ b/app-modules/events/database/factories/CheckInFactory.php @@ -22,6 +22,7 @@ public function definition(): array 'event_date' => Date::today(), 'method' => fake()->randomElement(CheckInMethod::cases()), 'payload' => null, + 'checked_in_at' => Date::now(), ]; } } diff --git a/app-modules/events/database/migrations/2026_05_22_000001_add_checked_in_at_to_events_check_ins_table.php b/app-modules/events/database/migrations/2026_05_22_000001_add_checked_in_at_to_events_check_ins_table.php new file mode 100644 index 000000000..8b7967eb8 --- /dev/null +++ b/app-modules/events/database/migrations/2026_05_22_000001_add_checked_in_at_to_events_check_ins_table.php @@ -0,0 +1,24 @@ +timestampTz('checked_in_at')->nullable()->after('payload'); + }); + } + + public function down(): void + { + Schema::table('events_check_ins', function (Blueprint $table): void { + $table->dropColumn('checked_in_at'); + }); + } +}; diff --git a/app-modules/events/lang/en/exceptions.php b/app-modules/events/lang/en/exceptions.php index 056dd6539..5f21cebf3 100644 --- a/app-modules/events/lang/en/exceptions.php +++ b/app-modules/events/lang/en/exceptions.php @@ -8,4 +8,8 @@ 'event_not_active' => 'This event is not available for enrollment.', 'invalid_enrollment_method' => 'This event requires application enrollment, not RSVP.', 'event_full' => 'This event has reached maximum capacity and does not have a waitlist.', + 'invalid_check_in_status' => 'Only confirmed or checked-in enrollments can be checked in.', + 'check_in_outside_event_date_range' => 'Check-in date must be within the event date range.', + 'already_checked_in_for_date' => 'This enrollment has already checked in for this date.', + 'invalid_check_in_actor' => 'Manual check-in requires the organizer user id.', ]; diff --git a/app-modules/events/lang/pt_BR/exceptions.php b/app-modules/events/lang/pt_BR/exceptions.php index 27d80f367..08bca7c23 100644 --- a/app-modules/events/lang/pt_BR/exceptions.php +++ b/app-modules/events/lang/pt_BR/exceptions.php @@ -8,4 +8,8 @@ 'event_not_active' => 'Este evento não está disponível para inscrição.', 'invalid_enrollment_method' => 'Esse evento exige inscrição via formulário, não RSVP.', 'event_full' => 'Este evento atingiu a capacidade máxima e não possui lista de espera.', + 'invalid_check_in_status' => 'Apenas inscrições confirmadas ou com check-in realizado podem receber check-in.', + 'check_in_outside_event_date_range' => 'A data do check-in deve estar dentro do período do evento.', + 'already_checked_in_for_date' => 'Esta inscrição já possui check-in para essa data.', + 'invalid_check_in_actor' => 'Check-in manual exige o ID do usuário organizador.', ]; diff --git a/app-modules/events/src/CheckIn/Actions/CheckInAction.php b/app-modules/events/src/CheckIn/Actions/CheckInAction.php new file mode 100644 index 000000000..e8d103f8a --- /dev/null +++ b/app-modules/events/src/CheckIn/Actions/CheckInAction.php @@ -0,0 +1,136 @@ + $payload + */ + public function handle( + Enrollment $enrollment, + CheckInMethod $method, + array $payload, + CarbonInterface $eventDate, + ): CheckIn { + return DB::transaction(function () use ($enrollment, $method, $payload, $eventDate): CheckIn { + $enrollment = $this->loadLockedEnrollment($enrollment); + $event = $enrollment->event; + $normalizedEventDate = Date::parse($eventDate->toDateString())->startOfDay(); + $actorUserId = $payload['actor_user_id'] ?? null; + + $this->validate($enrollment, $method, $payload, $normalizedEventDate); + $isFirstCheckIn = !CheckIn::query() + ->where('enrollment_id', $enrollment->id) + ->exists(); + + try { + $checkIn = CheckIn::query()->create([ + 'enrollment_id' => $enrollment->id, + 'method' => $method, + 'payload' => $payload, + 'event_date' => $normalizedEventDate, + 'checked_in_at' => now(), + ]); + } catch (UniqueConstraintViolationException) { + throw EnrollmentException::alreadyCheckedInForDate(); + } + + if ($isFirstCheckIn && $enrollment->status === EnrollmentStatus::Confirmed) { + $this->transitionToCheckedIn($enrollment, is_string($actorUserId) ? $actorUserId : null); + } + + $xpRewardOnCheckedIn = (int) ($event->enrollmentPolicy->xp_on_checked_in ?? 0); + + event(new ParticipantCheckedIn( + checkInId: $checkIn->id, + enrollmentId: $enrollment->id, + eventId: $enrollment->event_id, + userId: $enrollment->user_id, + eventDate: $normalizedEventDate->toDateString(), + xpRewardOnCheckedIn: $xpRewardOnCheckedIn, + )); + + return $checkIn->load('enrollment'); + }); + } + + private function loadLockedEnrollment(Enrollment $enrollment): Enrollment + { + return Enrollment::query() + ->with(['event.enrollmentPolicy']) + ->whereKey($enrollment->id) + ->lockForUpdate() + ->firstOrFail(); + } + + /** + * @param array $payload + */ + private function validate(Enrollment $enrollment, CheckInMethod $method, array $payload, CarbonInterface $eventDate): void + { + throw_if( + $method === CheckInMethod::Manual && blank($payload['actor_user_id'] ?? null), + EnrollmentException::invalidCheckInActor(), + ); + + throw_unless( + in_array($enrollment->status, [EnrollmentStatus::Confirmed, EnrollmentStatus::CheckedIn], strict: true), + EnrollmentException::invalidCheckInStatus(), + ); + + $event = $enrollment->event; + $eventDateString = $eventDate->toDateString(); + + throw_unless( + $eventDateString >= $event->starts_at->toDateString() && $eventDateString <= $event->ends_at->toDateString(), + EnrollmentException::checkInOutsideEventDateRange(), + ); + + throw_if( + CheckIn::query() + ->where('enrollment_id', $enrollment->id) + ->whereDate('event_date', $eventDate->toDateString()) + ->exists(), + EnrollmentException::alreadyCheckedInForDate(), + ); + } + + private function transitionToCheckedIn(Enrollment $enrollment, ?string $actorUserId): void + { + $fromStatus = $enrollment->status; + + throw_unless( + $fromStatus->canTransitionTo(EnrollmentStatus::CheckedIn), + EnrollmentException::invalidCheckInStatus(), + ); + + $enrollment->forceFill([ + 'status' => EnrollmentStatus::CheckedIn, + 'checked_in_at' => now(), + ])->save(); + + EnrollmentTransition::query()->create([ + 'enrollment_id' => $enrollment->id, + 'from_status' => $fromStatus, + 'to_status' => EnrollmentStatus::CheckedIn, + 'actor_id' => $actorUserId, + 'triggered_by' => TriggeredBy::Admin, + ]); + } +} diff --git a/app-modules/events/src/CheckIn/Events/ParticipantCheckedIn.php b/app-modules/events/src/CheckIn/Events/ParticipantCheckedIn.php new file mode 100644 index 000000000..8ddddc728 --- /dev/null +++ b/app-modules/events/src/CheckIn/Events/ParticipantCheckedIn.php @@ -0,0 +1,22 @@ +|null $payload + * @property Carbon $checked_in_at * @property Carbon $created_at * @property Carbon $updated_at */ @@ -35,6 +36,7 @@ final class CheckIn extends Model 'event_date', 'method', 'payload', + 'checked_in_at', ]; /** @return BelongsTo */ @@ -55,6 +57,7 @@ protected function casts(): array 'event_date' => 'date', 'method' => CheckInMethod::class, 'payload' => 'array', + 'checked_in_at' => 'datetime', ]; } } diff --git a/app-modules/events/src/Enrollment/Exceptions/EnrollmentException.php b/app-modules/events/src/Enrollment/Exceptions/EnrollmentException.php index 8ffc2cff5..80c2d3465 100644 --- a/app-modules/events/src/Enrollment/Exceptions/EnrollmentException.php +++ b/app-modules/events/src/Enrollment/Exceptions/EnrollmentException.php @@ -48,4 +48,36 @@ public static function eventFull(): self Response::HTTP_UNPROCESSABLE_ENTITY, ); } + + public static function invalidCheckInStatus(): self + { + return new self( + __('events::exceptions.invalid_check_in_status'), + Response::HTTP_UNPROCESSABLE_ENTITY, + ); + } + + public static function checkInOutsideEventDateRange(): self + { + return new self( + __('events::exceptions.check_in_outside_event_date_range'), + Response::HTTP_UNPROCESSABLE_ENTITY, + ); + } + + public static function alreadyCheckedInForDate(): self + { + return new self( + __('events::exceptions.already_checked_in_for_date'), + Response::HTTP_CONFLICT, + ); + } + + public static function invalidCheckInActor(): self + { + return new self( + __('events::exceptions.invalid_check_in_actor'), + Response::HTTP_UNPROCESSABLE_ENTITY, + ); + } } diff --git a/app-modules/events/tests/Feature/CheckInActionTest.php b/app-modules/events/tests/Feature/CheckInActionTest.php new file mode 100644 index 000000000..bda30d2d0 --- /dev/null +++ b/app-modules/events/tests/Feature/CheckInActionTest.php @@ -0,0 +1,167 @@ +create(); + $participant = User::factory()->create(); + $startsAt = now()->setTime(9, 0); + + $event = Event::factory() + ->for($tenant) + ->create(array_merge([ + 'starts_at' => $startsAt, + 'ends_at' => $startsAt->clone()->setTime(18, 0), + 'status' => EventStatus::Published, + ], $eventAttributes)); + + return Enrollment::factory()->create(array_merge([ + 'event_id' => $event->id, + 'user_id' => $participant->id, + 'status' => EnrollmentStatus::Confirmed, + 'confirmed_at' => now(), + ], $enrollmentAttributes)); +} + +test('when organizer checks in confirmed enrollment, then check-in is recorded and enrollment transitions', function (): void { + EventFacade::fake([ParticipantCheckedIn::class]); + + $organizer = User::factory()->create(); + $enrollment = createConfirmedEnrollmentForCheckIn(); + + $checkIn = resolve(CheckInAction::class)->handle( + enrollment: $enrollment, + method: CheckInMethod::Manual, + payload: ['actor_user_id' => $organizer->id], + eventDate: now(), + ); + + expect($checkIn)->toBeInstanceOf(CheckIn::class) + ->and($checkIn->enrollment_id)->toBe($enrollment->id) + ->and($checkIn->method)->toBe(CheckInMethod::Manual) + ->and($checkIn->event_date->isSameDay(now()))->toBeTrue() + ->and($checkIn->checked_in_at)->not->toBeNull() + ->and($checkIn->payload)->toBe(['actor_user_id' => $organizer->id]) + ->and($enrollment->fresh()->status)->toBe(EnrollmentStatus::CheckedIn) + ->and($enrollment->fresh()->checked_in_at)->not->toBeNull(); + + $transition = EnrollmentTransition::query() + ->where('enrollment_id', $enrollment->id) + ->where('to_status', EnrollmentStatus::CheckedIn) + ->first(); + + expect($transition)->not->toBeNull() + ->and($transition->from_status)->toBe(EnrollmentStatus::Confirmed) + ->and($transition->actor_id)->toBe($organizer->id) + ->and($transition->triggered_by)->toBe(TriggeredBy::Admin); + + EventFacade::assertDispatched(fn (ParticipantCheckedIn $event): bool => $event->enrollmentId === $enrollment->id + && $event->eventId === $enrollment->event_id + && $event->userId === $enrollment->user_id + && $event->checkInId === $checkIn->id); +}); + +test('when checked-in enrollment checks in on another event date, then only check-in record is created', function (): void { + $organizer = User::factory()->create(); + $startsAt = now()->setTime(9, 0); + $enrollment = createConfirmedEnrollmentForCheckIn([ + 'starts_at' => $startsAt, + 'ends_at' => $startsAt->clone()->addDay()->setTime(18, 0), + ], [ + 'status' => EnrollmentStatus::CheckedIn, + 'checked_in_at' => now(), + ]); + + CheckIn::factory()->create([ + 'enrollment_id' => $enrollment->id, + 'event_date' => $startsAt->toDateString(), + 'method' => CheckInMethod::Manual, + ]); + + $checkIn = resolve(CheckInAction::class)->handle( + enrollment: $enrollment, + method: CheckInMethod::Manual, + payload: ['actor_user_id' => $organizer->id], + eventDate: $startsAt->clone()->addDay(), + ); + + expect($checkIn->event_date->isSameDay($startsAt->clone()->addDay()))->toBeTrue() + ->and(CheckIn::query()->where('enrollment_id', $enrollment->id)->count())->toBe(2) + ->and(EnrollmentTransition::query() + ->where('enrollment_id', $enrollment->id) + ->where('to_status', EnrollmentStatus::CheckedIn) + ->count())->toBe(0); +}); + +test('when enrollment already has check-in for event date, then duplicate is rejected', function (): void { + $enrollment = createConfirmedEnrollmentForCheckIn(); + + CheckIn::factory()->create([ + 'enrollment_id' => $enrollment->id, + 'event_date' => now()->toDateString(), + 'method' => CheckInMethod::Manual, + ]); + + expect(fn (): CheckIn => resolve(CheckInAction::class)->handle( + enrollment: $enrollment, + method: CheckInMethod::Manual, + payload: ['actor_user_id' => User::factory()->create()->id], + eventDate: now(), + ))->toThrow(EnrollmentException::class); +}); + +test('when check-in date is outside event date range, then check-in is rejected', function (): void { + $startsAt = now()->setTime(9, 0); + $enrollment = createConfirmedEnrollmentForCheckIn([ + 'starts_at' => $startsAt, + 'ends_at' => $startsAt->clone()->setTime(18, 0), + ]); + + expect(fn (): CheckIn => resolve(CheckInAction::class)->handle( + enrollment: $enrollment, + method: CheckInMethod::Manual, + payload: ['actor_user_id' => User::factory()->create()->id], + eventDate: $startsAt->clone()->addDay(), + ))->toThrow(EnrollmentException::class); +}); + +test('when enrollment status is not confirmed or checked in, then check-in is rejected', function (): void { + $enrollment = createConfirmedEnrollmentForCheckIn(enrollmentAttributes: [ + 'status' => EnrollmentStatus::Waitlisted, + 'confirmed_at' => null, + ]); + + expect(fn (): CheckIn => resolve(CheckInAction::class)->handle( + enrollment: $enrollment, + method: CheckInMethod::Manual, + payload: ['actor_user_id' => User::factory()->create()->id], + eventDate: now(), + ))->toThrow(EnrollmentException::class); +}); + +test('when manual check-in has no actor user id, then check-in is rejected', function (): void { + $enrollment = createConfirmedEnrollmentForCheckIn(); + + expect(fn (): CheckIn => resolve(CheckInAction::class)->handle( + enrollment: $enrollment, + method: CheckInMethod::Manual, + payload: [], + eventDate: now(), + ))->toThrow(EnrollmentException::class); +}); diff --git a/app-modules/events/tests/Feature/EventResourceTest.php b/app-modules/events/tests/Feature/EventResourceTest.php index 4fbb7f2ea..abc4fef3c 100644 --- a/app-modules/events/tests/Feature/EventResourceTest.php +++ b/app-modules/events/tests/Feature/EventResourceTest.php @@ -4,8 +4,11 @@ use Filament\Facades\Filament; use He4rt\Events\CheckIn\Enums\CheckInMethod; +use He4rt\Events\CheckIn\Models\CheckIn; use He4rt\Events\Enrollment\Enums\AttendanceRequirement; use He4rt\Events\Enrollment\Enums\EnrollmentMethod; +use He4rt\Events\Enrollment\Enums\EnrollmentStatus; +use He4rt\Events\Enrollment\Models\Enrollment; use He4rt\Events\Enrollment\Models\EnrollmentPolicy; use He4rt\Events\Event\Enums\EventType; use He4rt\Events\Event\Models\Event; @@ -137,3 +140,88 @@ ]) ->assertSuccessful(); }); + +test('when admin checks in an enrollment from relation manager, then participant is checked in', function (): void { + $event = Event::factory()->create([ + 'starts_at' => now()->setTime(9, 0), + 'ends_at' => now()->setTime(18, 0), + ]); + + $enrollment = Enrollment::factory()->create([ + 'event_id' => $event->id, + 'status' => EnrollmentStatus::Confirmed, + 'confirmed_at' => now(), + ]); + + livewire(EnrollmentsRelationManager::class, [ + 'ownerRecord' => $event, + 'pageClass' => EditEvent::class, + ]) + ->callTableAction('checkIn', $enrollment, data: [ + 'event_date' => now()->toDateString(), + ]) + ->assertHasNoTableActionErrors(); + + expect($enrollment->fresh()->status)->toBe(EnrollmentStatus::CheckedIn) + ->and(CheckIn::query()->where('enrollment_id', $enrollment->id)->count())->toBe(1); +}); + +test('when admin bulk checks in selected enrollments, then all selected participants are checked in', function (): void { + $event = Event::factory()->create([ + 'starts_at' => now()->setTime(9, 0), + 'ends_at' => now()->setTime(18, 0), + ]); + + $enrollments = Enrollment::factory() + ->count(2) + ->create([ + 'event_id' => $event->id, + 'status' => EnrollmentStatus::Confirmed, + 'confirmed_at' => now(), + ]); + + livewire(EnrollmentsRelationManager::class, [ + 'ownerRecord' => $event, + 'pageClass' => EditEvent::class, + ]) + ->callTableBulkAction('checkInSelected', $enrollments, data: [ + 'event_date' => now()->toDateString(), + ]) + ->assertHasNoTableBulkActionErrors(); + + expect(Enrollment::query()->whereKey($enrollments->pluck('id'))->where('status', EnrollmentStatus::CheckedIn)->count())->toBe(2) + ->and(CheckIn::query()->whereIn('enrollment_id', $enrollments->pluck('id'))->count())->toBe(2); +}); + +test('when enrollment has check-ins, then relation manager shows check-in history', function (): void { + $startsAt = now()->setTime(9, 0); + $event = Event::factory()->create([ + 'starts_at' => $startsAt, + 'ends_at' => $startsAt->clone()->addDay()->setTime(18, 0), + ]); + + $enrollment = Enrollment::factory()->create([ + 'event_id' => $event->id, + 'status' => EnrollmentStatus::CheckedIn, + 'checked_in_at' => now(), + ]); + + CheckIn::factory()->create([ + 'enrollment_id' => $enrollment->id, + 'event_date' => $startsAt->toDateString(), + 'method' => CheckInMethod::Manual, + ]); + + CheckIn::factory()->create([ + 'enrollment_id' => $enrollment->id, + 'event_date' => $startsAt->clone()->addDay()->toDateString(), + 'method' => CheckInMethod::Manual, + ]); + + livewire(EnrollmentsRelationManager::class, [ + 'ownerRecord' => $event, + 'pageClass' => EditEvent::class, + ]) + ->assertSee($startsAt->toDateString()) + ->assertSee($startsAt->clone()->addDay()->toDateString()); +}); diff --git a/app-modules/panel-admin/src/Filament/Resources/Events/RelationManagers/EnrollmentsRelationManager.php b/app-modules/panel-admin/src/Filament/Resources/Events/RelationManagers/EnrollmentsRelationManager.php index fed021bc8..b7a78338d 100644 --- a/app-modules/panel-admin/src/Filament/Resources/Events/RelationManagers/EnrollmentsRelationManager.php +++ b/app-modules/panel-admin/src/Filament/Resources/Events/RelationManagers/EnrollmentsRelationManager.php @@ -4,11 +4,25 @@ namespace He4rt\PanelAdmin\Filament\Resources\Events\RelationManagers; +use Filament\Actions\Action; +use Filament\Actions\BulkAction; +use Filament\Actions\BulkActionGroup; +use Filament\Forms\Components\DatePicker; +use Filament\Notifications\Notification; use Filament\Resources\RelationManagers\RelationManager; +use Filament\Support\Icons\Heroicon; use Filament\Tables\Columns\TextColumn; use Filament\Tables\Filters\SelectFilter; use Filament\Tables\Table; +use He4rt\Events\CheckIn\Actions\CheckInAction; +use He4rt\Events\CheckIn\Enums\CheckInMethod; +use He4rt\Events\CheckIn\Models\CheckIn; use He4rt\Events\Enrollment\Enums\EnrollmentStatus; +use He4rt\Events\Enrollment\Models\Enrollment; +use He4rt\Events\Event\Models\Event; +use Illuminate\Database\Eloquent\Builder; +use Illuminate\Database\Eloquent\Collection; +use Illuminate\Support\Facades\Date; final class EnrollmentsRelationManager extends RelationManager { @@ -23,6 +37,7 @@ public function table(Table $table): Table { return $table ->recordTitleAttribute('id') + ->modifyQueryUsing(fn (Builder $query): Builder => $query->with(['checkIns', 'user'])) ->columns([ TextColumn::make('user.name') ->label('Participant') @@ -44,6 +59,15 @@ public function table(Table $table): Table ->dateTime() ->sortable(), + TextColumn::make('check_in_history') + ->label('Check-in History') + ->state(fn (Enrollment $record): string => $record->checkIns + ->sortBy('event_date') + ->map(fn (CheckIn $checkIn): string => $checkIn->event_date->toDateString()) + ->implode(', ')) + ->placeholder('-') + ->toggleable(), + TextColumn::make('cancelled_at') ->label('Cancelled At') ->dateTime() @@ -54,6 +78,87 @@ public function table(Table $table): Table SelectFilter::make('status') ->label('Status') ->options(EnrollmentStatus::class), + ]) + ->recordActions([ + $this->checkInAction(), + ]) + ->toolbarActions([ + BulkActionGroup::make([ + $this->bulkCheckInAction(), + ]), ]); } + + private function checkInAction(): Action + { + return Action::make('checkIn') + ->label('Check In') + ->icon(Heroicon::OutlinedCheckCircle) + ->color('success') + ->visible(fn (Enrollment $record): bool => $record->status->is(EnrollmentStatus::Confirmed, EnrollmentStatus::CheckedIn)) + ->schema($this->checkInSchema()) + ->action(function (Enrollment $record, array $data): void { + $this->checkIn($record, $data); + + Notification::make() + ->success() + ->title('Participant checked in.') + ->send(); + }); + } + + private function bulkCheckInAction(): BulkAction + { + return BulkAction::make('checkInSelected') + ->label('Check In Selected') + ->icon(Heroicon::OutlinedCheckCircle) + ->color('success') + ->schema($this->checkInSchema()) + ->action(function (Collection $records, array $data): void { + foreach ($records as $record) { + if (!$record instanceof Enrollment) { + continue; + } + + $this->checkIn($record, $data); + } + + Notification::make() + ->success() + ->title('Selected participants checked in.') + ->send(); + }) + ->deselectRecordsAfterCompletion(); + } + + /** + * @return array + */ + private function checkInSchema(): array + { + /** @var Event $event */ + $event = $this->getOwnerRecord(); + + return [ + DatePicker::make('event_date') + ->label('Date') + ->default(now()) + ->minDate($event->starts_at->toDateString()) + ->maxDate($event->ends_at->toDateString()) + ->required(), + ]; + } + + /** + * @param array $data + */ + private function checkIn(Enrollment $enrollment, array $data): CheckIn + { + return resolve(CheckInAction::class)->handle( + enrollment: $enrollment, + method: CheckInMethod::Manual, + payload: ['actor_user_id' => auth()->id()], + eventDate: Date::parse($data['event_date']), + ); + } } From 6c648a6451bdf58cfb78c13b4a738bd3cc022cda Mon Sep 17 00:00:00 2001 From: Davi Castello Branco Tavares de Oliveira Date: Fri, 22 May 2026 20:06:04 -0400 Subject: [PATCH 2/7] fix(events): satisfy phpstan in event models Tighten nullable access, make enrollment response matching exhaustive, and add Eloquent builder generics to Events scopes. --- .../events/src/Enrollment/Actions/EnrollUserAction.php | 2 +- app-modules/events/src/Enrollment/Enums/EnrollmentStatus.php | 1 + app-modules/events/src/Enrollment/Models/Enrollment.php | 3 +++ app-modules/events/src/Event/Models/Event.php | 4 ++++ 4 files changed, 9 insertions(+), 1 deletion(-) diff --git a/app-modules/events/src/Enrollment/Actions/EnrollUserAction.php b/app-modules/events/src/Enrollment/Actions/EnrollUserAction.php index a43493e6a..bff9b7318 100644 --- a/app-modules/events/src/Enrollment/Actions/EnrollUserAction.php +++ b/app-modules/events/src/Enrollment/Actions/EnrollUserAction.php @@ -53,7 +53,7 @@ public function handle(EnrollUserDTO $dto): Enrollment enrollmentId: $enrollment->id, eventId: $dto->eventId, userId: $dto->userId, - xpRewardOnConfirmed: $policy?->xp_on_confirmed ?? 0, + xpRewardOnConfirmed: $policy->xp_on_confirmed ?? 0, )); } diff --git a/app-modules/events/src/Enrollment/Enums/EnrollmentStatus.php b/app-modules/events/src/Enrollment/Enums/EnrollmentStatus.php index 70d1e9930..79b4fbb83 100644 --- a/app-modules/events/src/Enrollment/Enums/EnrollmentStatus.php +++ b/app-modules/events/src/Enrollment/Enums/EnrollmentStatus.php @@ -90,6 +90,7 @@ public function getResponseMessage(): string return match ($this) { self::Confirmed => __('events::pages.confirm_presence_success'), self::Waitlisted => __('events::pages.waitlist_success'), + default => '', }; } } diff --git a/app-modules/events/src/Enrollment/Models/Enrollment.php b/app-modules/events/src/Enrollment/Models/Enrollment.php index 3d163885e..093d44f42 100644 --- a/app-modules/events/src/Enrollment/Models/Enrollment.php +++ b/app-modules/events/src/Enrollment/Models/Enrollment.php @@ -97,16 +97,19 @@ protected static function newFactory(): EnrollmentFactory return EnrollmentFactory::new(); } + /** @param Builder $query */ protected function scopeConfirmed(Builder $query): void { $query->where('status', EnrollmentStatus::Confirmed); } + /** @param Builder $query */ protected function scopeWaitlisted(Builder $query): void { $query->where('status', EnrollmentStatus::Waitlisted); } + /** @param Builder $query */ protected function scopeActive(Builder $query): void { $query->whereNotIn('status', [ diff --git a/app-modules/events/src/Event/Models/Event.php b/app-modules/events/src/Event/Models/Event.php index c6c61e216..cd81e73e0 100644 --- a/app-modules/events/src/Event/Models/Event.php +++ b/app-modules/events/src/Event/Models/Event.php @@ -97,21 +97,25 @@ protected static function newFactory(): EventFactory return EventFactory::new(); } + /** @param Builder<\Illuminate\Support\Facades\Event> $query */ protected function scopePublished(Builder $query): void { $query->where('status', EventStatus::Published); } + /** @param Builder<\Illuminate\Support\Facades\Event> $query */ protected function scopeViewableByParticipant(Builder $query): void { $query->whereIn('status', EventStatus::viewableByParticipant()); } + /** @param Builder<\Illuminate\Support\Facades\Event> $query */ protected function scopeUpcoming(Builder $query): void { $query->where('starts_at', '>', now()); } + /** @param Builder<\Illuminate\Support\Facades\Event> $query */ protected function scopeActive(Builder $query): void { $query->where('status', EventStatus::Published) From 5e2cb50e2e386ca6eb39cc2755fb2c757f4cf793 Mon Sep 17 00:00:00 2001 From: Davi Castello Branco Tavares de Oliveira Date: Fri, 22 May 2026 20:13:13 -0400 Subject: [PATCH 3/7] fix(events): correct event scope phpstan types Use Builder for Event model scopes so PHPStan resolves the generic type to the model instead of the Event facade. --- app-modules/events/src/Event/Models/Event.php | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/app-modules/events/src/Event/Models/Event.php b/app-modules/events/src/Event/Models/Event.php index cd81e73e0..5f6006fd4 100644 --- a/app-modules/events/src/Event/Models/Event.php +++ b/app-modules/events/src/Event/Models/Event.php @@ -97,25 +97,25 @@ protected static function newFactory(): EventFactory return EventFactory::new(); } - /** @param Builder<\Illuminate\Support\Facades\Event> $query */ + /** @param Builder $query */ protected function scopePublished(Builder $query): void { $query->where('status', EventStatus::Published); } - /** @param Builder<\Illuminate\Support\Facades\Event> $query */ + /** @param Builder $query */ protected function scopeViewableByParticipant(Builder $query): void { $query->whereIn('status', EventStatus::viewableByParticipant()); } - /** @param Builder<\Illuminate\Support\Facades\Event> $query */ + /** @param Builder $query */ protected function scopeUpcoming(Builder $query): void { $query->where('starts_at', '>', now()); } - /** @param Builder<\Illuminate\Support\Facades\Event> $query */ + /** @param Builder $query */ protected function scopeActive(Builder $query): void { $query->where('status', EventStatus::Published) From 42ba6388f50adccf43c930125b9f6f238eb9ad4d Mon Sep 17 00:00:00 2001 From: Davi Castello Branco Tavares de Oliveira Date: Fri, 22 May 2026 21:28:27 -0400 Subject: [PATCH 4/7] refactor(events): extract transition action and own check-in exceptions Extract enrollment status transition logic from CheckInAction into a reusable TransitionEnrollmentAction, so future transitions (attended, cancelled) reuse the same pattern. The action accepts an optional timestamp to preserve the check-in record's actual time. Move check-in exceptions out of EnrollmentException into a dedicated CheckInException, respecting the bounded context boundary between the CheckIn and Enrollment modules. --- app-modules/events/lang/en/check_in.php | 10 +++ app-modules/events/lang/en/exceptions.php | 5 +- app-modules/events/lang/pt_BR/check_in.php | 10 +++ app-modules/events/lang/pt_BR/exceptions.php | 5 +- .../src/CheckIn/Actions/CheckInAction.php | 45 ++++--------- .../CheckIn/Exceptions/CheckInException.php | 43 +++++++++++++ .../Actions/TransitionEnrollmentAction.php | 64 +++++++++++++++++++ .../Exceptions/EnrollmentException.php | 41 +++--------- .../tests/Feature/CheckInActionTest.php | 12 ++-- .../tests/Feature/EventResourceTest.php | 1 - 10 files changed, 158 insertions(+), 78 deletions(-) create mode 100644 app-modules/events/lang/en/check_in.php create mode 100644 app-modules/events/lang/pt_BR/check_in.php create mode 100644 app-modules/events/src/CheckIn/Exceptions/CheckInException.php create mode 100644 app-modules/events/src/Enrollment/Actions/TransitionEnrollmentAction.php diff --git a/app-modules/events/lang/en/check_in.php b/app-modules/events/lang/en/check_in.php new file mode 100644 index 000000000..c163fc1fe --- /dev/null +++ b/app-modules/events/lang/en/check_in.php @@ -0,0 +1,10 @@ + 'Only confirmed or checked-in enrollments can be checked in.', + 'check_in_outside_event_date_range' => 'Check-in date must be within the event date range.', + 'already_checked_in_for_date' => 'This enrollment has already checked in for this date.', + 'invalid_check_in_actor' => 'Manual check-in requires the organizer user id.', +]; diff --git a/app-modules/events/lang/en/exceptions.php b/app-modules/events/lang/en/exceptions.php index 5f21cebf3..8389239d4 100644 --- a/app-modules/events/lang/en/exceptions.php +++ b/app-modules/events/lang/en/exceptions.php @@ -3,13 +3,10 @@ declare(strict_types=1); return [ + 'invalid_transition' => 'Cannot transition enrollment from :from to :to.', 'already_enrolled' => 'You are already enrolled in this event.', 'event_past' => 'This event has already started and is no longer accepting enrollments.', 'event_not_active' => 'This event is not available for enrollment.', 'invalid_enrollment_method' => 'This event requires application enrollment, not RSVP.', 'event_full' => 'This event has reached maximum capacity and does not have a waitlist.', - 'invalid_check_in_status' => 'Only confirmed or checked-in enrollments can be checked in.', - 'check_in_outside_event_date_range' => 'Check-in date must be within the event date range.', - 'already_checked_in_for_date' => 'This enrollment has already checked in for this date.', - 'invalid_check_in_actor' => 'Manual check-in requires the organizer user id.', ]; diff --git a/app-modules/events/lang/pt_BR/check_in.php b/app-modules/events/lang/pt_BR/check_in.php new file mode 100644 index 000000000..46a65b78e --- /dev/null +++ b/app-modules/events/lang/pt_BR/check_in.php @@ -0,0 +1,10 @@ + 'Apenas inscrições confirmadas ou com check-in realizado podem receber check-in.', + 'check_in_outside_event_date_range' => 'A data do check-in deve estar dentro do período do evento.', + 'already_checked_in_for_date' => 'Esta inscrição já possui check-in para essa data.', + 'invalid_check_in_actor' => 'Check-in manual exige o ID do usuário organizador.', +]; diff --git a/app-modules/events/lang/pt_BR/exceptions.php b/app-modules/events/lang/pt_BR/exceptions.php index 08bca7c23..460e186fe 100644 --- a/app-modules/events/lang/pt_BR/exceptions.php +++ b/app-modules/events/lang/pt_BR/exceptions.php @@ -3,13 +3,10 @@ declare(strict_types=1); return [ + 'invalid_transition' => 'Não é possível transicionar inscrição de :from para :to.', 'already_enrolled' => 'Você já está inscrito neste evento.', 'event_past' => 'Este evento já começou e não aceita mais inscrições.', 'event_not_active' => 'Este evento não está disponível para inscrição.', 'invalid_enrollment_method' => 'Esse evento exige inscrição via formulário, não RSVP.', 'event_full' => 'Este evento atingiu a capacidade máxima e não possui lista de espera.', - 'invalid_check_in_status' => 'Apenas inscrições confirmadas ou com check-in realizado podem receber check-in.', - 'check_in_outside_event_date_range' => 'A data do check-in deve estar dentro do período do evento.', - 'already_checked_in_for_date' => 'Esta inscrição já possui check-in para essa data.', - 'invalid_check_in_actor' => 'Check-in manual exige o ID do usuário organizador.', ]; diff --git a/app-modules/events/src/CheckIn/Actions/CheckInAction.php b/app-modules/events/src/CheckIn/Actions/CheckInAction.php index e8d103f8a..a79bedc17 100644 --- a/app-modules/events/src/CheckIn/Actions/CheckInAction.php +++ b/app-modules/events/src/CheckIn/Actions/CheckInAction.php @@ -7,12 +7,12 @@ use Carbon\CarbonInterface; use He4rt\Events\CheckIn\Enums\CheckInMethod; use He4rt\Events\CheckIn\Events\ParticipantCheckedIn; +use He4rt\Events\CheckIn\Exceptions\CheckInException; use He4rt\Events\CheckIn\Models\CheckIn; +use He4rt\Events\Enrollment\Actions\TransitionEnrollmentAction; use He4rt\Events\Enrollment\Enums\EnrollmentStatus; use He4rt\Events\Enrollment\Enums\TriggeredBy; -use He4rt\Events\Enrollment\Exceptions\EnrollmentException; use He4rt\Events\Enrollment\Models\Enrollment; -use He4rt\Events\Enrollment\Models\EnrollmentTransition; use Illuminate\Database\UniqueConstraintViolationException; use Illuminate\Support\Facades\Date; use Illuminate\Support\Facades\DB; @@ -48,11 +48,17 @@ public function handle( 'checked_in_at' => now(), ]); } catch (UniqueConstraintViolationException) { - throw EnrollmentException::alreadyCheckedInForDate(); + throw CheckInException::alreadyCheckedInForDate(); } if ($isFirstCheckIn && $enrollment->status === EnrollmentStatus::Confirmed) { - $this->transitionToCheckedIn($enrollment, is_string($actorUserId) ? $actorUserId : null); + resolve(TransitionEnrollmentAction::class)->handle( + enrollment: $enrollment, + toStatus: EnrollmentStatus::CheckedIn, + triggeredBy: TriggeredBy::Admin, + actorId: is_string($actorUserId) ? $actorUserId : null, + timestamp: $checkIn->checked_in_at, + ); } $xpRewardOnCheckedIn = (int) ($event->enrollmentPolicy->xp_on_checked_in ?? 0); @@ -86,12 +92,12 @@ private function validate(Enrollment $enrollment, CheckInMethod $method, array $ { throw_if( $method === CheckInMethod::Manual && blank($payload['actor_user_id'] ?? null), - EnrollmentException::invalidCheckInActor(), + CheckInException::invalidCheckInActor(), ); throw_unless( in_array($enrollment->status, [EnrollmentStatus::Confirmed, EnrollmentStatus::CheckedIn], strict: true), - EnrollmentException::invalidCheckInStatus(), + CheckInException::invalidCheckInStatus(), ); $event = $enrollment->event; @@ -99,7 +105,7 @@ private function validate(Enrollment $enrollment, CheckInMethod $method, array $ throw_unless( $eventDateString >= $event->starts_at->toDateString() && $eventDateString <= $event->ends_at->toDateString(), - EnrollmentException::checkInOutsideEventDateRange(), + CheckInException::checkInOutsideEventDateRange(), ); throw_if( @@ -107,30 +113,7 @@ private function validate(Enrollment $enrollment, CheckInMethod $method, array $ ->where('enrollment_id', $enrollment->id) ->whereDate('event_date', $eventDate->toDateString()) ->exists(), - EnrollmentException::alreadyCheckedInForDate(), + CheckInException::alreadyCheckedInForDate(), ); } - - private function transitionToCheckedIn(Enrollment $enrollment, ?string $actorUserId): void - { - $fromStatus = $enrollment->status; - - throw_unless( - $fromStatus->canTransitionTo(EnrollmentStatus::CheckedIn), - EnrollmentException::invalidCheckInStatus(), - ); - - $enrollment->forceFill([ - 'status' => EnrollmentStatus::CheckedIn, - 'checked_in_at' => now(), - ])->save(); - - EnrollmentTransition::query()->create([ - 'enrollment_id' => $enrollment->id, - 'from_status' => $fromStatus, - 'to_status' => EnrollmentStatus::CheckedIn, - 'actor_id' => $actorUserId, - 'triggered_by' => TriggeredBy::Admin, - ]); - } } diff --git a/app-modules/events/src/CheckIn/Exceptions/CheckInException.php b/app-modules/events/src/CheckIn/Exceptions/CheckInException.php new file mode 100644 index 000000000..a36d17cd3 --- /dev/null +++ b/app-modules/events/src/CheckIn/Exceptions/CheckInException.php @@ -0,0 +1,43 @@ + 'confirmed_at', + 'checked_in' => 'checked_in_at', + 'attended' => 'attended_at', + 'cancelled' => 'cancelled_at', + ]; + + /** + * @param array $metadata + */ + public function handle( + Enrollment $enrollment, + EnrollmentStatus $toStatus, + TriggeredBy $triggeredBy, + ?string $actorId = null, + ?string $reason = null, + array $metadata = [], + ?CarbonInterface $timestamp = null, + ): EnrollmentTransition { + $fromStatus = $enrollment->status; + + throw_unless( + $fromStatus->canTransitionTo($toStatus), + EnrollmentException::invalidTransition($fromStatus, $toStatus), + ); + + $timestampColumn = $this->resolveTimestampColumn($toStatus); + + $enrollment->forceFill([ + 'status' => $toStatus, + $timestampColumn => $timestamp ?? now(), + ])->save(); + + return EnrollmentTransition::query()->create([ + 'enrollment_id' => $enrollment->id, + 'from_status' => $fromStatus, + 'to_status' => $toStatus, + 'actor_id' => $actorId, + 'triggered_by' => $triggeredBy, + 'reason' => $reason, + 'metadata' => $metadata ?: null, + ]); + } + + private function resolveTimestampColumn(EnrollmentStatus $status): ?string + { + return self::TIMESTAMP_MAP[$status->value] ?? null; + } +} diff --git a/app-modules/events/src/Enrollment/Exceptions/EnrollmentException.php b/app-modules/events/src/Enrollment/Exceptions/EnrollmentException.php index 80c2d3465..1aba6ed07 100644 --- a/app-modules/events/src/Enrollment/Exceptions/EnrollmentException.php +++ b/app-modules/events/src/Enrollment/Exceptions/EnrollmentException.php @@ -5,10 +5,19 @@ namespace He4rt\Events\Enrollment\Exceptions; use Exception; +use He4rt\Events\Enrollment\Enums\EnrollmentStatus; use Symfony\Component\HttpFoundation\Response; final class EnrollmentException extends Exception { + public static function invalidTransition(EnrollmentStatus $from, EnrollmentStatus $to): self + { + return new self( + __('events::exceptions.invalid_transition', ['from' => $from->value, 'to' => $to->value]), + Response::HTTP_UNPROCESSABLE_ENTITY, + ); + } + public static function alreadyEnrolled(): self { return new self( @@ -48,36 +57,4 @@ public static function eventFull(): self Response::HTTP_UNPROCESSABLE_ENTITY, ); } - - public static function invalidCheckInStatus(): self - { - return new self( - __('events::exceptions.invalid_check_in_status'), - Response::HTTP_UNPROCESSABLE_ENTITY, - ); - } - - public static function checkInOutsideEventDateRange(): self - { - return new self( - __('events::exceptions.check_in_outside_event_date_range'), - Response::HTTP_UNPROCESSABLE_ENTITY, - ); - } - - public static function alreadyCheckedInForDate(): self - { - return new self( - __('events::exceptions.already_checked_in_for_date'), - Response::HTTP_CONFLICT, - ); - } - - public static function invalidCheckInActor(): self - { - return new self( - __('events::exceptions.invalid_check_in_actor'), - Response::HTTP_UNPROCESSABLE_ENTITY, - ); - } } diff --git a/app-modules/events/tests/Feature/CheckInActionTest.php b/app-modules/events/tests/Feature/CheckInActionTest.php index bda30d2d0..5eb163c82 100644 --- a/app-modules/events/tests/Feature/CheckInActionTest.php +++ b/app-modules/events/tests/Feature/CheckInActionTest.php @@ -5,10 +5,10 @@ use He4rt\Events\CheckIn\Actions\CheckInAction; use He4rt\Events\CheckIn\Enums\CheckInMethod; use He4rt\Events\CheckIn\Events\ParticipantCheckedIn; +use He4rt\Events\CheckIn\Exceptions\CheckInException; use He4rt\Events\CheckIn\Models\CheckIn; use He4rt\Events\Enrollment\Enums\EnrollmentStatus; use He4rt\Events\Enrollment\Enums\TriggeredBy; -use He4rt\Events\Enrollment\Exceptions\EnrollmentException; use He4rt\Events\Enrollment\Models\Enrollment; use He4rt\Events\Enrollment\Models\EnrollmentTransition; use He4rt\Events\Event\Enums\EventStatus; @@ -85,13 +85,13 @@ function createConfirmedEnrollmentForCheckIn(array $eventAttributes = [], array 'ends_at' => $startsAt->clone()->addDay()->setTime(18, 0), ], [ 'status' => EnrollmentStatus::CheckedIn, - 'checked_in_at' => now(), ]); CheckIn::factory()->create([ 'enrollment_id' => $enrollment->id, 'event_date' => $startsAt->toDateString(), 'method' => CheckInMethod::Manual, + 'checked_in_at' => now(), ]); $checkIn = resolve(CheckInAction::class)->handle( @@ -123,7 +123,7 @@ function createConfirmedEnrollmentForCheckIn(array $eventAttributes = [], array method: CheckInMethod::Manual, payload: ['actor_user_id' => User::factory()->create()->id], eventDate: now(), - ))->toThrow(EnrollmentException::class); + ))->toThrow(CheckInException::class); }); test('when check-in date is outside event date range, then check-in is rejected', function (): void { @@ -138,7 +138,7 @@ function createConfirmedEnrollmentForCheckIn(array $eventAttributes = [], array method: CheckInMethod::Manual, payload: ['actor_user_id' => User::factory()->create()->id], eventDate: $startsAt->clone()->addDay(), - ))->toThrow(EnrollmentException::class); + ))->toThrow(CheckInException::class); }); test('when enrollment status is not confirmed or checked in, then check-in is rejected', function (): void { @@ -152,7 +152,7 @@ function createConfirmedEnrollmentForCheckIn(array $eventAttributes = [], array method: CheckInMethod::Manual, payload: ['actor_user_id' => User::factory()->create()->id], eventDate: now(), - ))->toThrow(EnrollmentException::class); + ))->toThrow(CheckInException::class); }); test('when manual check-in has no actor user id, then check-in is rejected', function (): void { @@ -163,5 +163,5 @@ function createConfirmedEnrollmentForCheckIn(array $eventAttributes = [], array method: CheckInMethod::Manual, payload: [], eventDate: now(), - ))->toThrow(EnrollmentException::class); + ))->toThrow(CheckInException::class); }); diff --git a/app-modules/events/tests/Feature/EventResourceTest.php b/app-modules/events/tests/Feature/EventResourceTest.php index abc4fef3c..8d0763ab9 100644 --- a/app-modules/events/tests/Feature/EventResourceTest.php +++ b/app-modules/events/tests/Feature/EventResourceTest.php @@ -203,7 +203,6 @@ $enrollment = Enrollment::factory()->create([ 'event_id' => $event->id, 'status' => EnrollmentStatus::CheckedIn, - 'checked_in_at' => now(), ]); CheckIn::factory()->create([ From a474bd799b7c0a806c0c7c87c38bccde47084beb Mon Sep 17 00:00:00 2001 From: Davi Castello Branco Tavares de Oliveira Date: Sat, 23 May 2026 00:08:30 -0400 Subject: [PATCH 5/7] refactor(events): extract ManualCheckInAction and CheckInService from CheckInAction Replace the monolithic CheckInAction with two classes: - CheckInService: internal shared flow (validation, record creation, transition, event dispatch) - ManualCheckInAction: public action with clean signature, delegates to CheckInService Callers no longer pass CheckInMethod or payload internals. --- .../{CheckInAction.php => CheckInService.php} | 23 +++++------- .../CheckIn/Actions/ManualCheckInAction.php | 35 +++++++++++++++++++ .../tests/Feature/CheckInActionTest.php | 32 +++++++---------- .../EnrollmentsRelationManager.php | 8 ++--- 4 files changed, 59 insertions(+), 39 deletions(-) rename app-modules/events/src/CheckIn/Actions/{CheckInAction.php => CheckInService.php} (83%) create mode 100644 app-modules/events/src/CheckIn/Actions/ManualCheckInAction.php diff --git a/app-modules/events/src/CheckIn/Actions/CheckInAction.php b/app-modules/events/src/CheckIn/Actions/CheckInService.php similarity index 83% rename from app-modules/events/src/CheckIn/Actions/CheckInAction.php rename to app-modules/events/src/CheckIn/Actions/CheckInService.php index a79bedc17..775513e2c 100644 --- a/app-modules/events/src/CheckIn/Actions/CheckInAction.php +++ b/app-modules/events/src/CheckIn/Actions/CheckInService.php @@ -17,7 +17,7 @@ use Illuminate\Support\Facades\Date; use Illuminate\Support\Facades\DB; -final readonly class CheckInAction +final readonly class CheckInService { /** * @param array $payload @@ -27,14 +27,15 @@ public function handle( CheckInMethod $method, array $payload, CarbonInterface $eventDate, + string $actorUserId, + TriggeredBy $triggeredBy, ): CheckIn { - return DB::transaction(function () use ($enrollment, $method, $payload, $eventDate): CheckIn { + return DB::transaction(function () use ($enrollment, $method, $payload, $eventDate, $actorUserId, $triggeredBy): CheckIn { $enrollment = $this->loadLockedEnrollment($enrollment); $event = $enrollment->event; $normalizedEventDate = Date::parse($eventDate->toDateString())->startOfDay(); - $actorUserId = $payload['actor_user_id'] ?? null; - $this->validate($enrollment, $method, $payload, $normalizedEventDate); + $this->validateCommonRules($enrollment, $normalizedEventDate); $isFirstCheckIn = !CheckIn::query() ->where('enrollment_id', $enrollment->id) ->exists(); @@ -55,8 +56,8 @@ public function handle( resolve(TransitionEnrollmentAction::class)->handle( enrollment: $enrollment, toStatus: EnrollmentStatus::CheckedIn, - triggeredBy: TriggeredBy::Admin, - actorId: is_string($actorUserId) ? $actorUserId : null, + triggeredBy: $triggeredBy, + actorId: $actorUserId, timestamp: $checkIn->checked_in_at, ); } @@ -85,16 +86,8 @@ private function loadLockedEnrollment(Enrollment $enrollment): Enrollment ->firstOrFail(); } - /** - * @param array $payload - */ - private function validate(Enrollment $enrollment, CheckInMethod $method, array $payload, CarbonInterface $eventDate): void + private function validateCommonRules(Enrollment $enrollment, CarbonInterface $eventDate): void { - throw_if( - $method === CheckInMethod::Manual && blank($payload['actor_user_id'] ?? null), - CheckInException::invalidCheckInActor(), - ); - throw_unless( in_array($enrollment->status, [EnrollmentStatus::Confirmed, EnrollmentStatus::CheckedIn], strict: true), CheckInException::invalidCheckInStatus(), diff --git a/app-modules/events/src/CheckIn/Actions/ManualCheckInAction.php b/app-modules/events/src/CheckIn/Actions/ManualCheckInAction.php new file mode 100644 index 000000000..703bb4ab2 --- /dev/null +++ b/app-modules/events/src/CheckIn/Actions/ManualCheckInAction.php @@ -0,0 +1,35 @@ +handle( + enrollment: $enrollment, + method: CheckInMethod::Manual, + payload: ['actor_user_id' => $actorUserId], + eventDate: $eventDate, + actorUserId: $actorUserId, + triggeredBy: TriggeredBy::Admin, + ); + } +} diff --git a/app-modules/events/tests/Feature/CheckInActionTest.php b/app-modules/events/tests/Feature/CheckInActionTest.php index 5eb163c82..9155f2c70 100644 --- a/app-modules/events/tests/Feature/CheckInActionTest.php +++ b/app-modules/events/tests/Feature/CheckInActionTest.php @@ -2,7 +2,7 @@ declare(strict_types=1); -use He4rt\Events\CheckIn\Actions\CheckInAction; +use He4rt\Events\CheckIn\Actions\ManualCheckInAction; use He4rt\Events\CheckIn\Enums\CheckInMethod; use He4rt\Events\CheckIn\Events\ParticipantCheckedIn; use He4rt\Events\CheckIn\Exceptions\CheckInException; @@ -45,10 +45,9 @@ function createConfirmedEnrollmentForCheckIn(array $eventAttributes = [], array $organizer = User::factory()->create(); $enrollment = createConfirmedEnrollmentForCheckIn(); - $checkIn = resolve(CheckInAction::class)->handle( + $checkIn = resolve(ManualCheckInAction::class)->handle( enrollment: $enrollment, - method: CheckInMethod::Manual, - payload: ['actor_user_id' => $organizer->id], + actorUserId: $organizer->id, eventDate: now(), ); @@ -94,10 +93,9 @@ function createConfirmedEnrollmentForCheckIn(array $eventAttributes = [], array 'checked_in_at' => now(), ]); - $checkIn = resolve(CheckInAction::class)->handle( + $checkIn = resolve(ManualCheckInAction::class)->handle( enrollment: $enrollment, - method: CheckInMethod::Manual, - payload: ['actor_user_id' => $organizer->id], + actorUserId: $organizer->id, eventDate: $startsAt->clone()->addDay(), ); @@ -118,10 +116,9 @@ function createConfirmedEnrollmentForCheckIn(array $eventAttributes = [], array 'method' => CheckInMethod::Manual, ]); - expect(fn (): CheckIn => resolve(CheckInAction::class)->handle( + expect(fn (): CheckIn => resolve(ManualCheckInAction::class)->handle( enrollment: $enrollment, - method: CheckInMethod::Manual, - payload: ['actor_user_id' => User::factory()->create()->id], + actorUserId: User::factory()->create()->id, eventDate: now(), ))->toThrow(CheckInException::class); }); @@ -133,10 +130,9 @@ function createConfirmedEnrollmentForCheckIn(array $eventAttributes = [], array 'ends_at' => $startsAt->clone()->setTime(18, 0), ]); - expect(fn (): CheckIn => resolve(CheckInAction::class)->handle( + expect(fn (): CheckIn => resolve(ManualCheckInAction::class)->handle( enrollment: $enrollment, - method: CheckInMethod::Manual, - payload: ['actor_user_id' => User::factory()->create()->id], + actorUserId: User::factory()->create()->id, eventDate: $startsAt->clone()->addDay(), ))->toThrow(CheckInException::class); }); @@ -147,10 +143,9 @@ function createConfirmedEnrollmentForCheckIn(array $eventAttributes = [], array 'confirmed_at' => null, ]); - expect(fn (): CheckIn => resolve(CheckInAction::class)->handle( + expect(fn (): CheckIn => resolve(ManualCheckInAction::class)->handle( enrollment: $enrollment, - method: CheckInMethod::Manual, - payload: ['actor_user_id' => User::factory()->create()->id], + actorUserId: User::factory()->create()->id, eventDate: now(), ))->toThrow(CheckInException::class); }); @@ -158,10 +153,9 @@ function createConfirmedEnrollmentForCheckIn(array $eventAttributes = [], array test('when manual check-in has no actor user id, then check-in is rejected', function (): void { $enrollment = createConfirmedEnrollmentForCheckIn(); - expect(fn (): CheckIn => resolve(CheckInAction::class)->handle( + expect(fn (): CheckIn => resolve(ManualCheckInAction::class)->handle( enrollment: $enrollment, - method: CheckInMethod::Manual, - payload: [], + actorUserId: '', eventDate: now(), ))->toThrow(CheckInException::class); }); diff --git a/app-modules/panel-admin/src/Filament/Resources/Events/RelationManagers/EnrollmentsRelationManager.php b/app-modules/panel-admin/src/Filament/Resources/Events/RelationManagers/EnrollmentsRelationManager.php index b7a78338d..55c283c29 100644 --- a/app-modules/panel-admin/src/Filament/Resources/Events/RelationManagers/EnrollmentsRelationManager.php +++ b/app-modules/panel-admin/src/Filament/Resources/Events/RelationManagers/EnrollmentsRelationManager.php @@ -14,8 +14,7 @@ use Filament\Tables\Columns\TextColumn; use Filament\Tables\Filters\SelectFilter; use Filament\Tables\Table; -use He4rt\Events\CheckIn\Actions\CheckInAction; -use He4rt\Events\CheckIn\Enums\CheckInMethod; +use He4rt\Events\CheckIn\Actions\ManualCheckInAction; use He4rt\Events\CheckIn\Models\CheckIn; use He4rt\Events\Enrollment\Enums\EnrollmentStatus; use He4rt\Events\Enrollment\Models\Enrollment; @@ -154,10 +153,9 @@ private function checkInSchema(): array */ private function checkIn(Enrollment $enrollment, array $data): CheckIn { - return resolve(CheckInAction::class)->handle( + return resolve(ManualCheckInAction::class)->handle( enrollment: $enrollment, - method: CheckInMethod::Manual, - payload: ['actor_user_id' => auth()->id()], + actorUserId: (string) auth()->id(), eventDate: Date::parse($data['event_date']), ); } From b399e5629d2bd5fdab00f3137902dcf320ba48b6 Mon Sep 17 00:00:00 2001 From: Davi Castello Branco Tavares de Oliveira Date: Sat, 23 May 2026 14:49:52 -0400 Subject: [PATCH 6/7] refactor(events): replace positional params with DTOs in check-in and transition actions - Rename CheckInService -> CheckInAction, accepts CheckInDTO - ManualCheckInAction accepts ManualCheckInDTO - TransitionEnrollmentAction accepts TransitionEnrollmentDTO - Move TIMESTAMP_MAP to EnrollmentStatus::timestampColumn() --- .../{CheckInService.php => CheckInAction.php} | 41 +++++++-------- .../CheckIn/Actions/ManualCheckInAction.php | 33 +++++------- .../events/src/CheckIn/DTOs/CheckInDTO.php | 25 ++++++++++ .../src/CheckIn/DTOs/ManualCheckInDTO.php | 20 ++++++++ .../Actions/TransitionEnrollmentAction.php | 50 +++++-------------- .../DTOs/TransitionEnrollmentDTO.php | 26 ++++++++++ .../src/Enrollment/Enums/EnrollmentStatus.php | 8 +++ .../tests/Feature/CheckInActionTest.php | 49 +++++++++++------- .../EnrollmentsRelationManager.php | 9 ++-- 9 files changed, 159 insertions(+), 102 deletions(-) rename app-modules/events/src/CheckIn/Actions/{CheckInService.php => CheckInAction.php} (74%) create mode 100644 app-modules/events/src/CheckIn/DTOs/CheckInDTO.php create mode 100644 app-modules/events/src/CheckIn/DTOs/ManualCheckInDTO.php create mode 100644 app-modules/events/src/Enrollment/DTOs/TransitionEnrollmentDTO.php diff --git a/app-modules/events/src/CheckIn/Actions/CheckInService.php b/app-modules/events/src/CheckIn/Actions/CheckInAction.php similarity index 74% rename from app-modules/events/src/CheckIn/Actions/CheckInService.php rename to app-modules/events/src/CheckIn/Actions/CheckInAction.php index 775513e2c..5739fb856 100644 --- a/app-modules/events/src/CheckIn/Actions/CheckInService.php +++ b/app-modules/events/src/CheckIn/Actions/CheckInAction.php @@ -5,35 +5,26 @@ namespace He4rt\Events\CheckIn\Actions; use Carbon\CarbonInterface; -use He4rt\Events\CheckIn\Enums\CheckInMethod; +use He4rt\Events\CheckIn\DTOs\CheckInDTO; use He4rt\Events\CheckIn\Events\ParticipantCheckedIn; use He4rt\Events\CheckIn\Exceptions\CheckInException; use He4rt\Events\CheckIn\Models\CheckIn; use He4rt\Events\Enrollment\Actions\TransitionEnrollmentAction; +use He4rt\Events\Enrollment\DTOs\TransitionEnrollmentDTO; use He4rt\Events\Enrollment\Enums\EnrollmentStatus; -use He4rt\Events\Enrollment\Enums\TriggeredBy; use He4rt\Events\Enrollment\Models\Enrollment; use Illuminate\Database\UniqueConstraintViolationException; use Illuminate\Support\Facades\Date; use Illuminate\Support\Facades\DB; -final readonly class CheckInService +final readonly class CheckInAction { - /** - * @param array $payload - */ - public function handle( - Enrollment $enrollment, - CheckInMethod $method, - array $payload, - CarbonInterface $eventDate, - string $actorUserId, - TriggeredBy $triggeredBy, - ): CheckIn { - return DB::transaction(function () use ($enrollment, $method, $payload, $eventDate, $actorUserId, $triggeredBy): CheckIn { - $enrollment = $this->loadLockedEnrollment($enrollment); + public function handle(CheckInDTO $dto): CheckIn + { + return DB::transaction(function () use ($dto): CheckIn { + $enrollment = $this->loadLockedEnrollment($dto->enrollment); $event = $enrollment->event; - $normalizedEventDate = Date::parse($eventDate->toDateString())->startOfDay(); + $normalizedEventDate = Date::parse($dto->eventDate->toDateString())->startOfDay(); $this->validateCommonRules($enrollment, $normalizedEventDate); $isFirstCheckIn = !CheckIn::query() @@ -43,8 +34,8 @@ public function handle( try { $checkIn = CheckIn::query()->create([ 'enrollment_id' => $enrollment->id, - 'method' => $method, - 'payload' => $payload, + 'method' => $dto->method, + 'payload' => $dto->payload, 'event_date' => $normalizedEventDate, 'checked_in_at' => now(), ]); @@ -54,11 +45,13 @@ public function handle( if ($isFirstCheckIn && $enrollment->status === EnrollmentStatus::Confirmed) { resolve(TransitionEnrollmentAction::class)->handle( - enrollment: $enrollment, - toStatus: EnrollmentStatus::CheckedIn, - triggeredBy: $triggeredBy, - actorId: $actorUserId, - timestamp: $checkIn->checked_in_at, + new TransitionEnrollmentDTO( + enrollment: $enrollment, + toStatus: EnrollmentStatus::CheckedIn, + triggeredBy: $dto->triggeredBy, + actorId: $dto->actorUserId, + timestamp: $checkIn->checked_in_at, + ), ); } diff --git a/app-modules/events/src/CheckIn/Actions/ManualCheckInAction.php b/app-modules/events/src/CheckIn/Actions/ManualCheckInAction.php index 703bb4ab2..8f82399e1 100644 --- a/app-modules/events/src/CheckIn/Actions/ManualCheckInAction.php +++ b/app-modules/events/src/CheckIn/Actions/ManualCheckInAction.php @@ -4,32 +4,25 @@ namespace He4rt\Events\CheckIn\Actions; -use Carbon\CarbonInterface; +use He4rt\Events\CheckIn\DTOs\CheckInDTO; +use He4rt\Events\CheckIn\DTOs\ManualCheckInDTO; use He4rt\Events\CheckIn\Enums\CheckInMethod; -use He4rt\Events\CheckIn\Exceptions\CheckInException; use He4rt\Events\CheckIn\Models\CheckIn; use He4rt\Events\Enrollment\Enums\TriggeredBy; -use He4rt\Events\Enrollment\Models\Enrollment; final readonly class ManualCheckInAction { - public function handle( - Enrollment $enrollment, - string $actorUserId, - CarbonInterface $eventDate, - ): CheckIn { - throw_if( - blank($actorUserId), - CheckInException::invalidCheckInActor(), - ); - - return resolve(CheckInService::class)->handle( - enrollment: $enrollment, - method: CheckInMethod::Manual, - payload: ['actor_user_id' => $actorUserId], - eventDate: $eventDate, - actorUserId: $actorUserId, - triggeredBy: TriggeredBy::Admin, + public function handle(ManualCheckInDTO $dto): CheckIn + { + return resolve(CheckInAction::class)->handle( + new CheckInDTO( + enrollment: $dto->enrollment, + method: CheckInMethod::Manual, + payload: ['actor_user_id' => $dto->actorUserId], + eventDate: $dto->eventDate, + actorUserId: $dto->actorUserId, + triggeredBy: TriggeredBy::Admin, + ), ); } } diff --git a/app-modules/events/src/CheckIn/DTOs/CheckInDTO.php b/app-modules/events/src/CheckIn/DTOs/CheckInDTO.php new file mode 100644 index 000000000..becb5dcd6 --- /dev/null +++ b/app-modules/events/src/CheckIn/DTOs/CheckInDTO.php @@ -0,0 +1,25 @@ + $payload + */ + public function __construct( + public Enrollment $enrollment, + public CheckInMethod $method, + public array $payload, + public CarbonInterface $eventDate, + public string $actorUserId, + public TriggeredBy $triggeredBy, + ) {} +} diff --git a/app-modules/events/src/CheckIn/DTOs/ManualCheckInDTO.php b/app-modules/events/src/CheckIn/DTOs/ManualCheckInDTO.php new file mode 100644 index 000000000..261a00d3f --- /dev/null +++ b/app-modules/events/src/CheckIn/DTOs/ManualCheckInDTO.php @@ -0,0 +1,20 @@ +actorUserId), CheckInException::invalidCheckInActor()); + } +} diff --git a/app-modules/events/src/Enrollment/Actions/TransitionEnrollmentAction.php b/app-modules/events/src/Enrollment/Actions/TransitionEnrollmentAction.php index 46f4e25ef..e213ae4d3 100644 --- a/app-modules/events/src/Enrollment/Actions/TransitionEnrollmentAction.php +++ b/app-modules/events/src/Enrollment/Actions/TransitionEnrollmentAction.php @@ -4,61 +4,37 @@ namespace He4rt\Events\Enrollment\Actions; -use Carbon\CarbonInterface; -use He4rt\Events\Enrollment\Enums\EnrollmentStatus; -use He4rt\Events\Enrollment\Enums\TriggeredBy; +use He4rt\Events\Enrollment\DTOs\TransitionEnrollmentDTO; use He4rt\Events\Enrollment\Exceptions\EnrollmentException; -use He4rt\Events\Enrollment\Models\Enrollment; use He4rt\Events\Enrollment\Models\EnrollmentTransition; final readonly class TransitionEnrollmentAction { - private const array TIMESTAMP_MAP = [ - 'confirmed' => 'confirmed_at', - 'checked_in' => 'checked_in_at', - 'attended' => 'attended_at', - 'cancelled' => 'cancelled_at', - ]; - - /** - * @param array $metadata - */ - public function handle( - Enrollment $enrollment, - EnrollmentStatus $toStatus, - TriggeredBy $triggeredBy, - ?string $actorId = null, - ?string $reason = null, - array $metadata = [], - ?CarbonInterface $timestamp = null, - ): EnrollmentTransition { - $fromStatus = $enrollment->status; + public function handle(TransitionEnrollmentDTO $dto): EnrollmentTransition + { + $fromStatus = $dto->enrollment->status; + $toStatus = $dto->toStatus; throw_unless( $fromStatus->canTransitionTo($toStatus), EnrollmentException::invalidTransition($fromStatus, $toStatus), ); - $timestampColumn = $this->resolveTimestampColumn($toStatus); + $timestampColumn = $toStatus->timestampColumn(); - $enrollment->forceFill([ + $dto->enrollment->forceFill([ 'status' => $toStatus, - $timestampColumn => $timestamp ?? now(), + $timestampColumn => $dto->timestamp ?? now(), ])->save(); return EnrollmentTransition::query()->create([ - 'enrollment_id' => $enrollment->id, + 'enrollment_id' => $dto->enrollment->id, 'from_status' => $fromStatus, 'to_status' => $toStatus, - 'actor_id' => $actorId, - 'triggered_by' => $triggeredBy, - 'reason' => $reason, - 'metadata' => $metadata ?: null, + 'actor_id' => $dto->actorId, + 'triggered_by' => $dto->triggeredBy, + 'reason' => $dto->reason, + 'metadata' => $dto->metadata ?: null, ]); } - - private function resolveTimestampColumn(EnrollmentStatus $status): ?string - { - return self::TIMESTAMP_MAP[$status->value] ?? null; - } } diff --git a/app-modules/events/src/Enrollment/DTOs/TransitionEnrollmentDTO.php b/app-modules/events/src/Enrollment/DTOs/TransitionEnrollmentDTO.php new file mode 100644 index 000000000..c84a27bf7 --- /dev/null +++ b/app-modules/events/src/Enrollment/DTOs/TransitionEnrollmentDTO.php @@ -0,0 +1,26 @@ + $metadata + */ + public function __construct( + public Enrollment $enrollment, + public EnrollmentStatus $toStatus, + public TriggeredBy $triggeredBy, + public ?string $actorId = null, + public ?string $reason = null, + public array $metadata = [], + public ?CarbonInterface $timestamp = null, + ) {} +} diff --git a/app-modules/events/src/Enrollment/Enums/EnrollmentStatus.php b/app-modules/events/src/Enrollment/Enums/EnrollmentStatus.php index 79b4fbb83..1e0d46c15 100644 --- a/app-modules/events/src/Enrollment/Enums/EnrollmentStatus.php +++ b/app-modules/events/src/Enrollment/Enums/EnrollmentStatus.php @@ -65,6 +65,14 @@ public function canTransitionTo(self $target): bool }; } + public function timestampColumn(): ?string + { + return match ($this) { + self::Confirmed, self::CheckedIn, self::Attended, self::Cancelled => $this->value.'_at', + default => null, + }; + } + public function isTerminal(): bool { return in_array($this, [self::Attended, self::Cancelled, self::Rejected, self::NoShow], strict: true); diff --git a/app-modules/events/tests/Feature/CheckInActionTest.php b/app-modules/events/tests/Feature/CheckInActionTest.php index 9155f2c70..01a5dc38b 100644 --- a/app-modules/events/tests/Feature/CheckInActionTest.php +++ b/app-modules/events/tests/Feature/CheckInActionTest.php @@ -3,6 +3,7 @@ declare(strict_types=1); use He4rt\Events\CheckIn\Actions\ManualCheckInAction; +use He4rt\Events\CheckIn\DTOs\ManualCheckInDTO; use He4rt\Events\CheckIn\Enums\CheckInMethod; use He4rt\Events\CheckIn\Events\ParticipantCheckedIn; use He4rt\Events\CheckIn\Exceptions\CheckInException; @@ -46,9 +47,11 @@ function createConfirmedEnrollmentForCheckIn(array $eventAttributes = [], array $enrollment = createConfirmedEnrollmentForCheckIn(); $checkIn = resolve(ManualCheckInAction::class)->handle( - enrollment: $enrollment, - actorUserId: $organizer->id, - eventDate: now(), + new ManualCheckInDTO( + enrollment: $enrollment, + actorUserId: $organizer->id, + eventDate: now(), + ), ); expect($checkIn)->toBeInstanceOf(CheckIn::class) @@ -94,9 +97,11 @@ function createConfirmedEnrollmentForCheckIn(array $eventAttributes = [], array ]); $checkIn = resolve(ManualCheckInAction::class)->handle( - enrollment: $enrollment, - actorUserId: $organizer->id, - eventDate: $startsAt->clone()->addDay(), + new ManualCheckInDTO( + enrollment: $enrollment, + actorUserId: $organizer->id, + eventDate: $startsAt->clone()->addDay(), + ), ); expect($checkIn->event_date->isSameDay($startsAt->clone()->addDay()))->toBeTrue() @@ -117,9 +122,11 @@ function createConfirmedEnrollmentForCheckIn(array $eventAttributes = [], array ]); expect(fn (): CheckIn => resolve(ManualCheckInAction::class)->handle( - enrollment: $enrollment, - actorUserId: User::factory()->create()->id, - eventDate: now(), + new ManualCheckInDTO( + enrollment: $enrollment, + actorUserId: User::factory()->create()->id, + eventDate: now(), + ), ))->toThrow(CheckInException::class); }); @@ -131,9 +138,11 @@ function createConfirmedEnrollmentForCheckIn(array $eventAttributes = [], array ]); expect(fn (): CheckIn => resolve(ManualCheckInAction::class)->handle( - enrollment: $enrollment, - actorUserId: User::factory()->create()->id, - eventDate: $startsAt->clone()->addDay(), + new ManualCheckInDTO( + enrollment: $enrollment, + actorUserId: User::factory()->create()->id, + eventDate: $startsAt->clone()->addDay(), + ), ))->toThrow(CheckInException::class); }); @@ -144,9 +153,11 @@ function createConfirmedEnrollmentForCheckIn(array $eventAttributes = [], array ]); expect(fn (): CheckIn => resolve(ManualCheckInAction::class)->handle( - enrollment: $enrollment, - actorUserId: User::factory()->create()->id, - eventDate: now(), + new ManualCheckInDTO( + enrollment: $enrollment, + actorUserId: User::factory()->create()->id, + eventDate: now(), + ), ))->toThrow(CheckInException::class); }); @@ -154,8 +165,10 @@ function createConfirmedEnrollmentForCheckIn(array $eventAttributes = [], array $enrollment = createConfirmedEnrollmentForCheckIn(); expect(fn (): CheckIn => resolve(ManualCheckInAction::class)->handle( - enrollment: $enrollment, - actorUserId: '', - eventDate: now(), + new ManualCheckInDTO( + enrollment: $enrollment, + actorUserId: '', + eventDate: now(), + ), ))->toThrow(CheckInException::class); }); diff --git a/app-modules/panel-admin/src/Filament/Resources/Events/RelationManagers/EnrollmentsRelationManager.php b/app-modules/panel-admin/src/Filament/Resources/Events/RelationManagers/EnrollmentsRelationManager.php index 55c283c29..edb1020aa 100644 --- a/app-modules/panel-admin/src/Filament/Resources/Events/RelationManagers/EnrollmentsRelationManager.php +++ b/app-modules/panel-admin/src/Filament/Resources/Events/RelationManagers/EnrollmentsRelationManager.php @@ -15,6 +15,7 @@ use Filament\Tables\Filters\SelectFilter; use Filament\Tables\Table; use He4rt\Events\CheckIn\Actions\ManualCheckInAction; +use He4rt\Events\CheckIn\DTOs\ManualCheckInDTO; use He4rt\Events\CheckIn\Models\CheckIn; use He4rt\Events\Enrollment\Enums\EnrollmentStatus; use He4rt\Events\Enrollment\Models\Enrollment; @@ -154,9 +155,11 @@ private function checkInSchema(): array private function checkIn(Enrollment $enrollment, array $data): CheckIn { return resolve(ManualCheckInAction::class)->handle( - enrollment: $enrollment, - actorUserId: (string) auth()->id(), - eventDate: Date::parse($data['event_date']), + new ManualCheckInDTO( + enrollment: $enrollment, + actorUserId: (string) auth()->id(), + eventDate: Date::parse($data['event_date']), + ), ); } } From de8deb1b84071accf08fa8c1a78a3d8f14139ecd Mon Sep 17 00:00:00 2001 From: Davi Castello Branco Tavares de Oliveira Date: Sat, 23 May 2026 15:41:35 -0400 Subject: [PATCH 7/7] fix(events): tighten enrollment transition updates --- app-modules/events/lang/en/exceptions.php | 1 + app-modules/events/lang/pt_BR/exceptions.php | 1 + .../Actions/TransitionEnrollmentAction.php | 13 ++++++++----- .../src/Enrollment/Enums/EnrollmentStatus.php | 3 ++- .../Enrollment/Exceptions/EnrollmentException.php | 8 ++++++++ .../events/tests/Unit/EnrollmentStatusTest.php | 6 ++++++ 6 files changed, 26 insertions(+), 6 deletions(-) diff --git a/app-modules/events/lang/en/exceptions.php b/app-modules/events/lang/en/exceptions.php index 8389239d4..58511678b 100644 --- a/app-modules/events/lang/en/exceptions.php +++ b/app-modules/events/lang/en/exceptions.php @@ -9,4 +9,5 @@ 'event_not_active' => 'This event is not available for enrollment.', 'invalid_enrollment_method' => 'This event requires application enrollment, not RSVP.', 'event_full' => 'This event has reached maximum capacity and does not have a waitlist.', + 'response_message_not_implemented' => 'Response message is not implemented for enrollment status: :status.', ]; diff --git a/app-modules/events/lang/pt_BR/exceptions.php b/app-modules/events/lang/pt_BR/exceptions.php index 460e186fe..fdd707908 100644 --- a/app-modules/events/lang/pt_BR/exceptions.php +++ b/app-modules/events/lang/pt_BR/exceptions.php @@ -9,4 +9,5 @@ 'event_not_active' => 'Este evento não está disponível para inscrição.', 'invalid_enrollment_method' => 'Esse evento exige inscrição via formulário, não RSVP.', 'event_full' => 'Este evento atingiu a capacidade máxima e não possui lista de espera.', + 'response_message_not_implemented' => 'Mensagem de resposta não implementada para o status de inscrição: :status.', ]; diff --git a/app-modules/events/src/Enrollment/Actions/TransitionEnrollmentAction.php b/app-modules/events/src/Enrollment/Actions/TransitionEnrollmentAction.php index e213ae4d3..a68420708 100644 --- a/app-modules/events/src/Enrollment/Actions/TransitionEnrollmentAction.php +++ b/app-modules/events/src/Enrollment/Actions/TransitionEnrollmentAction.php @@ -20,12 +20,15 @@ public function handle(TransitionEnrollmentDTO $dto): EnrollmentTransition EnrollmentException::invalidTransition($fromStatus, $toStatus), ); - $timestampColumn = $toStatus->timestampColumn(); - - $dto->enrollment->forceFill([ + $attributes = [ 'status' => $toStatus, - $timestampColumn => $dto->timestamp ?? now(), - ])->save(); + ]; + + if ($timestampColumn = $toStatus->timestampColumn()) { + $attributes[$timestampColumn] = $dto->timestamp ?? now(); + } + + $dto->enrollment->update($attributes); return EnrollmentTransition::query()->create([ 'enrollment_id' => $dto->enrollment->id, diff --git a/app-modules/events/src/Enrollment/Enums/EnrollmentStatus.php b/app-modules/events/src/Enrollment/Enums/EnrollmentStatus.php index 1e0d46c15..500b9bb88 100644 --- a/app-modules/events/src/Enrollment/Enums/EnrollmentStatus.php +++ b/app-modules/events/src/Enrollment/Enums/EnrollmentStatus.php @@ -9,6 +9,7 @@ use Filament\Support\Contracts\HasIcon; use Filament\Support\Contracts\HasLabel; use Filament\Support\Icons\Heroicon; +use He4rt\Events\Enrollment\Exceptions\EnrollmentException; enum EnrollmentStatus: string implements HasColor, HasIcon, HasLabel { @@ -98,7 +99,7 @@ public function getResponseMessage(): string return match ($this) { self::Confirmed => __('events::pages.confirm_presence_success'), self::Waitlisted => __('events::pages.waitlist_success'), - default => '', + default => throw EnrollmentException::responseMessageNotImplemented($this), }; } } diff --git a/app-modules/events/src/Enrollment/Exceptions/EnrollmentException.php b/app-modules/events/src/Enrollment/Exceptions/EnrollmentException.php index 1aba6ed07..e72ce0a8c 100644 --- a/app-modules/events/src/Enrollment/Exceptions/EnrollmentException.php +++ b/app-modules/events/src/Enrollment/Exceptions/EnrollmentException.php @@ -57,4 +57,12 @@ public static function eventFull(): self Response::HTTP_UNPROCESSABLE_ENTITY, ); } + + public static function responseMessageNotImplemented(EnrollmentStatus $status): self + { + return new self( + __('events::exceptions.response_message_not_implemented', ['status' => $status->value]), + Response::HTTP_INTERNAL_SERVER_ERROR, + ); + } } diff --git a/app-modules/events/tests/Unit/EnrollmentStatusTest.php b/app-modules/events/tests/Unit/EnrollmentStatusTest.php index 1f0f934e7..35afe8448 100644 --- a/app-modules/events/tests/Unit/EnrollmentStatusTest.php +++ b/app-modules/events/tests/Unit/EnrollmentStatusTest.php @@ -3,6 +3,7 @@ declare(strict_types=1); use He4rt\Events\Enrollment\Enums\EnrollmentStatus; +use He4rt\Events\Enrollment\Exceptions\EnrollmentException; test('when a pending enrollment is evaluated, then it can transition to confirmed, rejected, or cancelled', function (): void { expect(EnrollmentStatus::Pending->canTransitionTo(EnrollmentStatus::Confirmed))->toBeTrue() @@ -74,3 +75,8 @@ [EnrollmentStatus::Confirmed, 'events::pages.confirm_presence_success'], [EnrollmentStatus::Waitlisted, 'events::pages.waitlist_success'], ]); + +test('when response message is requested for unsupported enrollment status, then it fails explicitly', function (): void { + expect(fn (): string => EnrollmentStatus::Pending->getResponseMessage()) + ->toThrow(EnrollmentException::class); +});