Skip to content

Ticket & State Machine

The ticket is the central entity of @nx/helpdesk. Each ticket has a lifecycle managed by an XState v5 state machine with 10 statuses, a full event audit trail, and asynchronous context enrichment.

Ticket Statuses

StatusCodeDescription
OpenOPENNewly created ticket, not yet assigned
AssignedASSIGNEDAssigned to a specific agent
In ProgressIN_PROGRESSAgent is actively working on it
Waiting for UserWAITING_USERRequires a response from the reporter
Waiting InternalWAITING_INTERNALDepends on another team or department
EscalatedESCALATEDEscalated to a higher tier
ResolvedRESOLVEDAgent marked as resolved, awaiting confirmation
ClosedCLOSEDCustomer confirmed — fully complete
ReopenedREOPENEDCustomer reopened after closure
Waiting AgentWAITING_AGENTWaiting for an agent to be assigned
PendingPENDINGHolding / not-yet-categorized
Assignment FailedASSIGNMENT_FAILEDAuto-assignment found no eligible agent

Authoritative enum: TicketStatuses in packages/core/src/models/schemas/helpdesk/ticket/schema.ts (12 values).

State Machine (XState v5)

Data Model

Ticket

ColumnTypeDescription
idbigintSnowflake ID
merchantIdbigintTenant scope
reporterIdbigintReporter (userId or agentId)
reporterTypeenumUSER / AGENT
categorybigintFK → TicketCategory
prioritysmallint100 LOW / 200 NORMAL / 300 HIGH / 400 URGENT
statusenum10 statuses (see table above)
subjectvarcharShort title
descriptiontextDetailed description
contextjsonbRich context data (order, product, …)
deletedAttimestampSoft delete

TicketMessage

ColumnTypeDescription
idbigintSnowflake ID
ticketIdbigintFK → Ticket
senderIdbigintSender ID
senderTypeenumUSER / AGENT / SYSTEM
messageTypeenumTEXT / IMAGE / FILE / SYSTEM_NOTE
contenttextMessage body
attachmentsjsonbList of attached files
isInternalbooleanInternal note (hidden from customer)

TicketEvent (Event Sourcing)

Every action on a ticket is written to TicketEvent, building a complete audit trail:

EventDescription
TICKET_CREATEDTicket was created
STATUS_CHANGEDStatus transition
ASSIGNEDAgent assigned
MESSAGE_ADDEDNew message
TAG_ADDED / TAG_REMOVEDTag change
SLA_WARNING / SLA_BREACHEDSLA events
ESCALATEDEscalation triggered
RESOLVED / CLOSEDEnd of lifecycle

REST API

Tickets (/tickets), including messages, status transitions, assignment, resolution confirmation, tags, and attachments — full endpoint reference rendered live from /v1/api/helpdesk/doc/openapi.json. Endpoint tables are intentionally not hand-maintained.

Use Cases

Ticket

Use CaseDescription
CreateTicketUseCaseCreates ticket, emits TICKET_CREATED event
ListTicketsUseCaseSearch and paginate tickets
GetTicketUseCaseFetch a single ticket's details
UpdateTicketStatusUseCaseTransition status via the state machine
AssignTicketUseCaseAssign ticket to an agent
AddMessageUseCaseAdd a message to a ticket
GetTicketMessagesUseCaseCursor-based message pagination
ConfirmResolutionUseCaseConfirm resolution → transition to CLOSED
EnrichTicketContextUseCaseEnrich context (order, product) asynchronously
AddAttachmentsToMessageUseCaseAttach files to a message

Context Enrichment Worker

When a ticket is created, context-enrichment.worker runs asynchronously to populate the context JSONB column:

Data is fetched from ProductRepository and SaleOrderRepository (both from @nx/core) and stored in the context column so agents have full context without querying other services.

Event-Driven Flow on Ticket Creation

Proprietary and Confidential. Unauthorized copying, distribution, or use of this software is strictly prohibited.