Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
100.00% |
17 / 17 |
|
100.00% |
2 / 2 |
CRAP | |
100.00% |
1 / 1 |
| ChangeVisitPropertyAction | |
100.00% |
17 / 17 |
|
100.00% |
2 / 2 |
3 | |
100.00% |
1 / 1 |
| __construct | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| execute | |
100.00% |
16 / 16 |
|
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\Exceptions\VisitException; |
| 13 | use ImovelZapAi\Visits\Infrastructure\Persistence\Eloquent\VisitModel; |
| 14 | |
| 15 | final class ChangeVisitPropertyAction |
| 16 | { |
| 17 | public function __construct( |
| 18 | private readonly VisitLogWriter $logWriter, |
| 19 | private readonly VisitSnapshotter $snapshotter, |
| 20 | ) {} |
| 21 | |
| 22 | public function execute(VisitModel $visit, User $actor, string $propertyId): VisitModel |
| 23 | { |
| 24 | if ($visit->status->isTerminal()) { |
| 25 | throw VisitException::invalidState($visit->status->value, 'change_property'); |
| 26 | } |
| 27 | |
| 28 | return DB::transaction(function () use ($visit, $actor, $propertyId): VisitModel { |
| 29 | $before = $this->snapshotter->snapshot($visit); |
| 30 | |
| 31 | $visit->forceFill(['property_id' => $propertyId])->save(); |
| 32 | |
| 33 | $this->logWriter->write( |
| 34 | $visit->tenant_id, |
| 35 | $visit->id, |
| 36 | VisitLogEvent::PropertyChanged, |
| 37 | actorUserId: (int) $actor->id, |
| 38 | before: $before, |
| 39 | after: $this->snapshotter->snapshot($visit->fresh()), |
| 40 | message: 'Imóvel da visita alterado.', |
| 41 | ); |
| 42 | |
| 43 | return $visit->fresh(['property']); |
| 44 | }); |
| 45 | } |
| 46 | } |