Require package reinstall after industry change and wipe old queues.
Deploy Ladill Queue / deploy (push) Successful in 1m5s

Changing industry now flags a pending reinstall with an app banner; reinstall clears queues, tickets, and service points before applying the new template.

Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
isaacclad
2026-07-17 20:10:25 +00:00
co-authored by Cursor
parent f1edf8a19e
commit a2d5a8affd
5 changed files with 276 additions and 29 deletions
@@ -4,9 +4,19 @@ namespace App\Services\Qms\Industry;
use App\Models\Branch;
use App\Models\Counter;
use App\Models\CustomerFeedback;
use App\Models\Department;
use App\Models\Device;
use App\Models\DisplayScreen;
use App\Models\Organization;
use App\Models\QueueAppointment;
use App\Models\QueueRule;
use App\Models\ServiceQueue;
use App\Models\ServiceSession;
use App\Models\Ticket;
use App\Models\TicketEvent;
use App\Models\TicketTransfer;
use App\Models\VoiceAnnouncement;
use App\Models\Workflow;
use App\Models\WorkflowStep;
use App\Services\Qms\AuditLogger;
@@ -27,7 +37,7 @@ class IndustryPackageInstaller
) {}
/**
* @param array{skip_stages?: bool, include_optional?: bool, actor_ref?: string|null} $options
* @param array{skip_stages?: bool, include_optional?: bool, actor_ref?: string|null, replace?: bool} $options
* @return array{
* package: string,
* departments: int,
@@ -35,7 +45,8 @@ class IndustryPackageInstaller
* service_points: int,
* workflow_id: int|null,
* skipped_stages: list<string>,
* skipped_reason: string|null
* skipped_reason: string|null,
* replaced: bool
* }
*/
public function install(Organization $organization, Branch $branch, ?string $packageKey = null, array $options = []): array
@@ -49,6 +60,7 @@ class IndustryPackageInstaller
$skipStages = (bool) ($options['skip_stages'] ?? false);
$includeOptional = (bool) ($options['include_optional'] ?? true);
$replace = (bool) ($options['replace'] ?? false);
$actor = $options['actor_ref'] ?? $organization->owner_ref;
// Care (and similar) own clinical stage provisioning — still apply terminology.
@@ -64,9 +76,18 @@ class IndustryPackageInstaller
'workflow_id' => null,
'skipped_stages' => [],
'skipped_reason' => $skipStages ? 'stages_managed_by_integration' : null,
'replaced' => false,
];
return DB::transaction(function () use ($organization, $branch, $package, $key, $skipStages, $includeOptional, $actor, &$result) {
return DB::transaction(function () use ($organization, $branch, $package, $key, $skipStages, $includeOptional, $replace, $actor, &$result) {
// Replacing a package wipes queues/tickets/points so the new template is clean.
// Care-managed healthcare keeps clinical queues (skip_stages).
if ($replace && ! $skipStages) {
$this->clearOperationalData($organization);
$result['replaced'] = true;
$organization->refresh();
}
$this->applyPackageSettings($organization, $package);
$departmentsByCode = [];
@@ -122,6 +143,7 @@ class IndustryPackageInstaller
$settings = $organization->settings ?? [];
$settings['industry'] = $key;
unset($settings['industry_package_needs_reinstall']);
$settings['industry_package'] = [
'key' => $key,
'version' => (int) config('industry_packages.version', 1),
@@ -152,6 +174,78 @@ class IndustryPackageInstaller
});
}
/**
* Whether the selected industry differs from the installed package template.
*/
public static function needsReinstall(Organization $organization): bool
{
if ((bool) data_get($organization->settings, 'industry_package_needs_reinstall')) {
return true;
}
$selected = data_get($organization->settings, 'industry');
$installed = data_get($organization->settings, 'industry_package.key');
return is_string($selected)
&& $selected !== ''
&& is_string($installed)
&& $installed !== ''
&& $selected !== $installed;
}
/**
* Wipe queues, service points, tickets, workflows, and related operational data.
* Keeps organization, branches, and members.
*/
public function clearOperationalData(Organization $organization): void
{
$orgId = (int) $organization->id;
$ownerRef = (string) $organization->owner_ref;
$ticketIds = Ticket::withTrashed()->where('organization_id', $orgId)->pluck('id');
$queueIds = ServiceQueue::withTrashed()->where('organization_id', $orgId)->pluck('id');
$counterIds = Counter::withTrashed()->where('organization_id', $orgId)->pluck('id');
$workflowIds = Workflow::withTrashed()->where('organization_id', $orgId)->pluck('id');
CustomerFeedback::query()->where('organization_id', $orgId)->delete();
VoiceAnnouncement::query()->where('organization_id', $orgId)->delete();
ServiceSession::query()->where('owner_ref', $ownerRef)->delete();
TicketTransfer::query()->where('owner_ref', $ownerRef)->delete();
TicketEvent::query()->where('owner_ref', $ownerRef)->delete();
QueueAppointment::withTrashed()->where('organization_id', $orgId)->forceDelete();
if ($ticketIds->isNotEmpty()) {
Ticket::withTrashed()->whereIn('id', $ticketIds)->forceDelete();
}
QueueRule::query()->where('owner_ref', $ownerRef)->delete();
if ($workflowIds->isNotEmpty()) {
WorkflowStep::query()->whereIn('workflow_id', $workflowIds)->delete();
Workflow::withTrashed()->whereIn('id', $workflowIds)->forceDelete();
}
DisplayScreen::withTrashed()->where('organization_id', $orgId)->forceDelete();
Device::withTrashed()->where('organization_id', $orgId)->forceDelete();
DB::table('queue_staff_sessions')->where('owner_ref', $ownerRef)->delete();
DB::table('queue_counter_assignments')->where('owner_ref', $ownerRef)->delete();
if ($counterIds->isNotEmpty()) {
DB::table('queue_counter_service_queue')->whereIn('counter_id', $counterIds)->delete();
Counter::withTrashed()->whereIn('id', $counterIds)->forceDelete();
}
if ($queueIds->isNotEmpty()) {
ServiceQueue::withTrashed()->whereIn('id', $queueIds)->forceDelete();
}
Department::withTrashed()
->where('owner_ref', $ownerRef)
->whereIn('branch_id', Branch::query()->where('organization_id', $orgId)->pluck('id'))
->forceDelete();
}
protected function applyPackageSettings(Organization $organization, IndustryPackage $package): void
{
// Persisted fully after install; noop placeholder for future hooks.