Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
96.30% |
26 / 27 |
|
50.00% |
1 / 2 |
CRAP | |
0.00% |
0 / 1 |
| AddDealNoteAction | |
96.30% |
26 / 27 |
|
50.00% |
1 / 2 |
3 | |
0.00% |
0 / 1 |
| __construct | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| execute | |
96.15% |
25 / 26 |
|
0.00% |
0 / 1 |
2 | |||
| 1 | <?php |
| 2 | |
| 3 | declare(strict_types=1); |
| 4 | |
| 5 | namespace ImovelZapAi\Sales\Application\Actions; |
| 6 | |
| 7 | use App\Models\User; |
| 8 | use Illuminate\Support\Facades\DB; |
| 9 | use ImovelZapAi\Sales\Application\Services\DealActivityRecorder; |
| 10 | use ImovelZapAi\Sales\Application\Services\DealHistoryWriter; |
| 11 | use ImovelZapAi\Sales\Domain\Enums\DealActivityType; |
| 12 | use ImovelZapAi\Sales\Domain\Enums\DealHistoryEvent; |
| 13 | use ImovelZapAi\Sales\Domain\Exceptions\DealException; |
| 14 | use ImovelZapAi\Sales\Infrastructure\Persistence\Eloquent\DealModel; |
| 15 | use ImovelZapAi\Sales\Infrastructure\Persistence\Eloquent\DealNoteModel; |
| 16 | |
| 17 | final class AddDealNoteAction |
| 18 | { |
| 19 | public function __construct( |
| 20 | private readonly DealActivityRecorder $activities, |
| 21 | private readonly DealHistoryWriter $history, |
| 22 | ) {} |
| 23 | |
| 24 | public function execute(DealModel $deal, User $actor, string $body): DealNoteModel |
| 25 | { |
| 26 | if (trim($body) === '') { |
| 27 | throw DealException::invalidState('observação vazia.'); |
| 28 | } |
| 29 | |
| 30 | return DB::transaction(function () use ($deal, $actor, $body): DealNoteModel { |
| 31 | $note = DealNoteModel::query()->create([ |
| 32 | 'tenant_id' => $deal->tenant_id, |
| 33 | 'deal_id' => $deal->id, |
| 34 | 'author_user_id' => $actor->id, |
| 35 | 'body' => $body, |
| 36 | ]); |
| 37 | |
| 38 | $this->activities->record( |
| 39 | $deal, |
| 40 | DealActivityType::NoteAdded, |
| 41 | 'Observação do corretor', |
| 42 | $body, |
| 43 | (int) $actor->id, |
| 44 | ['note_id' => $note->id], |
| 45 | ); |
| 46 | |
| 47 | $this->history->write( |
| 48 | $deal->tenant_id, |
| 49 | $deal->id, |
| 50 | DealHistoryEvent::NoteAdded, |
| 51 | actorUserId: (int) $actor->id, |
| 52 | message: 'Observação adicionada.', |
| 53 | ); |
| 54 | |
| 55 | return $note; |
| 56 | }); |
| 57 | } |
| 58 | } |