import { Type, validator, validators } from '@openmrs/esm-framework'; const columnTypes = [ 'actions', 'coming-from', 'extension', 'patient-age', 'patient-identifier', 'patient-name', 'priority', 'queue-number', 'queue', 'status', 'visit-start-time', 'wait-time', ] as const; type ColumnType = (typeof columnTypes)[number]; const queueEntryActions = ['move', 'call', 'edit', 'remove', 'delete', 'undo'] as const; export type QueueEntryAction = (typeof queueEntryActions)[number]; // Retired action names, mapped to the actions that replaced them. Still accepted in configuration and // resolved to their replacement when the action cell renders, so that older configurations keep working. export const deprecatedQueueEntryActions = { transition: 'move' } as const; export type ConfigurableQueueEntryAction = QueueEntryAction | keyof typeof deprecatedQueueEntryActions; const configurableQueueEntryActions = [...queueEntryActions, ...Object.keys(deprecatedQueueEntryActions)]; const appointmentStatusValues = [ 'Requested', 'WaitList', 'Scheduled', 'Arrived', 'CheckedIn', 'Completed', 'Cancelled', 'Missed', ] as const; export type AppointmentStatus = (typeof appointmentStatusValues)[number]; const statusIcons = ['Group', 'InProgress'] as const; type StatusIcon = (typeof statusIcons)[number]; // Options from https://react.carbondesignsystem.com/?path=/docs/components-tag--overview plus orange for priority tags const priorityTagColors = [ 'red', 'magenta', 'purple', 'blue', 'teal', 'cyan', 'gray', 'orange', 'green', 'warm-gray', 'cool-gray', 'high-contrast', 'outline', ] as const; type PriorityTagColor = (typeof priorityTagColors)[number]; const tagStyles = ['bold'] as const; type TagStyle = (typeof tagStyles)[number]; // Semantic colors used to highlight how long a patient has been waiting, mapped to Carbon color tokens in queue-duration.scss const waitTimeColors = ['red', 'orange'] as const; type WaitTimeColor = (typeof waitTimeColors)[number]; // equal to columnTypes but without extension export const builtInColumns = columnTypes.filter((columnType) => columnType !== 'extension'); const defaultIdentifierTypeUuid = '05a29f94-c0ed-11e2-94be-8c13b969e334'; // OpenMRS ID const defaultPriorityUuid = 'f4620bfa-3625-4883-bd3f-84c2cce14470'; const defaultEmergencyPriorityUuid = '04f6f7e0-e3cb-4e13-a133-4479f759574e'; const defaultUrgentPriorityUuid = 'dc3492ef-24a5-4fd9-b58d-4fd2acf7071f'; export const defaultPriorityConfig: PriorityConfig[] = [ { conceptUuid: defaultEmergencyPriorityUuid, style: null, color: 'red', }, { conceptUuid: defaultPriorityUuid, style: null, color: 'green', }, { conceptUuid: defaultUrgentPriorityUuid, style: null, color: 'orange', }, ]; export const defaultColumnConfig: ColumnConfig = { actions: { buttons: ['call'], overflowMenu: ['move', 'edit', 'remove', 'undo'], }, identifierTypeUuid: defaultIdentifierTypeUuid, statusConfigs: [], visitQueueNumberAttributeUuid: null, }; export const defaultQueueTable: TableDefinitions = { columns: ['patient-name', 'coming-from', 'priority', 'queue-number', 'queue', 'wait-time', 'actions'], appliedTo: [{ queue: '', status: '' }], }; export const configSchema = { priorityConfigs: { _type: Type.Array, _default: defaultPriorityConfig, _description: 'Add entries to configure the styling for specific priority tags.', _elements: { conceptUuid: { _type: Type.UUID, _description: 'The UUID of the priority concept to configure', }, color: { _type: Type.String, _description: 'The color of the tag. This is based on the "type" field of the Carbon Design System "Tag" component.', _validators: [validators.oneOf(priorityTagColors)], _default: 'gray', }, style: { _type: Type.String, _description: 'Style to apply to the tag', _validators: [validators.oneOf(tagStyles)], _default: null, }, }, }, waitTimeThresholds: { _type: Type.Array, _default: [], _description: 'Optional. Colour the wait-time value once a patient has waited at least the given number of minutes, ' + 'drawing attention to patients who have been in the queue a long time. The highest matching band wins. ' + 'Empty by default, so wait times are not coloured unless this is configured. ' + 'A reasonable starting point could be [{ waitTimeInMinutes: 120, color: "red" }], which turns the value red after two hours.', _elements: { waitTimeInMinutes: { _type: Type.Number, _description: 'The minimum wait time in minutes for this colour to apply', _validators: [validator((waitTime: number) => waitTime > 0, 'waitTimeInMinutes must be a positive number')], }, color: { _type: Type.String, _description: 'The colour to apply to the wait-time value once the threshold is reached', _validators: [validators.oneOf(waitTimeColors)], _default: 'red', }, }, }, appointmentStatuses: { _type: Type.Array, _default: ['Scheduled', 'CheckedIn', 'Completed', 'Missed'], _description: `Statuses of the patient's appointments for today to show in the current visit summary of the expanded queue table row. Set to an empty array to hide today's appointments. Available options: ${appointmentStatusValues.join(', ')}`, _elements: { _type: Type.String, _validators: [validators.oneOf(appointmentStatusValues)], }, }, callingStatus: { _type: Type.String, _default: 'calling', _description: 'The status string sent to the queueutil/assignticket endpoint when a patient is called, ' + 'and matched on the queue screen to trigger the blinking ticket display. Change to match ' + 'what your digital signage expects (e.g. "Now serving"). ' + 'Avoid "completed", which the queue backend treats as a signal to remove the ticket.', }, clinicAdministratorScreen: { privilege: { _type: Type.String, _default: 'App: Service Queues Clinic Administrator', _description: 'The privilege that grants a clinic-wide monitoring tab on the Service Queues dashboard, opened ' + 'first, with the standard waiting list on a second tab. Users without the privilege see no tabs ' + 'and their dashboard is unchanged. Must match the privilege defined in your distribution content; ' + 'until it is attached to a role, only System Developers see the tab.', }, }, concepts: { defaultPriorityConceptUuid: { _type: Type.ConceptUuid, _default: defaultPriorityUuid, _description: 'The UUID of the default priority for the queues eg Not urgent.', }, defaultStatusConceptUuid: { _type: Type.ConceptUuid, _default: '51ae5e4d-b72b-4912-bf31-a17efb690aeb', _description: 'The UUID of the default status for the queues eg Waiting.', }, defaultTransitionStatus: { _type: Type.ConceptUuid, _default: 'ca7494ae-437f-4fd0-8aae-b88b9a2ba47d', _description: 'The UUID of the default status for attending a service in the queues eg In Service.', }, emergencyPriorityConceptUuid: { _type: Type.ConceptUuid, _default: defaultEmergencyPriorityUuid, _description: 'The UUID of the priority with the highest sort weight for the queues eg Emergency.', }, waitingStatusConceptUuid: { _type: Type.ConceptUuid, _default: '51ae5e4d-b72b-4912-bf31-a17efb690aeb', _description: 'The concept UUID of the status for patients waiting to be attended to.', }, }, contactAttributeType: { _type: Type.UUID, _default: null, _description: 'The UUID of the person attribute type that captures contact information such as `Next of kin contact details`', }, customPatientChartUrl: { _type: Type.String, _default: '${openmrsSpaBase}/patient/${patientUuid}/chart', _description: `Template URL that will be used when clicking on the patient name in the queues table. Available arguments: patientUuid, openmrsSpaBase, openBase (openmrsSpaBase and openBase are available to any )`, _validators: [validators.isUrlWithTemplateParameters(['patientUuid'])], }, dashboardTitle: { _type: Type.Object, _default: { key: 'serviceQueues', value: 'Service queues', }, _description: 'The title to be displayed on the service queues dashboard', key: { _type: Type.String, _description: 'The translation key of the title to be displayed on the service queues dashboard', }, value: { _type: Type.String, _description: 'The translation value of the title to be displayed on the service queues dashboard', }, }, defaultIdentifierTypes: { _type: Type.Array, _default: ['05ee9cf4-7242-4a17-b4d4-00f707265c8a', 'f85081e2-b4be-4e48-b3a4-7994b69bb101'], _description: 'The identifier types to be displayed on all patient search result page', _elements: { _type: Type.String, }, }, defaultInitialServiceQueue: { _type: Type.String, _default: 'Outpatient Triage', _description: 'The name of the default service queue to be selected when the start visit form is opened', }, refreshIntervals: { dashboard: { active: { _type: Type.Number, _default: 60000, _description: 'How often (in ms) the queue dashboards poll for updates while the user is actively interacting with the tab.', _validators: [validators.inRange(1000, 3600000)], }, idle: { _type: Type.Number, _default: 120000, _description: 'How often (in ms) the queue dashboards poll for updates once the user has gone idle.', _validators: [validators.inRange(1000, 3600000)], }, }, queueScreen: { active: { _type: Type.Number, _default: 5000, _description: 'How often (in ms) the queue screen polls for updates while the user is actively interacting with the tab.', _validators: [validators.inRange(1000, 3600000)], }, idle: { _type: Type.Number, _default: 10000, _description: 'How often (in ms) the queue screen polls for updates once the user has gone idle. Kept short so an unattended, wall-mounted board stays reasonably current.', _validators: [validators.inRange(1000, 3600000)], }, }, }, queueTables: { columnDefinitions: { _type: Type.Array, _default: [], _description: "Custom columns for queue tables can be defined here. These columns will be referenced by their `id` in the `tableDefinitions` columns configuration. If the provided `id` matches a built-in column, the custom configuration will override the built-in column's configuration.", _elements: { id: { _type: Type.String, _description: 'The unique identifier for the column you are defining', }, columnType: { _type: Type.String, _default: '', _description: 'The type of column, if different from the ID', _validators: [validators.oneOf(columnTypes)], }, header: { _type: Type.String, _default: '', _description: 'The header text for the column. Will be translated if it is a valid translation key. If not provided, the header will be based on the columnType.', }, config: { actions: { buttons: { _type: Type.Array, _default: ['call'], _description: 'For columnType "actions". Configures the buttons to display in the action cell. It is recommended to only use one, and put the rest in the overflow menu. Valid actions are: ' + queueEntryActions.join(', ') + '. Deprecated: "transition" is still accepted but is treated as "move".', _elements: { _type: Type.String, _validators: [validators.oneOf(configurableQueueEntryActions)], }, }, overflowMenu: { _type: Type.Array, _default: ['edit', 'remove', 'undo'], _description: 'For columnType "actions". Configures the items to display in the overflow menu. Valid actions are: ' + queueEntryActions.join(', ') + '. Deprecated: "transition" is still accepted but is treated as "move".', _elements: { _type: Type.String, _validators: [validators.oneOf(configurableQueueEntryActions)], }, }, }, identifierTypeUuid: { _type: Type.UUID, _default: defaultIdentifierTypeUuid, _description: "For columnType 'patient-identifier'. The UUID of the identifier type to display", }, statusConfigs: { _type: Type.Array, _default: [], _description: 'For columnType "status". Configures the icons for each status.', _elements: { conceptUuid: { _type: Type.UUID, _description: 'The UUID of the status concept to configure', }, iconComponent: { _type: Type.String, _default: '', _description: 'The icon component to display for the status', _validators: [validators.oneOf(statusIcons)], }, }, }, visitQueueNumberAttributeUuid: { _type: Type.String, _default: null, _description: 'The UUID of the visit attribute that contains the visit queue number. This must be set to use the queue-number column if the top-level `visitQueueNumberAttributeUuid` config element is not set.', }, }, _validators: [ validator( (columnDfn: ColumnDefinition) => Boolean(columnDfn.columnType || columnTypes.some((c) => c === columnDfn.id)), (columnDfn) => `No columnType provided for column with ID '${ columnDfn.id }', and the ID is not a valid columnType. Valid column types are: ${columnTypes.join(', ')}.`, ), validator( (columnDfn: ColumnDefinition) => { return ( columnDfn.config.identifierTypeUuid === defaultIdentifierTypeUuid || columnHasType(columnDfn, 'patient-identifier') ); }, (columnDfn) => { return `Identifier type can only be set for 'patient-identifier' column type. Column ${ columnDfn.id } has type '${columnDfn.columnType ?? columnDfn.id}.`; }, ), validator( (columnDfn: ColumnDefinition) => { return ( !columnDfn.config.statusConfigs || columnDfn.config.statusConfigs.length === 0 || columnHasType(columnDfn, 'status') ); }, (columnDfn) => { return `Statuses can only be configured for 'status' column type. Column ${columnDfn.id} has type '${ columnDfn.columnType ?? columnDfn.id }`; }, ), ], }, }, tableDefinitions: { _type: Type.Array, _default: [defaultQueueTable], _elements: { columns: { _type: Type.Array, _elements: { _type: Type.String, }, }, appliedTo: { _type: Type.Array, _elements: { queue: { _type: Type.String, _default: '', _description: 'The UUID of the queue. If not provided, applies to all queues.', }, status: { _type: Type.String, _default: '', _description: 'The UUID of the status. If not provided, applies to all statuses.', }, }, }, }, }, _validators: [ validator( (queueConfig: TablesConfig) => { const validColumnIds = [...builtInColumns, ...queueConfig.columnDefinitions.map((colDef) => colDef.id)]; return queueConfig.tableDefinitions.every((t) => t.columns.every((c) => validColumnIds.includes(c))); }, (queueConfig: TablesConfig) => { const validColumnIds = new Set([ ...builtInColumns, ...queueConfig.columnDefinitions.map((colDef) => colDef.id), ]); return `A table definition references a column that is not defined. You can define additional columns in 'columnDefinitions'.\n\nCurrently defined column IDs are ${[ ...validColumnIds, ].join(', ')}.\n\nInvalid columns: ${queueConfig.tableDefinitions .map((t) => t.columns.filter((c) => !validColumnIds.has(c))) .flat() .join(', ')}`; }, ), ], }, showCheckedInPatientsBeforeSearch: { _type: Type.Boolean, _default: true, _description: 'Whether the "Add patient to queue" workspace lists patients who are checked in (have an active visit) ' + 'at the selected location but are not yet in a queue, before the user types a search term.', }, showRecommendedVisitTypeTab: { _type: Type.Boolean, _default: false, _description: 'Whether start visit form should display recommended visit type tab. Requires `visitTypeResourceUrl`', }, visitAttributeTypes: { _type: Type.Array, _elements: { _type: Type.Object, uuid: { _type: Type.UUID, _description: 'UUID of the visit attribute type', }, }, _description: 'Visit attribute types whose values show as tags in the current visit summary of the expanded queue table row, like the visit attribute tags in the patient banner. The defaults are the Punctuality and Insurance Policy Number types from the reference application demo data.', _default: [{ uuid: '57ea0cbb-064f-4d09-8cf4-e8228700491c' }, { uuid: 'aac48226-d143-4274-80e0-264db4e368ee' }], }, visitQueueNumberAttributeUuid: { _type: Type.UUID, _default: 'c0c579b0-8e59-401d-8a4a-976a0b183519', _description: 'The UUID of the visit attribute that contains the visit queue number.', }, visitTypeResourceUrl: { _type: Type.String, _default: '', _description: 'The `visitTypeResourceUrl`', }, _validators: [ validator((config: ConfigObject) => { const queueNumberColumnDefs = [ ...config.queueTables.columnDefinitions.filter((colDef) => columnHasType(colDef, 'queue-number')), { id: 'queue-number', config: defaultColumnConfig }, ]; const queueNumberColumnsUsed = config.queueTables.tableDefinitions .map((tableDef) => tableDef.columns) .flat() .filter((col) => queueNumberColumnDefs.map((d) => d.id).includes(col)); return Boolean( config.visitQueueNumberAttributeUuid || queueNumberColumnsUsed.every( (columnId) => queueNumberColumnDefs.find((d) => d.id === columnId).config.visitQueueNumberAttributeUuid, ), ); }, 'If a queue-number column is used in a table definition, the `visitQueueNumberAttributeUuid` must be set either at the top-level config or in the column definition.'), ], }; function columnHasType(columnDef: ColumnDefinition, type: ColumnType): boolean { return columnDef.columnType === type || columnDef.id === type; } export interface ConfigObject { priorityConfigs: Array; waitTimeThresholds: Array; appointmentStatuses: Array; callingStatus: string; clinicAdministratorScreen: { privilege: string; }; concepts: { defaultPriorityConceptUuid: string; defaultStatusConceptUuid: string; defaultTransitionStatus: string; emergencyPriorityConceptUuid: string; waitingStatusConceptUuid: string; }; defaultInitialServiceQueue: string; refreshIntervals: { dashboard: { active: number; idle: number }; queueScreen: { active: number; idle: number }; }; contactAttributeType: string; customPatientChartUrl: string; defaultIdentifierTypes: Array; dashboardTitle: { key: string; value: string; }; queueTables: TablesConfig; showCheckedInPatientsBeforeSearch: boolean; showRecommendedVisitTypeTab: boolean; visitAttributeTypes: Array<{ uuid: string }>; visitQueueNumberAttributeUuid: string | null; visitTypeResourceUrl: string; } interface TablesConfig { columnDefinitions: ColumnDefinition[]; /* A list of table definitions. A queue table (whether it is displaying entries from a particular queue+status combination, from a particular queue, or from multiple queues) will determine what columns to show based on these definitions. If multiple TableDefinitions have matching appliedTo criteria, the first one will be used. */ tableDefinitions: TableDefinitions[]; } export type ColumnDefinition = { id: string; columnType?: ColumnType; header?: string; config: ColumnConfig; }; export interface ActionsColumnConfig { actions: { buttons: ConfigurableQueueEntryAction[]; overflowMenu: ConfigurableQueueEntryAction[]; }; } export interface PatientIdentifierColumnConfig { identifierTypeUuid: string; // uuid of the identifier type } export interface PriorityConfig { conceptUuid: string; color: PriorityTagColor; style: TagStyle | null; } export interface WaitTimeThresholdConfig { waitTimeInMinutes: number; color: WaitTimeColor; } export interface StatusConfig { conceptUuid: string; iconComponent: StatusIcon; } export interface StatusColumnConfig { statusConfigs: StatusConfig[]; } export interface VisitAttributeQueueNumberColumnConfig { visitQueueNumberAttributeUuid: string; } export type ColumnConfig = ActionsColumnConfig & PatientIdentifierColumnConfig & StatusColumnConfig & VisitAttributeQueueNumberColumnConfig; export interface TableDefinitions { // Column IDs defined either in columnDefinitions or in builtInColumns columns: Array; // apply the columns to tables of any of the specified queue and status // (if appliedTo is null, apply to all tables, including the one in the service queue app home page) appliedTo?: Array<{ queue: string; status: string }>; }