@12-apps/marketplace 1.18.0 → 1.19.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.
@@ -17,6 +17,32 @@ type MarketplaceLifecycleAnswer = "moved" | "unchanged" | {
17
17
  reason: string;
18
18
  };
19
19
 
20
+ /**
21
+ * Where a menu link really points (FUT-6258). A host whose catalog lets one
22
+ * item be a variation of another can be handed an id that is the variation
23
+ * itself, with no variation named: a link stored that way names neither the
24
+ * item nor the variation the SKU means. The host answers where such an id
25
+ * points, and the engine stores that pair.
26
+ */
27
+ /** A link's stored target: the catalog item, and the variation of it the SKU names. */
28
+ interface MarketplaceLinkTarget {
29
+ menuItemId: string;
30
+ variationId: string;
31
+ }
32
+ /** The order port's optional half for link targets. */
33
+ interface LinkTargetOrderPort {
34
+ /**
35
+ * OPTIONAL. Where a link naming these catalog ids with no variation really
36
+ * points: for each id that is a variation of another item, that item and the
37
+ * variation; an id left out is stored as named. Asked ONCE per write, in
38
+ * bulk, before every menu-link write the engine makes (`upsertMenuLinks`,
39
+ * `setMenuLink`, the publish auto-link); the answer is then checked as any
40
+ * named pair is (`catalogHas`, `catalogHasVariation`). Absent: every link is
41
+ * stored exactly as named.
42
+ */
43
+ resolveLinkTarget?(clientId: string, subjectRefs: readonly string[]): Promise<ReadonlyMap<string, MarketplaceLinkTarget>>;
44
+ }
45
+
20
46
  /**
21
47
  * The order-side vocabulary the host's order port speaks: what a resolved line
22
48
  * is, what the queue shows, what a person stated, and why an order or a
@@ -227,7 +253,7 @@ interface MarketplaceAdmissionRequest {
227
253
  * transition has already been claimed and committed when they are called, and
228
254
  * a host that fails its half must not un-decide the acceptance.
229
255
  */
230
- interface MarketplaceOrderPort extends LogisticsOrderPort {
256
+ interface MarketplaceOrderPort extends LogisticsOrderPort, LinkTargetOrderPort {
231
257
  /**
232
258
  * Judge a decision BEFORE its claim commits, inside the claim's
233
259
  * transaction: the stock check (and the draw) of an accept, anything the
@@ -240,8 +266,7 @@ interface MarketplaceOrderPort extends LogisticsOrderPort {
240
266
  /**
241
267
  * Turn a normalized order into whatever this host sells; hand back its id.
242
268
  * `db` is the ingest's transaction client (the host's `transaction` port):
243
- * write the order through it, so the order and the engine's acceptance row
244
- * commit together or not at all.
269
+ * write the order through it: the order and the engine's acceptance row commit together or not at all.
245
270
  */
246
271
  create(clientId: string, order: NormalizedMarketplaceOrder, lines: readonly ResolvedOrderLine[], db: MarketplaceDb): Promise<string>;
247
272
  /** Does this catalog id exist and belong to this tenant? */
@@ -2086,4 +2111,4 @@ interface MarketplaceEngine extends MarketplaceCourierEngine, MarketplaceConform
2086
2111
  */
2087
2112
  declare function createMarketplaceEngine(config: MarketplaceEngineConfig): MarketplaceEngine;
2088
2113
 
2089
- export { type IngestionTarget as $, type AcceptanceDecision as A, type BulkProblem as B, type CancellationFailureReason as C, type CatalogueStatus as D, type CatalogueTarget as E, type DecideManyResult as F, type DecidedRow as G, type DecisionInput as H, type DecisionProblem as I, type DecisionProblemCode as J, type DecisionResult as K, type DeliveryOutcome as L, type DisputeAnswerInput as M, type DisputeAnswerJobPayload as N, type DisputeAnswerOutcome as O, DisputeAnswerRetryError as P, type DisputeAnswerView as Q, type DisputeRefusalCode as R, type DisputeRow as S, type DisputeStatus as T, type ExpirySweepResult as U, type FetchOrderJobPayload as V, type FetchOrderOutcome as W, FetchOrderRetryError as X, type InformStatusInput as Y, type IngestResult as Z, type IngestSettings as _, type AcceptanceStatus as a, catalogueDedupeKey as a$, type IntegrationRowInput as a0, type LinkFailure as a1, type LinkFinished as a2, type LinkPendingView as a3, type LinkRefusalCode as a4, MARKETPLACE_ACKNOWLEDGE_JOB as a5, MARKETPLACE_AVAILABILITY_JOB as a6, MARKETPLACE_CANCELLATION_REASONS_JOB as a7, MARKETPLACE_CATALOGUE_JOB as a8, MARKETPLACE_DISPUTE_ANSWER_JOB as a9, type MarketplacePorts as aA, type MarketplaceRefusal as aB, type MarketplaceSecretsPort as aC, type MenuLinkRowInput as aD, OUTBOUND_GIVE_UP_MS as aE, OUTBOUND_STALE_MS as aF, type OutboundAction as aG, type OutboundAttempt as aH, type OutboundJobPayload as aI, type OutboundOutcome as aJ, OutboundRetryError as aK, type OutboundSweepResult as aL, type PauseOutcome as aM, type PauseStoreInput as aN, type PauseStoreResult as aO, type PendingAcceptance as aP, type PendingInboxEvent as aQ, type ResolvedOrderLine as aR, type ResumeOutcome as aS, STORE_PAUSE_LEASE_MS as aT, type SaveMarketplaceIntegrationInput as aU, type StoreAvailabilityView as aV, type StorePauseFailureCode as aW, type StorePauseState as aX, type StorePauseWanted as aY, type VerifyDeliveryCodeInput as aZ, availabilityDedupeKey as a_, MARKETPLACE_FETCH_ORDER_JOB as aa, MARKETPLACE_OUTBOUND_JOB as ab, type MarketplaceAdmission as ac, type MarketplaceAdmissionRequest as ad, type MarketplaceAdmissionResult as ae, type MarketplaceAuditEntry as af, type MarketplaceAuditPort as ag, type MarketplaceAuthMode as ah, type MarketplaceCataloguePort as ai, type MarketplaceClockPort as aj, type MarketplaceConformityEngine as ak, type MarketplaceDb as al, type MarketplaceDeliveryCodeEngine as am, type MarketplaceDisputeEngine as an, type MarketplaceDisputeView as ao, type MarketplaceEngine as ap, type MarketplaceEngineConfig as aq, type MarketplaceEnqueuePort as ar, type MarketplaceIntegrationView as as, type MarketplaceLifecycleAnswer as at, type MarketplaceLifecycleFacts as au, type MarketplaceLogger as av, type MarketplaceMenuLinkView as aw, type MarketplaceOrderPort as ax, type MarketplaceOrderState as ay, type MarketplaceOrderSummary as az, type AcknowledgeJobPayload as b, createMarketplaceEngine as b0, disputeView as b1, outboundDedupeKey as b2, type AcknowledgeOutcome as c, AcknowledgeRetryError as d, type AnswerDisputeResult as e, type AvailabilityJobPayload as f, type AvailabilityOutcome as g, AvailabilityRetryError as h, type AvailabilitySweepResult as i, type BulkProblemCode as j, type BulkResult as k, type CancellationReasonsJobPayload as l, type CancellationReasonsOutcome as m, CancellationReasonsRetryError as n, type CancellationStatus as o, type CatalogueAction as p, type CatalogueActionResult as q, type CatalogueDocumentResult as r, type CatalogueFailedAction as s, type CatalogueFailureCode as t, type CatalogueJobPayload as u, type CatalogueOutcome as v, type CataloguePublicationView as w, type CataloguePublishInput as x, type CatalogueRefusalCode as y, CatalogueRetryError as z };
2114
+ export { type IngestionTarget as $, type AcceptanceDecision as A, type BulkProblem as B, type CancellationFailureReason as C, type CatalogueStatus as D, type CatalogueTarget as E, type DecideManyResult as F, type DecidedRow as G, type DecisionInput as H, type DecisionProblem as I, type DecisionProblemCode as J, type DecisionResult as K, type DeliveryOutcome as L, type DisputeAnswerInput as M, type DisputeAnswerJobPayload as N, type DisputeAnswerOutcome as O, DisputeAnswerRetryError as P, type DisputeAnswerView as Q, type DisputeRefusalCode as R, type DisputeRow as S, type DisputeStatus as T, type ExpirySweepResult as U, type FetchOrderJobPayload as V, type FetchOrderOutcome as W, FetchOrderRetryError as X, type InformStatusInput as Y, type IngestResult as Z, type IngestSettings as _, type AcceptanceStatus as a, type VerifyDeliveryCodeInput as a$, type IntegrationRowInput as a0, type LinkFailure as a1, type LinkFinished as a2, type LinkPendingView as a3, type LinkRefusalCode as a4, type LinkTargetOrderPort as a5, MARKETPLACE_ACKNOWLEDGE_JOB as a6, MARKETPLACE_AVAILABILITY_JOB as a7, MARKETPLACE_CANCELLATION_REASONS_JOB as a8, MARKETPLACE_CATALOGUE_JOB as a9, type MarketplaceOrderState as aA, type MarketplaceOrderSummary as aB, type MarketplacePorts as aC, type MarketplaceRefusal as aD, type MarketplaceSecretsPort as aE, type MenuLinkRowInput as aF, OUTBOUND_GIVE_UP_MS as aG, OUTBOUND_STALE_MS as aH, type OutboundAction as aI, type OutboundAttempt as aJ, type OutboundJobPayload as aK, type OutboundOutcome as aL, OutboundRetryError as aM, type OutboundSweepResult as aN, type PauseOutcome as aO, type PauseStoreInput as aP, type PauseStoreResult as aQ, type PendingAcceptance as aR, type PendingInboxEvent as aS, type ResolvedOrderLine as aT, type ResumeOutcome as aU, STORE_PAUSE_LEASE_MS as aV, type SaveMarketplaceIntegrationInput as aW, type StoreAvailabilityView as aX, type StorePauseFailureCode as aY, type StorePauseState as aZ, type StorePauseWanted as a_, MARKETPLACE_DISPUTE_ANSWER_JOB as aa, MARKETPLACE_FETCH_ORDER_JOB as ab, MARKETPLACE_OUTBOUND_JOB as ac, type MarketplaceAdmission as ad, type MarketplaceAdmissionRequest as ae, type MarketplaceAdmissionResult as af, type MarketplaceAuditEntry as ag, type MarketplaceAuditPort as ah, type MarketplaceAuthMode as ai, type MarketplaceCataloguePort as aj, type MarketplaceClockPort as ak, type MarketplaceConformityEngine as al, type MarketplaceDb as am, type MarketplaceDeliveryCodeEngine as an, type MarketplaceDisputeEngine as ao, type MarketplaceDisputeView as ap, type MarketplaceEngine as aq, type MarketplaceEngineConfig as ar, type MarketplaceEnqueuePort as as, type MarketplaceIntegrationView as at, type MarketplaceLifecycleAnswer as au, type MarketplaceLifecycleFacts as av, type MarketplaceLinkTarget as aw, type MarketplaceLogger as ax, type MarketplaceMenuLinkView as ay, type MarketplaceOrderPort as az, type AcknowledgeJobPayload as b, availabilityDedupeKey as b0, catalogueDedupeKey as b1, createMarketplaceEngine as b2, disputeView as b3, outboundDedupeKey as b4, type AcknowledgeOutcome as c, AcknowledgeRetryError as d, type AnswerDisputeResult as e, type AvailabilityJobPayload as f, type AvailabilityOutcome as g, AvailabilityRetryError as h, type AvailabilitySweepResult as i, type BulkProblemCode as j, type BulkResult as k, type CancellationReasonsJobPayload as l, type CancellationReasonsOutcome as m, CancellationReasonsRetryError as n, type CancellationStatus as o, type CatalogueAction as p, type CatalogueActionResult as q, type CatalogueDocumentResult as r, type CatalogueFailedAction as s, type CatalogueFailureCode as t, type CatalogueJobPayload as u, type CatalogueOutcome as v, type CataloguePublicationView as w, type CataloguePublishInput as x, type CatalogueRefusalCode as y, CatalogueRetryError as z };
@@ -1,6 +1,6 @@
1
1
  import { Hono, Context } from 'hono';
2
2
  import { ApiMarketplace, MarketplaceApiConfig, MarketplaceActor } from '../server/index.js';
3
- import '../engine-DRRhAjZK.js';
3
+ import '../engine-CaiVufoP.js';
4
4
  import '../adapter-ZpAWKoCk.js';
5
5
  import '@12-apps/wiring';
6
6
  import 'zod';
@@ -1,8 +1,8 @@
1
1
  import {
2
2
  createApiMarketplace
3
- } from "../chunk-BLHZ3MRB.js";
3
+ } from "../chunk-RS2QKQ6S.js";
4
4
  import "../chunk-LO7OKO6V.js";
5
- import "../chunk-MDEIRGNL.js";
5
+ import "../chunk-S2ISELNJ.js";
6
6
  import "../chunk-Q5RMVNU5.js";
7
7
  import {
8
8
  __name
@@ -42,6 +42,25 @@ var IFOOD_REASON_CODES = [
42
42
  "513"
43
43
  ];
44
44
 
45
+ // src/ifood/catalogs.ts
46
+ function defaultCatalogIdIn(body) {
47
+ let parsed2;
48
+ try {
49
+ parsed2 = JSON.parse(body);
50
+ } catch {
51
+ return null;
52
+ }
53
+ if (!Array.isArray(parsed2)) return null;
54
+ for (const entry of parsed2) {
55
+ if (typeof entry !== "object" || entry === null) continue;
56
+ const { catalogId, context } = entry;
57
+ if (typeof catalogId === "string" && catalogId !== "" && Array.isArray(context) && context.includes("DEFAULT")) return catalogId;
58
+ }
59
+ return null;
60
+ }
61
+ __name(defaultCatalogIdIn, "defaultCatalogIdIn");
62
+ var categoriesPath = /* @__PURE__ */ __name((catalogId) => `catalogs/${encodeURIComponent(catalogId)}/categories`, "categoriesPath");
63
+
45
64
  // src/ifood/outbound.ts
46
65
  var IFOOD_API = "https://merchant-api.ifood.com.br";
47
66
  var TOKEN_URL = `${IFOOD_API}/authentication/v1.0/oauth/token`;
@@ -240,23 +259,38 @@ function categoryIdIn(body, name) {
240
259
  return typeof found?.id === "string" ? found.id : null;
241
260
  }
242
261
  __name(categoryIdIn, "categoryIdIn");
243
- async function findCategory(ctx, name) {
244
- const listed = await call(ctx, "GET", "categories");
262
+ async function catalogOf(ctx) {
263
+ const listed = await call(ctx, "GET", "catalogs");
264
+ if ("kind" in listed) return listed;
265
+ if (!ok(listed)) return failureOf(listed);
266
+ const catalogId = defaultCatalogIdIn(listed.body);
267
+ return catalogId === null ? { kind: "refused", status: listed.status, message: `no DEFAULT catalog: ${listed.body.slice(0, 200)}` } : { catalogId };
268
+ }
269
+ __name(catalogOf, "catalogOf");
270
+ async function findCategoryIn(ctx, catalogId, name) {
271
+ const listed = await call(ctx, "GET", categoriesPath(catalogId));
245
272
  if ("kind" in listed) return listed;
246
273
  if (!ok(listed)) return failureOf(listed);
247
274
  return { id: categoryIdIn(listed.body, name) };
248
275
  }
276
+ __name(findCategoryIn, "findCategoryIn");
277
+ async function findCategory(ctx, name) {
278
+ const catalog = await catalogOf(ctx);
279
+ return "kind" in catalog ? catalog : findCategoryIn(ctx, catalog.catalogId, name);
280
+ }
249
281
  __name(findCategory, "findCategory");
250
282
  async function findOrCreateCategory(ctx, name) {
251
- const found = await findCategory(ctx, name);
283
+ const catalog = await catalogOf(ctx);
284
+ if ("kind" in catalog) return catalog;
285
+ const found = await findCategoryIn(ctx, catalog.catalogId, name);
252
286
  if ("kind" in found) return found;
253
287
  if (found.id) return { id: found.id };
254
- const created = await call(ctx, "POST", "categories", { name, status: "AVAILABLE", template: "DEFAULT" });
288
+ const created = await call(ctx, "POST", categoriesPath(catalog.catalogId), { name, status: "AVAILABLE", template: "DEFAULT" });
255
289
  if ("kind" in created) return created;
256
290
  if (!ok(created)) return failureOf(created);
257
291
  const id = parse(created.body)?.id;
258
292
  if (typeof id !== "string") return { kind: "retry", reason: "iFood created the category without an id" };
259
- const again = await findCategory(ctx, name);
293
+ const again = await findCategoryIn(ctx, catalog.catalogId, name);
260
294
  return "id" in again && again.id ? { id: again.id } : { id };
261
295
  }
262
296
  __name(findOrCreateCategory, "findOrCreateCategory");
@@ -524,7 +558,7 @@ __name(tokenFailure, "tokenFailure");
524
558
  function failureOf2(response) {
525
559
  const result = outboundResultOf(response);
526
560
  if (result.kind !== "refused") return { kind: "retry", reason: result.kind === "retry" ? result.reason : "unexpected answer" };
527
- const code = response.status === 403 || response.status === 404 ? "NOT_AUTHORIZED" : "REFUSED";
561
+ const code = response.status === 403 ? "NOT_AUTHORIZED" : "REFUSED";
528
562
  return { kind: "refused", code, status: result.status, message: result.message };
529
563
  }
530
564
  __name(failureOf2, "failureOf");
@@ -602,8 +636,18 @@ function itemsOf(rawBody, categoryId2, notices) {
602
636
  return items;
603
637
  }
604
638
  __name(itemsOf, "itemsOf");
639
+ async function catalogOf2(ctx) {
640
+ const listed = await get(ctx, "catalogs");
641
+ if ("kind" in listed) return listed;
642
+ if (!ok2(listed)) return failureOf2(listed);
643
+ const catalogId = defaultCatalogIdIn(listed.body);
644
+ return catalogId === null ? unexpected("catalog list (no DEFAULT catalog)", listed.body) : { catalogId };
645
+ }
646
+ __name(catalogOf2, "catalogOf");
605
647
  async function read(ctx) {
606
- const listed = await get(ctx, "categories");
648
+ const catalog = await catalogOf2(ctx);
649
+ if ("kind" in catalog) return catalog;
650
+ const listed = await get(ctx, categoriesPath(catalog.catalogId));
607
651
  if ("kind" in listed) return listed;
608
652
  if (!ok2(listed)) return failureOf2(listed);
609
653
  const parsed2 = parse2(listed.body);