Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
21 / 21
100.00% covered (success)
100.00%
2 / 2
CRAP
100.00% covered (success)
100.00%
1 / 1
SeedFinancialFromSignatureAction
100.00% covered (success)
100.00%
21 / 21
100.00% covered (success)
100.00%
2 / 2
5
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%
20 / 20
100.00% covered (success)
100.00%
1 / 1
4
1<?php
2
3declare(strict_types=1);
4
5namespace ImovelZapAi\Financial\Application\Actions;
6
7use ImovelZapAi\Financial\Domain\Exceptions\FinancialException;
8use ImovelZapAi\Financial\Infrastructure\Persistence\Eloquent\FinancialTransactionModel;
9use ImovelZapAi\Signatures\Infrastructure\Persistence\Eloquent\SignatureRequestModel;
10
11final class SeedFinancialFromSignatureAction
12{
13    public function __construct(
14        private readonly CreateDealFinancialBootstrapAction $bootstrap,
15    ) {}
16
17    /**
18     * @return array<string, mixed>|null
19     */
20    public function execute(string $tenantId, string $signatureRequestId): ?array
21    {
22        $signature = SignatureRequestModel::query()
23            ->where('tenant_id', $tenantId)
24            ->find($signatureRequestId);
25
26        if ($signature === null) {
27            throw FinancialException::notFound('Solicitação de assinatura');
28        }
29
30        if ($signature->deal_id === null) {
31            return null;
32        }
33
34        if (FinancialTransactionModel::query()
35            ->where('tenant_id', $tenantId)
36            ->where('deal_id', $signature->deal_id)
37            ->exists()) {
38            return null;
39        }
40
41        return $this->bootstrap->execute(
42            $tenantId,
43            (string) $signature->deal_id,
44            null,
45            true,
46            (string) $signature->id,
47            false,
48        );
49    }
50}