Skip to main content

Order Session

Source: src/data/order-session/

Service

createOrderSessionService / OrderSessionService

Factory: createOrderSessionService(repository: OrderSessionRepository) — returns OrderSessionService.

Manages persisted conversational order sessions with guarded state transitions. Sessions have a 30-minute TTL that resets on each interaction.

MethodParametersReturnsDescription
begin(ctx: RequestContext, input: { discordUserId: string, guildId?: string })Promise<OrderSessionRecord>Starts or returns the active session for a tenant/user pair
addItem(ctx: RequestContext, input: { discordUserId: string, item: OrderSessionItem })Promise<OrderSessionRecord>Adds an item to an active session; advances state when appropriate
setShipping(ctx: RequestContext, input: { discordUserId: string, shippingAddress: Record<string, unknown> })Promise<OrderSessionRecord>Stores shipping details and advances to payment selection
setPaymentMethod(ctx: RequestContext, input: { discordUserId: string, paymentMethod: PaymentMethod })Promise<OrderSessionRecord>Stores payment method and advances to ready-to-submit state
getActive(ctx: RequestContext, discordUserId: string)Promise<OrderSessionRecord | null>Returns the currently active session for the user, if any
markSubmitted(ctx: RequestContext, input: { discordUserId: string, orderId: string })Promise<OrderSessionRecord>Marks the active session as submitted and links final order id

State Machine

COLLECTING_ITEMS → COLLECTING_SHIPPING → SELECTING_PAYMENT → READY_TO_SUBMIT → SUBMITTED

Additional terminal states: CANCELLED, EXPIRED

Repository

OrderSessionRepository

MethodParametersReturns
getById(ctx: RequestContext, id: string)Promise<OrderSessionRecord | null>
getActiveByDiscordUser(ctx: RequestContext, discordUserId: string)Promise<OrderSessionRecord | null>
create(ctx: RequestContext, input: OrderSessionCreateInput & { id: string })Promise<OrderSessionRecord>
update(ctx: RequestContext, id: string, patch: OrderSessionPatchInput)Promise<OrderSessionRecord>

OrderSessionRecord

FieldType
idstring
tenantIdstring
discordUserIdstring
guildIdstring (optional)
stateOrderSessionState
itemsOrderSessionItem[]
shippingAddressRecord<string, unknown> (optional)
paymentMethodPaymentMethod (optional)
submittedOrderIdstring (optional)
expiresAtDate
createdAtDate
updatedAtDate

OrderSessionItem

FieldType
productIdstring
namestring
skustring
quantitynumber
unitPriceCentsnumber
currencystring

Validators

The order-session module does not have a separate validators.ts file. Types are defined in order-session.types.ts:

  • OrderSessionState — union of COLLECTING_ITEMS | COLLECTING_SHIPPING | SELECTING_PAYMENT | READY_TO_SUBMIT | SUBMITTED | CANCELLED | EXPIRED
  • OrderSessionItem — item shape used in sessions
  • OrderSessionRecord — full session record type
  • OrderSessionCreateInput{ discordUserId, guildId?, expiresAt }
  • OrderSessionPatchInput — partial of OrderSessionRecord excluding id, tenantId, discordUserId, createdAt