Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
100.00% |
23 / 23 |
|
100.00% |
1 / 1 |
CRAP | |
100.00% |
1 / 1 |
| CreateTransferAction | |
100.00% |
23 / 23 |
|
100.00% |
1 / 1 |
4 | |
100.00% |
1 / 1 |
| execute | |
100.00% |
23 / 23 |
|
100.00% |
1 / 1 |
4 | |||
| 1 | <?php |
| 2 | |
| 3 | declare(strict_types=1); |
| 4 | |
| 5 | namespace ImovelZapAi\Financial\Application\Actions; |
| 6 | |
| 7 | use Illuminate\Support\Facades\DB; |
| 8 | use ImovelZapAi\Financial\Domain\Enums\BeneficiaryType; |
| 9 | use ImovelZapAi\Financial\Domain\Enums\TransferStatus; |
| 10 | use ImovelZapAi\Financial\Domain\Exceptions\FinancialException; |
| 11 | use ImovelZapAi\Financial\Infrastructure\Persistence\Eloquent\FinancialCommissionSplitModel; |
| 12 | use ImovelZapAi\Financial\Infrastructure\Persistence\Eloquent\FinancialTransferModel; |
| 13 | |
| 14 | final class CreateTransferAction |
| 15 | { |
| 16 | /** |
| 17 | * @param array{ |
| 18 | * deal_id: string, |
| 19 | * amount: float|string, |
| 20 | * payee_type: string, |
| 21 | * payee_label: string, |
| 22 | * payee_user_id?: int|null, |
| 23 | * commission_split_id?: string|null, |
| 24 | * expected_at?: string|null, |
| 25 | * notes?: string|null, |
| 26 | * } $data |
| 27 | */ |
| 28 | public function execute(string $tenantId, array $data): FinancialTransferModel |
| 29 | { |
| 30 | return DB::transaction(function () use ($tenantId, $data): FinancialTransferModel { |
| 31 | $payeeType = BeneficiaryType::tryFrom((string) $data['payee_type']); |
| 32 | if ($payeeType === null) { |
| 33 | throw FinancialException::invalidState('tipo de favorecido inválido'); |
| 34 | } |
| 35 | |
| 36 | if (! empty($data['commission_split_id'])) { |
| 37 | $split = FinancialCommissionSplitModel::query() |
| 38 | ->where('tenant_id', $tenantId) |
| 39 | ->find($data['commission_split_id']); |
| 40 | |
| 41 | if ($split === null) { |
| 42 | throw FinancialException::notFound('Split de comissão'); |
| 43 | } |
| 44 | } |
| 45 | |
| 46 | return FinancialTransferModel::query()->create([ |
| 47 | 'tenant_id' => $tenantId, |
| 48 | 'deal_id' => $data['deal_id'], |
| 49 | 'commission_split_id' => $data['commission_split_id'] ?? null, |
| 50 | 'payee_type' => $payeeType, |
| 51 | 'payee_user_id' => $data['payee_user_id'] ?? null, |
| 52 | 'payee_label' => $data['payee_label'], |
| 53 | 'amount' => $data['amount'], |
| 54 | 'expected_at' => $data['expected_at'] ?? null, |
| 55 | 'status' => TransferStatus::Pending, |
| 56 | 'notes' => $data['notes'] ?? null, |
| 57 | ]); |
| 58 | }); |
| 59 | } |
| 60 | } |