implement admin panel

This commit is contained in:
Meghdad committed 2026-07-22 17:22:33 +03:30
1 parent 56a52be180
commit e8223fbc3e
92 files changed
+5426 -9

No files matched your search

+76
View File
@@ -0,0 +1,76 @@
<?php
use App\Domain\Loan\Enums\WorkflowConfigurationStatus;
use App\Models\Loan;
use App\Models\LoanType;
use App\Models\User;
use App\Models\WorkflowConfiguration;
use Illuminate\Foundation\Testing\LazilyRefreshDatabase;
uses(LazilyRefreshDatabase::class);
test('an authenticated administrator can open the admin resources', function () {
$user = User::factory()->create();
LoanType::factory()->create([
'code' => 'PERSONAL',
'name' => 'Personal Loan',
]);
$this->actingAs($user)
->get('/admin/loan-types')
->assertSuccessful()
->assertSee('Shape every lending journey from one place.')
->assertSee('Personal Loan');
$this->actingAs($user)
->get('/admin/workflow-configurations')
->assertSuccessful();
$this->actingAs($user)
->get('/admin/workflow-configurations/create')
->assertSuccessful()
->assertSee('Execution path');
$this->actingAs($user)
->get('/admin/stage-definitions')
->assertSuccessful();
$this->actingAs($user)
->get('/admin/loans')
->assertSuccessful();
});
test('loans are read only in the admin panel', function () {
$user = User::factory()->create();
$loan = Loan::factory()->create();
expect($user->can('view', $loan))->toBeTrue()
->and($user->can('create', Loan::class))->toBeFalse()
->and($user->can('update', $loan))->toBeFalse()
->and($user->can('delete', $loan))->toBeFalse();
$this->actingAs($user)
->get('/admin/loans/create')
->assertNotFound();
$this->actingAs($user)
->get('/admin/loans/'.$loan->public_id)
->assertSuccessful()
->assertSee($loan->public_id);
});
test('only draft workflow configurations are editable', function () {
$user = User::factory()->create();
$draft = WorkflowConfiguration::factory()->create();
$published = WorkflowConfiguration::factory()->published()->create();
expect($draft->status)->toBe(WorkflowConfigurationStatus::Draft)
->and($user->can('update', $draft))->toBeTrue()
->and($user->can('update', $published))->toBeFalse();
});
test('unverified users cannot access the admin panel', function () {
$user = User::factory()->unverified()->create();
expect($user->canAccessPanel(filament()->getPanel('admin')))->toBeFalse();
});
@@ -0,0 +1,106 @@
<?php
use App\Actions\WorkflowConfiguration\CreateWorkflowConfigurationAction;
use App\Actions\WorkflowConfiguration\PublishWorkflowConfigurationAction;
use App\Domain\Loan\Enums\LoanStage;
use App\Domain\Loan\Enums\WorkflowConfigurationStatus;
use App\Models\LoanType;
use App\Models\StageDefinition;
use App\Models\User;
use App\Models\WorkflowConfiguration;
use Illuminate\Foundation\Testing\LazilyRefreshDatabase;
use Illuminate\Validation\ValidationException;
uses(LazilyRefreshDatabase::class);
test('copying a workflow creates an isolated next draft version', function () {
$user = User::factory()->create();
$loanType = LoanType::factory()->create(['code' => 'PERSONAL']);
$stage = StageDefinition::factory()->forStage(LoanStage::Validation)->create();
$source = WorkflowConfiguration::factory()
->for($loanType, 'loanType')
->published()
->withStep($stage, 1)
->create(['version' => 1]);
$draft = app(CreateWorkflowConfigurationAction::class)->handle(
$loanType,
'Next personal workflow',
$user,
copyFrom: $source,
);
expect($draft->version)->toBe(2)
->and($draft->status)->toBe(WorkflowConfigurationStatus::Draft)
->and($draft->steps)->toHaveCount(1)
->and($draft->steps->first()->getKey())->not->toBe($source->steps->first()->getKey());
});
test('publishing archives the previous version atomically', function () {
$user = User::factory()->create();
$loanType = LoanType::factory()->create(['code' => 'PERSONAL']);
$stages = collect(LoanStage::cases())->mapWithKeys(fn (LoanStage $stage): array => [
$stage->value => StageDefinition::factory()->forStage($stage)->create(),
]);
$published = WorkflowConfiguration::factory()
->for($loanType, 'loanType')
->published()
->create(['version' => 1]);
$draft = WorkflowConfiguration::factory()
->for($loanType, 'loanType')
->create(['version' => 2]);
$stepDefinitions = [
[LoanStage::Validation, []],
[LoanStage::FraudCheck, ['fraudPrefix' => 'FRAUD', 'manualReviewPrefix' => 'REVIEW']],
[LoanStage::CreditCheck, ['rejectBelow' => 500, 'manualReviewMin' => 500, 'manualReviewMax' => 649, 'approveFrom' => 650]],
[LoanStage::ManagerApproval, ['activationThreshold' => 500_000_000, 'incomeMultiplier' => 20]],
];
foreach ($stepDefinitions as $index => [$stage, $rules]) {
$draft->steps()->create([
'stage_definition_id' => $stages[$stage->value]->getKey(),
'position' => $index + 1,
'rules' => $rules,
'is_enabled' => true,
]);
}
app(PublishWorkflowConfigurationAction::class)->handle($draft, $user);
expect($draft->refresh()->status)->toBe(WorkflowConfigurationStatus::Published)
->and($draft->published_at)->not->toBeNull()
->and($published->refresh()->status)->toBe(WorkflowConfigurationStatus::Archived);
});
test('publishing rejects a credit policy with a range gap', function () {
$user = User::factory()->create();
$loanType = LoanType::factory()->create(['code' => 'PERSONAL']);
$stages = collect(LoanStage::cases())->mapWithKeys(fn (LoanStage $stage): array => [
$stage->value => StageDefinition::factory()->forStage($stage)->create(),
]);
$draft = WorkflowConfiguration::factory()->for($loanType, 'loanType')->create();
$definitions = [
[LoanStage::Validation, []],
[LoanStage::FraudCheck, ['fraudPrefix' => 'FRAUD', 'manualReviewPrefix' => 'REVIEW']],
[LoanStage::CreditCheck, ['rejectBelow' => 500, 'manualReviewMin' => 500, 'manualReviewMax' => 649, 'approveFrom' => 700]],
[LoanStage::ManagerApproval, ['activationThreshold' => 500_000_000, 'incomeMultiplier' => 20]],
];
foreach ($definitions as $index => [$stage, $rules]) {
$draft->steps()->create([
'stage_definition_id' => $stages[$stage->value]->getKey(),
'position' => $index + 1,
'rules' => $rules,
'is_enabled' => true,
]);
}
expect(fn () => app(PublishWorkflowConfigurationAction::class)->handle($draft, $user))
->toThrow(ValidationException::class);
expect($draft->refresh()->status)->toBe(WorkflowConfigurationStatus::Draft);
});