Merge remote-tracking branch 'origin/main' into feature/admin-panel
This commit is contained in:
43 files changed
+1403
-10
No files matched your search
@@ -0,0 +1,82 @@
|
||||
<?php
|
||||
|
||||
namespace App\Actions\Loan;
|
||||
|
||||
use App\Domain\Loan\Enums\LoanStatus;
|
||||
use App\Domain\Loan\Enums\WorkflowConfigurationStatus;
|
||||
use App\Domain\Loan\Exceptions\InvalidRequestException;
|
||||
use App\Domain\Loan\Exceptions\InvalidWorkflowConfiguration;
|
||||
use App\Models\Loan;
|
||||
use App\Models\LoanType;
|
||||
use App\Models\WorkflowConfiguration;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
use Illuminate\Support\Str;
|
||||
|
||||
class CreateLoanAction
|
||||
{
|
||||
/**
|
||||
* @param array{
|
||||
* customerId: string,
|
||||
* amount: int,
|
||||
* phone: string,
|
||||
* loanType: string,
|
||||
* monthlyIncome: int,
|
||||
* creditScore: int,
|
||||
* hasGuarantor: bool
|
||||
* } $data
|
||||
*/
|
||||
public function handle(array $data): Loan
|
||||
{
|
||||
return DB::transaction(function () use ($data): Loan {
|
||||
$loanType = LoanType::query()
|
||||
->where('code', $data['loanType'])
|
||||
->where('is_active', true)
|
||||
->first();
|
||||
|
||||
if ($loanType === null) {
|
||||
throw new InvalidRequestException('The requested Loan type is unavailable.');
|
||||
}
|
||||
|
||||
$workflowConfiguration = $this->publishedWorkflowFor($loanType);
|
||||
$firstStep = $workflowConfiguration->steps->firstWhere('is_enabled', true);
|
||||
|
||||
if ($firstStep === null) {
|
||||
throw new InvalidWorkflowConfiguration('The published Workflow has no enabled steps.');
|
||||
}
|
||||
|
||||
$loan = Loan::query()->create([
|
||||
'public_id' => 'L-'.Str::ulid(),
|
||||
'customer_id' => $data['customerId'],
|
||||
'loan_type_id' => $loanType->getKey(),
|
||||
'workflow_configuration_id' => $workflowConfiguration->getKey(),
|
||||
'current_workflow_configuration_step_id' => $firstStep->getKey(),
|
||||
'amount' => $data['amount'],
|
||||
'phone' => $data['phone'],
|
||||
'monthly_income' => $data['monthlyIncome'],
|
||||
'credit_score' => $data['creditScore'],
|
||||
'has_guarantor' => $data['hasGuarantor'],
|
||||
'status' => LoanStatus::Submitted,
|
||||
]);
|
||||
|
||||
return $loan->setRelations([
|
||||
'loanType' => $loanType,
|
||||
'workflowConfiguration' => $workflowConfiguration,
|
||||
'currentStep' => $firstStep,
|
||||
]);
|
||||
});
|
||||
}
|
||||
|
||||
private function publishedWorkflowFor(LoanType $loanType): WorkflowConfiguration
|
||||
{
|
||||
$workflowConfiguration = $loanType->workflowConfigurations()
|
||||
->where('status', WorkflowConfigurationStatus::Published)
|
||||
->with('steps.stageDefinition')
|
||||
->first();
|
||||
|
||||
if ($workflowConfiguration === null) {
|
||||
throw new InvalidWorkflowConfiguration("Loan type {$loanType->code} has no published Workflow.");
|
||||
}
|
||||
|
||||
return $workflowConfiguration;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,23 @@
|
||||
<?php
|
||||
|
||||
namespace App\Actions\Loan;
|
||||
|
||||
use App\Domain\Loan\Exceptions\LoanNotFoundException;
|
||||
use App\Models\Loan;
|
||||
|
||||
class GetLoanAction
|
||||
{
|
||||
public function handle(string $loanId): Loan
|
||||
{
|
||||
$loan = Loan::query()
|
||||
->with(['loanType', 'currentStep.stageDefinition'])
|
||||
->where('public_id', $loanId)
|
||||
->first();
|
||||
|
||||
if ($loan === null) {
|
||||
throw new LoanNotFoundException("Loan {$loanId} was not found.");
|
||||
}
|
||||
|
||||
return $loan;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,31 @@
|
||||
<?php
|
||||
|
||||
namespace App\Actions\Loan;
|
||||
|
||||
use App\Domain\Loan\Exceptions\LoanNotFoundException;
|
||||
use App\Models\Loan;
|
||||
use App\Models\LoanHistory;
|
||||
use Illuminate\Database\Eloquent\Collection;
|
||||
|
||||
class GetLoanHistoryAction
|
||||
{
|
||||
/**
|
||||
* @return Collection<int, LoanHistory>
|
||||
*/
|
||||
public function handle(string $loanId): Collection
|
||||
{
|
||||
$loan = Loan::query()
|
||||
->select(['id', 'public_id'])
|
||||
->where('public_id', $loanId)
|
||||
->first();
|
||||
|
||||
if ($loan === null) {
|
||||
throw new LoanNotFoundException("Loan {$loanId} was not found.");
|
||||
}
|
||||
|
||||
return $loan->histories()
|
||||
->orderBy('executed_at')
|
||||
->orderBy('id')
|
||||
->get();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,38 @@
|
||||
<?php
|
||||
|
||||
namespace App\Actions\Loan;
|
||||
|
||||
use App\Domain\Loan\Exceptions\LoanNotFoundException;
|
||||
use App\Domain\Loan\Workflow\WorkflowEngine;
|
||||
use App\Models\Loan;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
|
||||
class ProcessLoanAction
|
||||
{
|
||||
public function __construct(private WorkflowEngine $workflowEngine) {}
|
||||
|
||||
public function handle(string $loanId): Loan
|
||||
{
|
||||
return DB::transaction(function () use ($loanId): Loan {
|
||||
$loan = Loan::query()
|
||||
->where('public_id', $loanId)
|
||||
->lockForUpdate()
|
||||
->first();
|
||||
|
||||
if ($loan === null) {
|
||||
throw new LoanNotFoundException("Loan {$loanId} was not found.");
|
||||
}
|
||||
|
||||
$loan->load([
|
||||
'loanType',
|
||||
'workflowConfiguration.steps.stageDefinition',
|
||||
'histories',
|
||||
'currentStep.stageDefinition',
|
||||
]);
|
||||
|
||||
$processedLoan = $this->workflowEngine->process($loan);
|
||||
|
||||
return $processedLoan->load(['loanType', 'currentStep.stageDefinition']);
|
||||
}, 3);
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user