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,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']);
|
||||
});
|
||||
Reference in new issue
Block a user