Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
18 / 18
100.00% covered (success)
100.00%
1 / 1
CRAP
100.00% covered (success)
100.00%
1 / 1
CompleteTransferAction
100.00% covered (success)
100.00%
18 / 18
100.00% covered (success)
100.00%
1 / 1
4
100.00% covered (success)
100.00%
1 / 1
 execute
100.00% covered (success)
100.00%
18 / 18
100.00% covered (success)
100.00%
1 / 1
4
1<?php
2
3declare(strict_types=1);
4
5namespace ImovelZapAi\Financial\Application\Actions;
6
7use Illuminate\Support\Facades\DB;
8use ImovelZapAi\Financial\Domain\Enums\TransferStatus;
9use ImovelZapAi\Financial\Domain\Events\TransferCompleted;
10use ImovelZapAi\Financial\Domain\Exceptions\FinancialException;
11use ImovelZapAi\Financial\Infrastructure\Persistence\Eloquent\FinancialTransferModel;
12
13final class CompleteTransferAction
14{
15    /**
16     * @param  array{proof_path?: string|null, paid_at?: string|null}  $data
17     */
18    public function execute(string $tenantId, string $transferId, array $data = []): FinancialTransferModel
19    {
20        return DB::transaction(function () use ($tenantId, $transferId, $data): FinancialTransferModel {
21            $transfer = FinancialTransferModel::query()
22                ->where('tenant_id', $tenantId)
23                ->find($transferId);
24
25            if ($transfer === null) {
26                throw FinancialException::notFound('Repasse');
27            }
28
29            if ($transfer->status === TransferStatus::Cancelled) {
30                throw FinancialException::invalidState('repasse cancelado');
31            }
32
33            if ($transfer->status === TransferStatus::Completed) {
34                return $transfer;
35            }
36
37            $transfer->update([
38                'status' => TransferStatus::Completed,
39                'paid_at' => $data['paid_at'] ?? now(),
40                'proof_path' => $data['proof_path'] ?? $transfer->proof_path,
41            ]);
42
43            TransferCompleted::dispatch($tenantId, (string) $transfer->id);
44
45            return $transfer->fresh();
46        });
47    }
48}