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.
| Method | Parameters | Returns | Description |
|---|---|---|---|
| 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
| Method | Parameters | Returns |
|---|---|---|
| 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
| Field | Type |
|---|---|
| id | string |
| tenantId | string |
| discordUserId | string |
| guildId | string (optional) |
| state | OrderSessionState |
| items | OrderSessionItem[] |
| shippingAddress | Record<string, unknown> (optional) |
| paymentMethod | PaymentMethod (optional) |
| submittedOrderId | string (optional) |
| expiresAt | Date |
| createdAt | Date |
| updatedAt | Date |
OrderSessionItem
| Field | Type |
|---|---|
| productId | string |
| name | string |
| sku | string |
| quantity | number |
| unitPriceCents | number |
| currency | string |
Validators
The order-session module does not have a separate validators.ts file. Types are defined in order-session.types.ts:
OrderSessionState— union ofCOLLECTING_ITEMS | COLLECTING_SHIPPING | SELECTING_PAYMENT | READY_TO_SUBMIT | SUBMITTED | CANCELLED | EXPIREDOrderSessionItem— item shape used in sessionsOrderSessionRecord— full session record typeOrderSessionCreateInput—{ discordUserId, guildId?, expiresAt }OrderSessionPatchInput— partial ofOrderSessionRecordexcludingid,tenantId,discordUserId,createdAt