Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
19 / 19
100.00% covered (success)
100.00%
2 / 2
CRAP
100.00% covered (success)
100.00%
1 / 1
CompleteDealTaskAction
100.00% covered (success)
100.00%
19 / 19
100.00% covered (success)
100.00%
2 / 2
4
100.00% covered (success)
100.00%
1 / 1
 __construct
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 execute
100.00% covered (success)
100.00%
18 / 18
100.00% covered (success)
100.00%
1 / 1
3
1<?php
2
3declare(strict_types=1);
4
5namespace ImovelZapAi\Sales\Application\Actions;
6
7use App\Models\User;
8use Illuminate\Support\Facades\DB;
9use ImovelZapAi\Sales\Application\Services\DealActivityRecorder;
10use ImovelZapAi\Sales\Domain\Enums\DealActivityType;
11use ImovelZapAi\Sales\Domain\Enums\DealTaskStatus;
12use ImovelZapAi\Sales\Domain\Exceptions\DealException;
13use ImovelZapAi\Sales\Infrastructure\Persistence\Eloquent\DealTaskModel;
14
15final class CompleteDealTaskAction
16{
17    public function __construct(
18        private readonly DealActivityRecorder $activities,
19    ) {}
20
21    public function execute(DealTaskModel $task, User $actor): DealTaskModel
22    {
23        if ($task->status !== DealTaskStatus::Open) {
24            throw DealException::invalidState('tarefa não está aberta.');
25        }
26
27        return DB::transaction(function () use ($task, $actor): DealTaskModel {
28            $task->forceFill([
29                'status' => DealTaskStatus::Done,
30                'completed_at' => now(),
31            ])->save();
32
33            $deal = $task->deal;
34            if ($deal !== null) {
35                $this->activities->record(
36                    $deal,
37                    DealActivityType::TaskCompleted,
38                    'Tarefa concluída: '.$task->title,
39                    actorUserId: (int) $actor->id,
40                    meta: ['task_id' => $task->id],
41                );
42            }
43
44            return $task->fresh();
45        });
46    }
47}