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);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,13 @@
|
||||
<?php
|
||||
|
||||
namespace App\Domain\Loan\Contracts;
|
||||
|
||||
use App\Models\Loan;
|
||||
|
||||
interface ConditionalStageInterface extends StageInterface
|
||||
{
|
||||
/**
|
||||
* @param array<string, mixed> $rules
|
||||
*/
|
||||
public function appliesTo(Loan $loan, array $rules): bool;
|
||||
}
|
||||
@@ -0,0 +1,14 @@
|
||||
<?php
|
||||
|
||||
namespace App\Domain\Loan\Contracts;
|
||||
|
||||
use App\Domain\Loan\Data\ExecutionResult;
|
||||
use App\Models\Loan;
|
||||
|
||||
interface StageInterface
|
||||
{
|
||||
/**
|
||||
* @param array<string, mixed> $rules
|
||||
*/
|
||||
public function execute(Loan $loan, array $rules): ExecutionResult;
|
||||
}
|
||||
@@ -0,0 +1,28 @@
|
||||
<?php
|
||||
|
||||
namespace App\Domain\Loan\Data;
|
||||
|
||||
use App\Domain\Loan\Enums\StageResultType;
|
||||
|
||||
final readonly class ExecutionResult
|
||||
{
|
||||
public function __construct(
|
||||
public StageResultType $type,
|
||||
public string $reason,
|
||||
) {}
|
||||
|
||||
public static function pass(): self
|
||||
{
|
||||
return new self(StageResultType::Pass, 'SUCCESS');
|
||||
}
|
||||
|
||||
public static function fail(string $reason): self
|
||||
{
|
||||
return new self(StageResultType::Fail, $reason);
|
||||
}
|
||||
|
||||
public static function manualReview(string $reason): self
|
||||
{
|
||||
return new self(StageResultType::ManualReview, $reason);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,16 @@
|
||||
<?php
|
||||
|
||||
namespace App\Domain\Loan\Exceptions;
|
||||
|
||||
use Illuminate\Contracts\Debug\ShouldntReport;
|
||||
use Illuminate\Http\JsonResponse;
|
||||
use Illuminate\Http\Request;
|
||||
use RuntimeException;
|
||||
|
||||
class InvalidRequestException extends RuntimeException implements ShouldntReport
|
||||
{
|
||||
public function render(Request $request): JsonResponse
|
||||
{
|
||||
return response()->json(['error' => 'INVALID_REQUEST'], 400);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,8 @@
|
||||
<?php
|
||||
|
||||
namespace App\Domain\Loan\Exceptions;
|
||||
|
||||
class InvalidWorkflowConfiguration extends WorkflowException
|
||||
{
|
||||
//
|
||||
}
|
||||
@@ -0,0 +1,16 @@
|
||||
<?php
|
||||
|
||||
namespace App\Domain\Loan\Exceptions;
|
||||
|
||||
use Illuminate\Contracts\Debug\ShouldntReport;
|
||||
use Illuminate\Http\JsonResponse;
|
||||
use Illuminate\Http\Request;
|
||||
use RuntimeException;
|
||||
|
||||
class LoanNotFoundException extends RuntimeException implements ShouldntReport
|
||||
{
|
||||
public function render(Request $request): JsonResponse
|
||||
{
|
||||
return response()->json(['error' => 'LOAN_NOT_FOUND'], 404);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,10 @@
|
||||
<?php
|
||||
|
||||
namespace App\Domain\Loan\Exceptions;
|
||||
|
||||
use RuntimeException;
|
||||
|
||||
class WorkflowException extends RuntimeException
|
||||
{
|
||||
//
|
||||
}
|
||||
@@ -0,0 +1,54 @@
|
||||
<?php
|
||||
|
||||
namespace App\Domain\Loan\Workflow\Stages;
|
||||
|
||||
use App\Domain\Loan\Contracts\StageInterface;
|
||||
use App\Domain\Loan\Data\ExecutionResult;
|
||||
use App\Domain\Loan\Exceptions\InvalidWorkflowConfiguration;
|
||||
use App\Models\Loan;
|
||||
|
||||
class CreditCheckStage implements StageInterface
|
||||
{
|
||||
/**
|
||||
* @param array<string, mixed> $rules
|
||||
*/
|
||||
public function execute(Loan $loan, array $rules): ExecutionResult
|
||||
{
|
||||
$rejectBelow = $this->integerRule($rules, 'rejectBelow');
|
||||
$manualReviewMin = $this->integerRule($rules, 'manualReviewMin');
|
||||
$manualReviewMax = $this->integerRule($rules, 'manualReviewMax');
|
||||
$approveFrom = $this->integerRule($rules, 'approveFrom');
|
||||
|
||||
if ($rejectBelow !== $manualReviewMin || $manualReviewMax + 1 !== $approveFrom) {
|
||||
throw new InvalidWorkflowConfiguration('Credit score ranges must be contiguous.');
|
||||
}
|
||||
|
||||
if ($loan->credit_score < $rejectBelow) {
|
||||
return ExecutionResult::fail('CREDIT_SCORE_TOO_LOW');
|
||||
}
|
||||
|
||||
if ($loan->credit_score >= $manualReviewMin && $loan->credit_score <= $manualReviewMax) {
|
||||
return ExecutionResult::manualReview('CREDIT_SCORE_REQUIRES_REVIEW');
|
||||
}
|
||||
|
||||
if ($loan->credit_score >= $approveFrom) {
|
||||
return ExecutionResult::pass();
|
||||
}
|
||||
|
||||
throw new InvalidWorkflowConfiguration('Credit score ranges contain a gap.');
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array<string, mixed> $rules
|
||||
*/
|
||||
private function integerRule(array $rules, string $key): int
|
||||
{
|
||||
$value = $rules[$key] ?? null;
|
||||
|
||||
if (! is_int($value)) {
|
||||
throw new InvalidWorkflowConfiguration("The {$key} rule must be an integer.");
|
||||
}
|
||||
|
||||
return $value;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,45 @@
|
||||
<?php
|
||||
|
||||
namespace App\Domain\Loan\Workflow\Stages;
|
||||
|
||||
use App\Domain\Loan\Contracts\StageInterface;
|
||||
use App\Domain\Loan\Data\ExecutionResult;
|
||||
use App\Domain\Loan\Exceptions\InvalidWorkflowConfiguration;
|
||||
use App\Models\Loan;
|
||||
use Illuminate\Support\Str;
|
||||
|
||||
class FraudCheckStage implements StageInterface
|
||||
{
|
||||
/**
|
||||
* @param array<string, mixed> $rules
|
||||
*/
|
||||
public function execute(Loan $loan, array $rules): ExecutionResult
|
||||
{
|
||||
$fraudPrefix = $this->stringRule($rules, 'fraudPrefix');
|
||||
$manualReviewPrefix = $this->stringRule($rules, 'manualReviewPrefix');
|
||||
|
||||
if (Str::startsWith($loan->customer_id, $fraudPrefix)) {
|
||||
return ExecutionResult::fail('FRAUD_DETECTED');
|
||||
}
|
||||
|
||||
if (Str::startsWith($loan->customer_id, $manualReviewPrefix)) {
|
||||
return ExecutionResult::manualReview('CUSTOMER_REQUIRES_REVIEW');
|
||||
}
|
||||
|
||||
return ExecutionResult::pass();
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array<string, mixed> $rules
|
||||
*/
|
||||
private function stringRule(array $rules, string $key): string
|
||||
{
|
||||
$value = $rules[$key] ?? null;
|
||||
|
||||
if (! is_string($value) || $value === '') {
|
||||
throw new InvalidWorkflowConfiguration("The {$key} rule must be a non-empty string.");
|
||||
}
|
||||
|
||||
return $value;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,22 @@
|
||||
<?php
|
||||
|
||||
namespace App\Domain\Loan\Workflow\Stages;
|
||||
|
||||
use App\Domain\Loan\Contracts\StageInterface;
|
||||
use App\Domain\Loan\Data\ExecutionResult;
|
||||
use App\Models\Loan;
|
||||
|
||||
class GuarantorCheckStage implements StageInterface
|
||||
{
|
||||
/**
|
||||
* @param array<string, mixed> $rules
|
||||
*/
|
||||
public function execute(Loan $loan, array $rules): ExecutionResult
|
||||
{
|
||||
if (! $loan->has_guarantor) {
|
||||
return ExecutionResult::fail('GUARANTOR_REQUIRED');
|
||||
}
|
||||
|
||||
return ExecutionResult::pass();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,47 @@
|
||||
<?php
|
||||
|
||||
namespace App\Domain\Loan\Workflow\Stages;
|
||||
|
||||
use App\Domain\Loan\Contracts\ConditionalStageInterface;
|
||||
use App\Domain\Loan\Data\ExecutionResult;
|
||||
use App\Domain\Loan\Exceptions\InvalidWorkflowConfiguration;
|
||||
use App\Models\Loan;
|
||||
|
||||
class ManagerApprovalStage implements ConditionalStageInterface
|
||||
{
|
||||
/**
|
||||
* @param array<string, mixed> $rules
|
||||
*/
|
||||
public function appliesTo(Loan $loan, array $rules): bool
|
||||
{
|
||||
return $loan->amount > $this->positiveIntegerRule($rules, 'activationThreshold');
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array<string, mixed> $rules
|
||||
*/
|
||||
public function execute(Loan $loan, array $rules): ExecutionResult
|
||||
{
|
||||
$incomeMultiplier = $this->positiveIntegerRule($rules, 'incomeMultiplier');
|
||||
|
||||
if ($loan->amount > $loan->monthly_income * $incomeMultiplier) {
|
||||
return ExecutionResult::fail('MANAGER_APPROVAL_DENIED');
|
||||
}
|
||||
|
||||
return ExecutionResult::pass();
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array<string, mixed> $rules
|
||||
*/
|
||||
private function positiveIntegerRule(array $rules, string $key): int
|
||||
{
|
||||
$value = $rules[$key] ?? null;
|
||||
|
||||
if (! is_int($value) || $value <= 0) {
|
||||
throw new InvalidWorkflowConfiguration("The {$key} rule must be a positive integer.");
|
||||
}
|
||||
|
||||
return $value;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,42 @@
|
||||
<?php
|
||||
|
||||
namespace App\Domain\Loan\Workflow\Stages;
|
||||
|
||||
use App\Domain\Loan\Contracts\StageInterface;
|
||||
use App\Domain\Loan\Data\ExecutionResult;
|
||||
use App\Models\Loan;
|
||||
|
||||
class ValidationStage implements StageInterface
|
||||
{
|
||||
/**
|
||||
* @param array<string, mixed> $rules
|
||||
*/
|
||||
public function execute(Loan $loan, array $rules): ExecutionResult
|
||||
{
|
||||
if (trim($loan->customer_id) === '') {
|
||||
return ExecutionResult::fail('INVALID_CUSTOMER_ID');
|
||||
}
|
||||
|
||||
if ($loan->amount <= 0) {
|
||||
return ExecutionResult::fail('INVALID_AMOUNT');
|
||||
}
|
||||
|
||||
if (preg_match('/^09\d{9}$/', $loan->phone) !== 1) {
|
||||
return ExecutionResult::fail('INVALID_PHONE');
|
||||
}
|
||||
|
||||
if (! in_array($loan->loanType->code, ['PERSONAL', 'BUSINESS'], true)) {
|
||||
return ExecutionResult::fail('INVALID_LOAN_TYPE');
|
||||
}
|
||||
|
||||
if ($loan->monthly_income < 0) {
|
||||
return ExecutionResult::fail('INVALID_MONTHLY_INCOME');
|
||||
}
|
||||
|
||||
if ($loan->credit_score < 0 || $loan->credit_score > 1000) {
|
||||
return ExecutionResult::fail('INVALID_CREDIT_SCORE');
|
||||
}
|
||||
|
||||
return ExecutionResult::pass();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,41 @@
|
||||
<?php
|
||||
|
||||
namespace App\Domain\Loan\Workflow;
|
||||
|
||||
use App\Domain\Loan\Contracts\StageInterface;
|
||||
use App\Domain\Loan\Enums\LoanStage;
|
||||
use App\Domain\Loan\Exceptions\InvalidWorkflowConfiguration;
|
||||
use App\Models\WorkflowConfigurationStep;
|
||||
use Illuminate\Contracts\Container\Container;
|
||||
|
||||
class WorkflowConfigurationResolver
|
||||
{
|
||||
public function __construct(private Container $container) {}
|
||||
|
||||
public function resolve(WorkflowConfigurationStep $step): StageInterface
|
||||
{
|
||||
$stageDefinition = $step->stageDefinition;
|
||||
|
||||
if (! $stageDefinition->is_active) {
|
||||
throw new InvalidWorkflowConfiguration("Stage {$stageDefinition->code} is inactive.");
|
||||
}
|
||||
|
||||
if (LoanStage::tryFrom($stageDefinition->code) === null) {
|
||||
throw new InvalidWorkflowConfiguration("Stage {$stageDefinition->code} is not a known Loan stage.");
|
||||
}
|
||||
|
||||
$handlerClass = config("workflow-stages.{$stageDefinition->code}");
|
||||
|
||||
if (! is_string($handlerClass) || ! is_a($handlerClass, StageInterface::class, true)) {
|
||||
throw new InvalidWorkflowConfiguration("Stage {$stageDefinition->code} has no trusted handler.");
|
||||
}
|
||||
|
||||
$handler = $this->container->make($handlerClass);
|
||||
|
||||
if (! $handler instanceof StageInterface) {
|
||||
throw new InvalidWorkflowConfiguration("Stage {$stageDefinition->code} handler is invalid.");
|
||||
}
|
||||
|
||||
return $handler;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,90 @@
|
||||
<?php
|
||||
|
||||
namespace App\Domain\Loan\Workflow;
|
||||
|
||||
use App\Domain\Loan\Contracts\ConditionalStageInterface;
|
||||
use App\Domain\Loan\Enums\LoanStage;
|
||||
use App\Domain\Loan\Enums\LoanStatus;
|
||||
use App\Domain\Loan\Enums\StageResultType;
|
||||
use App\Domain\Loan\Exceptions\InvalidWorkflowConfiguration;
|
||||
use App\Models\Loan;
|
||||
use App\Models\WorkflowConfigurationStep;
|
||||
|
||||
class WorkflowEngine
|
||||
{
|
||||
public function __construct(private WorkflowConfigurationResolver $resolver) {}
|
||||
|
||||
public function process(Loan $loan): Loan
|
||||
{
|
||||
if ($loan->status->isTerminal()) {
|
||||
return $loan;
|
||||
}
|
||||
|
||||
if (! $loan->relationLoaded('loanType') || ! $loan->relationLoaded('workflowConfiguration')) {
|
||||
throw new InvalidWorkflowConfiguration('The Loan workflow relationships must be loaded before processing.');
|
||||
}
|
||||
|
||||
$loan->forceFill(['status' => LoanStatus::InProgress])->save();
|
||||
|
||||
$executedStepIds = $loan->histories->pluck('workflow_configuration_step_id')->all();
|
||||
|
||||
foreach ($loan->workflowConfiguration->steps as $step) {
|
||||
if (! $step->is_enabled || in_array($step->getKey(), $executedStepIds, true)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$this->executeStep($loan, $step);
|
||||
|
||||
if ($loan->status->isTerminal()) {
|
||||
return $loan;
|
||||
}
|
||||
}
|
||||
|
||||
$loan->forceFill([
|
||||
'status' => LoanStatus::Approved,
|
||||
'current_workflow_configuration_step_id' => null,
|
||||
])->save();
|
||||
|
||||
return $loan;
|
||||
}
|
||||
|
||||
private function executeStep(Loan $loan, WorkflowConfigurationStep $step): void
|
||||
{
|
||||
$handler = $this->resolver->resolve($step);
|
||||
$rules = $step->rules;
|
||||
|
||||
if ($handler instanceof ConditionalStageInterface && ! $handler->appliesTo($loan, $rules)) {
|
||||
return;
|
||||
}
|
||||
|
||||
$loan->forceFill([
|
||||
'current_workflow_configuration_step_id' => $step->getKey(),
|
||||
])->save();
|
||||
|
||||
$result = $handler->execute($loan, $rules);
|
||||
$stage = LoanStage::from($step->stageDefinition->code);
|
||||
|
||||
$loan->histories()->create([
|
||||
'workflow_configuration_step_id' => $step->getKey(),
|
||||
'stage_code' => $stage,
|
||||
'rules_snapshot' => $rules,
|
||||
'result' => $result->type,
|
||||
'reason' => $result->reason,
|
||||
'executed_at' => now('UTC'),
|
||||
]);
|
||||
|
||||
if ($result->type === StageResultType::Fail) {
|
||||
$loan->forceFill([
|
||||
'status' => LoanStatus::Rejected,
|
||||
'current_workflow_configuration_step_id' => null,
|
||||
])->save();
|
||||
}
|
||||
|
||||
if ($result->type === StageResultType::ManualReview) {
|
||||
$loan->forceFill([
|
||||
'status' => LoanStatus::ManualReview,
|
||||
'current_workflow_configuration_step_id' => null,
|
||||
])->save();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,40 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Controllers\Api\V1;
|
||||
|
||||
use App\Actions\Loan\CreateLoanAction;
|
||||
use App\Actions\Loan\GetLoanAction;
|
||||
use App\Actions\Loan\GetLoanHistoryAction;
|
||||
use App\Actions\Loan\ProcessLoanAction;
|
||||
use App\Http\Controllers\Controller;
|
||||
use App\Http\Requests\StoreLoanRequest;
|
||||
use App\Http\Resources\LoanHistoryResource;
|
||||
use App\Http\Resources\LoanProcessResource;
|
||||
use App\Http\Resources\LoanResource;
|
||||
use Illuminate\Http\JsonResponse;
|
||||
use Illuminate\Http\Resources\Json\AnonymousResourceCollection;
|
||||
|
||||
class LoanController extends Controller
|
||||
{
|
||||
public function store(StoreLoanRequest $request, CreateLoanAction $createLoan): JsonResponse
|
||||
{
|
||||
$loan = $createLoan->handle($request->validated());
|
||||
|
||||
return (new LoanProcessResource($loan))->response()->setStatusCode(201);
|
||||
}
|
||||
|
||||
public function process(string $loanId, ProcessLoanAction $processLoan): JsonResponse
|
||||
{
|
||||
return (new LoanProcessResource($processLoan->handle($loanId)))->response();
|
||||
}
|
||||
|
||||
public function show(string $loanId, GetLoanAction $getLoan): JsonResponse
|
||||
{
|
||||
return (new LoanResource($getLoan->handle($loanId)))->response();
|
||||
}
|
||||
|
||||
public function history(string $loanId, GetLoanHistoryAction $getLoanHistory): AnonymousResourceCollection
|
||||
{
|
||||
return LoanHistoryResource::collection($getLoanHistory->handle($loanId));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,13 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Controllers;
|
||||
|
||||
use Illuminate\Http\JsonResponse;
|
||||
|
||||
class HealthCheckController extends Controller
|
||||
{
|
||||
public function __invoke(): JsonResponse
|
||||
{
|
||||
return response()->json(['status' => 'UP']);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,59 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Requests;
|
||||
|
||||
use App\Domain\Loan\Exceptions\InvalidRequestException;
|
||||
use Illuminate\Contracts\Validation\Validator;
|
||||
use Illuminate\Foundation\Http\FormRequest;
|
||||
use JsonException;
|
||||
|
||||
class StoreLoanRequest extends FormRequest
|
||||
{
|
||||
public function authorize(): bool
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array<string, array<int, string>>
|
||||
*/
|
||||
public function rules(): array
|
||||
{
|
||||
return [
|
||||
'customerId' => ['present', 'string', 'max:255'],
|
||||
'amount' => ['present', 'integer'],
|
||||
'phone' => ['present', 'string', 'max:255'],
|
||||
'loanType' => ['present', 'string', 'max:255'],
|
||||
'monthlyIncome' => ['present', 'integer'],
|
||||
'creditScore' => ['present', 'integer'],
|
||||
'hasGuarantor' => ['present', 'boolean'],
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array<string, mixed>
|
||||
*/
|
||||
public function validationData(): array
|
||||
{
|
||||
if (! $this->isJson()) {
|
||||
throw new InvalidRequestException('The request body must use JSON.');
|
||||
}
|
||||
|
||||
try {
|
||||
$data = json_decode($this->getContent(), true, 512, JSON_THROW_ON_ERROR);
|
||||
} catch (JsonException) {
|
||||
throw new InvalidRequestException('The request body is not valid JSON.');
|
||||
}
|
||||
|
||||
if (! is_array($data)) {
|
||||
throw new InvalidRequestException('The request body must be a JSON object.');
|
||||
}
|
||||
|
||||
return $data;
|
||||
}
|
||||
|
||||
protected function failedValidation(Validator $validator): never
|
||||
{
|
||||
throw new InvalidRequestException('The request structure is invalid.');
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,22 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Resources;
|
||||
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Http\Resources\Json\JsonResource;
|
||||
|
||||
class LoanHistoryResource extends JsonResource
|
||||
{
|
||||
/**
|
||||
* @return array<string, mixed>
|
||||
*/
|
||||
public function toArray(Request $request): array
|
||||
{
|
||||
return [
|
||||
'stage' => $this->stage_code->value,
|
||||
'result' => $this->result->value,
|
||||
'timestamp' => $this->executed_at->toIso8601ZuluString(),
|
||||
'reason' => $this->reason,
|
||||
];
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,21 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Resources;
|
||||
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Http\Resources\Json\JsonResource;
|
||||
|
||||
class LoanProcessResource extends JsonResource
|
||||
{
|
||||
/**
|
||||
* @return array<string, mixed>
|
||||
*/
|
||||
public function toArray(Request $request): array
|
||||
{
|
||||
return [
|
||||
'loanId' => $this->public_id,
|
||||
'status' => $this->status->value,
|
||||
'currentStage' => $this->currentStep?->stageDefinition->code,
|
||||
];
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,30 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Resources;
|
||||
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Http\Resources\Json\JsonResource;
|
||||
|
||||
class LoanResource extends JsonResource
|
||||
{
|
||||
/**
|
||||
* @return array<string, mixed>
|
||||
*/
|
||||
public function toArray(Request $request): array
|
||||
{
|
||||
return [
|
||||
'loanId' => $this->public_id,
|
||||
'customerId' => $this->customer_id,
|
||||
'amount' => $this->amount,
|
||||
'phone' => $this->phone,
|
||||
'loanType' => $this->loanType->code,
|
||||
'monthlyIncome' => $this->monthly_income,
|
||||
'creditScore' => $this->credit_score,
|
||||
'hasGuarantor' => $this->has_guarantor,
|
||||
'status' => $this->status->value,
|
||||
'currentStage' => $this->currentStep?->stageDefinition->code,
|
||||
'createdAt' => $this->created_at->toIso8601ZuluString(),
|
||||
'updatedAt' => $this->updated_at->toIso8601ZuluString(),
|
||||
];
|
||||
}
|
||||
}
|
||||
@@ -2,6 +2,8 @@
|
||||
|
||||
namespace App\Providers;
|
||||
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use Illuminate\Http\Resources\Json\JsonResource;
|
||||
use Illuminate\Support\ServiceProvider;
|
||||
|
||||
class AppServiceProvider extends ServiceProvider
|
||||
@@ -19,6 +21,7 @@ class AppServiceProvider extends ServiceProvider
|
||||
*/
|
||||
public function boot(): void
|
||||
{
|
||||
//
|
||||
Model::preventLazyLoading(! app()->isProduction());
|
||||
JsonResource::withoutWrapping();
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user