Initial Commit

This commit is contained in:
Mark Gregory
2026-08-25 12:36:04 +01:00
commit 903423cd6e
330 changed files with 39763 additions and 0 deletions
@@ -0,0 +1,28 @@
<?php
declare(strict_types=1);
namespace Modules\Helpdesk\Database\Factories;
use App\Models\User;
use Illuminate\Database\Eloquent\Factories\Factory;
use Illuminate\Support\Str;
use Modules\Helpdesk\Models\Ticket;
final class TicketFactory extends Factory
{
protected $model = Ticket::class;
public function definition(): array
{
$user = User::factory()->create();
return [
'reference' => 'THR-'.Str::upper(Str::random(8)),
'user_id' => $user->getKey(),
'subject' => fake()->sentence(5),
'body' => fake()->paragraph(),
'last_activity_at' => now(),
];
}
}
@@ -0,0 +1,26 @@
<?php
declare(strict_types=1);
use Illuminate\Database\Migrations\Migration;
return new class extends Migration
{
/**
* This migration predates the application's users table migration.
*
* It must remain as a no-op because existing installations may already
* have this migration recorded. The actual Helpdesk user columns are
* added by the later 2026_08_11_000006 Helpdesk migration once the users
* table is guaranteed to exist.
*/
public function up(): void
{
// Intentionally left blank.
}
public function down(): void
{
// Intentionally left blank.
}
};
@@ -0,0 +1,70 @@
<?php
declare(strict_types=1);
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
return new class extends Migration
{
public function up(): void
{
Schema::create('helpdesk_statuses', static function (Blueprint $table): void {
$table->id();
$table->string('name', 80);
$table->string('slug', 100)->unique();
$table->string('color', 30)->default('zinc');
$table->unsignedSmallInteger('sort_order')->default(0);
$table->boolean('is_closed')->default(false);
$table->timestamps();
});
Schema::create('helpdesk_priorities', static function (Blueprint $table): void {
$table->id();
$table->string('name', 80);
$table->string('slug', 100)->unique();
$table->string('color', 30)->default('zinc');
$table->unsignedSmallInteger('sort_order')->default(0);
$table->timestamps();
});
Schema::create('helpdesk_categories', static function (Blueprint $table): void {
$table->id();
$table->string('name', 120);
$table->string('slug', 140);
$table->string('color', 30)->default('blue');
$table->text('description')->nullable();
$table->timestamps();
$table->unique('slug');
});
Schema::create('helpdesk_teams', static function (Blueprint $table): void {
$table->id();
$table->string('name', 120);
$table->string('slug', 140);
$table->string('color', 30)->default('indigo');
$table->text('description')->nullable();
$table->timestamp('archived_at')->nullable();
$table->softDeletes();
$table->timestamps();
$table->unique('slug');
});
Schema::create('helpdesk_projects', static function (Blueprint $table): void {
$table->id();
$table->foreignId('team_id')->nullable()->constrained('helpdesk_teams')->nullOnDelete();
$table->string('title', 160);
$table->string('slug', 180);
$table->string('color', 30)->default('blue');
$table->text('description')->nullable();
$table->timestamp('archived_at')->nullable();
$table->softDeletes();
$table->timestamps();
$table->unique('slug');
});
}
};
@@ -0,0 +1,40 @@
<?php
declare(strict_types=1);
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
return new class extends Migration
{
public function up(): void
{
Schema::create('helpdesk_tickets', static function (Blueprint $table): void {
$table->id();
$table->string('reference', 32)->unique();
$table->foreignId('user_id')->constrained('users')->cascadeOnDelete();
$table->foreignId('agent_id')->nullable()->constrained('users')->nullOnDelete();
$table->foreignId('status_id')->constrained('helpdesk_statuses')->restrictOnDelete();
$table->foreignId('priority_id')->constrained('helpdesk_priorities')->restrictOnDelete();
$table->foreignId('category_id')->nullable()->constrained('helpdesk_categories')->nullOnDelete();
$table->foreignId('project_id')->nullable()->constrained('helpdesk_projects')->nullOnDelete();
$table->foreignId('team_id')->nullable()->constrained('helpdesk_teams')->nullOnDelete();
$table->string('subject', 200);
$table->longText('body');
$table->timestamp('due_at')->nullable();
$table->timestamp('completed_at')->nullable();
$table->timestamp('last_activity_at')->nullable();
$table->timestamp('archived_at')->nullable();
$table->softDeletes();
$table->timestamps();
$table->index('project_id');
$table->index(['team_id', 'status_id']);
$table->index(['status_id', 'priority_id']);
$table->index(['agent_id', 'completed_at']);
$table->index(['user_id', 'created_at']);
$table->index('last_activity_at');
});
}
};
@@ -0,0 +1,77 @@
<?php
declare(strict_types=1);
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
return new class extends Migration
{
public function up(): void
{
Schema::create('helpdesk_git_repositories', static function (Blueprint $table): void {
$table->id();
$table->foreignId('project_id')->nullable()->constrained('helpdesk_projects')->nullOnDelete();
$table->string('name', 160);
$table->string('provider', 40)->default('git');
$table->string('remote_url', 500);
$table->string('default_branch', 120)->default('main');
$table->string('webhook_secret', 120)->nullable();
$table->boolean('is_active')->default(true);
$table->timestamps();
$table->index('project_id');
$table->index(['provider', 'is_active']);
});
Schema::create('helpdesk_git_commits', static function (Blueprint $table): void {
$table->id();
$table->foreignId('repository_id')->constrained('helpdesk_git_repositories')->cascadeOnDelete();
$table->foreignId('ticket_id')->nullable()->constrained('helpdesk_tickets')->nullOnDelete();
$table->string('hash', 80);
$table->string('short_hash', 16);
$table->string('branch', 120)->nullable();
$table->string('author_name', 160)->nullable();
$table->string('author_email', 190)->nullable();
$table->text('message');
$table->string('url', 500)->nullable();
$table->timestamp('committed_at')->nullable();
$table->timestamps();
$table->unique(['repository_id', 'hash']);
$table->index(['ticket_id', 'committed_at']);
});
Schema::create('helpdesk_workflow_templates', static function (Blueprint $table): void {
$table->id();
$table->string('name', 160);
$table->string('event', 80);
$table->string('subject', 220);
$table->longText('html_body');
$table->longText('text_body')->nullable();
$table->string('to_role', 40)->default('requester');
$table->string('cc', 500)->nullable();
$table->string('bcc', 500)->nullable();
$table->boolean('is_active')->default(true);
$table->timestamps();
$table->index(['event', 'is_active']);
});
Schema::create('helpdesk_workflow_logs', static function (Blueprint $table): void {
$table->id();
$table->foreignId('template_id')->nullable()->constrained('helpdesk_workflow_templates')->nullOnDelete();
$table->foreignId('ticket_id')->nullable()->constrained('helpdesk_tickets')->nullOnDelete();
$table->string('event', 80);
$table->string('recipient', 190)->nullable();
$table->string('subject', 220)->nullable();
$table->string('status', 40)->default('pending');
$table->text('error')->nullable();
$table->timestamps();
$table->index(['ticket_id', 'event']);
$table->index(['status', 'created_at']);
});
}
};
@@ -0,0 +1,40 @@
<?php
declare(strict_types=1);
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
return new class extends Migration
{
public function up(): void
{
Schema::create('helpdesk_comments', static function (Blueprint $table): void {
$table->id();
$table->foreignId('ticket_id')->constrained('helpdesk_tickets')->cascadeOnDelete();
$table->foreignId('parent_id')->nullable()->constrained('helpdesk_comments')->cascadeOnDelete();
$table->foreignId('user_id')->nullable()->constrained('users')->nullOnDelete();
$table->longText('body');
$table->boolean('is_internal')->default(false);
$table->timestamps();
$table->index(['ticket_id', 'parent_id', 'created_at']);
});
Schema::create('helpdesk_attachments', static function (Blueprint $table): void {
$table->id();
$table->foreignId('ticket_id')->constrained('helpdesk_tickets')->cascadeOnDelete();
$table->foreignId('comment_id')->nullable()->constrained('helpdesk_comments')->cascadeOnDelete();
$table->foreignId('user_id')->nullable()->constrained('users')->nullOnDelete();
$table->string('disk', 80);
$table->string('path', 500);
$table->string('original_name', 255);
$table->string('mime_type', 160)->nullable();
$table->unsignedBigInteger('size')->default(0);
$table->timestamps();
$table->index(['ticket_id', 'comment_id', 'created_at']);
});
}
};
@@ -0,0 +1,21 @@
<?php
declare(strict_types=1);
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
return new class extends Migration
{
public function up(): void
{
Schema::create('helpdesk_category_user', static function (Blueprint $table): void {
$table->id();
$table->foreignId('category_id')->constrained('helpdesk_categories')->cascadeOnDelete();
$table->foreignId('user_id')->constrained('users')->cascadeOnDelete();
$table->timestamps();
$table->unique(['category_id', 'user_id']);
});
}
};
@@ -0,0 +1,39 @@
<?php
declare(strict_types=1);
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
return new class extends Migration
{
/**
* Run the migrations.
*/
public function up(): void
{
if (! Schema::hasTable('helpdesk_kanban_boards')) {
Schema::create('helpdesk_kanban_boards', function (Blueprint $table): void {
$table->id();
$table->string('name');
$table->string('slug')->unique();
$table->text('description')->nullable();
$table->boolean('is_default')->default(false);
$table->unsignedInteger('sort_order')->default(0);
$table->timestamps();
$table->softDeletes();
$table->index(['is_default', 'sort_order']);
});
}
}
/**
* Reverse the migrations.
*/
public function down(): void
{
Schema::dropIfExists('helpdesk_kanban_boards');
}
};
@@ -0,0 +1,31 @@
<?php
declare(strict_types=1);
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
return new class extends Migration
{
public function up(): void
{
if (! Schema::hasTable('helpdesk_kanban_board_statuses')) {
Schema::create('helpdesk_kanban_board_statuses', function (Blueprint $table): void {
$table->id();
$table->foreignId('board_id')->constrained('helpdesk_kanban_boards')->cascadeOnDelete();
$table->foreignId('status_id')->constrained('helpdesk_statuses')->cascadeOnDelete();
$table->unsignedInteger('sort_order')->default(0);
$table->timestamps();
$table->unique(['board_id', 'status_id']);
$table->index(['board_id', 'sort_order']);
});
}
}
public function down(): void
{
Schema::dropIfExists('helpdesk_kanban_board_statuses');
}
};
@@ -0,0 +1,234 @@
<?php
declare(strict_types=1);
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
return new class extends Migration
{
public function up(): void
{
if (! Schema::hasTable('helpdesk_products')) {
Schema::create('helpdesk_products', static function (Blueprint $table): void {
$table->id();
$table->string('name', 160);
$table->string('slug', 180);
$table->string('type', 60)->default('service');
$table->string('version', 80)->nullable();
$table->string('release_channel', 80)->nullable();
$table->text('description')->nullable();
$table->boolean('is_active')->default(true);
$table->timestamps();
$table->unique('slug');
});
}
if (! Schema::hasTable('helpdesk_sla_policies')) {
Schema::create('helpdesk_sla_policies', static function (Blueprint $table): void {
$table->id();
$table->foreignId('priority_id')->nullable()->constrained('helpdesk_priorities')->nullOnDelete();
$table->foreignId('category_id')->nullable()->constrained('helpdesk_categories')->nullOnDelete();
$table->string('name', 160);
$table->string('slug', 180);
$table->unsignedInteger('first_response_minutes')->default(240);
$table->unsignedInteger('resolution_minutes')->default(2880);
$table->boolean('business_hours_only')->default(false);
$table->boolean('is_default')->default(false);
$table->boolean('is_active')->default(true);
$table->timestamps();
$table->unique('slug');
$table->index(['priority_id', 'category_id', 'is_active'], 'hd_sla_priority_category_active_idx');
});
}
Schema::table('helpdesk_tickets', static function (Blueprint $table): void {
if (! Schema::hasColumn('helpdesk_tickets', 'sla_policy_id')) {
$table->foreignId('sla_policy_id')->nullable()->after('team_id')->constrained('helpdesk_sla_policies')->nullOnDelete();
}
if (! Schema::hasColumn('helpdesk_tickets', 'product_id')) {
$table->foreignId('product_id')->nullable()->after('project_id')->constrained('helpdesk_products')->nullOnDelete();
}
if (! Schema::hasColumn('helpdesk_tickets', 'first_response_at')) {
$table->timestamp('first_response_at')->nullable()->after('due_at');
}
if (! Schema::hasColumn('helpdesk_tickets', 'sla_first_response_due_at')) {
$table->timestamp('sla_first_response_due_at')->nullable()->after('first_response_at');
}
if (! Schema::hasColumn('helpdesk_tickets', 'sla_resolution_due_at')) {
$table->timestamp('sla_resolution_due_at')->nullable()->after('sla_first_response_due_at');
}
if (! Schema::hasColumn('helpdesk_tickets', 'sla_first_response_breached_at')) {
$table->timestamp('sla_first_response_breached_at')->nullable()->after('sla_resolution_due_at');
}
if (! Schema::hasColumn('helpdesk_tickets', 'sla_resolution_breached_at')) {
$table->timestamp('sla_resolution_breached_at')->nullable()->after('sla_first_response_breached_at');
}
if (! Schema::hasColumn('helpdesk_tickets', 'public_replies_count')) {
$table->unsignedInteger('public_replies_count')->default(0)->after('sla_resolution_breached_at');
}
if (! Schema::hasColumn('helpdesk_tickets', 'time_spent_minutes')) {
$table->unsignedInteger('time_spent_minutes')->default(0)->after('public_replies_count');
}
});
if (! Schema::hasIndex('helpdesk_tickets', ['sla_policy_id', 'sla_resolution_due_at'])) {
Schema::table('helpdesk_tickets', static function (Blueprint $table): void {
$table->index(['sla_policy_id', 'sla_resolution_due_at'], 'hd_tickets_sla_due_idx');
});
}
if (! Schema::hasIndex('helpdesk_tickets', ['product_id', 'completed_at'])) {
Schema::table('helpdesk_tickets', static function (Blueprint $table): void {
$table->index(['product_id', 'completed_at'], 'hd_tickets_product_closed_idx');
});
}
Schema::table('helpdesk_workflow_logs', static function (Blueprint $table): void {
if (! Schema::hasColumn('helpdesk_workflow_logs', 'tracking_token')) {
$table->string('tracking_token', 64)->nullable()->unique()->after('status');
}
if (! Schema::hasColumn('helpdesk_workflow_logs', 'first_opened_at')) {
$table->timestamp('first_opened_at')->nullable()->after('tracking_token');
}
if (! Schema::hasColumn('helpdesk_workflow_logs', 'last_opened_at')) {
$table->timestamp('last_opened_at')->nullable()->after('first_opened_at');
}
if (! Schema::hasColumn('helpdesk_workflow_logs', 'opened_count')) {
$table->unsignedInteger('opened_count')->default(0)->after('last_opened_at');
}
});
if (! Schema::hasTable('helpdesk_canned_responses')) {
Schema::create('helpdesk_canned_responses', static function (Blueprint $table): void {
$table->id();
$table->foreignId('category_id')->nullable()->constrained('helpdesk_categories')->nullOnDelete();
$table->foreignId('team_id')->nullable()->constrained('helpdesk_teams')->nullOnDelete();
$table->string('name', 160);
$table->string('slug', 180);
$table->string('visibility', 40)->default('agents');
$table->string('subject', 220)->nullable();
$table->longText('body');
$table->boolean('is_active')->default(true);
$table->unsignedInteger('sort_order')->default(0);
$table->timestamps();
$table->unique('slug');
$table->index(['category_id', 'team_id', 'is_active'], 'hd_canned_category_team_active_idx');
});
}
if (! Schema::hasTable('helpdesk_automation_rules')) {
Schema::create('helpdesk_automation_rules', static function (Blueprint $table): void {
$table->id();
$table->string('name', 160);
$table->string('slug', 180);
$table->string('match_sender', 190)->nullable();
$table->string('match_subject', 220)->nullable();
$table->string('match_mailbox', 190)->nullable();
$table->foreignId('match_category_id')->nullable()->constrained('helpdesk_categories')->nullOnDelete();
$table->foreignId('set_priority_id')->nullable()->constrained('helpdesk_priorities')->nullOnDelete();
$table->foreignId('set_category_id')->nullable()->constrained('helpdesk_categories')->nullOnDelete();
$table->foreignId('set_team_id')->nullable()->constrained('helpdesk_teams')->nullOnDelete();
$table->foreignId('set_project_id')->nullable()->constrained('helpdesk_projects')->nullOnDelete();
$table->foreignId('set_status_id')->nullable()->constrained('helpdesk_statuses')->nullOnDelete();
$table->foreignId('set_agent_id')->nullable()->constrained('users')->nullOnDelete();
$table->boolean('is_active')->default(true);
$table->unsignedInteger('sort_order')->default(0);
$table->timestamps();
$table->unique('slug');
$table->index(['is_active', 'sort_order']);
});
}
if (! Schema::hasTable('helpdesk_ticket_sla_events')) {
Schema::create('helpdesk_ticket_sla_events', static function (Blueprint $table): void {
$table->id();
$table->foreignId('ticket_id')->constrained('helpdesk_tickets')->cascadeOnDelete();
$table->foreignId('sla_policy_id')->nullable()->constrained('helpdesk_sla_policies')->nullOnDelete();
$table->string('event', 80);
$table->timestamp('due_at')->nullable();
$table->timestamp('occurred_at')->nullable();
$table->boolean('breached')->default(false);
$table->json('meta')->nullable();
$table->timestamps();
$table->index(['ticket_id', 'event']);
$table->index(['breached', 'due_at']);
});
}
if (! Schema::hasTable('helpdesk_ticket_ratings')) {
Schema::create('helpdesk_ticket_ratings', static function (Blueprint $table): void {
$table->id();
$table->foreignId('ticket_id')->constrained('helpdesk_tickets')->cascadeOnDelete();
$table->foreignId('agent_id')->nullable()->constrained('users')->nullOnDelete();
$table->foreignId('team_id')->nullable()->constrained('helpdesk_teams')->nullOnDelete();
$table->foreignId('category_id')->nullable()->constrained('helpdesk_categories')->nullOnDelete();
$table->unsignedTinyInteger('rating');
$table->string('token', 80)->unique();
$table->text('comment')->nullable();
$table->timestamp('rated_at')->nullable();
$table->timestamps();
$table->index(['agent_id', 'rating']);
$table->index(['team_id', 'category_id']);
});
}
if (! Schema::hasTable('helpdesk_tags')) {
Schema::create('helpdesk_tags', static function (Blueprint $table): void {
$table->id();
$table->string('name', 80);
$table->string('slug', 100);
$table->string('color', 30)->default('zinc');
$table->timestamps();
$table->unique('slug');
});
}
if (! Schema::hasTable('helpdesk_ticket_tag')) {
Schema::create('helpdesk_ticket_tag', static function (Blueprint $table): void {
$table->foreignId('ticket_id')->constrained('helpdesk_tickets')->cascadeOnDelete();
$table->foreignId('tag_id')->constrained('helpdesk_tags')->cascadeOnDelete();
$table->primary(['ticket_id', 'tag_id']);
});
}
if (! Schema::hasTable('helpdesk_checklist_templates')) {
Schema::create('helpdesk_checklist_templates', static function (Blueprint $table): void {
$table->id();
$table->foreignId('category_id')->nullable()->constrained('helpdesk_categories')->nullOnDelete();
$table->foreignId('project_id')->nullable()->constrained('helpdesk_projects')->nullOnDelete();
$table->string('name', 160);
$table->string('slug', 180);
$table->json('items');
$table->boolean('is_active')->default(true);
$table->timestamps();
$table->unique('slug');
});
}
if (! Schema::hasIndex('helpdesk_checklist_templates', ['category_id', 'project_id', 'is_active'])) {
Schema::table('helpdesk_checklist_templates', static function (Blueprint $table): void {
$table->index(['category_id', 'project_id', 'is_active'], 'hd_checklist_tpl_scope_active_idx');
});
}
if (! Schema::hasTable('helpdesk_checklist_items')) {
Schema::create('helpdesk_checklist_items', static function (Blueprint $table): void {
$table->id();
$table->foreignId('ticket_id')->constrained('helpdesk_tickets')->cascadeOnDelete();
$table->foreignId('template_id')->nullable()->constrained('helpdesk_checklist_templates')->nullOnDelete();
$table->string('title', 220);
$table->text('description')->nullable();
$table->boolean('is_completed')->default(false);
$table->foreignId('completed_by')->nullable()->constrained('users')->nullOnDelete();
$table->timestamp('completed_at')->nullable();
$table->unsignedInteger('sort_order')->default(0);
$table->timestamps();
$table->index(['ticket_id', 'is_completed']);
});
}
}
};
@@ -0,0 +1,39 @@
<?php
declare(strict_types=1);
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
return new class extends Migration
{
/**
* Add auditable Git webhook ingestion support.
*/
public function up(): void
{
if (Schema::hasTable('helpdesk_git_webhook_logs')) {
return;
}
Schema::create('helpdesk_git_webhook_logs', static function (Blueprint $table): void {
$table->id();
$table->foreignId('repository_id')->constrained('helpdesk_git_repositories')->cascadeOnDelete();
$table->string('provider', 40);
$table->string('delivery_id', 190)->nullable();
$table->string('event', 120)->nullable();
$table->string('status', 40)->default('accepted');
$table->string('message', 500)->nullable();
$table->unsignedInteger('commit_count')->default(0);
$table->unsignedInteger('linked_count')->default(0);
$table->unsignedInteger('ignored_count')->default(0);
$table->json('payload')->nullable();
$table->timestamp('received_at')->nullable();
$table->timestamps();
$table->index(['repository_id', 'status', 'received_at'], 'hd_git_webhook_repo_status_received_idx');
$table->index(['provider', 'event']);
});
}
};
@@ -0,0 +1,48 @@
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
return new class extends Migration
{
/**
* Run the migrations.
*/
public function up(): void
{
foreach ([
'helpdesk_statuses',
'helpdesk_priorities',
'helpdesk_categories',
'helpdesk_projects',
'helpdesk_teams',
] as $table) {
if (! Schema::hasColumn($table, 'is_default')) {
Schema::table($table, function (Blueprint $table): void {
$table->boolean('is_default')->default(false);
});
}
}
}
/**
* Reverse the migrations.
*/
public function down(): void
{
foreach ([
'helpdesk_statuses',
'helpdesk_priorities',
'helpdesk_categories',
'helpdesk_projects',
'helpdesk_teams',
] as $tableName) {
if (Schema::hasColumn($tableName, 'is_default')) {
Schema::table($tableName, function (Blueprint $table): void {
$table->dropColumn('is_default');
});
}
}
}
};
@@ -0,0 +1,50 @@
<?php
declare(strict_types=1);
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
return new class extends Migration
{
/**
* Run the migrations.
*/
public function up(): void
{
foreach ([
'helpdesk_statuses',
'helpdesk_priorities',
'helpdesk_categories',
'helpdesk_projects',
'helpdesk_teams',
] as $tableName) {
if (Schema::hasTable($tableName) && ! Schema::hasColumn($tableName, 'is_default')) {
Schema::table($tableName, static function (Blueprint $table): void {
$table->boolean('is_default')->default(false);
});
}
}
}
/**
* Reverse the migrations.
*/
public function down(): void
{
foreach ([
'helpdesk_statuses',
'helpdesk_priorities',
'helpdesk_categories',
'helpdesk_projects',
'helpdesk_teams',
] as $tableName) {
if (Schema::hasTable($tableName) && Schema::hasColumn($tableName, 'is_default')) {
Schema::table($tableName, static function (Blueprint $table): void {
$table->dropColumn('is_default');
});
}
}
}
};
@@ -0,0 +1,76 @@
<?php
declare(strict_types=1);
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Schema;
return new class extends Migration
{
/**
* Add single-board ticket assignment and board ownership.
*/
public function up(): void
{
if (Schema::hasTable('helpdesk_kanban_boards') && ! Schema::hasColumn('helpdesk_kanban_boards', 'owner_id')) {
Schema::table('helpdesk_kanban_boards', static function (Blueprint $table): void {
$table->foreignId('owner_id')
->nullable()
->after('description')
->constrained('users')
->nullOnDelete();
$table->index('owner_id');
});
}
if (Schema::hasTable('helpdesk_tickets') && ! Schema::hasColumn('helpdesk_tickets', 'board_id')) {
Schema::table('helpdesk_tickets', static function (Blueprint $table): void {
$table->foreignId('board_id')
->nullable()
->after('status_id')
->constrained('helpdesk_kanban_boards')
->nullOnDelete();
$table->index(['board_id', 'status_id']);
});
}
if (Schema::hasTable('helpdesk_tickets') && Schema::hasTable('helpdesk_kanban_boards') && Schema::hasColumn('helpdesk_tickets', 'board_id')) {
$defaultBoardId = DB::table('helpdesk_kanban_boards')
->where('is_default', true)
->orderBy('sort_order')
->value('id') ?: DB::table('helpdesk_kanban_boards')->orderBy('sort_order')->value('id');
if ($defaultBoardId !== null) {
DB::table('helpdesk_tickets')
->whereNull('board_id')
->update(['board_id' => $defaultBoardId]);
}
}
}
/**
* Reverse the migration.
*/
public function down(): void
{
if (Schema::hasTable('helpdesk_tickets') && Schema::hasColumn('helpdesk_tickets', 'board_id')) {
Schema::table('helpdesk_tickets', static function (Blueprint $table): void {
$table->dropForeign(['board_id']);
$table->dropIndex(['board_id', 'status_id']);
$table->dropColumn('board_id');
});
}
if (Schema::hasTable('helpdesk_kanban_boards') && Schema::hasColumn('helpdesk_kanban_boards', 'owner_id')) {
Schema::table('helpdesk_kanban_boards', static function (Blueprint $table): void {
$table->dropForeign(['owner_id']);
$table->dropIndex(['owner_id']);
$table->dropColumn('owner_id');
});
}
}
};
@@ -0,0 +1,60 @@
<?php
declare(strict_types=1);
use Illuminate\Database\Migrations\Migration;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Schema;
return new class extends Migration
{
/**
* Ensure existing tickets have one concrete Kanban board assignment.
*/
public function up(): void
{
if (! Schema::hasTable('helpdesk_tickets') || ! Schema::hasColumn('helpdesk_tickets', 'board_id')) {
return;
}
if (! Schema::hasTable('helpdesk_kanban_boards')) {
return;
}
$defaultBoardId = DB::table('helpdesk_kanban_boards')
->where('is_default', true)
->orderBy('sort_order')
->value('id') ?: DB::table('helpdesk_kanban_boards')->orderBy('sort_order')->value('id');
if ($defaultBoardId === null) {
return;
}
DB::table('helpdesk_tickets')
->whereNull('board_id')
->update([
'board_id' => $defaultBoardId,
'updated_at' => now(),
]);
DB::table('helpdesk_tickets')
->whereNotNull('board_id')
->whereNotExists(function ($query): void {
$query->selectRaw('1')
->from('helpdesk_kanban_boards')
->whereColumn('helpdesk_kanban_boards.id', 'helpdesk_tickets.board_id');
})
->update([
'board_id' => $defaultBoardId,
'updated_at' => now(),
]);
}
/**
* This data repair is intentionally not reversed.
*/
public function down(): void
{
// No-op: do not remove repaired board ownership from existing tickets.
}
};
@@ -0,0 +1,65 @@
<?php
declare(strict_types=1);
use Illuminate\Database\Migrations\Migration;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Schema;
return new class extends Migration
{
/**
* Backfill status columns for every Kanban board that currently has no columns.
*/
public function up(): void
{
if (! Schema::hasTable('helpdesk_kanban_boards')
|| ! Schema::hasTable('helpdesk_kanban_board_statuses')
|| ! Schema::hasTable('helpdesk_statuses')) {
return;
}
$statuses = DB::table('helpdesk_statuses')
->orderBy('sort_order')
->orderBy('name')
->get(['id']);
if ($statuses->isEmpty()) {
return;
}
$boards = DB::table('helpdesk_kanban_boards')
->orderBy('sort_order')
->orderBy('name')
->get(['id']);
foreach ($boards as $board) {
$hasColumns = DB::table('helpdesk_kanban_board_statuses')
->where('board_id', $board->id)
->exists();
if ($hasColumns) {
continue;
}
foreach ($statuses as $index => $status) {
DB::table('helpdesk_kanban_board_statuses')->updateOrInsert(
['board_id' => $board->id, 'status_id' => $status->id],
[
'sort_order' => ($index + 1) * 10,
'created_at' => now(),
'updated_at' => now(),
],
);
}
}
}
/**
* The migration only repairs missing board columns and is intentionally not destructive.
*/
public function down(): void
{
// No-op. Do not remove configured board columns.
}
};
@@ -0,0 +1,132 @@
<?php
declare(strict_types=1);
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Schema;
use Illuminate\Support\Str;
use Modules\Helpdesk\Models\Comment;
return new class extends Migration
{
public function up(): void
{
Schema::table('helpdesk_comments', static function (Blueprint $table): void {
if (! Schema::hasColumn('helpdesk_comments', 'guid')) {
$table->uuid('guid')->nullable()->after('id');
}
if (! Schema::hasColumn('helpdesk_comments', 'unid')) {
$table->string('unid', 64)->nullable()->after('guid');
}
if (! Schema::hasColumn('helpdesk_comments', 'replica_id')) {
$table->string('replica_id', 64)->nullable()->after('unid');
}
if (! Schema::hasColumn('helpdesk_comments', 'parent_unid')) {
$table->string('parent_unid', 64)->nullable()->after('parent_id');
}
if (! Schema::hasColumn('helpdesk_comments', 'root_unid')) {
$table->string('root_unid', 64)->nullable()->after('parent_unid');
}
if (! Schema::hasColumn('helpdesk_comments', 'originator_id')) {
$table->string('originator_id', 96)->nullable()->after('root_unid');
}
if (! Schema::hasColumn('helpdesk_comments', 'sync_sequence')) {
$table->unsignedBigInteger('sync_sequence')->default(1)->after('originator_id');
}
if (! Schema::hasColumn('helpdesk_comments', 'sync_hash')) {
$table->string('sync_hash', 128)->nullable()->after('sync_sequence');
}
if (! Schema::hasColumn('helpdesk_comments', 'source_system')) {
$table->string('source_system', 80)->default('thredary-helpdesk')->after('sync_hash');
}
if (! Schema::hasColumn('helpdesk_comments', 'source_record_id')) {
$table->string('source_record_id', 120)->nullable()->after('source_system');
}
if (! Schema::hasColumn('helpdesk_comments', 'body_text')) {
$table->text('body_text')->nullable()->after('body');
}
if (! Schema::hasColumn('helpdesk_comments', 'replicated_at')) {
$table->timestamp('replicated_at')->nullable()->after('updated_at');
}
if (! Schema::hasColumn('helpdesk_comments', 'synced_at')) {
$table->timestamp('synced_at')->nullable()->after('replicated_at');
}
if (! Schema::hasColumn('helpdesk_comments', 'tombstoned_at')) {
$table->timestamp('tombstoned_at')->nullable()->after('synced_at');
}
});
Comment::query()->orderBy('id')->chunkById(100, static function ($comments): void {
foreach ($comments as $comment) {
$guid = $comment->guid ?: (string) Str::uuid();
$unid = $comment->unid ?: strtoupper(hash('sha256', $guid.'|'.$comment->ticket_id.'|'.$comment->id));
$parent = $comment->parent_id ? Comment::query()->find($comment->parent_id) : null;
$parentUnid = $parent?->unid;
$rootUnid = $parent?->root_unid ?: $parentUnid ?: $unid;
DB::table('helpdesk_comments')
->where('id', $comment->id)
->update([
'guid' => $guid,
'unid' => $unid,
'parent_unid' => $parentUnid,
'root_unid' => $rootUnid,
'originator_id' => $comment->originator_id ?: $unid.':1',
'sync_sequence' => $comment->sync_sequence ?: 1,
'sync_hash' => hash('sha256', (string) $comment->body),
'body_text' => trim(strip_tags((string) $comment->body)),
'source_system' => $comment->source_system ?: 'thredary-helpdesk',
]);
}
});
Schema::table('helpdesk_comments', static function (Blueprint $table): void {
$table->unique('guid', 'helpdesk_comments_guid_unique');
$table->unique('unid', 'helpdesk_comments_unid_unique');
$table->index(['ticket_id', 'root_unid', 'parent_unid'], 'helpdesk_comments_lotus_thread_index');
$table->index(['ticket_id', 'is_internal', 'created_at'], 'helpdesk_comments_visibility_index');
});
}
public function down(): void
{
Schema::table('helpdesk_comments', static function (Blueprint $table): void {
$table->dropUnique('helpdesk_comments_guid_unique');
$table->dropUnique('helpdesk_comments_unid_unique');
$table->dropIndex('helpdesk_comments_lotus_thread_index');
$table->dropIndex('helpdesk_comments_visibility_index');
$table->dropColumn([
'guid',
'unid',
'replica_id',
'parent_unid',
'root_unid',
'originator_id',
'sync_sequence',
'sync_hash',
'source_system',
'source_record_id',
'body_text',
'replicated_at',
'synced_at',
'tombstoned_at',
]);
});
}
};
@@ -0,0 +1,53 @@
<?php
declare(strict_types=1);
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Schema;
use Illuminate\Support\Str;
return new class extends Migration
{
public function up(): void
{
Schema::table('helpdesk_comments', static function (Blueprint $table): void {
if (! Schema::hasColumn('helpdesk_comments', 'subject')) {
$table->string('subject', 180)->nullable()->after('user_id');
}
});
DB::table('helpdesk_comments')
->whereNull('subject')
->orWhere('subject', '')
->orderBy('id')
->select(['id', 'parent_id', 'body', 'body_text', 'is_internal'])
->chunkById(100, static function ($comments): void {
foreach ($comments as $comment) {
$plain = trim(preg_replace('/\s+/', ' ', strip_tags((string) ($comment->body_text ?: $comment->body))) ?: '');
$subject = (bool) $comment->is_internal
? 'Internal note'
: ((int) ($comment->parent_id ?? 0) > 0 ? 'RE: ' : '').Str::limit($plain !== '' ? $plain : 'Response', 120, '');
DB::table('helpdesk_comments')
->where('id', $comment->id)
->update(['subject' => $subject]);
}
});
Schema::table('helpdesk_comments', static function (Blueprint $table): void {
$table->index(['ticket_id', 'subject'], 'helpdesk_comments_ticket_subject_index');
});
}
public function down(): void
{
Schema::table('helpdesk_comments', static function (Blueprint $table): void {
if (Schema::hasColumn('helpdesk_comments', 'subject')) {
$table->dropIndex('helpdesk_comments_ticket_subject_index');
$table->dropColumn('subject');
}
});
}
};
@@ -0,0 +1,40 @@
<?php
declare(strict_types=1);
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
return new class extends Migration
{
public function up(): void
{
Schema::table('helpdesk_comments', static function (Blueprint $table): void {
if (! Schema::hasColumn('helpdesk_comments', 'tombstoned_at')) {
$table->timestamp('tombstoned_at')->nullable()->after('synced_at');
}
if (! Schema::hasColumn('helpdesk_comments', 'deleted_by')) {
$table->foreignId('deleted_by')->nullable()->after('tombstoned_at')->constrained('users')->nullOnDelete();
}
if (! Schema::hasColumn('helpdesk_comments', 'delete_reason')) {
$table->string('delete_reason', 255)->nullable()->after('deleted_by');
}
});
}
public function down(): void
{
Schema::table('helpdesk_comments', static function (Blueprint $table): void {
if (Schema::hasColumn('helpdesk_comments', 'deleted_by')) {
$table->dropConstrainedForeignId('deleted_by');
}
if (Schema::hasColumn('helpdesk_comments', 'delete_reason')) {
$table->dropColumn('delete_reason');
}
});
}
};
@@ -0,0 +1,41 @@
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
return new class extends Migration
{
/**
* Run the migrations.
*/
public function up(): void
{
if (Schema::hasTable('helpdesk_ticket_time_logs')) {
return;
}
Schema::create('helpdesk_ticket_time_logs', function (Blueprint $table): void {
$table->id();
$table->foreignId('ticket_id')->constrained('helpdesk_tickets')->cascadeOnDelete();
$table->foreignId('user_id')->constrained('users')->cascadeOnDelete();
$table->timestamp('started_at');
$table->timestamp('ended_at')->nullable();
$table->unsignedInteger('duration_seconds')->default(0);
$table->unsignedInteger('minutes')->default(0);
$table->text('description')->nullable();
$table->timestamps();
$table->index(['ticket_id', 'user_id', 'ended_at'], 'helpdesk_timer_active_idx');
$table->index(['ticket_id', 'started_at'], 'helpdesk_timer_ticket_started_idx');
});
}
/**
* Reverse the migrations.
*/
public function down(): void
{
Schema::dropIfExists('helpdesk_ticket_time_logs');
}
};
@@ -0,0 +1,41 @@
<?php
declare(strict_types=1);
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
return new class extends Migration
{
/**
* Run the migrations.
*/
public function up(): void
{
if (Schema::hasTable('helpdesk_time_logs')) {
return;
}
Schema::create('helpdesk_time_logs', function (Blueprint $table): void {
$table->id();
$table->foreignId('ticket_id')->constrained('helpdesk_tickets')->cascadeOnDelete();
$table->foreignId('user_id')->constrained('users')->cascadeOnDelete();
$table->unsignedInteger('minutes');
$table->text('description')->nullable();
$table->timestamp('logged_at')->useCurrent();
$table->timestamps();
$table->index(['ticket_id', 'logged_at']);
$table->index(['user_id', 'logged_at']);
});
}
/**
* Reverse the migrations.
*/
public function down(): void
{
Schema::dropIfExists('helpdesk_time_logs');
}
};
@@ -0,0 +1,62 @@
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
return new class extends Migration
{
/**
* Add persistent timer fields to the existing helpdesk time log table.
*/
public function up(): void
{
foreach (['helpdesk_time_logs', 'time_logs'] as $tableName) {
if (! Schema::hasTable($tableName)) {
continue;
}
Schema::table($tableName, function (Blueprint $table) use ($tableName): void {
if (! Schema::hasColumn($tableName, 'started_at')) {
$startedAt = $table->timestamp('started_at')->nullable();
if (Schema::hasColumn($tableName, 'logged_at')) {
$startedAt->after('logged_at');
}
}
if (! Schema::hasColumn($tableName, 'ended_at')) {
$endedAt = $table->timestamp('ended_at')->nullable();
if (Schema::hasColumn($tableName, 'started_at')) {
$endedAt->after('started_at');
}
}
if (! Schema::hasColumn($tableName, 'duration_seconds')) {
$table->unsignedInteger('duration_seconds')->default(0)->after('minutes');
}
});
}
}
/**
* Remove persistent timer fields from the existing helpdesk time log table.
*/
public function down(): void
{
foreach (['helpdesk_time_logs', 'time_logs'] as $tableName) {
if (! Schema::hasTable($tableName)) {
continue;
}
Schema::table($tableName, function (Blueprint $table) use ($tableName): void {
foreach (['duration_seconds', 'ended_at', 'started_at'] as $column) {
if (Schema::hasColumn($tableName, $column)) {
$table->dropColumn($column);
}
}
});
}
}
};
@@ -0,0 +1,45 @@
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
return new class extends Migration
{
/**
* Add the logged_at timestamp used by ticket time history and timer-created entries.
*/
public function up(): void
{
if (! Schema::hasTable('helpdesk_ticket_time_logs')) {
return;
}
if (Schema::hasColumn('helpdesk_ticket_time_logs', 'logged_at')) {
return;
}
Schema::table('helpdesk_ticket_time_logs', function (Blueprint $table): void {
$table->timestamp('logged_at')->nullable()->after('ended_at')->index();
});
}
/**
* Remove the logged_at timestamp if this migration is rolled back.
*/
public function down(): void
{
if (! Schema::hasTable('helpdesk_ticket_time_logs')) {
return;
}
if (! Schema::hasColumn('helpdesk_ticket_time_logs', 'logged_at')) {
return;
}
Schema::table('helpdesk_ticket_time_logs', function (Blueprint $table): void {
$table->dropIndex(['logged_at']);
$table->dropColumn('logged_at');
});
}
};
@@ -0,0 +1,40 @@
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
return new class extends Migration
{
/**
* Add seconds precision to helpdesk ticket timer logs.
*/
public function up(): void
{
if (! Schema::hasTable('helpdesk_ticket_time_logs')) {
return;
}
Schema::table('helpdesk_ticket_time_logs', function (Blueprint $table): void {
if (! Schema::hasColumn('helpdesk_ticket_time_logs', 'seconds')) {
$table->unsignedInteger('seconds')->default(0)->after('minutes');
}
});
}
/**
* Remove seconds precision from helpdesk ticket timer logs.
*/
public function down(): void
{
if (! Schema::hasTable('helpdesk_ticket_time_logs')) {
return;
}
Schema::table('helpdesk_ticket_time_logs', function (Blueprint $table): void {
if (Schema::hasColumn('helpdesk_ticket_time_logs', 'seconds')) {
$table->dropColumn('seconds');
}
});
}
};
@@ -0,0 +1,68 @@
<?php
declare(strict_types=1);
use Illuminate\Database\Migrations\Migration;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Schema;
return new class extends Migration
{
/**
* Normalise legacy Flux/Tailwind colour names to the approved Thredary palette.
*/
public function up(): void
{
foreach ($this->lookupTables() as $table) {
if (! Schema::hasTable($table) || ! Schema::hasColumn($table, 'color')) {
continue;
}
foreach ($this->colourMap() as $legacy => $approved) {
DB::table($table)->where('color', $legacy)->update(['color' => $approved]);
}
}
}
/**
* The migration is intentionally one-way because approved colours are the new baseline.
*/
public function down(): void
{
// No-op: do not reintroduce legacy colour aliases.
}
/**
* @return list<string>
*/
private function lookupTables(): array
{
return [
'helpdesk_statuses',
'helpdesk_priorities',
'helpdesk_categories',
'helpdesk_teams',
'helpdesk_projects',
'helpdesk_tags',
];
}
/**
* @return array<string, string>
*/
private function colourMap(): array
{
return [
'emerald' => 'green',
'lime' => 'green',
'rose' => 'red',
'yellow' => 'amber',
'orange' => 'amber',
'sky' => 'blue',
'cyan' => 'blue',
'violet' => 'purple',
'slate' => 'gray',
'zinc' => 'gray',
];
}
};
@@ -0,0 +1,64 @@
<?php
declare(strict_types=1);
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
return new class extends Migration
{
/**
* Add optional attachment metadata used by previews, source viewing and audit trails.
*/
public function up(): void
{
Schema::table('helpdesk_attachments', static function (Blueprint $table): void {
if (! Schema::hasColumn('helpdesk_attachments', 'extension')) {
$table->string('extension', 32)->nullable()->after('original_name')->index();
}
if (! Schema::hasColumn('helpdesk_attachments', 'category')) {
$table->string('category', 32)->nullable()->after('extension')->index();
}
if (! Schema::hasColumn('helpdesk_attachments', 'checksum_sha256')) {
$table->string('checksum_sha256', 64)->nullable()->after('size')->index();
}
if (! Schema::hasColumn('helpdesk_attachments', 'download_count')) {
$table->unsignedInteger('download_count')->default(0)->after('checksum_sha256');
}
if (! Schema::hasColumn('helpdesk_attachments', 'last_downloaded_at')) {
$table->timestamp('last_downloaded_at')->nullable()->after('download_count');
}
if (! Schema::hasColumn('helpdesk_attachments', 'deleted_at')) {
$table->timestamp('deleted_at')->nullable()->after('last_downloaded_at')->index();
}
if (! Schema::hasColumn('helpdesk_attachments', 'deleted_by')) {
$table->foreignId('deleted_by')->nullable()->after('deleted_at')->constrained('users')->nullOnDelete();
}
});
}
/**
* Remove attachment experience metadata.
*/
public function down(): void
{
Schema::table('helpdesk_attachments', static function (Blueprint $table): void {
if (Schema::hasColumn('helpdesk_attachments', 'deleted_by')) {
$table->dropConstrainedForeignId('deleted_by');
}
foreach (['deleted_at', 'last_downloaded_at', 'download_count', 'checksum_sha256', 'category', 'extension'] as $column) {
if (Schema::hasColumn('helpdesk_attachments', $column)) {
$table->dropColumn($column);
}
}
});
}
};
@@ -0,0 +1,54 @@
<?php
declare(strict_types=1);
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
return new class extends Migration
{
public function up(): void
{
if (! Schema::hasTable('users')) {
return;
}
$addAgent = ! Schema::hasColumn('users', 'helpdesk_agent');
$addAdmin = ! Schema::hasColumn('users', 'helpdesk_admin');
if (! $addAgent && ! $addAdmin) {
return;
}
Schema::table('users', static function (Blueprint $table) use ($addAgent, $addAdmin): void {
if ($addAgent) {
$table->boolean('helpdesk_agent')->default(false)->after('password');
}
if ($addAdmin) {
$table->boolean('helpdesk_admin')->default(false)->after('helpdesk_agent');
}
});
}
public function down(): void
{
if (! Schema::hasTable('users')) {
return;
}
$drop = array_values(array_filter(
['helpdesk_agent', 'helpdesk_admin'],
static fn (string $column): bool => Schema::hasColumn('users', $column),
));
if ($drop === []) {
return;
}
Schema::table('users', static function (Blueprint $table) use ($drop): void {
$table->dropColumn($drop);
});
}
};
@@ -0,0 +1,267 @@
<?php
declare(strict_types=1);
namespace Modules\Helpdesk\Database\Seeders;
use Illuminate\Database\Seeder;
use Illuminate\Support\Facades\Schema;
use Illuminate\Support\Str;
use Modules\Helpdesk\Models\CannedResponse;
use Modules\Helpdesk\Models\Category;
use Modules\Helpdesk\Models\ChecklistTemplate;
use Modules\Helpdesk\Models\KanbanBoard;
use Modules\Helpdesk\Models\Priority;
use Modules\Helpdesk\Models\Product;
use Modules\Helpdesk\Models\Project;
use Modules\Helpdesk\Models\SlaPolicy;
use Modules\Helpdesk\Models\Status;
use Modules\Helpdesk\Models\Tag;
use Modules\Helpdesk\Models\Team;
use Modules\Helpdesk\Models\WorkflowTemplate;
final class HelpdeskDatabaseSeeder extends Seeder
{
/**
* Seed the Helpdesk module lookup data and default workflow templates.
*/
public function run(): void
{
$this->seedStatuses();
$this->seedPriorities();
$this->seedCategories();
$this->seedTeamsAndProjects();
$this->seedKanbanBoards();
$this->seedWorkflowTemplates();
$this->seedRedmineInspiredDefaults();
}
/**
* Seed ticket workflow statuses.
*/
private function seedStatuses(): void
{
Status::query()->where('is_default', true)->update(['is_default' => false]);
foreach ([
['name' => 'New', 'slug' => 'new', 'color' => 'blue', 'sort_order' => 5, 'is_closed' => false, 'is_default' => true],
['name' => 'Open', 'slug' => 'open', 'color' => 'green', 'sort_order' => 10, 'is_closed' => false, 'is_default' => false],
['name' => 'In Progress', 'slug' => 'in-progress', 'color' => 'indigo', 'sort_order' => 20, 'is_closed' => false, 'is_default' => false],
['name' => 'Waiting Customer', 'slug' => 'waiting-customer', 'color' => 'amber', 'sort_order' => 30, 'is_closed' => false, 'is_default' => false],
['name' => 'Waiting Third Party', 'slug' => 'waiting-third-party', 'color' => 'purple', 'sort_order' => 40, 'is_closed' => false, 'is_default' => false],
['name' => 'Resolved', 'slug' => 'resolved', 'color' => 'green', 'sort_order' => 80, 'is_closed' => true, 'is_default' => false],
['name' => 'Closed', 'slug' => 'closed', 'color' => 'gray', 'sort_order' => 90, 'is_closed' => true, 'is_default' => false],
['name' => 'Cancelled', 'slug' => 'cancelled', 'color' => 'red', 'sort_order' => 100, 'is_closed' => true, 'is_default' => false],
] as $row) {
Status::query()->updateOrCreate(['slug' => $row['slug']], $row);
}
}
/**
* Seed ticket priorities.
*/
private function seedPriorities(): void
{
Priority::query()->where('is_default', true)->update(['is_default' => false]);
foreach ([
['name' => 'Low', 'slug' => 'low', 'color' => 'gray', 'sort_order' => 10, 'is_default' => false],
['name' => 'Normal', 'slug' => 'normal', 'color' => 'blue', 'sort_order' => 20, 'is_default' => true],
['name' => 'High', 'slug' => 'high', 'color' => 'amber', 'sort_order' => 30, 'is_default' => false],
['name' => 'Urgent', 'slug' => 'urgent', 'color' => 'red', 'sort_order' => 40, 'is_default' => false],
['name' => 'Critical', 'slug' => 'critical', 'color' => 'red', 'sort_order' => 50, 'is_default' => false],
] as $row) {
Priority::query()->updateOrCreate(['slug' => $row['slug']], $row);
}
}
/**
* Seed ticket categories.
*/
private function seedCategories(): void
{
Category::query()
->where('is_default', true)
->update(['is_default' => false]);
foreach ([
['name' => 'Development', 'color' => 'indigo', 'description' => 'Software changes, enhancements and development work.', 'is_default' => true],
['name' => 'Systems Deployment', 'color' => 'blue', 'description' => 'Deployment, release, infrastructure and environment requests.', 'is_default' => false],
['name' => 'Concepts', 'color' => 'purple', 'description' => 'Ideas, concepts, design discussion and future improvements.', 'is_default' => false],
['name' => 'Bug', 'color' => 'red', 'description' => 'Defects, regressions and incorrect behaviour.', 'is_default' => false],
] as $row) {
Category::query()->updateOrCreate(
['slug' => Str::slug($row['name'])],
$row + ['slug' => Str::slug($row['name'])],
);
}
}
/**
* Seed the default support team and a default project.
*/
private function seedTeamsAndProjects(): void
{
Team::query()
->where('is_default', true)
->update(['is_default' => false]);
Project::query()
->where('is_default', true)
->update(['is_default' => false]);
$team = Team::query()->updateOrCreate(
['slug' => 'support'],
[
'name' => 'Support',
'slug' => 'support',
'color' => 'blue',
'description' => 'Default support team for triage, ownership and ticket routing.',
'is_default' => true,
],
);
Project::query()->updateOrCreate(
['slug' => 'support-desk'],
[
'team_id' => $team->id,
'title' => 'Support Desk',
'slug' => 'support-desk',
'color' => 'indigo',
'description' => 'Default project for operational helpdesk tickets.',
'is_default' => true,
],
);
}
/**
* Seed the default configurable Kanban board.
*/
private function seedKanbanBoards(): void
{
KanbanBoard::query()->where('is_default', true)->update(['is_default' => false]);
$board = KanbanBoard::query()->updateOrCreate(
['slug' => 'standard-support'],
[
'name' => 'Standard Support',
'slug' => 'standard-support',
'description' => 'Default Helpdesk workflow board covering new work through closure.',
'is_default' => true,
'sort_order' => 1,
],
);
$statuses = Status::query()
->whereIn('slug', ['new', 'open', 'in-progress', 'stalled', 'on-hold', 'closed'])
->orderBy('sort_order')
->get();
$sync = [];
foreach ($statuses as $index => $status) {
$sync[$status->id] = ['sort_order' => ($index + 1) * 10];
}
$board->statuses()->syncWithoutDetaching($sync);
}
/**
* Seed default professional workflow email templates.
*/
private function seedWorkflowTemplates(): void
{
foreach ([
['name' => 'Ticket raised requester notice', 'event' => 'ticket.created', 'subject' => 'Ticket {{ ticket.reference }} has been raised', 'html_body' => '<p>Hello {{ requester.name }},</p><p>Your ticket <strong>{{ ticket.reference }}</strong> has been raised successfully.</p><p><strong>Subject:</strong> {{ ticket.subject }}</p><p><strong>Priority:</strong> {{ ticket.priority }}</p>', 'to_role' => 'requester'],
['name' => 'Ticket updated requester notice', 'event' => 'ticket.updated', 'subject' => 'Ticket {{ ticket.reference }} has been updated', 'html_body' => '<p>Hello {{ requester.name }},</p><p>Your ticket <strong>{{ ticket.reference }}</strong> has been updated.</p><p><strong>Status:</strong> {{ ticket.status }}</p><p><strong>Priority:</strong> {{ ticket.priority }}</p>', 'to_role' => 'requester'],
['name' => 'Ticket response requester notice', 'event' => 'ticket.commented', 'subject' => 'New response on ticket {{ ticket.reference }}', 'html_body' => '<p>Hello {{ requester.name }},</p><p>A new response has been added to <strong>{{ ticket.reference }}</strong>.</p><p><strong>Subject:</strong> {{ ticket.subject }}</p>', 'to_role' => 'requester'],
['name' => 'Ticket closed requester notice', 'event' => 'ticket.closed', 'subject' => 'Ticket {{ ticket.reference }} has been closed', 'html_body' => '<p>Hello {{ requester.name }},</p><p>Your ticket <strong>{{ ticket.reference }}</strong> has been closed.</p><p>Thank you for using the support desk.</p>', 'to_role' => 'requester'],
] as $template) {
WorkflowTemplate::query()->updateOrCreate(
['name' => $template['name'], 'event' => $template['event']],
$template + ['text_body' => null, 'cc' => null, 'bcc' => null, 'is_active' => true],
);
}
}
/**
* Seed RedmineUP-inspired professional helpdesk defaults.
*/
private function seedRedmineInspiredDefaults(): void
{
$medium = Priority::query()->where('slug', 'medium')->first();
$bug = Category::query()->where('slug', 'bug')->first();
SlaPolicy::query()->updateOrCreate(
['slug' => 'standard-support-sla'],
[
'priority_id' => $medium?->id,
'category_id' => null,
'name' => 'Standard Support SLA',
'slug' => 'standard-support-sla',
'first_response_minutes' => 240,
'resolution_minutes' => 2880,
'business_hours_only' => false,
'is_default' => true,
'is_active' => true,
],
);
CannedResponse::query()->updateOrCreate(
['slug' => 'professional-acknowledgement'],
[
'category_id' => null,
'team_id' => null,
'name' => 'Professional acknowledgement',
'slug' => 'professional-acknowledgement',
'visibility' => 'agents',
'subject' => null,
'body' => '<p>Hello {{ requester.name }},</p><p>Thank you for the update on {{ ticket.reference }}. I am reviewing this now and will come back with the next action shortly.</p><p>Regards,<br>{{ agent.name }}</p>',
'is_active' => true,
'sort_order' => 10,
],
);
ChecklistTemplate::query()->updateOrCreate(
['slug' => 'bug-triage-checklist'],
[
'category_id' => $bug?->id,
'project_id' => null,
'name' => 'Bug triage checklist',
'slug' => 'bug-triage-checklist',
'items' => ['Confirm reproduction steps', 'Check recent deployment/build', 'Identify affected account', 'Record fix or workaround'],
'is_active' => true,
],
);
foreach ([
['name' => 'Escalated', 'slug' => 'escalated', 'color' => 'red'],
['name' => 'Waiting on customer', 'slug' => 'waiting-on-customer', 'color' => 'amber'],
] as $tag) {
Tag::query()->updateOrCreate(
['slug' => $tag['slug']],
$tag + [],
);
}
if (Schema::hasColumn('helpdesk_tags', 'is_default')) {
Tag::query()
->where('is_default', true)
->update(['is_default' => false]);
}
Product::query()->updateOrCreate(
['slug' => 'thredary'],
[
'name' => 'Thredary',
'slug' => 'thredary',
'type' => 'service',
'version' => null,
'release_channel' => 'core',
'description' => 'Default service record for Thredary platform support tickets.',
'is_active' => true,
],
);
}
}