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
@@ -3,5 +3,5 @@
|
||||
test('the application returns a successful response', function () {
|
||||
$response = $this->get('/');
|
||||
|
||||
$response->assertStatus(200);
|
||||
$response->assertSuccessful();
|
||||
});
|
||||
@@ -0,0 +1,62 @@
|
||||
<?php
|
||||
|
||||
use App\Domain\Loan\Enums\LoanStatus;
|
||||
use App\Models\Loan;
|
||||
use Database\Seeders\BankFlowSeeder;
|
||||
|
||||
beforeEach(function () {
|
||||
$this->seed(BankFlowSeeder::class);
|
||||
});
|
||||
|
||||
it('creates a submitted loan assigned to the published workflow', function () {
|
||||
$response = $this->postJson('/api/v1/loans', validLoanPayload());
|
||||
|
||||
$response->assertCreated()->assertExactJson([
|
||||
'loanId' => $response->json('loanId'),
|
||||
'status' => 'SUBMITTED',
|
||||
'currentStage' => 'VALIDATION',
|
||||
]);
|
||||
|
||||
$loan = Loan::query()->where('public_id', $response->json('loanId'))->firstOrFail();
|
||||
|
||||
expect($loan->status)->toBe(LoanStatus::Submitted)
|
||||
->and($loan->workflow_configuration_id)->not->toBeNull()
|
||||
->and($loan->current_workflow_configuration_step_id)->not->toBeNull();
|
||||
});
|
||||
|
||||
it('defers semantic validation until processing', function (array $overrides, string $reason) {
|
||||
$createResponse = $this->postJson('/api/v1/loans', validLoanPayload($overrides));
|
||||
|
||||
$createResponse->assertCreated();
|
||||
|
||||
$this->postJson("/api/v1/loans/{$createResponse->json('loanId')}/process")
|
||||
->assertSuccessful()
|
||||
->assertJsonPath('status', 'REJECTED');
|
||||
|
||||
$loan = Loan::query()->where('public_id', $createResponse->json('loanId'))->firstOrFail();
|
||||
|
||||
expect($loan->histories()->value('reason'))->toBe($reason);
|
||||
})->with([
|
||||
'customer ID' => [['customerId' => ''], 'INVALID_CUSTOMER_ID'],
|
||||
'amount' => [['amount' => 0], 'INVALID_AMOUNT'],
|
||||
'phone' => [['phone' => '08121234567'], 'INVALID_PHONE'],
|
||||
'monthly income' => [['monthlyIncome' => -1], 'INVALID_MONTHLY_INCOME'],
|
||||
'credit score' => [['creditScore' => 1001], 'INVALID_CREDIT_SCORE'],
|
||||
]);
|
||||
|
||||
it('returns invalid request for malformed JSON', function () {
|
||||
$response = $this->call(
|
||||
'POST',
|
||||
'/api/v1/loans',
|
||||
server: ['CONTENT_TYPE' => 'application/json', 'HTTP_ACCEPT' => 'application/json'],
|
||||
content: '{"customerId":',
|
||||
);
|
||||
|
||||
$response->assertBadRequest()->assertExactJson(['error' => 'INVALID_REQUEST']);
|
||||
});
|
||||
|
||||
it('returns invalid request for an unavailable loan type', function () {
|
||||
$this->postJson('/api/v1/loans', validLoanPayload(['loanType' => 'UNKNOWN']))
|
||||
->assertBadRequest()
|
||||
->assertExactJson(['error' => 'INVALID_REQUEST']);
|
||||
});
|
||||
@@ -0,0 +1,28 @@
|
||||
<?php
|
||||
|
||||
use Database\Seeders\BankFlowSeeder;
|
||||
|
||||
beforeEach(function () {
|
||||
$this->seed(BankFlowSeeder::class);
|
||||
});
|
||||
|
||||
it('returns unwrapped history in deterministic chronological order', function () {
|
||||
$loanId = $this->postJson('/api/v1/loans', validLoanPayload())->json('loanId');
|
||||
$this->postJson("/api/v1/loans/{$loanId}/process")->assertSuccessful();
|
||||
|
||||
$this->getJson("/api/v1/loans/{$loanId}/history")
|
||||
->assertSuccessful()
|
||||
->assertJsonCount(3)
|
||||
->assertJsonPath('0.stage', 'VALIDATION')
|
||||
->assertJsonPath('1.stage', 'FRAUD_CHECK')
|
||||
->assertJsonPath('2.stage', 'CREDIT_CHECK')
|
||||
->assertJsonStructure([
|
||||
'*' => ['stage', 'result', 'timestamp', 'reason'],
|
||||
]);
|
||||
});
|
||||
|
||||
it('returns the documented error for an unknown loan', function () {
|
||||
$this->getJson('/api/v1/loans/L-UNKNOWN/history')
|
||||
->assertNotFound()
|
||||
->assertExactJson(['error' => 'LOAN_NOT_FOUND']);
|
||||
});
|
||||
@@ -0,0 +1,33 @@
|
||||
<?php
|
||||
|
||||
use Database\Seeders\BankFlowSeeder;
|
||||
|
||||
beforeEach(function () {
|
||||
$this->seed(BankFlowSeeder::class);
|
||||
});
|
||||
|
||||
it('returns the complete loan representation', function () {
|
||||
$loanId = $this->postJson('/api/v1/loans', validLoanPayload())->json('loanId');
|
||||
|
||||
$this->getJson("/api/v1/loans/{$loanId}")
|
||||
->assertSuccessful()
|
||||
->assertJson([
|
||||
'loanId' => $loanId,
|
||||
'customerId' => 'C-1001',
|
||||
'amount' => 400_000_000,
|
||||
'phone' => '09121234567',
|
||||
'loanType' => 'PERSONAL',
|
||||
'monthlyIncome' => 50_000_000,
|
||||
'creditScore' => 720,
|
||||
'hasGuarantor' => false,
|
||||
'status' => 'SUBMITTED',
|
||||
'currentStage' => 'VALIDATION',
|
||||
])
|
||||
->assertJsonStructure(['createdAt', 'updatedAt']);
|
||||
});
|
||||
|
||||
it('returns the documented error for an unknown loan', function () {
|
||||
$this->getJson('/api/v1/loans/L-UNKNOWN')
|
||||
->assertNotFound()
|
||||
->assertExactJson(['error' => 'LOAN_NOT_FOUND']);
|
||||
});
|
||||
@@ -0,0 +1,7 @@
|
||||
<?php
|
||||
|
||||
it('reports service health', function () {
|
||||
$this->getJson('/health')
|
||||
->assertSuccessful()
|
||||
->assertExactJson(['status' => 'UP']);
|
||||
});
|
||||
@@ -0,0 +1,128 @@
|
||||
<?php
|
||||
|
||||
use App\Models\Loan;
|
||||
use Database\Seeders\BankFlowSeeder;
|
||||
|
||||
beforeEach(function () {
|
||||
$this->seed(BankFlowSeeder::class);
|
||||
});
|
||||
|
||||
it('approves a valid personal loan and records applicable stages', function () {
|
||||
$loanId = $this->postJson('/api/v1/loans', validLoanPayload())->json('loanId');
|
||||
|
||||
$this->postJson("/api/v1/loans/{$loanId}/process")
|
||||
->assertSuccessful()
|
||||
->assertExactJson([
|
||||
'loanId' => $loanId,
|
||||
'status' => 'APPROVED',
|
||||
'currentStage' => null,
|
||||
]);
|
||||
|
||||
$loan = Loan::query()->where('public_id', $loanId)->firstOrFail();
|
||||
|
||||
expect($loan->histories()->orderBy('id')->pluck('stage_code')->all())->toBe([
|
||||
'VALIDATION',
|
||||
'FRAUD_CHECK',
|
||||
'CREDIT_CHECK',
|
||||
]);
|
||||
});
|
||||
|
||||
it('rejects a business loan without a guarantor', function () {
|
||||
$loanId = $this->postJson('/api/v1/loans', validLoanPayload([
|
||||
'loanType' => 'BUSINESS',
|
||||
'hasGuarantor' => false,
|
||||
]))->json('loanId');
|
||||
|
||||
$this->postJson("/api/v1/loans/{$loanId}/process")
|
||||
->assertSuccessful()
|
||||
->assertJsonPath('status', 'REJECTED');
|
||||
|
||||
$loan = Loan::query()->where('public_id', $loanId)->firstOrFail();
|
||||
|
||||
expect($loan->histories()->latest('id')->value('reason'))->toBe('GUARANTOR_REQUIRED');
|
||||
});
|
||||
|
||||
it('approves a valid business loan with a guarantor', function () {
|
||||
$loanId = $this->postJson('/api/v1/loans', validLoanPayload([
|
||||
'loanType' => 'BUSINESS',
|
||||
'hasGuarantor' => true,
|
||||
]))->json('loanId');
|
||||
|
||||
$this->postJson("/api/v1/loans/{$loanId}/process")
|
||||
->assertSuccessful()
|
||||
->assertJsonPath('status', 'APPROVED');
|
||||
|
||||
$loan = Loan::query()->where('public_id', $loanId)->firstOrFail();
|
||||
|
||||
expect($loan->histories()->orderBy('id')->pluck('stage_code')->all())->toBe([
|
||||
'VALIDATION',
|
||||
'FRAUD_CHECK',
|
||||
'GUARANTOR_CHECK',
|
||||
'CREDIT_CHECK',
|
||||
]);
|
||||
});
|
||||
|
||||
it('stops for fraud and credit manual review decisions', function (array $overrides, string $stage) {
|
||||
$loanId = $this->postJson('/api/v1/loans', validLoanPayload($overrides))->json('loanId');
|
||||
|
||||
$this->postJson("/api/v1/loans/{$loanId}/process")
|
||||
->assertSuccessful()
|
||||
->assertJsonPath('status', 'MANUAL_REVIEW')
|
||||
->assertJsonPath('currentStage', null);
|
||||
|
||||
$loan = Loan::query()->where('public_id', $loanId)->firstOrFail();
|
||||
|
||||
expect($loan->histories()->latest('id')->value('stage_code'))->toBe($stage);
|
||||
})->with([
|
||||
'fraud review' => [['customerId' => 'REVIEW-1001'], 'FRAUD_CHECK'],
|
||||
'credit review' => [['creditScore' => 600], 'CREDIT_CHECK'],
|
||||
]);
|
||||
|
||||
it('executes manager approval only above its threshold', function (
|
||||
int $amount,
|
||||
int $monthlyIncome,
|
||||
string $expectedStatus,
|
||||
int $expectedHistoryCount,
|
||||
) {
|
||||
$loanId = $this->postJson('/api/v1/loans', validLoanPayload([
|
||||
'amount' => $amount,
|
||||
'monthlyIncome' => $monthlyIncome,
|
||||
]))->json('loanId');
|
||||
|
||||
$this->postJson("/api/v1/loans/{$loanId}/process")
|
||||
->assertSuccessful()
|
||||
->assertJsonPath('status', $expectedStatus);
|
||||
|
||||
$loan = Loan::query()->where('public_id', $loanId)->firstOrFail();
|
||||
|
||||
expect($loan->histories()->count())->toBe($expectedHistoryCount);
|
||||
})->with([
|
||||
'skipped at threshold' => [500_000_000, 20_000_000, 'APPROVED', 3],
|
||||
'manager rejection' => [600_000_000, 20_000_000, 'REJECTED', 4],
|
||||
'manager approval' => [600_000_000, 50_000_000, 'APPROVED', 4],
|
||||
]);
|
||||
|
||||
it('is idempotent after reaching a terminal status', function (array $overrides, string $status) {
|
||||
$loanId = $this->postJson('/api/v1/loans', validLoanPayload($overrides))->json('loanId');
|
||||
|
||||
$this->postJson("/api/v1/loans/{$loanId}/process")->assertSuccessful();
|
||||
|
||||
$loan = Loan::query()->where('public_id', $loanId)->firstOrFail();
|
||||
$historyCount = $loan->histories()->count();
|
||||
|
||||
$this->postJson("/api/v1/loans/{$loanId}/process")
|
||||
->assertSuccessful()
|
||||
->assertJsonPath('status', $status);
|
||||
|
||||
expect($loan->histories()->count())->toBe($historyCount);
|
||||
})->with([
|
||||
'approved' => [[], 'APPROVED'],
|
||||
'rejected' => [['customerId' => 'FRAUD-1001'], 'REJECTED'],
|
||||
'manual review' => [['customerId' => 'REVIEW-1001'], 'MANUAL_REVIEW'],
|
||||
]);
|
||||
|
||||
it('returns the documented error for an unknown loan', function () {
|
||||
$this->postJson('/api/v1/loans/L-UNKNOWN/process')
|
||||
->assertNotFound()
|
||||
->assertExactJson(['error' => 'LOAN_NOT_FOUND']);
|
||||
});
|
||||
+15
-4
@@ -1,6 +1,6 @@
|
||||
<?php
|
||||
|
||||
use Illuminate\Foundation\Testing\RefreshDatabase;
|
||||
use Illuminate\Foundation\Testing\LazilyRefreshDatabase;
|
||||
use Tests\TestCase;
|
||||
|
||||
/*
|
||||
@@ -15,7 +15,7 @@ use Tests\TestCase;
|
||||
*/
|
||||
|
||||
pest()->extend(TestCase::class)
|
||||
// ->use(RefreshDatabase::class)
|
||||
->use(LazilyRefreshDatabase::class)
|
||||
->in('Feature');
|
||||
|
||||
/*
|
||||
@@ -44,7 +44,18 @@ expect()->extend('toBeOne', function () {
|
||||
|
|
||||
*/
|
||||
|
||||
function something()
|
||||
/**
|
||||
* @return array<string, mixed>
|
||||
*/
|
||||
function validLoanPayload(array $overrides = []): array
|
||||
{
|
||||
// ..
|
||||
return array_replace([
|
||||
'customerId' => 'C-1001',
|
||||
'amount' => 400_000_000,
|
||||
'phone' => '09121234567',
|
||||
'loanType' => 'PERSONAL',
|
||||
'monthlyIncome' => 50_000_000,
|
||||
'creditScore' => 720,
|
||||
'hasGuarantor' => false,
|
||||
], $overrides);
|
||||
}
|
||||
@@ -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'],
|
||||
]);
|
||||
Reference in new issue
Block a user