feat: implement BankFlow loan workflow API endpoints

This commit is contained in:
m.ebrahimi committed 2026-07-22 17:31:18 +03:30
1 parent 56a52be180
commit 7824e8f015
43 files changed
+1403 -10

No files matched your search

@@ -0,0 +1,38 @@
<?php
use App\Domain\Loan\Enums\StageResultType;
use App\Domain\Loan\Workflow\Stages\CreditCheckStage;
use App\Models\Loan;
it('handles credit score boundaries', function (int $creditScore, StageResultType $expected) {
$result = (new CreditCheckStage)->execute(
new Loan(['credit_score' => $creditScore]),
[
'rejectBelow' => 500,
'manualReviewMin' => 500,
'manualReviewMax' => 649,
'approveFrom' => 650,
],
);
expect($result->type)->toBe($expected);
})->with([
'below manual range' => [499, StageResultType::Fail],
'manual lower boundary' => [500, StageResultType::ManualReview],
'manual upper boundary' => [649, StageResultType::ManualReview],
'approval boundary' => [650, StageResultType::Pass],
]);
it('uses overridden credit rules', function () {
$result = (new CreditCheckStage)->execute(
new Loan(['credit_score' => 699]),
[
'rejectBelow' => 650,
'manualReviewMin' => 650,
'manualReviewMax' => 699,
'approveFrom' => 700,
],
);
expect($result->type)->toBe(StageResultType::ManualReview);
});
@@ -0,0 +1,18 @@
<?php
use App\Domain\Loan\Enums\StageResultType;
use App\Domain\Loan\Workflow\Stages\FraudCheckStage;
use App\Models\Loan;
it('maps customer prefixes to fraud outcomes', function (string $customerId, StageResultType $expected) {
$result = (new FraudCheckStage)->execute(
new Loan(['customer_id' => $customerId]),
['fraudPrefix' => 'FRAUD', 'manualReviewPrefix' => 'REVIEW'],
);
expect($result->type)->toBe($expected);
})->with([
'fraud' => ['FRAUD-1001', StageResultType::Fail],
'manual review' => ['REVIEW-1001', StageResultType::ManualReview],
'pass' => ['C-1001', StageResultType::Pass],
]);
@@ -0,0 +1,17 @@
<?php
use App\Domain\Loan\Enums\StageResultType;
use App\Domain\Loan\Workflow\Stages\GuarantorCheckStage;
use App\Models\Loan;
it('requires a guarantor', function (bool $hasGuarantor, StageResultType $expected) {
$result = (new GuarantorCheckStage)->execute(
new Loan(['has_guarantor' => $hasGuarantor]),
[],
);
expect($result->type)->toBe($expected);
})->with([
'missing' => [false, StageResultType::Fail],
'present' => [true, StageResultType::Pass],
]);
@@ -0,0 +1,34 @@
<?php
use App\Domain\Loan\Enums\StageResultType;
use App\Domain\Loan\Workflow\Stages\ManagerApprovalStage;
use App\Models\Loan;
it('only applies above the activation threshold', function (int $amount, bool $expected) {
$stage = new ManagerApprovalStage;
expect($stage->appliesTo(
new Loan(['amount' => $amount]),
['activationThreshold' => 500_000_000],
))->toBe($expected);
})->with([
'below' => [499_999_999, false],
'at threshold' => [500_000_000, false],
'above' => [500_000_001, true],
]);
it('compares the requested amount with configured income capacity', function (
int $amount,
int $monthlyIncome,
StageResultType $expected,
) {
$result = (new ManagerApprovalStage)->execute(
new Loan(['amount' => $amount, 'monthly_income' => $monthlyIncome]),
['incomeMultiplier' => 20],
);
expect($result->type)->toBe($expected);
})->with([
'denied' => [600_000_000, 20_000_000, StageResultType::Fail],
'approved' => [600_000_000, 50_000_000, StageResultType::Pass],
]);
@@ -0,0 +1,46 @@
<?php
use App\Domain\Loan\Enums\StageResultType;
use App\Domain\Loan\Workflow\Stages\ValidationStage;
use App\Models\Loan;
use App\Models\LoanType;
it('passes a valid application', function () {
$loan = (new Loan([
'customer_id' => 'C-1001',
'amount' => 400_000_000,
'phone' => '09121234567',
'monthly_income' => 50_000_000,
'credit_score' => 720,
'has_guarantor' => false,
]))->setRelation('loanType', new LoanType(['code' => 'PERSONAL']));
expect((new ValidationStage)->execute($loan, [])->type)->toBe(StageResultType::Pass);
});
it('returns the first validation failure', function (array $attributes, string $reason) {
$loanType = new LoanType(['code' => $attributes['loan_type_code'] ?? 'PERSONAL']);
unset($attributes['loan_type_code']);
$loan = (new Loan(array_replace([
'customer_id' => 'C-1001',
'amount' => 400_000_000,
'phone' => '09121234567',
'monthly_income' => 50_000_000,
'credit_score' => 720,
'has_guarantor' => false,
], $attributes)))->setRelation('loanType', $loanType);
$result = (new ValidationStage)->execute($loan, []);
expect($result->type)->toBe(StageResultType::Fail)
->and($result->reason)->toBe($reason);
})->with([
'customer ID' => [['customer_id' => ''], 'INVALID_CUSTOMER_ID'],
'amount' => [['amount' => 0], 'INVALID_AMOUNT'],
'phone' => [['phone' => '08121234567'], 'INVALID_PHONE'],
'loan type' => [['loan_type_code' => 'OTHER'], 'INVALID_LOAN_TYPE'],
'monthly income' => [['monthly_income' => -1], 'INVALID_MONTHLY_INCOME'],
'credit score below range' => [['credit_score' => -1], 'INVALID_CREDIT_SCORE'],
'credit score above range' => [['credit_score' => 1001], 'INVALID_CREDIT_SCORE'],
]);