Issue Queue tickets for every waiting appointment when integration is on.
Deploy Ladill Care / deploy (push) Successful in 51s
Deploy Ladill Care / deploy (push) Successful in 51s
Patient queue syncs consultation and specialty waiters; specialty contexts now provision desks so check-in/onboarding no longer leaves half the list on local positions only. Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
@@ -39,8 +39,8 @@ class QueueController extends Controller
|
||||
|
||||
$queueIntegration = ['enabled' => false];
|
||||
if ($branchId && $this->queueBridge->isEnabled($organization)) {
|
||||
// Catch up tickets for waiters created before Queue was linked (demo / pre-enable).
|
||||
$this->queueBridge->syncMissingTickets($organization, CareQueueContexts::CONSULTATION, $branchId, 50);
|
||||
// Catch up tickets for every waiting appointment (consultation + specialties).
|
||||
$this->queueBridge->syncMissingAppointmentTickets($organization, $branchId, 100);
|
||||
$queueIntegration = $this->queueBridge->listMeta($organization, CareQueueContexts::CONSULTATION, $branchId);
|
||||
}
|
||||
|
||||
|
||||
@@ -214,8 +214,17 @@ class SettingsController extends Controller
|
||||
->where('is_active', true)
|
||||
->pluck('id');
|
||||
foreach ($branches as $branchId) {
|
||||
try {
|
||||
$bridge->syncMissingAppointmentTickets($organization, (int) $branchId, 50);
|
||||
} catch (\Throwable) {
|
||||
// Continue other branches.
|
||||
}
|
||||
foreach (array_keys(CareQueueContexts::departments()) as $context) {
|
||||
if ($context === CareQueueContexts::RECEPTION) {
|
||||
if (in_array($context, [
|
||||
CareQueueContexts::RECEPTION,
|
||||
CareQueueContexts::CONSULTATION,
|
||||
CareQueueContexts::TRIAGE,
|
||||
], true)) {
|
||||
continue;
|
||||
}
|
||||
try {
|
||||
|
||||
@@ -66,15 +66,16 @@ class CareQueueBridge
|
||||
return $appointment;
|
||||
}
|
||||
|
||||
$point = $this->resolveConsultationPoint($organization, $appointment);
|
||||
$point = $this->resolveAppointmentPoint($organization, $appointment);
|
||||
|
||||
if (CareQueueContexts::usesAssignedRouting($context) && ! $point) {
|
||||
if (CareQueueContexts::usesAssignedRouting($context) && empty($point['counter_uuid'] ?? null)) {
|
||||
$appointment->forceFill([
|
||||
'queue_routing_status' => CareQueueContexts::ROUTING_UNRESOLVED,
|
||||
])->save();
|
||||
Log::info('care.queue_issue_unresolved_assignment', [
|
||||
'appointment_id' => $appointment->id,
|
||||
'practitioner_id' => $appointment->practitioner_id,
|
||||
'context' => $context,
|
||||
]);
|
||||
|
||||
return $appointment->fresh();
|
||||
@@ -280,6 +281,44 @@ class CareQueueBridge
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Issue Queue tickets for every waiting appointment at a branch (consultation + specialties).
|
||||
*/
|
||||
public function syncMissingAppointmentTickets(Organization $organization, int $branchId, int $limit = 100): int
|
||||
{
|
||||
if (! $this->isEnabled($organization) || $branchId < 1 || $limit < 1) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
$owner = (string) $organization->owner_ref;
|
||||
$appointments = Appointment::owned($owner)
|
||||
->where('organization_id', $organization->id)
|
||||
->where('branch_id', $branchId)
|
||||
->whereIn('status', [Appointment::STATUS_WAITING, Appointment::STATUS_CHECKED_IN])
|
||||
->where(function ($q) {
|
||||
$q->whereNull('queue_ticket_uuid')->orWhere('queue_ticket_uuid', '');
|
||||
})
|
||||
->with(['patient', 'organization', 'practitioner', 'visit'])
|
||||
->orderBy('queue_position')
|
||||
->orderBy('waiting_at')
|
||||
->orderBy('checked_in_at')
|
||||
->orderBy('id')
|
||||
->limit($limit)
|
||||
->get();
|
||||
|
||||
$issued = 0;
|
||||
foreach ($appointments as $appointment) {
|
||||
$updated = $this->issueForAppointment($organization, $appointment->fresh([
|
||||
'patient', 'organization', 'practitioner', 'visit',
|
||||
]));
|
||||
if ($updated && filled($updated->queue_ticket_uuid)) {
|
||||
$issued++;
|
||||
}
|
||||
}
|
||||
|
||||
return $issued;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array{ticket: ?array<string, mixed>, entity: ?Model, resources: ?array<string, mixed>}
|
||||
*/
|
||||
@@ -405,43 +444,41 @@ class CareQueueBridge
|
||||
/**
|
||||
* @return array<string, mixed>|null
|
||||
*/
|
||||
protected function resolveConsultationPoint(Organization $organization, Appointment $appointment): ?array
|
||||
protected function resolveAppointmentPoint(Organization $organization, Appointment $appointment): ?array
|
||||
{
|
||||
$branchId = (int) $appointment->branch_id;
|
||||
$context = $this->contextForAppointment($organization, $appointment);
|
||||
|
||||
// Assigned-only consultation requires a doctor — leave Unassigned until one is chosen.
|
||||
if (! $appointment->practitioner_id) {
|
||||
return null;
|
||||
$this->provisioner->ensure($organization, $context, $branchId);
|
||||
$organization = $organization->fresh() ?? $organization;
|
||||
|
||||
if ($appointment->practitioner_id) {
|
||||
$point = $this->provisioner->pointForPractitioner(
|
||||
$organization,
|
||||
$branchId,
|
||||
(int) $appointment->practitioner_id,
|
||||
$context,
|
||||
);
|
||||
if ($point && ! empty($point['counter_uuid'])) {
|
||||
return $point;
|
||||
}
|
||||
|
||||
// Refresh points for this context only, then retry the assigned desk.
|
||||
$this->provisioner->ensure($organization, $context, $branchId);
|
||||
$organization = $organization->fresh() ?? $organization;
|
||||
$point = $this->provisioner->pointForPractitioner(
|
||||
$organization,
|
||||
$branchId,
|
||||
(int) $appointment->practitioner_id,
|
||||
$context,
|
||||
);
|
||||
if ($point && ! empty($point['counter_uuid'])) {
|
||||
return $point;
|
||||
}
|
||||
}
|
||||
|
||||
$point = $this->provisioner->pointForPractitioner(
|
||||
$organization,
|
||||
$branchId,
|
||||
(int) $appointment->practitioner_id,
|
||||
);
|
||||
if ($point) {
|
||||
return $point;
|
||||
}
|
||||
|
||||
// Practitioner assigned but point not provisioned yet — refresh this branch only.
|
||||
$this->provisioner->ensure($organization, CareQueueContexts::CONSULTATION, $branchId);
|
||||
|
||||
$point = $this->provisioner->pointForPractitioner(
|
||||
$organization->fresh(),
|
||||
$branchId,
|
||||
(int) $appointment->practitioner_id,
|
||||
);
|
||||
if ($point) {
|
||||
return $point;
|
||||
}
|
||||
|
||||
// Prefer a desk ticket over leaving waiters Unassigned while counters catch up
|
||||
// after demo seed / deferred settings provision.
|
||||
return $this->provisioner->defaultPoint(
|
||||
$organization->fresh(),
|
||||
CareQueueContexts::CONSULTATION,
|
||||
$branchId,
|
||||
);
|
||||
// Prefer a desk ticket over leaving waiters without a Queue number.
|
||||
return $this->provisioner->defaultPoint($organization, $context, $branchId);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -458,7 +495,7 @@ class CareQueueBridge
|
||||
): ?array {
|
||||
if ($context === CareQueueContexts::CONSULTATION || CareQueueContexts::isSpecialty($context)) {
|
||||
if ($practitionerId) {
|
||||
return $this->provisioner->pointForPractitioner($organization, $branchId, $practitionerId)
|
||||
return $this->provisioner->pointForPractitioner($organization, $branchId, $practitionerId, $context)
|
||||
?? collect($resources['points'] ?? [])->first(
|
||||
fn ($p) => ($p['kind'] ?? '') === 'practitioner' && (int) ($p['ref_id'] ?? 0) === $practitionerId
|
||||
);
|
||||
@@ -473,7 +510,7 @@ class CareQueueBridge
|
||||
})
|
||||
->first();
|
||||
if ($prac) {
|
||||
return $this->provisioner->pointForPractitioner($organization, $branchId, $prac->id);
|
||||
return $this->provisioner->pointForPractitioner($organization, $branchId, $prac->id, $context);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -198,9 +198,21 @@ class CareQueueProvisioner
|
||||
$owner = (string) $organization->owner_ref;
|
||||
$priorByKey = collect($stub['points'] ?? [])->keyBy(fn ($p) => (string) ($p['external_key'] ?? ''));
|
||||
|
||||
$points = match ($context) {
|
||||
CareQueueContexts::CONSULTATION => $this->practitionerPoints($organization, $branch, $priorByKey),
|
||||
CareQueueContexts::TRIAGE => $this->memberRolePoints(
|
||||
$points = match (true) {
|
||||
$context === CareQueueContexts::CONSULTATION => $this->practitionerPoints(
|
||||
$organization,
|
||||
$branch,
|
||||
CareQueueContexts::CONSULTATION,
|
||||
$priorByKey,
|
||||
),
|
||||
CareQueueContexts::isSpecialty($context) => $this->practitionerPoints(
|
||||
$organization,
|
||||
$branch,
|
||||
$context,
|
||||
$priorByKey,
|
||||
data_get($organization->settings, "specialty_module_provisioning.{$context}.department_ids", []),
|
||||
),
|
||||
$context === CareQueueContexts::TRIAGE => $this->memberRolePoints(
|
||||
$organization,
|
||||
$branch,
|
||||
['nurse'],
|
||||
@@ -208,7 +220,7 @@ class CareQueueProvisioner
|
||||
'Nurse station',
|
||||
$priorByKey,
|
||||
),
|
||||
CareQueueContexts::PHARMACY => $this->memberRolePoints(
|
||||
$context === CareQueueContexts::PHARMACY => $this->memberRolePoints(
|
||||
$organization,
|
||||
$branch,
|
||||
['pharmacist'],
|
||||
@@ -216,7 +228,7 @@ class CareQueueProvisioner
|
||||
'Pharmacy counter',
|
||||
$priorByKey,
|
||||
),
|
||||
CareQueueContexts::LABORATORY, CareQueueContexts::IMAGING => $this->memberRolePoints(
|
||||
in_array($context, [CareQueueContexts::LABORATORY, CareQueueContexts::IMAGING], true) => $this->memberRolePoints(
|
||||
$organization,
|
||||
$branch,
|
||||
['lab_technician'],
|
||||
@@ -224,7 +236,7 @@ class CareQueueProvisioner
|
||||
$context === CareQueueContexts::IMAGING ? 'Imaging room' : 'Lab desk',
|
||||
$priorByKey,
|
||||
),
|
||||
CareQueueContexts::BILLING => $this->memberRolePoints(
|
||||
$context === CareQueueContexts::BILLING => $this->memberRolePoints(
|
||||
$organization,
|
||||
$branch,
|
||||
['cashier'],
|
||||
@@ -232,7 +244,7 @@ class CareQueueProvisioner
|
||||
'Cashier desk',
|
||||
$priorByKey,
|
||||
),
|
||||
CareQueueContexts::RECEPTION => $this->memberRolePoints(
|
||||
$context === CareQueueContexts::RECEPTION => $this->memberRolePoints(
|
||||
$organization,
|
||||
$branch,
|
||||
['receptionist'],
|
||||
@@ -264,23 +276,37 @@ class CareQueueProvisioner
|
||||
}
|
||||
|
||||
/**
|
||||
* @param list<int|string>|null $departmentIds
|
||||
* @param \Illuminate\Support\Collection<string, array<string, mixed>> $priorByKey
|
||||
* @return list<array<string, mixed>>
|
||||
*/
|
||||
protected function practitionerPoints(Organization $organization, Branch $branch, $priorByKey): array
|
||||
{
|
||||
protected function practitionerPoints(
|
||||
Organization $organization,
|
||||
Branch $branch,
|
||||
string $context,
|
||||
$priorByKey,
|
||||
?array $departmentIds = null,
|
||||
): array {
|
||||
$practitioners = Practitioner::owned($organization->owner_ref)
|
||||
->where('organization_id', $organization->id)
|
||||
->where('branch_id', $branch->id)
|
||||
->where('is_active', true)
|
||||
->when(
|
||||
is_array($departmentIds) && $departmentIds !== [],
|
||||
fn ($q) => $q->whereIn('department_id', array_map('intval', $departmentIds)),
|
||||
)
|
||||
->orderBy('name')
|
||||
->get();
|
||||
|
||||
$points = [];
|
||||
$roomIndex = 1;
|
||||
$roomPrefix = CareQueueContexts::isSpecialty($context)
|
||||
? (string) (config("care.specialty_modules.{$context}.label") ?? 'Room')
|
||||
: 'Consultation Room';
|
||||
|
||||
foreach ($practitioners as $practitioner) {
|
||||
$key = CareQueueContexts::pointExternalKey(
|
||||
CareQueueContexts::CONSULTATION,
|
||||
$context,
|
||||
$branch->id,
|
||||
'practitioner',
|
||||
$practitioner->id,
|
||||
@@ -288,7 +314,7 @@ class CareQueueProvisioner
|
||||
$prior = $priorByKey->get($key, []);
|
||||
$room = trim((string) ($practitioner->room ?? ''));
|
||||
if ($room === '') {
|
||||
$room = 'Consultation Room '.$roomIndex;
|
||||
$room = $roomPrefix.' '.$roomIndex;
|
||||
}
|
||||
$roomIndex++;
|
||||
|
||||
@@ -407,13 +433,17 @@ class CareQueueProvisioner
|
||||
}
|
||||
|
||||
/**
|
||||
* Resolve a service point for a practitioner (consultation) or member staff_ref.
|
||||
* Resolve a service point for a practitioner on a queue context.
|
||||
*
|
||||
* @return array<string, mixed>|null
|
||||
*/
|
||||
public function pointForPractitioner(Organization $organization, int $branchId, int $practitionerId): ?array
|
||||
{
|
||||
$resources = $this->resourcesFor($organization, CareQueueContexts::CONSULTATION, $branchId);
|
||||
public function pointForPractitioner(
|
||||
Organization $organization,
|
||||
int $branchId,
|
||||
int $practitionerId,
|
||||
string $context = CareQueueContexts::CONSULTATION,
|
||||
): ?array {
|
||||
$resources = $this->resourcesFor($organization, $context, $branchId);
|
||||
if (! $resources) {
|
||||
return null;
|
||||
}
|
||||
@@ -495,7 +525,11 @@ class CareQueueProvisioner
|
||||
if ($existing && ! empty($existing['queue_uuid']) && (
|
||||
! empty($existing['points']) || ! empty($existing['counter_uuid'])
|
||||
) && $modeMatches) {
|
||||
return $existing;
|
||||
// Specialty module stubs often only store a legacy counter — still provision
|
||||
// practitioner points so check-in / sync can assign real desks.
|
||||
if (! CareQueueContexts::isSpecialty($context) || ! empty($existing['points'])) {
|
||||
return $existing;
|
||||
}
|
||||
}
|
||||
|
||||
if (! $this->shouldProvision($organization)) {
|
||||
@@ -503,7 +537,8 @@ class CareQueueProvisioner
|
||||
}
|
||||
|
||||
if (CareQueueContexts::isSpecialty($context)) {
|
||||
return $existing;
|
||||
return $this->provisionSpecialtyContextBranch($organization->fresh(), $context, $branchId)
|
||||
?? $existing;
|
||||
}
|
||||
|
||||
if (! CareQueueContexts::isDepartment($context)) {
|
||||
@@ -515,6 +550,69 @@ class CareQueueProvisioner
|
||||
return $this->resourcesFor($organization->fresh(), $context, $branchId);
|
||||
}
|
||||
|
||||
/**
|
||||
* Provision a single specialty queue for one branch (idempotent).
|
||||
*
|
||||
* @return array<string, mixed>|null
|
||||
*/
|
||||
public function provisionSpecialtyContextBranch(Organization $organization, string $context, int $branchId): ?array
|
||||
{
|
||||
if (! CareQueueContexts::isSpecialty($context) || ! $this->shouldProvision($organization)) {
|
||||
return $this->resourcesFor($organization, $context, $branchId);
|
||||
}
|
||||
|
||||
$definition = config("care.specialty_modules.{$context}");
|
||||
if (! is_array($definition)) {
|
||||
return null;
|
||||
}
|
||||
|
||||
$branch = Branch::owned((string) $organization->owner_ref)
|
||||
->where('organization_id', $organization->id)
|
||||
->whereKey($branchId)
|
||||
->first();
|
||||
if (! $branch) {
|
||||
return null;
|
||||
}
|
||||
|
||||
$meta = [
|
||||
'name' => (string) ($definition['queue_name'] ?? $definition['label'] ?? $context),
|
||||
'prefix' => (string) ($definition['queue_prefix'] ?? strtoupper(substr($context, 0, 3))),
|
||||
'routing_mode' => 'assigned_only',
|
||||
'type' => 'consultation',
|
||||
];
|
||||
|
||||
$provisioning = (array) data_get($organization->settings, 'specialty_module_provisioning', []);
|
||||
$record = is_array($provisioning[$context] ?? null) ? $provisioning[$context] : [];
|
||||
$queues = is_array($record['queues'] ?? null) ? $record['queues'] : [];
|
||||
$byBranch = collect($queues)->keyBy(fn ($q) => (string) ($q['branch_id'] ?? ''));
|
||||
$prior = $byBranch->get((string) $branch->id, []);
|
||||
if (! is_array($prior)) {
|
||||
$prior = [];
|
||||
}
|
||||
$prior['module'] = $context;
|
||||
|
||||
$stub = $this->provisionBranchDepartment(
|
||||
$organization,
|
||||
(string) $organization->owner_ref,
|
||||
$context,
|
||||
$meta,
|
||||
$branch,
|
||||
$prior,
|
||||
);
|
||||
$byBranch->put((string) $branch->id, $stub);
|
||||
|
||||
$provisioning[$context] = array_merge($record, [
|
||||
'active' => true,
|
||||
'queues' => $byBranch->values()->all(),
|
||||
]);
|
||||
|
||||
$settings = $organization->settings ?? [];
|
||||
$settings['specialty_module_provisioning'] = $provisioning;
|
||||
$organization->update(['settings' => $settings]);
|
||||
|
||||
return $stub;
|
||||
}
|
||||
|
||||
/**
|
||||
* Provision a single department queue for one branch (idempotent).
|
||||
*/
|
||||
|
||||
@@ -186,17 +186,20 @@ class DemoTenantSeeder
|
||||
return;
|
||||
}
|
||||
|
||||
// Scoped per branch/context (not full-org) so demos finish quickly while
|
||||
// still leaving waiters with real tickets instead of "Unassigned".
|
||||
$contexts = [
|
||||
CareQueueContexts::CONSULTATION,
|
||||
CareQueueContexts::PHARMACY,
|
||||
CareQueueContexts::LABORATORY,
|
||||
CareQueueContexts::BILLING,
|
||||
];
|
||||
|
||||
// Scoped per branch (not full-org) so demos finish quickly while
|
||||
// still leaving waiters with real tickets instead of local positions only.
|
||||
foreach ($branches as $branch) {
|
||||
foreach ($contexts as $context) {
|
||||
try {
|
||||
$bridge->syncMissingAppointmentTickets($organization, (int) $branch->id, 100);
|
||||
} catch (\Throwable) {
|
||||
// Continue — page load will retry.
|
||||
}
|
||||
|
||||
foreach ([
|
||||
CareQueueContexts::PHARMACY,
|
||||
CareQueueContexts::LABORATORY,
|
||||
CareQueueContexts::BILLING,
|
||||
] as $context) {
|
||||
try {
|
||||
$bridge->syncMissingTickets($organization, $context, (int) $branch->id, 50);
|
||||
} catch (\Throwable) {
|
||||
|
||||
Reference in New Issue
Block a user