Kaizen
Browse modulesCheckoutcheckout/serverClasses

Class: CartService

Defined in: server/checkout/cart-service.ts:99

The cart aggregate (proposal §Cart — state machine, seam & pricing injection). Carts are mutable, disposable, INPUT-only: items store consumer inputs, never prices — pricing is computed on read through the injected priceCart adapter. finalize is the one-way seam to an order.

Extends

  • BaseCheckoutService

Constructors

Constructor

new CartService(deps: CheckoutFeatureDeps): CartService;

Defined in: server/checkout/base-checkout-service.ts:43

Parameters

ParameterType
depsCheckoutFeatureDeps

Returns

CartService

Inherited from

BaseCheckoutService.constructor

Properties

deps

protected readonly deps: CheckoutFeatureDeps;

Defined in: server/checkout/base-checkout-service.ts:43

Inherited from

BaseCheckoutService.deps

Accessors

actorId

Get Signature

get protected actorId(): string | null;

Defined in: server/checkout/base-checkout-service.ts:45

Returns

string | null

Inherited from

BaseCheckoutService.actorId

Methods

addItem()

addItem(
   scope: CartAccessScope, 
   cartId: string, 
   input: AddItemInput
): Promise<{
  cartId: string;
  createdAt: Date;
  createdBy: string | null;
  deletedAt: Date | null;
  deletedBy: string | null;
  id: string;
  metadata: JsonValue;
  organizationId: string;
  quantity: number;
  referenceId: string | null;
  referenceType: string;
  updatedAt: Date;
  updatedBy: string | null;
}>;

Defined in: server/checkout/cart-service.ts:133

Add an input line to an open cart. Items carry INPUTS only (referenceType/referenceId/quantity/metadata) — no price field: prices are computed on read. Requires trusted cart scope, refreshes activity, and advances the cart version.

Parameters

ParameterType
scopeCartAccessScope
cartIdstring
inputAddItemInput

Returns

Promise<{ cartId: string; createdAt: Date; createdBy: string | null; deletedAt: Date | null; deletedBy: string | null; id: string; metadata: JsonValue; organizationId: string; quantity: number; referenceId: string | null; referenceType: string; updatedAt: Date; updatedBy: string | null; }>


createCart()

createCart(scope: CartAccessScope, input?: CreateCartInput): Promise<{
  createdAt: Date;
  createdBy: string | null;
  currencyCode: string;
  customerId: string | null;
  deletedAt: Date | null;
  deletedBy: string | null;
  expiresAt: Date | null;
  externalRef: string | null;
  guestEmail: string | null;
  id: string;
  lastActivityAt: Date;
  metadata: JsonValue;
  organizationId: string;
  status: CartStatus;
  updatedAt: Date;
  updatedBy: string | null;
  version: number;
}>;

Defined in: server/checkout/cart-service.ts:101

Create an open cart from trusted identity scope.

Parameters

ParameterType
scopeCartAccessScope
inputCreateCartInput

Returns

Promise<{ createdAt: Date; createdBy: string | null; currencyCode: string; customerId: string | null; deletedAt: Date | null; deletedBy: string | null; expiresAt: Date | null; externalRef: string | null; guestEmail: string | null; id: string; lastActivityAt: Date; metadata: JsonValue; organizationId: string; status: CartStatus; updatedAt: Date; updatedBy: string | null; version: number; }>


expireCarts()

expireCarts(organizationId: string, cartIds: string[]): Promise<number>;

Defined in: server/checkout/cart-service.ts:302

Mark the given carts expired (the expires_at inventory-hold lapse). Skips any cart already in a terminal state — only open carts transition. Returns the number transitioned.

Parameters

ParameterType
organizationIdstring
cartIdsstring[]

Returns

Promise<number>


expireStaleCarts()

expireStaleCarts(organizationId: string, before: Date): Promise<number>;

Defined in: server/checkout/cart-service.ts:312

Sweep: mark one organization's open carts idle past before as abandoned (proposal §Cart state machine: "cleanup job: idle > threshold → abandoned"). The consumer runs this on a cron — taproot runs no background jobs.

Parameters

ParameterType
organizationIdstring
beforeDate

Returns

Promise<number>


finalize()

finalize(
   scope: CartAccessScope, 
   cartId: string, 
   opts: FinalizeOptions
): Promise<BigIntsAsNumbers<{
  anomalyAt: Date | null;
  applicationFeeTotalCents: bigint | null;
  authorizedTotalCents: bigint;
  authorizeStatus: OrderAuthorizeStatus;
  capturedTotalCents: bigint;
  cartId: string | null;
  chargeStatus: OrderChargeStatus;
  closedAt: Date | null;
  code: string;
  createdAt: Date;
  createdBy: string | null;
  currencyCode: string;
  customerEmail: string | null;
  customerId: string | null;
  customerTotalCents: bigint;
  disputedTotalCents: bigint;
  externalRef: string | null;
  id: string;
  idempotencyKey: string | null;
  metadata: JsonValue;
  notes: string | null;
  operatorId: string | null;
  organizationId: string;
  orgBorneTotalCents: bigint;
  origin: string;
  placedAt: Date | null;
  platformBorneTotalCents: bigint;
  processingFeeTotalCents: bigint | null;
  refundedTotalCents: bigint;
  requestHash: string | null;
  status: OrderStatus;
  statusUpdatedAt: Date;
  statusUpdatedBy: string | null;
  subtotalCents: bigint;
  updatedAt: Date;
  updatedBy: string | null;
}>>;

Defined in: server/checkout/cart-service.ts:225

The one-way seam (proposal §finalize): open cart → row lock → version bump → re-price → expected-total guard → createOrder → cart converted → append order.placed. Returns the created (or, on re-entry, existing) order.

RE-ENTRY (proposal §finalize step 1): a converted cart + a matching (organizationId, idempotencyKey) order returns that prior order unconditionally — no request-hash check, because the priced payload doesn't exist until step 2 re-prices, so the converted-cart + key hit IS the idempotent handle. Any other non-open status, or converted with a different / absent key → CartNotOpenError(status).

Parameters

ParameterType
scopeCartAccessScope
cartIdstring
optsFinalizeOptions

Returns

Promise<BigIntsAsNumbers<{ anomalyAt: Date | null; applicationFeeTotalCents: bigint | null; authorizedTotalCents: bigint; authorizeStatus: OrderAuthorizeStatus; capturedTotalCents: bigint; cartId: string | null; chargeStatus: OrderChargeStatus; closedAt: Date | null; code: string; createdAt: Date; createdBy: string | null; currencyCode: string; customerEmail: string | null; customerId: string | null; customerTotalCents: bigint; disputedTotalCents: bigint; externalRef: string | null; id: string; idempotencyKey: string | null; metadata: JsonValue; notes: string | null; operatorId: string | null; organizationId: string; orgBorneTotalCents: bigint; origin: string; placedAt: Date | null; platformBorneTotalCents: bigint; processingFeeTotalCents: bigint | null; refundedTotalCents: bigint; requestHash: string | null; status: OrderStatus; statusUpdatedAt: Date; statusUpdatedBy: string | null; subtotalCents: bigint; updatedAt: Date; updatedBy: string | null; }>>


findStaleCarts()

findStaleCarts(organizationId: string, before: Date): Promise<{
  createdAt: Date;
  createdBy: string | null;
  currencyCode: string;
  customerId: string | null;
  deletedAt: Date | null;
  deletedBy: string | null;
  expiresAt: Date | null;
  externalRef: string | null;
  guestEmail: string | null;
  id: string;
  lastActivityAt: Date;
  metadata: JsonValue;
  organizationId: string;
  status: CartStatus;
  updatedAt: Date;
  updatedBy: string | null;
  version: number;
}[]>;

Defined in: server/checkout/cart-service.ts:293

One organization's open carts idle before the cutoff.

Parameters

ParameterType
organizationIdstring
beforeDate

Returns

Promise<{ createdAt: Date; createdBy: string | null; currencyCode: string; customerId: string | null; deletedAt: Date | null; deletedBy: string | null; expiresAt: Date | null; externalRef: string | null; guestEmail: string | null; id: string; lastActivityAt: Date; metadata: JsonValue; organizationId: string; status: CartStatus; updatedAt: Date; updatedBy: string | null; version: number; }[]>


getCart()

getCart(scope: CartAccessScope, cartId: string): Promise<{
  createdAt: Date;
  createdBy: string | null;
  currencyCode: string;
  customerId: string | null;
  deletedAt: Date | null;
  deletedBy: string | null;
  expiresAt: Date | null;
  externalRef: string | null;
  guestEmail: string | null;
  id: string;
  lastActivityAt: Date;
  metadata: JsonValue;
  organizationId: string;
  status: CartStatus;
  updatedAt: Date;
  updatedBy: string | null;
  version: number;
}>;

Defined in: server/checkout/cart-service.ts:121

Fetch a scoped cart; missing, foreign, or deleted → not found.

Parameters

ParameterType
scopeCartAccessScope
cartIdstring

Returns

Promise<{ createdAt: Date; createdBy: string | null; currencyCode: string; customerId: string | null; deletedAt: Date | null; deletedBy: string | null; expiresAt: Date | null; externalRef: string | null; guestEmail: string | null; id: string; lastActivityAt: Date; metadata: JsonValue; organizationId: string; status: CartStatus; updatedAt: Date; updatedBy: string | null; version: number; }>


getPricedCart()

getPricedCart(scope: CartAccessScope, cartId: string): Promise<PricedCartSnapshot>;

Defined in: server/checkout/cart-service.ts:191

Re-price the cart through the injected priceCart adapter (proposal §Pricing injection). The adapter owns the consumer catalog + classify policy — taproot only hands it the cart and its live items.

Parameters

ParameterType
scopeCartAccessScope
cartIdstring

Returns

Promise<PricedCartSnapshot>


recomputeTenderFromLedger()

protected recomputeTenderFromLedger(
   repos: CheckoutRepositories, 
   payment: PaymentRow, 
   overrides?: Partial<Pick<TenderFacts, "clientActionRequired" | "clientActionRequiredAt">>
): Promise<{
  fold: TenderComputation;
  patch: TenderBucketPatch;
}>;

Defined in: server/checkout/base-checkout-service.ts:94

Re-project a tender from its persisted ledger without writing it. Explicit clientActionRequiredAt: null clears the stored timestamp.

Parameters

ParameterType
reposCheckoutRepositories
paymentPaymentRow
overrides?Partial<Pick<TenderFacts, "clientActionRequired" | "clientActionRequiredAt">>

Returns

Promise<{ fold: TenderComputation; patch: TenderBucketPatch; }>

Inherited from

BaseCheckoutService.recomputeTenderFromLedger

reconcileClosedAt()

protected reconcileClosedAt(
   repos: CheckoutRepositories, 
   order: OrderRow, 
   now: Date
): Promise<void>;

Defined in: server/checkout/base-checkout-service.ts:121

Reconcile closedAt and the once-per-order order.paid event after a money write. The caller must hold the order lock in an ambient transaction.

Parameters

ParameterType
reposCheckoutRepositories
orderOrderRow
nowDate

Returns

Promise<void>

Inherited from

BaseCheckoutService.reconcileClosedAt

recordCanonicalTransactions()

protected recordCanonicalTransactions(
   repos: CheckoutRepositories, 
   context: RecordTxnContext, 
   transactions: CanonicalTxn[]
): Promise<RecordedCanonicalTxn[]>;

Defined in: server/checkout/base-checkout-service.ts:61

Record canonical transactions sequentially and identify both fresh inserts and pending rows resolved in place. Callers own the resulting side effects.

Parameters

ParameterType
reposCheckoutRepositories
contextRecordTxnContext
transactionsCanonicalTxn[]

Returns

Promise<RecordedCanonicalTxn[]>

Inherited from

BaseCheckoutService.recordCanonicalTransactions

removeItem()

removeItem(
   scope: CartAccessScope, 
   cartId: string, 
   itemId: string
): Promise<void>;

Defined in: server/checkout/cart-service.ts:174

Soft-delete an open cart's item; refresh activity and advance version.

Parameters

ParameterType
scopeCartAccessScope
cartIdstring
itemIdstring

Returns

Promise<void>


transaction()

protected transaction<T>(fn: (deps: CheckoutFeatureDeps) => Promise<T>): Promise<T>;

Defined in: server/checkout/base-checkout-service.ts:49

Type Parameters

Type Parameter
T

Parameters

ParameterType
fn(deps: CheckoutFeatureDeps) => Promise<T>

Returns

Promise<T>

Inherited from

BaseCheckoutService.transaction

updateItem()

updateItem(
   scope: CartAccessScope, 
   cartId: string, 
   itemId: string, 
   input: UpdateItemInput
): Promise<{
  cartId: string;
  createdAt: Date;
  createdBy: string | null;
  deletedAt: Date | null;
  deletedBy: string | null;
  id: string;
  metadata: JsonValue;
  organizationId: string;
  quantity: number;
  referenceId: string | null;
  referenceType: string;
  updatedAt: Date;
  updatedBy: string | null;
}>;

Defined in: server/checkout/cart-service.ts:155

Update an open cart's item; refresh activity and advance version.

Parameters

ParameterType
scopeCartAccessScope
cartIdstring
itemIdstring
inputUpdateItemInput

Returns

Promise<{ cartId: string; createdAt: Date; createdBy: string | null; deletedAt: Date | null; deletedBy: string | null; id: string; metadata: JsonValue; organizationId: string; quantity: number; referenceId: string | null; referenceType: string; updatedAt: Date; updatedBy: string | null; }>

On this page