Skip to main content

Inventory

Source: src/data/inventory/

Service

createInventoryService / InventoryService

Factory: createInventoryService() — returns InventoryService.

Creates inventory lifecycle orchestration with idempotent transitions. Internally uses InventorySnapshotModel and InventoryTransactionModel (Mongoose) for atomic delta-based inventory tracking.

MethodParametersReturnsDescription
reserve(ctx: RequestContext, input: InventoryTransitionInput)Promise<void>Reserves inventory quantity for the order lifecycle
release(ctx: RequestContext, input: InventoryTransitionInput)Promise<void>Releases previously reserved inventory quantity
finalizeSale(ctx: RequestContext, input: InventoryTransitionInput)Promise<void>Finalizes sale quantity after successful payment checkpoint

InventoryTransitionInput

FieldType
orderIdstring
productIdstring
quantitynumber
reasonstring (optional)

There is also an internal factory createInventoryServiceWithDeps({ snapshotModel, transactionModel }) for testing.

Repository

InventoryRepository

MethodParametersReturns
getById(ctx: RequestContext, id: string)Promise<InventoryRecord | null>
listByTenant(ctx: RequestContext)Promise<InventoryRecord[]>
create(ctx: RequestContext, record: InventoryRecord)Promise<void>
update(ctx: RequestContext, id: string, patch: Partial<Omit<InventoryRecord, 'id' | 'tenantId'>>)Promise<InventoryRecord>
delete(ctx: RequestContext, id: string)Promise<void>

InventoryRecord

FieldType
idstring
tenantIdstring
createdAtDate
updatedAtDate
dataRecord<string, unknown> (optional)

Validators

inventorySnapshotSchema

FieldTypeRequiredDefault
tenantIdstringYes
productIdstringYes
quantityAvailablenumber (int)Yes
quantityReservednumber (int)Yes
updatedAtDate (coerced)Yes

inventoryTransactionTypeSchema

Enum: ADJUSTMENT, RESERVE, RELEASE, SALE, RESTOCK

inventoryTransactionSchema

FieldTypeRequiredDefault
tenantIdstringYes
productIdstringYes
orderIdstringNo
typeinventoryTransactionTypeSchemaYes
quantityDeltanumber (int)Yes
reasonstringNo
createdAtDate (coerced)Yes

Inferred Types

  • InventorySnapshotz.infer<typeof inventorySnapshotSchema>
  • InventoryTransactionz.infer<typeof inventoryTransactionSchema>
  • InventoryTransactionTypez.infer<typeof inventoryTransactionTypeSchema>