Repository navigation
feat(events): implement manual check-in #278
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 6 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
c98a100
feat(events): add manual participant check-in
davicbtoliveira 6c648a6
fix(events): satisfy phpstan in event models
davicbtoliveira 5e2cb50
fix(events): correct event scope phpstan types
davicbtoliveira 42ba638
refactor(events): extract transition action and own check-in exceptions
davicbtoliveira a474bd7
refactor(events): extract ManualCheckInAction and CheckInService from…
davicbtoliveira b399e56
refactor(events): replace positional params with DTOs in check-in and…
davicbtoliveira de8deb1
fix(events): tighten enrollment transition updates
davicbtoliveira File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
24 changes: 24 additions & 0 deletions
24
...nts/database/migrations/2026_05_22_000001_add_checked_in_at_to_events_check_ins_table.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,24 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| use Illuminate\Database\Migrations\Migration; | ||
| use Illuminate\Database\Schema\Blueprint; | ||
| use Illuminate\Support\Facades\Schema; | ||
|
|
||
| return new class extends Migration | ||
| { | ||
| public function up(): void | ||
| { | ||
| Schema::table('events_check_ins', function (Blueprint $table): void { | ||
| $table->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'); | ||
| }); | ||
| } | ||
| }; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,10 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| return [ | ||
| '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.', | ||
| ]; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,10 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| return [ | ||
| '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.', | ||
| ]; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
105 changes: 105 additions & 0 deletions
105
app-modules/events/src/CheckIn/Actions/CheckInAction.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,105 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| namespace He4rt\Events\CheckIn\Actions; | ||
|
|
||
| use Carbon\CarbonInterface; | ||
| 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\Models\Enrollment; | ||
| use Illuminate\Database\UniqueConstraintViolationException; | ||
| use Illuminate\Support\Facades\Date; | ||
| use Illuminate\Support\Facades\DB; | ||
|
|
||
| final readonly class CheckInAction | ||
| { | ||
| public function handle(CheckInDTO $dto): CheckIn | ||
| { | ||
| return DB::transaction(function () use ($dto): CheckIn { | ||
| $enrollment = $this->loadLockedEnrollment($dto->enrollment); | ||
| $event = $enrollment->event; | ||
| $normalizedEventDate = Date::parse($dto->eventDate->toDateString())->startOfDay(); | ||
|
|
||
| $this->validateCommonRules($enrollment, $normalizedEventDate); | ||
| $isFirstCheckIn = !CheckIn::query() | ||
| ->where('enrollment_id', $enrollment->id) | ||
| ->exists(); | ||
|
|
||
| try { | ||
| $checkIn = CheckIn::query()->create([ | ||
| 'enrollment_id' => $enrollment->id, | ||
| 'method' => $dto->method, | ||
| 'payload' => $dto->payload, | ||
| 'event_date' => $normalizedEventDate, | ||
| 'checked_in_at' => now(), | ||
| ]); | ||
| } catch (UniqueConstraintViolationException) { | ||
| throw CheckInException::alreadyCheckedInForDate(); | ||
| } | ||
|
|
||
| if ($isFirstCheckIn && $enrollment->status === EnrollmentStatus::Confirmed) { | ||
| resolve(TransitionEnrollmentAction::class)->handle( | ||
| new TransitionEnrollmentDTO( | ||
| enrollment: $enrollment, | ||
| toStatus: EnrollmentStatus::CheckedIn, | ||
| triggeredBy: $dto->triggeredBy, | ||
| actorId: $dto->actorUserId, | ||
| timestamp: $checkIn->checked_in_at, | ||
| ), | ||
| ); | ||
| } | ||
|
|
||
| $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(); | ||
| } | ||
|
|
||
| private function validateCommonRules(Enrollment $enrollment, CarbonInterface $eventDate): void | ||
| { | ||
| throw_unless( | ||
| in_array($enrollment->status, [EnrollmentStatus::Confirmed, EnrollmentStatus::CheckedIn], strict: true), | ||
| CheckInException::invalidCheckInStatus(), | ||
| ); | ||
|
|
||
| $event = $enrollment->event; | ||
| $eventDateString = $eventDate->toDateString(); | ||
|
|
||
| throw_unless( | ||
| $eventDateString >= $event->starts_at->toDateString() && $eventDateString <= $event->ends_at->toDateString(), | ||
| CheckInException::checkInOutsideEventDateRange(), | ||
| ); | ||
|
|
||
| throw_if( | ||
| CheckIn::query() | ||
| ->where('enrollment_id', $enrollment->id) | ||
| ->whereDate('event_date', $eventDate->toDateString()) | ||
| ->exists(), | ||
| CheckInException::alreadyCheckedInForDate(), | ||
| ); | ||
| } | ||
| } |
28 changes: 28 additions & 0 deletions
28
app-modules/events/src/CheckIn/Actions/ManualCheckInAction.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,28 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| namespace He4rt\Events\CheckIn\Actions; | ||
|
|
||
| use He4rt\Events\CheckIn\DTOs\CheckInDTO; | ||
| use He4rt\Events\CheckIn\DTOs\ManualCheckInDTO; | ||
| use He4rt\Events\CheckIn\Enums\CheckInMethod; | ||
| use He4rt\Events\CheckIn\Models\CheckIn; | ||
| use He4rt\Events\Enrollment\Enums\TriggeredBy; | ||
|
|
||
| final readonly class ManualCheckInAction | ||
| { | ||
| 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, | ||
| ), | ||
| ); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,25 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| namespace He4rt\Events\CheckIn\DTOs; | ||
|
|
||
| use Carbon\CarbonInterface; | ||
| use He4rt\Events\CheckIn\Enums\CheckInMethod; | ||
| use He4rt\Events\Enrollment\Enums\TriggeredBy; | ||
| use He4rt\Events\Enrollment\Models\Enrollment; | ||
|
|
||
| final readonly class CheckInDTO | ||
| { | ||
| /** | ||
| * @param array<string, mixed> $payload | ||
| */ | ||
| public function __construct( | ||
| public Enrollment $enrollment, | ||
| public CheckInMethod $method, | ||
| public array $payload, | ||
| public CarbonInterface $eventDate, | ||
| public string $actorUserId, | ||
| public TriggeredBy $triggeredBy, | ||
| ) {} | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,20 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| namespace He4rt\Events\CheckIn\DTOs; | ||
|
|
||
| use Carbon\CarbonInterface; | ||
| use He4rt\Events\CheckIn\Exceptions\CheckInException; | ||
| use He4rt\Events\Enrollment\Models\Enrollment; | ||
|
|
||
| final readonly class ManualCheckInDTO | ||
| { | ||
| public function __construct( | ||
| public Enrollment $enrollment, | ||
| public string $actorUserId, | ||
| public CarbonInterface $eventDate, | ||
| ) { | ||
| throw_if(blank($this->actorUserId), CheckInException::invalidCheckInActor()); | ||
| } | ||
| } |
22 changes: 22 additions & 0 deletions
22
app-modules/events/src/CheckIn/Events/ParticipantCheckedIn.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,22 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| namespace He4rt\Events\CheckIn\Events; | ||
|
|
||
| use Illuminate\Contracts\Events\ShouldDispatchAfterCommit; | ||
| use Illuminate\Foundation\Events\Dispatchable; | ||
|
|
||
| final readonly class ParticipantCheckedIn implements ShouldDispatchAfterCommit | ||
| { | ||
| use Dispatchable; | ||
|
|
||
| public function __construct( | ||
| public string $checkInId, | ||
| public string $enrollmentId, | ||
| public string $eventId, | ||
| public string $userId, | ||
| public string $eventDate, | ||
| public int $xpRewardOnCheckedIn, | ||
| ) {} | ||
| } |
43 changes: 43 additions & 0 deletions
43
app-modules/events/src/CheckIn/Exceptions/CheckInException.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,43 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| namespace He4rt\Events\CheckIn\Exceptions; | ||
|
|
||
| use Exception; | ||
| use Symfony\Component\HttpFoundation\Response; | ||
|
|
||
| final class CheckInException extends Exception | ||
| { | ||
| public static function invalidCheckInStatus(): self | ||
| { | ||
| return new self( | ||
| __('events::check_in.invalid_check_in_status'), | ||
| Response::HTTP_UNPROCESSABLE_ENTITY, | ||
| ); | ||
| } | ||
|
|
||
| public static function checkInOutsideEventDateRange(): self | ||
| { | ||
| return new self( | ||
| __('events::check_in.check_in_outside_event_date_range'), | ||
| Response::HTTP_UNPROCESSABLE_ENTITY, | ||
| ); | ||
| } | ||
|
|
||
| public static function alreadyCheckedInForDate(): self | ||
| { | ||
| return new self( | ||
| __('events::check_in.already_checked_in_for_date'), | ||
| Response::HTTP_CONFLICT, | ||
| ); | ||
| } | ||
|
|
||
| public static function invalidCheckInActor(): self | ||
| { | ||
| return new self( | ||
| __('events::check_in.invalid_check_in_actor'), | ||
| Response::HTTP_UNPROCESSABLE_ENTITY, | ||
| ); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
40 changes: 40 additions & 0 deletions
40
app-modules/events/src/Enrollment/Actions/TransitionEnrollmentAction.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,40 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| namespace He4rt\Events\Enrollment\Actions; | ||
|
|
||
| use He4rt\Events\Enrollment\DTOs\TransitionEnrollmentDTO; | ||
| use He4rt\Events\Enrollment\Exceptions\EnrollmentException; | ||
| use He4rt\Events\Enrollment\Models\EnrollmentTransition; | ||
|
|
||
| final readonly class TransitionEnrollmentAction | ||
| { | ||
| public function handle(TransitionEnrollmentDTO $dto): EnrollmentTransition | ||
| { | ||
| $fromStatus = $dto->enrollment->status; | ||
| $toStatus = $dto->toStatus; | ||
|
|
||
| throw_unless( | ||
| $fromStatus->canTransitionTo($toStatus), | ||
| EnrollmentException::invalidTransition($fromStatus, $toStatus), | ||
| ); | ||
|
|
||
| $timestampColumn = $toStatus->timestampColumn(); | ||
|
|
||
| $dto->enrollment->forceFill([ | ||
| 'status' => $toStatus, | ||
| $timestampColumn => $dto->timestamp ?? now(), | ||
| ])->save(); | ||
|
|
||
| return EnrollmentTransition::query()->create([ | ||
| 'enrollment_id' => $dto->enrollment->id, | ||
| 'from_status' => $fromStatus, | ||
| 'to_status' => $toStatus, | ||
| 'actor_id' => $dto->actorId, | ||
| 'triggered_by' => $dto->triggeredBy, | ||
| 'reason' => $dto->reason, | ||
| 'metadata' => $dto->metadata ?: null, | ||
| ]); | ||
| } | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.