@12-apps/marketplace 1.5.0 → 1.7.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.
Files changed (50) hide show
  1. package/ADOPTING.md +1 -0
  2. package/README.md +1 -1
  3. package/dist/99food/index.d.ts +1 -1
  4. package/dist/99food/index.js +7 -4
  5. package/dist/99food/index.js.map +1 -1
  6. package/dist/{adapter-CCWMqt2g.d.ts → adapter-DOjTak1H.d.ts} +37 -2
  7. package/dist/{chunk-RGMSD7S7.js → chunk-L3DCOZUU.js} +7 -1
  8. package/dist/chunk-L3DCOZUU.js.map +1 -0
  9. package/dist/{chunk-FKUEFVVA.js → chunk-SJUKBCVO.js} +34 -17
  10. package/dist/chunk-SJUKBCVO.js.map +1 -0
  11. package/dist/{chunk-UONWZIBO.js → chunk-YVUZHXUF.js} +2 -2
  12. package/dist/{engine-C3uq_BJd.d.ts → engine-DG1fg43Y.d.ts} +32 -7
  13. package/dist/hono/index.d.ts +2 -2
  14. package/dist/hono/index.js +2 -2
  15. package/dist/ifood/index.d.ts +1 -1
  16. package/dist/ifood/index.js +38 -4
  17. package/dist/ifood/index.js.map +1 -1
  18. package/dist/index.d.ts +3 -3
  19. package/dist/index.js +17 -7
  20. package/dist/index.js.map +1 -1
  21. package/dist/manifest/server.d.ts +2 -2
  22. package/dist/manifest/server.js +2 -2
  23. package/dist/rappi/index.d.ts +1 -1
  24. package/dist/rappi/index.js +4 -3
  25. package/dist/rappi/index.js.map +1 -1
  26. package/dist/server/index.d.ts +2 -2
  27. package/dist/server/index.js +2 -2
  28. package/package.json +1 -1
  29. package/prisma/marketplace.prisma +3 -3
  30. package/prisma/migrations/20261125180000_marketplace_cancellation_failure_reason_widen/migration.sql +16 -0
  31. package/src/99food/outbound.ts +7 -2
  32. package/src/core/adapter.ts +17 -2
  33. package/src/core/cancellation.ts +4 -1
  34. package/src/core/catalogue.ts +4 -3
  35. package/src/core/decisions.ts +3 -2
  36. package/src/core/fetch-order.ts +2 -2
  37. package/src/core/ingest.ts +32 -4
  38. package/src/core/order-fetch-marks.ts +13 -8
  39. package/src/core/outbound.ts +15 -11
  40. package/src/core/pipeline.ts +13 -1
  41. package/src/core/ports.ts +26 -4
  42. package/src/core/state.ts +1 -1
  43. package/src/core/types.ts +25 -0
  44. package/src/ifood/normalize.ts +53 -0
  45. package/src/ifood/outbound.ts +2 -1
  46. package/src/index.ts +3 -0
  47. package/src/rappi/outbound.ts +2 -1
  48. package/dist/chunk-FKUEFVVA.js.map +0 -1
  49. package/dist/chunk-RGMSD7S7.js.map +0 -1
  50. /package/dist/{chunk-UONWZIBO.js.map → chunk-YVUZHXUF.js.map} +0 -0
@@ -1,4 +1,4 @@
1
- import { q as CatalogueItem, p as CatalogueImage, a as NormalizedMarketplaceOrder, R as MarketplaceHttp, c as MarketplaceAdapter, i as MarketplaceProvider, L as MarketplaceCredentials, H as CatalogueWarningCode, j as CancellationReason, Z as OutboundOrderRef, N as NormalizedEvent, b as CatalogueMenuOptions, w as CatalogueReadResult } from './adapter-CCWMqt2g.js';
1
+ import { q as CatalogueItem, p as CatalogueImage, a as NormalizedMarketplaceOrder, T as MarketplaceHttp, c as MarketplaceAdapter, i as MarketplaceProvider, L as MarketplaceCredentials, H as CatalogueWarningCode, j as CancellationReason, $ as OutboundOrderRef, N as NormalizedEvent, b as CatalogueMenuOptions, w as CatalogueReadResult } from './adapter-DOjTak1H.js';
2
2
 
3
3
  /**
4
4
  * What the engine needs FROM its host, and nothing more.
@@ -147,11 +147,22 @@ type MarketplaceAdmissionResult = {
147
147
  /** Why the engine tells the host to refuse an order. Vocabulary, not copy. */
148
148
  type MarketplaceRefusal = "REJECTED" | "EXPIRED" | "CANCELLED";
149
149
  /**
150
- * Why the store's own cancellation request FAILED: REFUSED (the marketplace
151
- * said no, or every attempt failed) or TIMEOUT (no answer inside the
152
- * marketplace's cancellation window). Vocabulary, not copy.
150
+ * Why the store's own cancellation request FAILED. Vocabulary, not copy.
151
+ *
152
+ * - `REFUSED`: the marketplace judged the request and denied it: its denial
153
+ * event, an answer that is its business refusal, or an adapter that has no
154
+ * cancellation API to call. An adapter that cannot tell a business 4xx from
155
+ * a malformed request (iFood, Rappi) reports every final 4xx as REFUSED.
156
+ * - `UNREACHABLE`: our own request never got a judgement: no credentials or
157
+ * ones that do not open, a token the marketplace did not grant, a request
158
+ * the marketplace calls malformed (99Food's 400), or every attempt failed
159
+ * on the network or a 5xx. Retrying, or fixing the connection, can help.
160
+ * - `TIMEOUT`: no answer inside the marketplace's cancellation window.
161
+ *
162
+ * A host written before `UNREACHABLE` existed treats an unknown value as it
163
+ * treats REFUSED: the order stays and a person finishes in the marketplace's app.
153
164
  */
154
- type CancellationFailureReason = "REFUSED" | "TIMEOUT";
165
+ type CancellationFailureReason = "REFUSED" | "UNREACHABLE" | "TIMEOUT";
155
166
  /**
156
167
  * The host's domain, as five questions the engine asks and never answers.
157
168
  *
@@ -180,6 +191,17 @@ interface MarketplaceOrderPort {
180
191
  catalogHas(clientId: string, subjectRef: string): Promise<boolean>;
181
192
  /** The store said yes, or the connection auto-accepts. */
182
193
  onAccepted(clientId: string, orderId: string): Promise<void>;
194
+ /**
195
+ * OPTIONAL. May an arriving order of this tenant be accepted automatically?
196
+ * Asked once per NEW order of a connection with auto-accept on, before the
197
+ * ingest's transaction (never for a redelivery, never with auto-accept off).
198
+ * `false` declines the automatic acceptance: the order lands exactly as it
199
+ * would with auto-accept off — AWAITING on the clock, no confirm queued, no
200
+ * `onAccepted` — and a person decides it. The order itself is never refused.
201
+ * A throw is logged and declines too. Absent: every auto-accept proceeds.
202
+ * Answer it cheaply: it sits on the ingest path of every order.
203
+ */
204
+ mayAutoAccept?(clientId: string): Promise<boolean>;
183
205
  /** The store said no, the marketplace cancelled, or the clock lapsed. */
184
206
  onRefused(clientId: string, orderId: string, reason: MarketplaceRefusal): Promise<void>;
185
207
  /**
@@ -869,7 +891,10 @@ interface IngestResult {
869
891
  created: boolean;
870
892
  /** SKUs that arrived with nothing behind them, in this order. */
871
893
  unmappedSkus: string[];
872
- /** Whether the connection's policy made this order born accepted. */
894
+ /**
895
+ * Whether this order was born accepted: the connection auto-accepts AND the
896
+ * host's `mayAutoAccept`, when bound, allowed it.
897
+ */
873
898
  autoAccepted: boolean;
874
899
  }
875
900
  /** The store's operating decisions, read from the connection at ingest time. */
@@ -1184,7 +1209,7 @@ interface MarketplaceOrderState {
1184
1209
  confirmFailedAt: Date | null;
1185
1210
  cancellation: CancellationStatus | null;
1186
1211
  cancellationReasonCode: string | null;
1187
- /** Why the request FAILED (REFUSED or TIMEOUT); null unless `cancellation` is FAILED, and on rows from before it. */
1212
+ /** Why the request FAILED (REFUSED, UNREACHABLE or TIMEOUT); null unless `cancellation` is FAILED, and on rows from before it. */
1188
1213
  cancellationFailure: CancellationFailureReason | null;
1189
1214
  /** The adapter's app link, or null when no adapter is registered for it. */
1190
1215
  appUrl: string | null;
@@ -1,7 +1,7 @@
1
1
  import { Hono, Context } from 'hono';
2
2
  import { ApiMarketplace, MarketplaceApiConfig, MarketplaceActor } from '../server/index.js';
3
- import '../engine-C3uq_BJd.js';
4
- import '../adapter-CCWMqt2g.js';
3
+ import '../engine-DG1fg43Y.js';
4
+ import '../adapter-DOjTak1H.js';
5
5
  import '@12-apps/wiring';
6
6
  import 'zod';
7
7
 
@@ -1,8 +1,8 @@
1
1
  import {
2
2
  createApiMarketplace
3
- } from "../chunk-UONWZIBO.js";
3
+ } from "../chunk-YVUZHXUF.js";
4
4
  import "../chunk-UYFYGGCS.js";
5
- import "../chunk-FKUEFVVA.js";
5
+ import "../chunk-SJUKBCVO.js";
6
6
  import {
7
7
  __name
8
8
  } from "../chunk-7QVYU63E.js";
@@ -1,4 +1,4 @@
1
- import { N as NormalizedEvent, d as PushCatalogue, e as CatalogueItemOut, f as CatalogueReader, M as MarketplaceAvailability, A as AdapterCallContext, S as StorePauseRequest, g as StorePauseResult, h as StorePauseRef, O as OutboundResult, c as MarketplaceAdapter } from '../adapter-CCWMqt2g.js';
1
+ import { N as NormalizedEvent, d as PushCatalogue, e as CatalogueItemOut, f as CatalogueReader, M as MarketplaceAvailability, A as AdapterCallContext, S as StorePauseRequest, g as StorePauseResult, h as StorePauseRef, O as OutboundResult, c as MarketplaceAdapter } from '../adapter-DOjTak1H.js';
2
2
 
3
3
  /**
4
4
  * The words the iFood adapter needs from its host, and nothing else.
@@ -10,12 +10,14 @@ import {
10
10
  firstDate,
11
11
  firstString,
12
12
  pick,
13
+ toCents,
13
14
  verifyHmacSha256Hex
14
15
  } from "../chunk-BD7DS3XM.js";
15
16
  import {
16
17
  answered,
17
- outboundResultOf
18
- } from "../chunk-RGMSD7S7.js";
18
+ outboundResultOf,
19
+ tokenGrantResultOf
20
+ } from "../chunk-L3DCOZUU.js";
19
21
  import {
20
22
  __name
21
23
  } from "../chunk-7QVYU63E.js";
@@ -68,7 +70,7 @@ async function bearer(ctx) {
68
70
  clientSecret: ctx.credentials.clientSecret
69
71
  }).toString()
70
72
  });
71
- if (response.status < 200 || response.status >= 300) return outboundResultOf(response);
73
+ if (response.status < 200 || response.status >= 300) return tokenGrantResultOf(response);
72
74
  const token = tokenOf(response.body);
73
75
  if (!token) return { kind: "retry", reason: "iFood answered the token grant without a token" };
74
76
  ctx.tokens.set(key, { token: token.token, expiresAt: ctx.now.getTime() + token.expiresInSeconds * 1e3 });
@@ -590,6 +592,28 @@ function readDeliveredBy(order) {
590
592
  return stated === "MERCHANT" ? "MERCHANT" : null;
591
593
  }
592
594
  __name(readDeliveredBy, "readDeliveredBy");
595
+ function sponsorOf(name) {
596
+ const stated = name?.toUpperCase();
597
+ if (stated === "IFOOD") return "MARKETPLACE";
598
+ return stated === "MERCHANT" ? "MERCHANT" : "OTHER";
599
+ }
600
+ __name(sponsorOf, "sponsorOf");
601
+ function readDiscounts(order) {
602
+ const benefits = pick(order, "benefits");
603
+ if (!Array.isArray(benefits)) return void 0;
604
+ return benefits.flatMap((benefit) => {
605
+ const shares = pick(benefit, "sponsorshipValues");
606
+ const parts = Array.isArray(shares) ? shares.map((share) => ({
607
+ cents: toCents(pick(share, "value")),
608
+ sponsor: sponsorOf(firstString(share, [["name"]])),
609
+ name: firstString(share, [["description"]])
610
+ })) : [{ cents: toCents(pick(benefit, "value")), sponsor: "OTHER", name: null }];
611
+ return parts.flatMap(
612
+ (part) => part.cents === null || part.cents <= 0 ? [] : [{ cents: part.cents, sponsor: part.sponsor, ...part.name === null ? {} : { name: part.name } }]
613
+ );
614
+ });
615
+ }
616
+ __name(readDiscounts, "readDiscounts");
593
617
  function readAddress(order, missingStreetNumber) {
594
618
  const raw = pick(order, "delivery", "deliveryAddress");
595
619
  const street = firstString(raw, [["streetName"], ["street"]]);
@@ -606,6 +630,10 @@ function readAddress(order, missingStreetNumber) {
606
630
  };
607
631
  }
608
632
  __name(readAddress, "readAddress");
633
+ function optional(key, value) {
634
+ return value === void 0 ? {} : { [key]: value };
635
+ }
636
+ __name(optional, "optional");
609
637
  function readOrder(payload, externalOrderId, missingStreetNumber) {
610
638
  const order = pick(payload, "order") ?? payload;
611
639
  const rawItems = pick(order, "items");
@@ -628,7 +656,13 @@ function readOrder(payload, externalOrderId, missingStreetNumber) {
628
656
  placedAt: firstDate(order, [["createdAt"]]) ?? firstDate(payload, [["createdAt"]]) ?? /* @__PURE__ */ new Date(),
629
657
  orderType: readOrderType(order),
630
658
  deliveredBy: readDeliveredBy(order),
631
- deliveryAddress: readAddress(order, missingStreetNumber)
659
+ deliveryAddress: readAddress(order, missingStreetNumber),
660
+ // ESTIMATED names (iFood's order-details guide: `total.deliveryFee`,
661
+ // `total.subTotal`, `benefits[]`). Fee and subtotal are null when unstated,
662
+ // never 0; `discounts` is absent when `benefits` is.
663
+ deliveryFeeCents: firstCents(order, [["total", "deliveryFee"]]),
664
+ subtotalCents: firstCents(order, [["total", "subTotal"]]),
665
+ ...optional("discounts", readDiscounts(order))
632
666
  };
633
667
  }
634
668
  __name(readOrder, "readOrder");