Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
100.00% |
23 / 23 |
|
100.00% |
2 / 2 |
CRAP | |
100.00% |
1 / 1 |
| CheckInVisitAction | |
100.00% |
23 / 23 |
|
100.00% |
2 / 2 |
3 | |
100.00% |
1 / 1 |
| __construct | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| execute | |
100.00% |
22 / 22 |
|
100.00% |
1 / 1 |
2 | |||
| 1 | <?php |
| 2 | |
| 3 | declare(strict_types=1); |
| 4 | |
| 5 | namespace ImovelZapAi\Visits\Application\Actions; |
| 6 | |
| 7 | use App\Models\User; |
| 8 | use Illuminate\Support\Facades\DB; |
| 9 | use ImovelZapAi\Visits\Application\Services\VisitLogWriter; |
| 10 | use ImovelZapAi\Visits\Application\Services\VisitSnapshotter; |
| 11 | use ImovelZapAi\Visits\Domain\Enums\VisitLogEvent; |
| 12 | use ImovelZapAi\Visits\Domain\Enums\VisitStatus; |
| 13 | use ImovelZapAi\Visits\Domain\Exceptions\VisitException; |
| 14 | use ImovelZapAi\Visits\Infrastructure\Persistence\Eloquent\VisitModel; |
| 15 | |
| 16 | final class CheckInVisitAction |
| 17 | { |
| 18 | public function __construct( |
| 19 | private readonly VisitLogWriter $logWriter, |
| 20 | private readonly VisitSnapshotter $snapshotter, |
| 21 | ) {} |
| 22 | |
| 23 | public function execute( |
| 24 | VisitModel $visit, |
| 25 | User $actor, |
| 26 | ?float $latitude = null, |
| 27 | ?float $longitude = null, |
| 28 | ?string $notes = null, |
| 29 | ): VisitModel { |
| 30 | if (! $visit->status->canCheckIn()) { |
| 31 | throw VisitException::invalidState($visit->status->value, 'check_in'); |
| 32 | } |
| 33 | |
| 34 | return DB::transaction(function () use ($visit, $actor, $latitude, $longitude, $notes): VisitModel { |
| 35 | $before = $this->snapshotter->snapshot($visit); |
| 36 | |
| 37 | $visit->forceFill([ |
| 38 | 'status' => VisitStatus::InProgress, |
| 39 | 'check_in_at' => now(), |
| 40 | 'check_in_latitude' => $latitude, |
| 41 | 'check_in_longitude' => $longitude, |
| 42 | 'check_in_notes' => $notes, |
| 43 | ])->save(); |
| 44 | |
| 45 | $this->logWriter->write( |
| 46 | $visit->tenant_id, |
| 47 | $visit->id, |
| 48 | VisitLogEvent::CheckedIn, |
| 49 | actorUserId: (int) $actor->id, |
| 50 | before: $before, |
| 51 | after: $this->snapshotter->snapshot($visit->fresh()), |
| 52 | message: 'Check-in registrado.', |
| 53 | ); |
| 54 | |
| 55 | return $visit->fresh(); |
| 56 | }); |
| 57 | } |
| 58 | } |