@12-apps/marketplace 1.5.0 → 1.6.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
@@ -17,6 +17,7 @@ error at mount, never a default that quietly adopts another host's policy.
17
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). |
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
19
  | `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
+ | `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. |
20
21
  | `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. |
21
22
  | `order.onCancellationConfirmed` | OPTIONAL — binding it OPTS IN to request first. `(clientId, orderId) => Promise<void>` | The marketplace CONFIRMED the store's own cancellation request (its event, or the job's answer for a marketplace that answers in the response); the row is REJECTED/CONFIRMED by then. Run the host's cancel here. Bound: the store's refusals and cancels (`decide` REJECT, `recordStoreCancellation`) only REQUEST the cancellation and keep the order as it was until this is called, once (a redelivered confirmation calls nothing), INSTEAD of `onRefused`. Not bound (a 1.0.0 host): everything as 1.0.0 — REJECTED at request time, `onRefused(…, "REJECTED")` from `decide`, and `onRefused(…, "CANCELLED")` on the confirmation. A throw is logged. |
22
23
  | `order.onCancellationFailed` | OPTIONAL. `(clientId, orderId, reason: "REFUSED" \| "TIMEOUT") => Promise<void>` | The store's request FAILED: REFUSED (the marketplace's refusal event, the job's 4xx, its last attempt dying, or — request first — a marketplace whose API cannot take it, at once) or TIMEOUT (the outbound sweep, past the marketplace's answer window, or the 24-hour give-up). Called once, by the path that moved the row. Request first: the order stays as it was (AWAITING or ACCEPTED) and may be accepted again; show the adapter's app link and tell the person. A throw is logged. |
package/README.md CHANGED
@@ -39,7 +39,7 @@ engine code (`src/__tests__/second-adapter.test.ts`).
39
39
  | **Canonical types** | `NormalizedEvent`, `NormalizedMarketplaceOrder`: what every adapter normalizes into. Poorer than any marketplace's payload on purpose; the raw payload stays in the inbox row. |
40
40
  | **Inbox** | Every delivery recorded before it is processed, deduped on `(provider, eventId)`. A redelivery is a DB-level no-op; a crash mid-ingest leaves a PENDING row the sweep retries. |
41
41
  | **Pipeline** | normalize → inbox → ingest / cancel / cancellation-refused / ignore. Never throws. |
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. |
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
45
  | **SKU map** | External SKU → host catalog id, every SKU ever seen a row; mapping checks the id is the tenant's. |
@@ -13,7 +13,7 @@ import {
13
13
  CatalogueRetryError,
14
14
  FetchOrderRetryError,
15
15
  OutboundRetryError
16
- } from "./chunk-FKUEFVVA.js";
16
+ } from "./chunk-NFZT5TWR.js";
17
17
  import {
18
18
  __name
19
19
  } from "./chunk-7QVYU63E.js";
@@ -473,4 +473,4 @@ export {
473
473
  MARKETPLACE_JOBS,
474
474
  createApiMarketplace
475
475
  };
476
- //# sourceMappingURL=chunk-UONWZIBO.js.map
476
+ //# sourceMappingURL=chunk-4CJ7B5OR.js.map
@@ -760,8 +760,7 @@ async function composeLines(db, clientId, order, seenAt) {
760
760
  }));
761
761
  }
762
762
  __name(composeLines, "composeLines");
763
- function acceptanceData(clientId, orderId, order, settings, now, clockStartsAt) {
764
- const accepted = settings.autoAccept;
763
+ function acceptanceData(clientId, orderId, order, settings, accepted, now, clockStartsAt) {
765
764
  return {
766
765
  clientId,
767
766
  orderId,
@@ -780,6 +779,20 @@ function acceptanceData(clientId, orderId, order, settings, now, clockStartsAt)
780
779
  };
781
780
  }
782
781
  __name(acceptanceData, "acceptanceData");
782
+ async function hostAllowsAutoAccept(ctx, clientId, order) {
783
+ const ask = ctx.ports.order.mayAutoAccept;
784
+ if (!ask) return true;
785
+ try {
786
+ return await ask.call(ctx.ports.order, clientId);
787
+ } catch (error) {
788
+ ctx.ports.logger.warn(
789
+ `marketplace: mayAutoAccept threw for ${order.provider} order ${order.externalOrderId}; it lands awaiting a decision`,
790
+ error
791
+ );
792
+ return false;
793
+ }
794
+ }
795
+ __name(hostAllowsAutoAccept, "hostAllowsAutoAccept");
783
796
  async function ingestMarketplaceOrder(ctx, input) {
784
797
  const { clientId, order, settings } = input;
785
798
  const now = input.now ?? ctx.ports.clock.now();
@@ -792,11 +805,11 @@ async function ingestMarketplaceOrder(ctx, input) {
792
805
  if (existing) {
793
806
  return { orderId: existing.orderId, created: false, unmappedSkus: [], autoAccepted: false };
794
807
  }
795
- const accepted = settings.autoAccept;
808
+ const accepted = settings.autoAccept && await hostAllowsAutoAccept(ctx, clientId, order);
796
809
  const { orderId, unmappedSkus } = await ctx.ports.transaction(async (tx) => {
797
810
  const lines = await composeLines(tx, clientId, order, now);
798
811
  const created = await ctx.ports.order.create(clientId, order, lines, tx);
799
- await tx.marketplaceAcceptance.create({ data: acceptanceData(clientId, created, order, settings, now, clockStartsAt) });
812
+ await tx.marketplaceAcceptance.create({ data: acceptanceData(clientId, created, order, settings, accepted, now, clockStartsAt) });
800
813
  await markMarketplaceEventSeen(tx, clientId, order.provider, now);
801
814
  return { orderId: created, unmappedSkus: lines.filter((l) => l.subjectRef === null).map((l) => l.externalSku) };
802
815
  });
@@ -1993,4 +2006,4 @@ export {
1993
2006
  AvailabilityRetryError,
1994
2007
  runAvailability
1995
2008
  };
1996
- //# sourceMappingURL=chunk-FKUEFVVA.js.map
2009
+ //# sourceMappingURL=chunk-NFZT5TWR.js.map