Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
20 / 20
100.00% covered (success)
100.00%
2 / 2
CRAP
100.00% covered (success)
100.00%
1 / 1
AddProposalCommentAction
100.00% covered (success)
100.00%
20 / 20
100.00% covered (success)
100.00%
2 / 2
2
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%
19 / 19
100.00% covered (success)
100.00%
1 / 1
1
1<?php
2
3declare(strict_types=1);
4
5namespace ImovelZapAi\Proposals\Application\Actions;
6
7use App\Models\User;
8use ImovelZapAi\Proposals\Application\Services\ProposalHistoryWriter;
9use ImovelZapAi\Proposals\Domain\Enums\ProposalHistoryEvent;
10use ImovelZapAi\Proposals\Infrastructure\Persistence\Eloquent\ProposalCommentModel;
11use ImovelZapAi\Proposals\Infrastructure\Persistence\Eloquent\ProposalModel;
12
13final class AddProposalCommentAction
14{
15    public function __construct(private readonly ProposalHistoryWriter $history) {}
16
17    public function execute(
18        ProposalModel $proposal,
19        string $body,
20        string $authorType = 'broker',
21        ?User $actor = null,
22        ?string $authorName = null,
23    ): ProposalCommentModel {
24        $comment = ProposalCommentModel::query()->create([
25            'tenant_id' => $proposal->tenant_id,
26            'proposal_id' => $proposal->id,
27            'proposal_version_id' => $proposal->current_version_id,
28            'author_type' => $authorType,
29            'author_user_id' => $actor?->id,
30            'author_name' => $authorName ?? $actor?->name,
31            'body' => $body,
32            'visible_to_client' => true,
33        ]);
34
35        $this->history->write(
36            $proposal->tenant_id,
37            $proposal->id,
38            ProposalHistoryEvent::CommentAdded,
39            actorType: $authorType,
40            actorUserId: $actor?->id,
41            message: 'Comentário adicionado à proposta.',
42        );
43
44        return $comment;
45    }
46}