make app database
This commit is contained in:
73
database/factories/LoanFactory.php
Normal file
73
database/factories/LoanFactory.php
Normal file
@@ -0,0 +1,73 @@
|
||||
<?php
|
||||
|
||||
namespace Database\Factories;
|
||||
|
||||
use App\Domain\Loan\Enums\LoanStatus;
|
||||
use App\Domain\Loan\Enums\StageResultType;
|
||||
use App\Models\Loan;
|
||||
use App\Models\LoanHistory;
|
||||
use App\Models\WorkflowConfiguration;
|
||||
use App\Models\WorkflowConfigurationStep;
|
||||
use Illuminate\Database\Eloquent\Factories\Factory;
|
||||
|
||||
/**
|
||||
* @extends Factory<Loan>
|
||||
*/
|
||||
class LoanFactory extends Factory
|
||||
{
|
||||
/**
|
||||
* Define the model's default state.
|
||||
*
|
||||
* @return array<string, mixed>
|
||||
*/
|
||||
public function definition(): array
|
||||
{
|
||||
return [
|
||||
'public_id' => 'L-'.fake()->unique()->numerify('#####'),
|
||||
'customer_id' => 'C-'.fake()->unique()->numerify('####'),
|
||||
'workflow_configuration_id' => WorkflowConfiguration::factory()->published(),
|
||||
'loan_type_id' => static fn (array $attributes): int => WorkflowConfiguration::query()
|
||||
->findOrFail($attributes['workflow_configuration_id'])
|
||||
->loan_type_id,
|
||||
'current_workflow_configuration_step_id' => null,
|
||||
'amount' => fake()->numberBetween(1_000_000, 1_000_000_000),
|
||||
'phone' => '09'.fake()->numerify('#########'),
|
||||
'monthly_income' => fake()->numberBetween(1_000_000, 100_000_000),
|
||||
'credit_score' => fake()->numberBetween(0, 1000),
|
||||
'has_guarantor' => fake()->boolean(),
|
||||
'status' => LoanStatus::Submitted,
|
||||
];
|
||||
}
|
||||
|
||||
public function forWorkflow(WorkflowConfiguration $workflowConfiguration): static
|
||||
{
|
||||
return $this
|
||||
->for($workflowConfiguration->loanType, 'loanType')
|
||||
->for($workflowConfiguration, 'workflowConfiguration');
|
||||
}
|
||||
|
||||
public function atStep(WorkflowConfigurationStep $step): static
|
||||
{
|
||||
return $this->forWorkflow($step->workflowConfiguration)->state([
|
||||
'current_workflow_configuration_step_id' => $step->getKey(),
|
||||
]);
|
||||
}
|
||||
|
||||
public function withHistory(
|
||||
WorkflowConfigurationStep $step,
|
||||
StageResultType $result = StageResultType::Pass,
|
||||
string $reason = 'SUCCESS',
|
||||
): static {
|
||||
return $this->has(
|
||||
LoanHistory::factory()
|
||||
->for($step, 'workflowConfigurationStep')
|
||||
->state([
|
||||
'stage_code' => $step->stageDefinition->code,
|
||||
'rules_snapshot' => $step->rules,
|
||||
'result' => $result,
|
||||
'reason' => $reason,
|
||||
]),
|
||||
'histories',
|
||||
);
|
||||
}
|
||||
}
|
||||
45
database/factories/LoanHistoryFactory.php
Normal file
45
database/factories/LoanHistoryFactory.php
Normal file
@@ -0,0 +1,45 @@
|
||||
<?php
|
||||
|
||||
namespace Database\Factories;
|
||||
|
||||
use App\Domain\Loan\Enums\StageResultType;
|
||||
use App\Models\Loan;
|
||||
use App\Models\LoanHistory;
|
||||
use App\Models\WorkflowConfigurationStep;
|
||||
use Illuminate\Database\Eloquent\Factories\Factory;
|
||||
|
||||
/**
|
||||
* @extends Factory<LoanHistory>
|
||||
*/
|
||||
class LoanHistoryFactory extends Factory
|
||||
{
|
||||
/**
|
||||
* Define the model's default state.
|
||||
*
|
||||
* @return array<string, mixed>
|
||||
*/
|
||||
public function definition(): array
|
||||
{
|
||||
return [
|
||||
'loan_id' => Loan::factory(),
|
||||
'workflow_configuration_step_id' => static function (array $attributes): int {
|
||||
$loan = Loan::query()->findOrFail($attributes['loan_id']);
|
||||
|
||||
return WorkflowConfigurationStep::factory()
|
||||
->for($loan->workflowConfiguration)
|
||||
->create()
|
||||
->getKey();
|
||||
},
|
||||
'stage_code' => static fn (array $attributes): string => WorkflowConfigurationStep::query()
|
||||
->findOrFail($attributes['workflow_configuration_step_id'])
|
||||
->stageDefinition
|
||||
->code,
|
||||
'rules_snapshot' => static fn (array $attributes): array => WorkflowConfigurationStep::query()
|
||||
->findOrFail($attributes['workflow_configuration_step_id'])
|
||||
->rules,
|
||||
'result' => StageResultType::Pass,
|
||||
'reason' => 'SUCCESS',
|
||||
'executed_at' => now(),
|
||||
];
|
||||
}
|
||||
}
|
||||
33
database/factories/LoanTypeFactory.php
Normal file
33
database/factories/LoanTypeFactory.php
Normal file
@@ -0,0 +1,33 @@
|
||||
<?php
|
||||
|
||||
namespace Database\Factories;
|
||||
|
||||
use App\Models\LoanType;
|
||||
use Illuminate\Database\Eloquent\Factories\Factory;
|
||||
|
||||
/**
|
||||
* @extends Factory<LoanType>
|
||||
*/
|
||||
class LoanTypeFactory extends Factory
|
||||
{
|
||||
/**
|
||||
* Define the model's default state.
|
||||
*
|
||||
* @return array<string, mixed>
|
||||
*/
|
||||
public function definition(): array
|
||||
{
|
||||
return [
|
||||
'code' => fake()->unique()->bothify('TYPE_????'),
|
||||
'name' => fake()->words(2, true),
|
||||
'is_active' => true,
|
||||
];
|
||||
}
|
||||
|
||||
public function inactive(): static
|
||||
{
|
||||
return $this->state(fn (array $attributes) => [
|
||||
'is_active' => false,
|
||||
]);
|
||||
}
|
||||
}
|
||||
44
database/factories/StageDefinitionFactory.php
Normal file
44
database/factories/StageDefinitionFactory.php
Normal file
@@ -0,0 +1,44 @@
|
||||
<?php
|
||||
|
||||
namespace Database\Factories;
|
||||
|
||||
use App\Domain\Loan\Enums\LoanStage;
|
||||
use App\Models\StageDefinition;
|
||||
use Illuminate\Database\Eloquent\Factories\Factory;
|
||||
|
||||
/**
|
||||
* @extends Factory<StageDefinition>
|
||||
*/
|
||||
class StageDefinitionFactory extends Factory
|
||||
{
|
||||
/**
|
||||
* Define the model's default state.
|
||||
*
|
||||
* @return array<string, mixed>
|
||||
*/
|
||||
public function definition(): array
|
||||
{
|
||||
$stage = fake()->randomElement(LoanStage::cases());
|
||||
|
||||
return [
|
||||
'code' => $stage->value,
|
||||
'name' => str($stage->name)->headline()->toString(),
|
||||
'is_active' => true,
|
||||
];
|
||||
}
|
||||
|
||||
public function forStage(LoanStage $stage): static
|
||||
{
|
||||
return $this->state(fn (array $attributes) => [
|
||||
'code' => $stage->value,
|
||||
'name' => str($stage->name)->headline()->toString(),
|
||||
]);
|
||||
}
|
||||
|
||||
public function inactive(): static
|
||||
{
|
||||
return $this->state(fn (array $attributes) => [
|
||||
'is_active' => false,
|
||||
]);
|
||||
}
|
||||
}
|
||||
65
database/factories/WorkflowConfigurationFactory.php
Normal file
65
database/factories/WorkflowConfigurationFactory.php
Normal file
@@ -0,0 +1,65 @@
|
||||
<?php
|
||||
|
||||
namespace Database\Factories;
|
||||
|
||||
use App\Domain\Loan\Enums\WorkflowConfigurationStatus;
|
||||
use App\Models\LoanType;
|
||||
use App\Models\StageDefinition;
|
||||
use App\Models\WorkflowConfiguration;
|
||||
use App\Models\WorkflowConfigurationStep;
|
||||
use Illuminate\Database\Eloquent\Factories\Factory;
|
||||
|
||||
/**
|
||||
* @extends Factory<WorkflowConfiguration>
|
||||
*/
|
||||
class WorkflowConfigurationFactory extends Factory
|
||||
{
|
||||
/**
|
||||
* Define the model's default state.
|
||||
*
|
||||
* @return array<string, mixed>
|
||||
*/
|
||||
public function definition(): array
|
||||
{
|
||||
return [
|
||||
'loan_type_id' => LoanType::factory(),
|
||||
'name' => fake()->words(3, true),
|
||||
'version' => 1,
|
||||
'status' => WorkflowConfigurationStatus::Draft,
|
||||
'published_at' => null,
|
||||
'created_by' => null,
|
||||
];
|
||||
}
|
||||
|
||||
public function published(): static
|
||||
{
|
||||
return $this->state(fn (array $attributes) => [
|
||||
'status' => WorkflowConfigurationStatus::Published,
|
||||
'published_at' => now(),
|
||||
]);
|
||||
}
|
||||
|
||||
public function archived(): static
|
||||
{
|
||||
return $this->state(fn (array $attributes) => [
|
||||
'status' => WorkflowConfigurationStatus::Archived,
|
||||
'published_at' => fake()->dateTimeBetween('-1 year', '-1 day'),
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array<string, mixed> $rules
|
||||
*/
|
||||
public function withStep(StageDefinition $stageDefinition, int $position, array $rules = []): static
|
||||
{
|
||||
return $this->has(
|
||||
WorkflowConfigurationStep::factory()
|
||||
->for($stageDefinition, 'stageDefinition')
|
||||
->state([
|
||||
'position' => $position,
|
||||
'rules' => $rules,
|
||||
]),
|
||||
'steps',
|
||||
);
|
||||
}
|
||||
}
|
||||
37
database/factories/WorkflowConfigurationStepFactory.php
Normal file
37
database/factories/WorkflowConfigurationStepFactory.php
Normal file
@@ -0,0 +1,37 @@
|
||||
<?php
|
||||
|
||||
namespace Database\Factories;
|
||||
|
||||
use App\Models\StageDefinition;
|
||||
use App\Models\WorkflowConfiguration;
|
||||
use App\Models\WorkflowConfigurationStep;
|
||||
use Illuminate\Database\Eloquent\Factories\Factory;
|
||||
|
||||
/**
|
||||
* @extends Factory<WorkflowConfigurationStep>
|
||||
*/
|
||||
class WorkflowConfigurationStepFactory extends Factory
|
||||
{
|
||||
/**
|
||||
* Define the model's default state.
|
||||
*
|
||||
* @return array<string, mixed>
|
||||
*/
|
||||
public function definition(): array
|
||||
{
|
||||
return [
|
||||
'workflow_configuration_id' => WorkflowConfiguration::factory(),
|
||||
'stage_definition_id' => StageDefinition::factory(),
|
||||
'position' => fake()->unique()->numberBetween(1, 1000),
|
||||
'rules' => [],
|
||||
'is_enabled' => true,
|
||||
];
|
||||
}
|
||||
|
||||
public function disabled(): static
|
||||
{
|
||||
return $this->state(fn (array $attributes) => [
|
||||
'is_enabled' => false,
|
||||
]);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user