Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
96.15% |
25 / 26 |
|
0.00% |
0 / 1 |
CRAP | |
0.00% |
0 / 1 |
| CreateFinancialTransactionAction | |
96.15% |
25 / 26 |
|
0.00% |
0 / 1 |
2 | |
0.00% |
0 / 1 |
| execute | |
96.15% |
25 / 26 |
|
0.00% |
0 / 1 |
2 | |||
| 1 | <?php |
| 2 | |
| 3 | declare(strict_types=1); |
| 4 | |
| 5 | namespace ImovelZapAi\Financial\Application\Actions; |
| 6 | |
| 7 | use App\Models\User; |
| 8 | use Illuminate\Support\Facades\DB; |
| 9 | use ImovelZapAi\Financial\Domain\Enums\TransactionStatus; |
| 10 | use ImovelZapAi\Financial\Domain\Enums\TransactionType; |
| 11 | use ImovelZapAi\Financial\Domain\Events\TransactionCreated; |
| 12 | use ImovelZapAi\Financial\Domain\Exceptions\FinancialException; |
| 13 | use ImovelZapAi\Financial\Infrastructure\Persistence\Eloquent\FinancialTransactionModel; |
| 14 | use ImovelZapAi\Sales\Infrastructure\Persistence\Eloquent\DealModel; |
| 15 | |
| 16 | final class CreateFinancialTransactionAction |
| 17 | { |
| 18 | /** |
| 19 | * @param array{ |
| 20 | * deal_id: string, |
| 21 | * type: string, |
| 22 | * amount: float|string, |
| 23 | * status?: string|null, |
| 24 | * currency?: string|null, |
| 25 | * due_at?: string|null, |
| 26 | * description?: string|null, |
| 27 | * signature_request_id?: string|null, |
| 28 | * business_document_id?: string|null, |
| 29 | * meta?: array|null, |
| 30 | * } $data |
| 31 | */ |
| 32 | public function execute(string $tenantId, ?User $actor, array $data): FinancialTransactionModel |
| 33 | { |
| 34 | return DB::transaction(function () use ($tenantId, $actor, $data): FinancialTransactionModel { |
| 35 | $deal = DealModel::query() |
| 36 | ->where('tenant_id', $tenantId) |
| 37 | ->find($data['deal_id']); |
| 38 | |
| 39 | if ($deal === null) { |
| 40 | throw FinancialException::notFound('Oportunidade'); |
| 41 | } |
| 42 | |
| 43 | $type = TransactionType::tryFrom((string) $data['type']) ?? TransactionType::Other; |
| 44 | $status = TransactionStatus::tryFrom((string) ($data['status'] ?? TransactionStatus::Pending->value)) |
| 45 | ?? TransactionStatus::Pending; |
| 46 | |
| 47 | $transaction = FinancialTransactionModel::query()->create([ |
| 48 | 'tenant_id' => $tenantId, |
| 49 | 'deal_id' => $deal->id, |
| 50 | 'signature_request_id' => $data['signature_request_id'] ?? null, |
| 51 | 'business_document_id' => $data['business_document_id'] ?? null, |
| 52 | 'type' => $type, |
| 53 | 'status' => $status, |
| 54 | 'amount' => $data['amount'], |
| 55 | 'currency' => $data['currency'] ?? 'BRL', |
| 56 | 'due_at' => $data['due_at'] ?? null, |
| 57 | 'description' => $data['description'] ?? $type->label(), |
| 58 | 'meta' => $data['meta'] ?? null, |
| 59 | 'created_by' => $actor?->id, |
| 60 | ]); |
| 61 | |
| 62 | TransactionCreated::dispatch($tenantId, (string) $transaction->id); |
| 63 | |
| 64 | return $transaction; |
| 65 | }); |
| 66 | } |
| 67 | } |