@12-apps/marketplace 1.9.0 → 1.10.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/ADOPTING.md CHANGED
@@ -14,8 +14,9 @@ error at mount, never a default that quietly adopts another host's policy.
14
14
  | `db` | `() => Promise<MarketplaceDb>` | The Prisma client generated from a schema that includes `prisma/marketplace.prisma`. Structural: the four delegates `marketplaceIntegration`, `marketplaceMenuLink`, `marketplaceInboxEvent`, `marketplaceAcceptance`, and the catalogue's `marketplaceCataloguePublication` (optional in the type; the catalogue calls need it). |
15
15
  | `transaction` | `<T>(fn: (db: MarketplaceDb) => Promise<T>) => Promise<T>` | Prisma's interactive `$transaction`: run `fn` with the transaction's client; a throw rolls back. Every bulk write and every decision (admission + claim) is one transaction. |
16
16
  | `order.admit` | `(request: { clientId; orderId; decision: "ACCEPT" \| "REJECT"; admission: MarketplaceAdmission; db: MarketplaceDb }) => Promise<{ admitted: true } \| { admitted: false; code: string; message: string; details?: unknown }>` | Judge each decision BEFORE its claim commits, inside the claim's transaction (`request.db` is the transaction's client: draw the stock there, and a refusal anywhere in the call rolls the draw back). `admission` is `{ admitStockPastZero?: boolean }` as the person stated it. Refuse with your own code: the route answers 409 with it (`MARKETPLACE_STOCK_ASK` and the short lines in `details`). Never called for an ingest or `recordStoreCancellation`. |
17
- | `order.create` | `(clientId, order: NormalizedMarketplaceOrder, lines: readonly ResolvedOrderLine[], db: MarketplaceDb) => Promise<string>` | Create the host's own order from a normalized one; return its id. `lines[i].subjectRef` is the mapped catalog id or null (never refuse the order for a null). `db` is the ingest's transaction client: write the order through it, so the order and the acceptance row commit together (a lost race rolls the host's order back instead of leaving an orphan a redelivery would duplicate). |
17
+ | `order.create` | `(clientId, order: NormalizedMarketplaceOrder, lines: readonly ResolvedOrderLine[], db: MarketplaceDb) => Promise<string>` | Create the host's own order from a normalized one; return its id. `lines[i].subjectRef` is the mapped catalog id or null (never refuse the order for a null); `lines[i].variationRef` is the variation of it the SKU was mapped to, or null for the item alone. `db` is the ingest's transaction client: write the order through it, so the order and the acceptance row commit together (a lost race rolls the host's order back instead of leaving an orphan a redelivery would duplicate). |
18
18
  | `order.catalogHas` | `(clientId, subjectRef) => Promise<boolean>` | Does this catalog id exist and belong to this tenant? Asked before a SKU is mapped. |
19
+ | `order.catalogHasVariation` (optional) | `(clientId, subjectRef, variationRef) => Promise<boolean>` | Is `variationRef` a variation of this tenant's catalog id `subjectRef`? Asked before a SKU is mapped to a variation, after `catalogHas`. Absent: a mapping that names a variation is refused (`CATALOG_ITEM_NOT_FOUND` at `variationId`). |
19
20
  | `order.onAccepted` | `(clientId, orderId) => Promise<void>` | The store accepted, or the connection auto-accepts. Start whatever accepting means here (the ticket, the board). Runs after the commit and AFTER the confirm is queued; a throw is logged and cannot un-accept or lose the confirm. Atomic work (the stock draw) belongs in `admit`. |
20
21
  | `order.mayAutoAccept` | OPTIONAL. `(clientId) => Promise<boolean>` | May a NEW order of this tenant be accepted automatically? Asked once per order, before the ingest's transaction, and only when the connection auto-accepts (never for a redelivery). `false` declines the automatic acceptance: the order lands exactly as with auto-accept off (AWAITING on the clock, no confirm queued, no `onAccepted`) and a person decides it; the order is never refused for it. A throw is logged and declines too. Not bound: every auto-accept proceeds, as before. It sits on every order's ingest, so answer it from something already in hand rather than a fresh query. |
21
22
  | `order.onRefused` | `(clientId, orderId, reason: "REJECTED" \| "EXPIRED" \| "CANCELLED") => Promise<void>` | Refuse/cancel the host order: the store refused on the clock, the clock lapsed, or the marketplace cancelled. After the commit and after the cancellation request is queued; a throw on a decision is logged. NOT called for `recordStoreCancellation`. A host that binds `onCancellationConfirmed` (request first) does not get `"REJECTED"` from `decide` either, nor `"CANCELLED"` for a cancellation the store asked for: `onCancellationConfirmed` replaces both. |
@@ -292,3 +293,28 @@ if you want one). The `MarketplaceDb.marketplaceStorePause` delegate is
292
293
  OPTIONAL in the type, so a 1.x host compiles before it migrates; `pauseStore`
293
294
  and `resumeStore` throw naming the migration until it does, and
294
295
  `availabilityState` reads every marketplace OPEN.
296
+
297
+ ## 8. Mapping a SKU to a variation (FUT-4927)
298
+
299
+ A marketplace SKU usually names one flavour or size of an item. A link can now
300
+ carry that variation beside the catalog id: `variationId` on
301
+ `upsertMenuLinks` / `setMenuLink` rows, on `PUT …/menu-links` and its MCP
302
+ tool, and on every link view. Absent or null maps the item alone, exactly as
303
+ before; a write always sets both halves, so re-mapping to the item clears a
304
+ variation and an unmap clears both. A variation on an unmap is refused.
305
+
306
+ **The port.** Bind `order.catalogHasVariation(clientId, subjectRef,
307
+ variationRef)` to answer whether the variation is one of that item's and the
308
+ tenant's. Without it every mapping that names a variation is refused, and
309
+ mapping to the item works as it did.
310
+
311
+ **The line.** `ResolvedOrderLine.variationRef` is the mapped variation, or
312
+ null. Write it on the order line with `subjectRef`; a host that ignores it
313
+ keeps selling the item alone.
314
+
315
+ **The database.** Migration `20261126230000_marketplace_menu_link_variation`
316
+ adds the nullable `marketplace_menu_links.variation_id`: additive, no foreign
317
+ key, every existing link stays the item alone. The engine selects the column,
318
+ so a Prisma client generated before the migration throws on the unknown
319
+ `variationId`: on the bump, sync the partial, run `prisma generate` and apply
320
+ the migration together.
package/README.md CHANGED
@@ -42,7 +42,7 @@ engine code (`src/__tests__/second-adapter.test.ts`).
42
42
  | **Ingest** | Idempotent on `(provider, externalOrderId)`. Unmapped SKUs are rows on the attention list, never a rejection. The deadline is stamped at ingest; auto-accept is born accepted in one write and queues the confirm, unless the host's optional `order.mayAutoAccept` declines it, and then the order lands AWAITING as if auto-accept were off. |
43
43
  | **Acceptance clock** | `AWAITING → ACCEPTED | REJECTED | EXPIRED`, every transition predicated on the current state. The sweep never touches an accepted order. |
44
44
  | **Decisions** | Many at once, all-or-nothing: accept (with the person's admission and a preparation time), or refuse with a reason code validated against the adapter's own list and, for a reason that needs them, the order's offending SKUs. The host admits each decision inside the claim's transaction (`order.admit`: the stock ask, the draw). For a host that binds `onCancellationConfirmed`, the store's refusal or cancel is a REQUEST: only the request moves, the order stays as it was until the marketplace answers, and the host hears the answer through the optional `onCancellationConfirmed` / `onCancellationFailed` (REFUSED or TIMEOUT). `suggestReason` preselects the item-unavailable reason when the host says stock is short. |
45
- | **SKU map** | External SKU → host catalog id, every SKU ever seen a row; mapping checks the id is the tenant's. |
45
+ | **SKU map** | External SKU → host catalog id, every SKU ever seen a row; mapping checks the id is the tenant's. A link may name ONE variation of that id (`variationId`): the host's optional `order.catalogHasVariation` checks it belongs, and ingest hands it on the line as `variationRef` beside `subjectRef`. Without a variation the SKU maps to the item alone, as before. |
46
46
  | **Integration settings** | Enabled, environment, merchant id, auto-accept, acknowledgement window; credentials sealed as one blob by the host's secrets port, never returned. A connection is archived (reversible, audited), never deleted. |
47
47
  | **Outbound engine** | The `marketplace.outbound` job: confirm after accept (with the preparation time where the marketplace takes one), cancellation request with the stored reason code and SKUs. 2xx done; a 4xx other than 408/425/429 refused and final; anything else retried until the last attempt, which ends FAILED whatever went wrong. Stamps `confirmed_at`, `confirm_failed_at`, `cancellation_status` (PENDING, REQUESTED, CONFIRMED, FAILED). Idempotent by the row. Queued BEFORE the host's half runs, and the `marketplace.outbound-sweep` job re-queues every call still owed, so nothing is lost. The same sweep fails a request still waiting (never delivered, or delivered and unanswered) as TIMEOUT once past its answer window, and its retries stop (the adapter's `cancellationAnswerWindowMs`, or the engine's `cancellationWindowMs`), so no request waits forever. |
48
48
  | **Order fetch** | For a marketplace whose events name an order without carrying it (Open Delivery's `CREATED`), the adapter marks the event `orderToFetch`: the webhook records it PENDING and queues `marketplace.fetch-order`, which calls the adapter's `fetchOrder` and ingests through the same ingest path. No request calls out; the reconciliation sweep re-queues a row still owed, and a host that holds the job holds every fetch. |
@@ -667,6 +667,7 @@ var LINK_SELECT = {
667
667
  externalSku: true,
668
668
  externalName: true,
669
669
  menuItemId: true,
670
+ variationId: true,
670
671
  lastSeenAt: true
671
672
  };
672
673
  function toView2(row) {
@@ -676,6 +677,9 @@ function toView2(row) {
676
677
  externalSku: row.externalSku,
677
678
  externalName: row.externalName ?? row.externalSku,
678
679
  menuItemId: row.menuItemId,
680
+ // A row with no variation key (a fake db) reads as the item alone. A Prisma client
681
+ // generated before the migration throws on `variationId` instead: sync, generate, migrate.
682
+ variationId: row.menuItemId === null ? null : row.variationId ?? null,
679
683
  lastSeenAt: row.lastSeenAt
680
684
  };
681
685
  }
@@ -696,6 +700,7 @@ async function listMarketplaceMenuLinks(ctx, input) {
696
700
  }
697
701
  __name(listMarketplaceMenuLinks, "listMarketplaceMenuLinks");
698
702
  async function upsertLink(db, input) {
703
+ const variationId = input.menuItemId === null ? null : input.variationId ?? null;
699
704
  const row = await db.marketplaceMenuLink.upsert({
700
705
  where: {
701
706
  clientId_provider_externalSku: {
@@ -709,12 +714,13 @@ async function upsertLink(db, input) {
709
714
  provider: input.provider,
710
715
  externalSku: input.externalSku,
711
716
  externalName: input.externalName ?? input.externalSku,
712
- menuItemId: input.menuItemId
717
+ menuItemId: input.menuItemId,
718
+ variationId
713
719
  },
714
720
  // `externalName` is NOT rewritten here. It is the marketplace's snapshot of
715
721
  // what it called the item, and an operator's typing is not a better record
716
722
  // of that than the marketplace's own text.
717
- update: { menuItemId: input.menuItemId },
723
+ update: { menuItemId: input.menuItemId, variationId },
718
724
  select: LINK_SELECT
719
725
  });
720
726
  return toView2(row);
@@ -739,9 +745,12 @@ async function resolveSkus(db, input) {
739
745
  lastSeenAt: input.seenAt
740
746
  },
741
747
  update: { lastSeenAt: input.seenAt },
742
- select: { menuItemId: true }
748
+ select: { menuItemId: true, variationId: true }
749
+ });
750
+ resolved.set(sku.externalSku, {
751
+ menuItemId: row.menuItemId,
752
+ variationId: row.menuItemId === null ? null : row.variationId ?? null
743
753
  });
744
- resolved.set(sku.externalSku, row.menuItemId);
745
754
  }
746
755
  return resolved;
747
756
  }
@@ -765,7 +774,8 @@ async function composeLines(db, clientId, order, seenAt) {
765
774
  quantity: line.quantity,
766
775
  unitPriceCents: line.unitPriceCents,
767
776
  note: line.note,
768
- subjectRef: mapping.get(line.externalSku) ?? null
777
+ subjectRef: mapping.get(line.externalSku)?.menuItemId ?? null,
778
+ variationRef: mapping.get(line.externalSku)?.variationId ?? null
769
779
  }));
770
780
  }
771
781
  __name(composeLines, "composeLines");
@@ -2196,4 +2206,4 @@ export {
2196
2206
  AvailabilityRetryError,
2197
2207
  runAvailability
2198
2208
  };
2199
- //# sourceMappingURL=chunk-BMCERPQX.js.map
2209
+ //# sourceMappingURL=chunk-2BA2EHNF.js.map