166 lines
5.3 KiB
PHP
Executable File
166 lines
5.3 KiB
PHP
Executable File
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace Modules\Helpdesk\Database\factories;
|
|
|
|
use App\Models\User;
|
|
use Illuminate\Database\Eloquent\Factories\Factory;
|
|
use Illuminate\Database\Eloquent\Model;
|
|
use Illuminate\Support\Facades\Schema;
|
|
use Illuminate\Support\Str;
|
|
use Modules\Company\Models\Company;
|
|
use Modules\Helpdesk\Models\Ticket;
|
|
|
|
/**
|
|
* @extends Factory<Ticket>
|
|
*/
|
|
final class TicketFactory extends Factory
|
|
{
|
|
private const FACTORY_BODY = 'Factory helpdesk ticket body';
|
|
|
|
/**
|
|
* The model name for the factory.
|
|
*
|
|
* @var class-string<Ticket>
|
|
*/
|
|
protected $model = Ticket::class;
|
|
|
|
/**
|
|
* Define the model's default state.
|
|
*
|
|
* @return array<string, mixed>
|
|
*/
|
|
public function definition(): array
|
|
{
|
|
$company = Company::query()->create([
|
|
'name' => 'Helpdesk Factory Company ' . Str::random(8),
|
|
'slug' => 'helpdesk-factory-company-' . Str::lower(Str::random(8)),
|
|
'status' => 'active',
|
|
'timezone' => 'Europe/London',
|
|
'billing_email' => 'helpdesk-factory-' . Str::lower(Str::random(8)) . '@example.test',
|
|
'is_system_company' => false,
|
|
]);
|
|
$user = User::factory()->create([
|
|
'company_id' => $company->getKey(),
|
|
]);
|
|
|
|
return $this->ticketPayload($company, $user);
|
|
}
|
|
|
|
/**
|
|
* Build a ticket payload using only columns that exist in the current schema.
|
|
*
|
|
* @return array<string, mixed>
|
|
*/
|
|
private function ticketPayload(Company $company, User $user): array
|
|
{
|
|
$ticket = new Ticket;
|
|
$table = $ticket->getTable();
|
|
|
|
$data = [];
|
|
|
|
$put = static function (string $column, mixed $value) use (&$data, $table): void {
|
|
if (Schema::hasColumn($table, $column)) {
|
|
$data[$column] = $value;
|
|
}
|
|
};
|
|
|
|
$put('company_id', $company->getKey());
|
|
$put('user_id', $user->getKey());
|
|
$put('owner_id', $user->getKey());
|
|
$put('created_by_id', $user->getKey());
|
|
$put('assigned_to_id', $user->getKey());
|
|
$put('agent_id', $user->getKey());
|
|
|
|
$put('reference', 'HD-' . now()->format('Y') . '-' . str_pad((string) $this->faker->unique()->numberBetween(1, 9999), 4, '0', STR_PAD_LEFT));
|
|
$put('subject', 'Factory helpdesk ticket');
|
|
$put('title', 'Factory helpdesk ticket');
|
|
$put('body', self::FACTORY_BODY);
|
|
$put('details', self::FACTORY_BODY);
|
|
$put('description', self::FACTORY_BODY);
|
|
$put('search_text', 'factory helpdesk ticket body');
|
|
$put('last_activity_at', now());
|
|
|
|
$statusId = $this->lookupId(
|
|
'Modules\\Helpdesk\\Models\\TicketStatus',
|
|
['name' => 'Open'],
|
|
['slug' => 'open', 'color' => 'blue', 'is_closed' => false, 'sort_order' => 10],
|
|
);
|
|
|
|
$priorityId = $this->lookupId(
|
|
'Modules\\Helpdesk\\Models\\TicketPriority',
|
|
['name' => 'Normal'],
|
|
['slug' => 'normal', 'color' => 'blue', 'sort_order' => 20],
|
|
);
|
|
|
|
$categoryId = $this->lookupId(
|
|
'Modules\\Helpdesk\\Models\\TicketCategory',
|
|
['name' => 'General'],
|
|
['slug' => 'general', 'color' => 'blue', 'sort_order' => 10],
|
|
);
|
|
|
|
$teamId = $this->lookupId(
|
|
'Modules\\Helpdesk\\Models\\HelpdeskTeam',
|
|
['name' => 'Support'],
|
|
['slug' => 'support', 'color' => 'blue', 'sort_order' => 10],
|
|
);
|
|
|
|
$projectId = $this->lookupId(
|
|
'Modules\\Helpdesk\\Models\\HelpdeskProject',
|
|
['name' => 'General'],
|
|
['slug' => 'general', 'color' => 'blue', 'sort_order' => 10],
|
|
);
|
|
|
|
$put('status_id', $statusId);
|
|
$put('ticket_status_id', $statusId);
|
|
$put('priority_id', $priorityId);
|
|
$put('ticket_priority_id', $priorityId);
|
|
$put('category_id', $categoryId);
|
|
$put('ticket_category_id', $categoryId);
|
|
$put('team_id', $teamId);
|
|
$put('helpdesk_team_id', $teamId);
|
|
$put('project_id', $projectId);
|
|
$put('helpdesk_project_id', $projectId);
|
|
|
|
$unid = strtoupper(str_replace('-', '', (string) Str::uuid()));
|
|
$put('guid', (string) Str::uuid());
|
|
$put('uuid', (string) Str::uuid());
|
|
$put('unid', $unid);
|
|
$put('root_unid', $unid);
|
|
$put('sync_sequence', 1);
|
|
$put('source_system', 'phpunit');
|
|
|
|
return array_filter($data, static fn (mixed $value): bool => $value !== null);
|
|
}
|
|
|
|
/**
|
|
* Resolve or create a lookup row when the model exists.
|
|
*/
|
|
private function lookupId(string $class, array $identity, array $attributes): ?int
|
|
{
|
|
if (! class_exists($class)) {
|
|
return null;
|
|
}
|
|
|
|
/** @var class-string<Model> $class */
|
|
$model = new $class;
|
|
|
|
if (! Schema::hasTable($model->getTable())) {
|
|
return null;
|
|
}
|
|
|
|
$payload = array_merge($identity, $attributes);
|
|
|
|
foreach (['slug', 'color', 'sort_order', 'is_closed'] as $column) {
|
|
if (array_key_exists($column, $payload) && ! Schema::hasColumn($model->getTable(), $column)) {
|
|
unset($payload[$column]);
|
|
}
|
|
}
|
|
|
|
$row = $class::query()->firstOrCreate($identity, $payload);
|
|
|
|
return (int) $row->getKey();
|
|
}
|
|
}
|