implement admin panel
This commit is contained in:
92 files changed
+5426
-9
No files matched your search
@@ -0,0 +1,62 @@
|
||||
<?php
|
||||
|
||||
namespace App\Actions\WorkflowConfiguration;
|
||||
|
||||
use App\Domain\Loan\Enums\WorkflowConfigurationStatus;
|
||||
use App\Models\LoanType;
|
||||
use App\Models\User;
|
||||
use App\Models\WorkflowConfiguration;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
|
||||
class CreateWorkflowConfigurationAction
|
||||
{
|
||||
public function __construct(private ValidateWorkflowStepDataAction $validateWorkflowStepData) {}
|
||||
|
||||
/**
|
||||
* @param array<int, array{stage_definition_id: int, rules?: array<string, mixed>, is_enabled?: bool}> $steps
|
||||
*/
|
||||
public function handle(
|
||||
LoanType $loanType,
|
||||
string $name,
|
||||
User $creator,
|
||||
array $steps = [],
|
||||
?WorkflowConfiguration $copyFrom = null,
|
||||
): WorkflowConfiguration {
|
||||
return DB::transaction(function () use ($loanType, $name, $creator, $steps, $copyFrom): WorkflowConfiguration {
|
||||
$lockedLoanType = LoanType::query()->lockForUpdate()->findOrFail($loanType->getKey());
|
||||
|
||||
$nextVersion = ((int) $lockedLoanType->workflowConfigurations()->max('version')) + 1;
|
||||
|
||||
$workflowConfiguration = $lockedLoanType->workflowConfigurations()->create([
|
||||
'name' => $name,
|
||||
'version' => $nextVersion,
|
||||
'status' => WorkflowConfigurationStatus::Draft,
|
||||
'created_by' => $creator->getKey(),
|
||||
]);
|
||||
|
||||
$stepsToCreate = $copyFrom === null
|
||||
? $steps
|
||||
: $copyFrom->steps()
|
||||
->get(['stage_definition_id', 'rules', 'is_enabled'])
|
||||
->map(fn ($step): array => [
|
||||
'stage_definition_id' => $step->stage_definition_id,
|
||||
'rules' => $step->rules,
|
||||
'is_enabled' => $step->is_enabled,
|
||||
])
|
||||
->all();
|
||||
|
||||
$validatedSteps = $this->validateWorkflowStepData->handle($stepsToCreate);
|
||||
|
||||
foreach ($validatedSteps as $index => $step) {
|
||||
$workflowConfiguration->steps()->create([
|
||||
'stage_definition_id' => $step['stage_definition_id'],
|
||||
'position' => $index + 1,
|
||||
'rules' => $step['rules'] ?? [],
|
||||
'is_enabled' => $step['is_enabled'] ?? true,
|
||||
]);
|
||||
}
|
||||
|
||||
return $workflowConfiguration->load(['loanType', 'steps.stageDefinition', 'creator']);
|
||||
});
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,46 @@
|
||||
<?php
|
||||
|
||||
namespace App\Actions\WorkflowConfiguration;
|
||||
|
||||
use App\Domain\Loan\Enums\WorkflowConfigurationStatus;
|
||||
use App\Models\User;
|
||||
use App\Models\WorkflowConfiguration;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
use Illuminate\Validation\ValidationException;
|
||||
|
||||
class PublishWorkflowConfigurationAction
|
||||
{
|
||||
public function __construct(private ValidateWorkflowConfigurationAction $validateWorkflowConfiguration) {}
|
||||
|
||||
public function handle(WorkflowConfiguration $workflowConfiguration, User $publisher): WorkflowConfiguration
|
||||
{
|
||||
return DB::transaction(function () use ($workflowConfiguration, $publisher): WorkflowConfiguration {
|
||||
$lockedConfiguration = WorkflowConfiguration::query()
|
||||
->with(['loanType', 'steps.stageDefinition'])
|
||||
->lockForUpdate()
|
||||
->findOrFail($workflowConfiguration->getKey());
|
||||
|
||||
if (! $lockedConfiguration->isEditable()) {
|
||||
throw ValidationException::withMessages([
|
||||
'status' => 'Only a draft workflow configuration can be published.',
|
||||
]);
|
||||
}
|
||||
|
||||
$this->validateWorkflowConfiguration->handle($lockedConfiguration);
|
||||
|
||||
WorkflowConfiguration::query()
|
||||
->whereBelongsTo($lockedConfiguration->loanType)
|
||||
->where('status', WorkflowConfigurationStatus::Published)
|
||||
->lockForUpdate()
|
||||
->update(['status' => WorkflowConfigurationStatus::Archived]);
|
||||
|
||||
$lockedConfiguration->update([
|
||||
'status' => WorkflowConfigurationStatus::Published,
|
||||
'published_at' => now(),
|
||||
'created_by' => $publisher->getKey(),
|
||||
]);
|
||||
|
||||
return $lockedConfiguration->refresh()->load(['loanType', 'steps.stageDefinition', 'creator']);
|
||||
});
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,46 @@
|
||||
<?php
|
||||
|
||||
namespace App\Actions\WorkflowConfiguration;
|
||||
|
||||
use App\Models\WorkflowConfiguration;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
use Illuminate\Validation\ValidationException;
|
||||
|
||||
class UpdateWorkflowConfigurationAction
|
||||
{
|
||||
public function __construct(private ValidateWorkflowStepDataAction $validateWorkflowStepData) {}
|
||||
|
||||
/**
|
||||
* @param array<int, array{stage_definition_id: int, rules?: array<string, mixed>, is_enabled?: bool}> $steps
|
||||
*/
|
||||
public function handle(WorkflowConfiguration $workflowConfiguration, string $name, array $steps): WorkflowConfiguration
|
||||
{
|
||||
$validatedSteps = $this->validateWorkflowStepData->handle($steps);
|
||||
|
||||
return DB::transaction(function () use ($workflowConfiguration, $name, $validatedSteps): WorkflowConfiguration {
|
||||
$lockedConfiguration = WorkflowConfiguration::query()
|
||||
->lockForUpdate()
|
||||
->findOrFail($workflowConfiguration->getKey());
|
||||
|
||||
if (! $lockedConfiguration->isEditable()) {
|
||||
throw ValidationException::withMessages([
|
||||
'status' => 'Only draft workflow configurations can be changed.',
|
||||
]);
|
||||
}
|
||||
|
||||
$lockedConfiguration->update(['name' => $name]);
|
||||
$lockedConfiguration->steps()->delete();
|
||||
|
||||
foreach ($validatedSteps as $index => $step) {
|
||||
$lockedConfiguration->steps()->create([
|
||||
'stage_definition_id' => $step['stage_definition_id'],
|
||||
'position' => $index + 1,
|
||||
'rules' => $step['rules'] ?? [],
|
||||
'is_enabled' => $step['is_enabled'] ?? true,
|
||||
]);
|
||||
}
|
||||
|
||||
return $lockedConfiguration->load(['loanType', 'steps.stageDefinition', 'creator']);
|
||||
});
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,63 @@
|
||||
<?php
|
||||
|
||||
namespace App\Actions\WorkflowConfiguration;
|
||||
|
||||
use App\Domain\Loan\Enums\LoanStage;
|
||||
use App\Models\WorkflowConfiguration;
|
||||
use Illuminate\Validation\ValidationException;
|
||||
|
||||
class ValidateWorkflowConfigurationAction
|
||||
{
|
||||
public function __construct(private ValidateWorkflowStepDataAction $validateWorkflowStepData) {}
|
||||
|
||||
public function handle(WorkflowConfiguration $workflowConfiguration): void
|
||||
{
|
||||
$workflowConfiguration->loadMissing(['loanType', 'steps.stageDefinition']);
|
||||
|
||||
$enabledSteps = $workflowConfiguration->steps
|
||||
->where('is_enabled', true)
|
||||
->values();
|
||||
|
||||
$stepData = $enabledSteps
|
||||
->map(fn ($step): array => [
|
||||
'stage_definition_id' => $step->stage_definition_id,
|
||||
'rules' => $step->rules,
|
||||
'is_enabled' => true,
|
||||
])
|
||||
->all();
|
||||
|
||||
$this->validateWorkflowStepData->handle($stepData);
|
||||
|
||||
$enabledCodes = $enabledSteps->pluck('stageDefinition.code')->all();
|
||||
$errors = [];
|
||||
|
||||
foreach ($this->requiredStageCodes($workflowConfiguration->loanType->code) as $requiredStageCode) {
|
||||
if (! in_array($requiredStageCode, $enabledCodes, true)) {
|
||||
$errors['steps'] = "The {$requiredStageCode} stage is required for {$workflowConfiguration->loanType->name}.";
|
||||
}
|
||||
}
|
||||
|
||||
if ($errors !== []) {
|
||||
throw ValidationException::withMessages($errors);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @return list<string>
|
||||
*/
|
||||
private function requiredStageCodes(string $loanTypeCode): array
|
||||
{
|
||||
$required = [
|
||||
LoanStage::Validation->value,
|
||||
LoanStage::FraudCheck->value,
|
||||
LoanStage::CreditCheck->value,
|
||||
LoanStage::ManagerApproval->value,
|
||||
];
|
||||
|
||||
if ($loanTypeCode === 'BUSINESS') {
|
||||
$required[] = LoanStage::GuarantorCheck->value;
|
||||
}
|
||||
|
||||
return $required;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,115 @@
|
||||
<?php
|
||||
|
||||
namespace App\Actions\WorkflowConfiguration;
|
||||
|
||||
use App\Domain\Loan\Enums\LoanStage;
|
||||
use App\Models\StageDefinition;
|
||||
use Illuminate\Support\Arr;
|
||||
use Illuminate\Support\Facades\Validator;
|
||||
use Illuminate\Validation\ValidationException;
|
||||
|
||||
class ValidateWorkflowStepDataAction
|
||||
{
|
||||
/**
|
||||
* @param array<int, array{stage_definition_id: int, rules?: array<string, mixed>, is_enabled?: bool}> $steps
|
||||
* @return array<int, array{stage_definition_id: int, rules: array<string, mixed>, is_enabled: bool}>
|
||||
*/
|
||||
public function handle(array $steps): array
|
||||
{
|
||||
$validatedSteps = Validator::make(['steps' => $steps], [
|
||||
'steps' => ['required', 'array', 'min:1'],
|
||||
'steps.*.stage_definition_id' => ['required', 'integer', 'distinct', 'exists:stage_definitions,id'],
|
||||
'steps.*.rules' => ['present', 'array'],
|
||||
'steps.*.is_enabled' => ['required', 'boolean'],
|
||||
])->validate()['steps'];
|
||||
|
||||
$stageDefinitions = StageDefinition::query()
|
||||
->whereKey(collect($validatedSteps)->pluck('stage_definition_id'))
|
||||
->get()
|
||||
->keyBy('id');
|
||||
|
||||
$registeredStageCodes = collect(LoanStage::cases())->pluck('value')->all();
|
||||
$errors = [];
|
||||
|
||||
foreach ($validatedSteps as $index => $step) {
|
||||
$stageDefinition = $stageDefinitions[$step['stage_definition_id']];
|
||||
|
||||
if (! $stageDefinition->is_active) {
|
||||
$errors["steps.{$index}.stage_definition_id"] = "{$stageDefinition->code} is inactive.";
|
||||
}
|
||||
|
||||
if (! in_array($stageDefinition->code, $registeredStageCodes, true)) {
|
||||
$errors["steps.{$index}.stage_definition_id"] = "{$stageDefinition->code} is not registered by the application.";
|
||||
}
|
||||
|
||||
$rulesValidator = Validator::make($step['rules'], $this->rulesFor($stageDefinition->code));
|
||||
|
||||
foreach ($rulesValidator->errors()->toArray() as $field => $messages) {
|
||||
$errors["steps.{$index}.rules.{$field}"] = Arr::first($messages);
|
||||
}
|
||||
|
||||
$this->validateCreditRanges($stageDefinition->code, $step['rules'], $index, $errors);
|
||||
}
|
||||
|
||||
if ($errors !== []) {
|
||||
throw ValidationException::withMessages($errors);
|
||||
}
|
||||
|
||||
return array_map(fn (array $step): array => [
|
||||
'stage_definition_id' => $step['stage_definition_id'],
|
||||
'rules' => $step['rules'],
|
||||
'is_enabled' => $step['is_enabled'],
|
||||
], $validatedSteps);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array<string, list<string>>
|
||||
*/
|
||||
private function rulesFor(string $stageCode): array
|
||||
{
|
||||
return match ($stageCode) {
|
||||
LoanStage::FraudCheck->value => [
|
||||
'fraudPrefix' => ['required', 'string', 'max:50'],
|
||||
'manualReviewPrefix' => ['required', 'string', 'max:50', 'different:fraudPrefix'],
|
||||
],
|
||||
LoanStage::GuarantorCheck->value => [
|
||||
'guarantorRequired' => ['required', 'boolean'],
|
||||
],
|
||||
LoanStage::CreditCheck->value => [
|
||||
'rejectBelow' => ['required', 'integer', 'between:0,1000'],
|
||||
'manualReviewMin' => ['required', 'integer', 'between:0,1000'],
|
||||
'manualReviewMax' => ['required', 'integer', 'between:0,1000'],
|
||||
'approveFrom' => ['required', 'integer', 'between:0,1000'],
|
||||
],
|
||||
LoanStage::ManagerApproval->value => [
|
||||
'activationThreshold' => ['required', 'integer', 'min:0'],
|
||||
'incomeMultiplier' => ['required', 'integer', 'min:1'],
|
||||
],
|
||||
default => [],
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array<string, mixed> $rules
|
||||
* @param array<string, string|null> $errors
|
||||
*/
|
||||
private function validateCreditRanges(string $stageCode, array $rules, int $index, array &$errors): void
|
||||
{
|
||||
if ($stageCode !== LoanStage::CreditCheck->value
|
||||
|| ! isset($rules['rejectBelow'], $rules['manualReviewMin'], $rules['manualReviewMax'], $rules['approveFrom'])) {
|
||||
return;
|
||||
}
|
||||
|
||||
if ($rules['rejectBelow'] !== $rules['manualReviewMin']) {
|
||||
$errors["steps.{$index}.rules.rejectBelow"] = 'The rejection boundary must meet the manual-review minimum without a gap.';
|
||||
}
|
||||
|
||||
if ($rules['manualReviewMin'] > $rules['manualReviewMax']) {
|
||||
$errors["steps.{$index}.rules.manualReviewMax"] = 'The manual-review maximum must be at least its minimum.';
|
||||
}
|
||||
|
||||
if ($rules['approveFrom'] !== $rules['manualReviewMax'] + 1) {
|
||||
$errors["steps.{$index}.rules.approveFrom"] = 'The approval boundary must immediately follow the manual-review range.';
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user