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.
| Method | Parameters | Returns | Description |
|---|
| 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 |
| Field | Type |
|---|
| orderId | string |
| productId | string |
| quantity | number |
| reason | string (optional) |
There is also an internal factory createInventoryServiceWithDeps({ snapshotModel, transactionModel }) for testing.
Repository
InventoryRepository
| Method | Parameters | Returns |
|---|
| 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
| Field | Type |
|---|
| id | string |
| tenantId | string |
| createdAt | Date |
| updatedAt | Date |
| data | Record<string, unknown> (optional) |
Validators
inventorySnapshotSchema
| Field | Type | Required | Default |
|---|
| tenantId | string | Yes | — |
| productId | string | Yes | — |
| quantityAvailable | number (int) | Yes | — |
| quantityReserved | number (int) | Yes | — |
| updatedAt | Date (coerced) | Yes | — |
inventoryTransactionTypeSchema
Enum: ADJUSTMENT, RESERVE, RELEASE, SALE, RESTOCK
inventoryTransactionSchema
| Field | Type | Required | Default |
|---|
| tenantId | string | Yes | — |
| productId | string | Yes | — |
| orderId | string | No | — |
| type | inventoryTransactionTypeSchema | Yes | — |
| quantityDelta | number (int) | Yes | — |
| reason | string | No | — |
| createdAt | Date (coerced) | Yes | — |
Inferred Types
InventorySnapshot — z.infer<typeof inventorySnapshotSchema>
InventoryTransaction — z.infer<typeof inventoryTransactionSchema>
InventoryTransactionType — z.infer<typeof inventoryTransactionTypeSchema>