Skip to content

armgit / spree

Public Imported from GitHub
0

Open Source Platform for DTC, B2B Commerce, Marketplaces & Omnichannel. REST APIs, TypeScript SDKs, and production-ready Next.js storefront. Self-host it. Own your data. No vendor lock-in. Zero platform fees.

6.7 KB
Raw Blame
import { ORDER_FULFILLMENT_STATUSES, subsetOf } from '@spree/core'
import i18n from 'i18next'
import { formatMoney } from './formatters'

/**
 * Which line items sit in which fulfillment, and which units nobody has
 * claimed yet. The order payload describes the two halves separately —
 * `order.items` is the full basket, each fulfillment carries its own
 * `fulfillment_items` — so the card has to join them back together to say
 * what is being shipped where.
 */

/** The line-item fields the grouping reads. */
export interface GroupableLineItem {
  id: string
  variant_id?: string | null
  name: string
  options_text?: string | null
  quantity: number
  thumbnail_url?: string | null
  price?: string | null
  currency?: string | null
}

/** The fulfillment-item fields the grouping reads. */
export interface GroupableFulfillmentItem {
  line_item_id?: string | null
  variant_id?: string | null
  name?: string | null
  options_text?: string | null
  quantity: number
}

/** The fulfillment fields the grouping reads. */
export interface GroupableFulfillment {
  status: string
  fulfillment_items?: GroupableFulfillmentItem[] | null
}

/**
 * One row in a fulfillment's (or the unfulfilled group's) item list: the
 * line item it descends from, plus how many of its units this group holds.
 */
export interface FulfillmentItemRow {
  /**
   * Stable within one group — a line item appears at most once per group,
   * plus once per replacement variant an exchange ships against it.
   */
  key: string
  lineItem: GroupableLineItem | null
  /** Falls back to the fulfillment item's own copy when the line item is gone. */
  name: string
  optionsText: string | null
  thumbnailUrl: string | null
  displayPrice: string | null
  quantity: number
}

/**
 * Sums, per line item, how many units every fulfillment claims. Fulfillment
 * items that reference no line item are skipped — there is nothing to join
 * them to, and counting them would understate the remainder.
 */
function claimedQuantities(fulfillments: GroupableFulfillment[]): Map<string, number> {
  const claimed = new Map<string, number>()

  for (const fulfillment of fulfillments) {
    for (const item of fulfillment.fulfillment_items ?? []) {
      if (!item.line_item_id) continue
      claimed.set(item.line_item_id, (claimed.get(item.line_item_id) ?? 0) + item.quantity)
    }
  }

  return claimed
}

/**
 * Sums, per line item, how many units have left the merchant's hands. Those
 * units are gone, so an edit can neither remove the row nor push its quantity
 * below them. Delivered counts too — it is downstream of fulfilled. Canceled
 * fulfillments restock their units and do not count.
 */
/**
 * Fulfillment statuses meaning the goods have left the merchant's hands.
 * The order-level rollup uses the same two words for "everything has gone
 * out", so the pages gating order editing share this list.
 */
export const GONE_STATUSES = ['fulfilled', 'delivered']

/** The order-level fulfillment statuses the order lists filter on. */
export const FILTERABLE_ORDER_FULFILLMENT_STATUSES = subsetOf(ORDER_FULFILLMENT_STATUSES, [
  'backorder',
  'canceled',
  'partial',
  'unfulfilled',
  'fulfilled',
  'delivered',
])

export function fulfilledQuantities(fulfillments: GroupableFulfillment[]): Map<string, number> {
  const fulfilled = new Map<string, number>()

  for (const fulfillment of fulfillments) {
    if (!GONE_STATUSES.includes(fulfillment.status)) continue

    for (const item of fulfillment.fulfillment_items ?? []) {
      if (!item.line_item_id) continue
      fulfilled.set(item.line_item_id, (fulfilled.get(item.line_item_id) ?? 0) + item.quantity)
    }
  }

  return fulfilled
}

/**
 * An exchange ships its replacement against the original line item, so the
 * unit's variant is the only thing that names what is actually in the box.
 */
function isReplacement(
  lineItem: GroupableLineItem | undefined,
  item: Pick<GroupableFulfillmentItem, 'variant_id'> | null,
): boolean {
  return Boolean(
    lineItem?.variant_id && item?.variant_id && lineItem.variant_id !== item.variant_id,
  )
}

/**
 * Builds a row, preferring the line item's own copy over the fulfillment
 * item's — unless the unit is a replacement, whose name, image and price the
 * line item does not describe.
 */
function buildRow(
  key: string,
  lineItem: GroupableLineItem | undefined,
  fallback: Pick<GroupableFulfillmentItem, 'variant_id' | 'name' | 'options_text'> | null,
  quantity: number,
): FulfillmentItemRow {
  const copy = isReplacement(lineItem, fallback) ? undefined : lineItem

  return {
    key,
    lineItem: lineItem ?? null,
    name: copy?.name ?? fallback?.name ?? '',
    optionsText: copy?.options_text ?? fallback?.options_text ?? null,
    thumbnailUrl: copy?.thumbnail_url ?? null,
    displayPrice:
      copy?.price && copy.currency
        ? formatMoney(copy.price, copy.currency, i18n.language || 'en', { unitPrice: true })
        : null,
    quantity,
  }
}

/**
 * The items one fulfillment holds, joined to their line items for the image
 * and the price the fulfillment item does not carry. Several fulfillment
 * items can descend from the same line item (units split by on-hand versus
 * backordered status), so they are merged into a single row.
 */
export function fulfillmentItemRows(
  fulfillment: GroupableFulfillment,
  lineItems: GroupableLineItem[],
): FulfillmentItemRow[] {
  const byId = new Map(lineItems.map((lineItem) => [lineItem.id, lineItem]))
  const rows = new Map<string, FulfillmentItemRow>()

  for (const item of fulfillment.fulfillment_items ?? []) {
    // A fulfillment item with no line item still ships; key it by variant so
    // it renders rather than vanishing.
    const lineItem = item.line_item_id ? byId.get(item.line_item_id) : undefined
    const key = isReplacement(lineItem, item)
      ? `${item.line_item_id}:${item.variant_id}`
      : (item.line_item_id ?? item.variant_id)
    if (!key) continue

    const existing = rows.get(key)
    if (existing) {
      existing.quantity += item.quantity
      continue
    }

    rows.set(key, buildRow(key, lineItem, item, item.quantity))
  }

  return [...rows.values()]
}

/**
 * Line-item units no fulfillment has claimed: the ordered quantity minus what
 * every fulfillment holds. Only positive remainders are returned, so an order
 * whose fulfillments account for everything yields an empty list.
 */
export function unfulfilledItemRows(
  lineItems: GroupableLineItem[],
  fulfillments: GroupableFulfillment[],
): FulfillmentItemRow[] {
  const claimed = claimedQuantities(fulfillments)

  return lineItems.flatMap((lineItem) => {
    const remainder = lineItem.quantity - (claimed.get(lineItem.id) ?? 0)
    if (remainder <= 0) return []

    return [buildRow(lineItem.id, lineItem, null, remainder)]
  })
}