feat: implement BankFlow loan workflow API endpoints
This commit is contained in:
1 parent
56a52be180
commit
7824e8f015
43 files changed
+1403
-10
No files matched your search
@@ -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(),
|
||||
];
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user