@12-apps/marketplace 1.6.0 → 1.8.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 (74) hide show
  1. package/ADOPTING.md +17 -0
  2. package/README.md +56 -0
  3. package/dist/99food/index.d.ts +4 -2
  4. package/dist/99food/index.js +38 -6
  5. package/dist/99food/index.js.map +1 -1
  6. package/dist/{adapter-CCWMqt2g.d.ts → adapter-CYJzu1c9.d.ts} +81 -2
  7. package/dist/{chunk-4CJ7B5OR.js → chunk-6N2HEV3U.js} +4 -4
  8. package/dist/chunk-6N2HEV3U.js.map +1 -0
  9. package/dist/{chunk-NFZT5TWR.js → chunk-BMCERPQX.js} +214 -24
  10. package/dist/chunk-BMCERPQX.js.map +1 -0
  11. package/dist/{chunk-RGMSD7S7.js → chunk-HEXLCTOG.js} +7 -1
  12. package/dist/chunk-HEXLCTOG.js.map +1 -0
  13. package/dist/{chunk-UYFYGGCS.js → chunk-NP4EYKPO.js} +3 -1
  14. package/dist/chunk-NP4EYKPO.js.map +1 -0
  15. package/dist/{engine-w2TgRrlc.d.ts → engine-wmW-VuBL.d.ts} +123 -9
  16. package/dist/hono/index.d.ts +2 -2
  17. package/dist/hono/index.js +3 -3
  18. package/dist/ifood/index.d.ts +1 -1
  19. package/dist/ifood/index.js +66 -6
  20. package/dist/ifood/index.js.map +1 -1
  21. package/dist/index.d.ts +3 -3
  22. package/dist/index.js +119 -12
  23. package/dist/index.js.map +1 -1
  24. package/dist/manifest/index.d.ts +3 -0
  25. package/dist/manifest/index.js +1 -1
  26. package/dist/manifest/server.d.ts +2 -2
  27. package/dist/manifest/server.js +3 -3
  28. package/dist/rappi/index.d.ts +7 -1
  29. package/dist/rappi/index.js +23 -5
  30. package/dist/rappi/index.js.map +1 -1
  31. package/dist/server/index.d.ts +6 -2
  32. package/dist/server/index.js +3 -3
  33. package/package.json +1 -1
  34. package/prisma/marketplace.prisma +48 -4
  35. package/prisma/migrations/20261125180000_marketplace_cancellation_failure_reason_widen/migration.sql +16 -0
  36. package/prisma/migrations/20261126120000_marketplace_status_updates/migration.sql +60 -0
  37. package/src/99food/index.ts +6 -0
  38. package/src/99food/normalize.ts +7 -1
  39. package/src/99food/outbound.ts +62 -2
  40. package/src/core/adapter.ts +58 -3
  41. package/src/core/bulk.ts +1 -1
  42. package/src/core/cancellation.ts +4 -1
  43. package/src/core/catalogue.ts +4 -3
  44. package/src/core/decisions.ts +3 -2
  45. package/src/core/engine.ts +9 -0
  46. package/src/core/fetch-order.ts +2 -2
  47. package/src/core/ingest.ts +6 -0
  48. package/src/core/integration.ts +17 -0
  49. package/src/core/lifecycle-port.ts +16 -0
  50. package/src/core/lifecycle.ts +133 -0
  51. package/src/core/order-fetch-marks.ts +13 -8
  52. package/src/core/outbound-queue.ts +24 -4
  53. package/src/core/outbound-sweep.ts +49 -5
  54. package/src/core/outbound.ts +26 -11
  55. package/src/core/pipeline.ts +35 -3
  56. package/src/core/ports.ts +54 -7
  57. package/src/core/state.ts +1 -1
  58. package/src/core/status-inform.ts +51 -0
  59. package/src/core/status-job.ts +207 -0
  60. package/src/core/status-store.ts +85 -0
  61. package/src/core/types.ts +39 -0
  62. package/src/ifood/index.ts +7 -1
  63. package/src/ifood/normalize.ts +70 -0
  64. package/src/ifood/outbound.ts +37 -1
  65. package/src/index.ts +9 -0
  66. package/src/rappi/index.ts +6 -1
  67. package/src/rappi/normalize.ts +6 -0
  68. package/src/rappi/outbound.ts +33 -3
  69. package/src/server/mcp.ts +2 -0
  70. package/src/server/webhook.ts +3 -1
  71. package/dist/chunk-4CJ7B5OR.js.map +0 -1
  72. package/dist/chunk-NFZT5TWR.js.map +0 -1
  73. package/dist/chunk-RGMSD7S7.js.map +0 -1
  74. package/dist/chunk-UYFYGGCS.js.map +0 -1
package/dist/index.js CHANGED
@@ -1,5 +1,6 @@
1
1
  import {
2
2
  AvailabilityRetryError,
3
+ CANCELLED_BEFORE_INGEST_REASON,
3
4
  CATALOGUE_GIVE_UP_MS,
4
5
  CATALOGUE_ROW_SELECT,
5
6
  CATALOGUE_STALE_MS,
@@ -21,6 +22,7 @@ import {
21
22
  availabilityDedupeKey,
22
23
  awaitOrderBody,
23
24
  cancelAcceptance,
25
+ cancelledBeforeIngest,
24
26
  catalogueConnection,
25
27
  catalogueDedupeKey,
26
28
  catalogueTable,
@@ -42,11 +44,13 @@ import {
42
44
  markCancelledBeforeIngest,
43
45
  merchantMismatch,
44
46
  notPendingProblem,
47
+ noteMarketplaceStep,
45
48
  outboundDedupeKey,
46
49
  pauseTable,
47
50
  planCancellation,
48
51
  readOutboundConnection,
49
52
  readPause,
53
+ readStatusRows,
50
54
  recordInboxEvent,
51
55
  refusalData,
52
56
  requestData,
@@ -61,12 +65,14 @@ import {
61
65
  runOutbound,
62
66
  saveMarketplaceIntegration,
63
67
  settleInboxEvent,
68
+ statusDedupeKey,
69
+ statusTable,
64
70
  suggestReason,
65
71
  sweepOutbound,
66
72
  tellCancellationFailed,
67
73
  upsertLink,
68
74
  viewOf
69
- } from "./chunk-NFZT5TWR.js";
75
+ } from "./chunk-BMCERPQX.js";
70
76
  import {
71
77
  centsFromMajor
72
78
  } from "./chunk-WN2EK4KX.js";
@@ -75,8 +81,9 @@ import {
75
81
  } from "./chunk-M2V4RFMA.js";
76
82
  import {
77
83
  answered,
78
- outboundResultOf
79
- } from "./chunk-RGMSD7S7.js";
84
+ outboundResultOf,
85
+ tokenGrantResultOf
86
+ } from "./chunk-HEXLCTOG.js";
80
87
  import {
81
88
  __name
82
89
  } from "./chunk-7QVYU63E.js";
@@ -100,7 +107,7 @@ function unknownProviders(ctx, rows, list) {
100
107
  );
101
108
  }
102
109
  __name(unknownProviders, "unknownProviders");
103
- var SETTING_KEYS = ["enabled", "environment", "externalMerchantId", "autoAccept", "ackSlaSeconds", "credentials"];
110
+ var SETTING_KEYS = ["enabled", "environment", "externalMerchantId", "autoAccept", "followStatus", "ackSlaSeconds", "credentials"];
104
111
  function integrationRowProblems(row, index, existing) {
105
112
  if (row.operation === "archive" || row.operation === "unarchive") {
106
113
  return existing.has(row.provider) ? [] : [{ path: ["integrations", index, "provider"], code: "NOT_FOUND" }];
@@ -433,8 +440,8 @@ __name(publishRefusal, "publishRefusal");
433
440
  async function publishToCatalogue(ctx, input) {
434
441
  const table = await usableTable(ctx, input.provider);
435
442
  if (!table) return refused("UNSUPPORTED");
436
- const refusal = await publishRefusal(ctx, input);
437
- if (refusal) return refused(refusal);
443
+ const refusal2 = await publishRefusal(ctx, input);
444
+ if (refusal2) return refused(refusal2);
438
445
  const existing = await findRow(table, input);
439
446
  const now = ctx.ports.clock.now();
440
447
  const price = input.priceCents === void 0 ? {} : { priceCents: input.priceCents };
@@ -493,8 +500,8 @@ async function readCatalogueDocument(ctx, input) {
493
500
  const catalogue = ctx.registry.get(input.provider)?.catalogue;
494
501
  const table = await usableTable(ctx, input.provider);
495
502
  if (!table || catalogue?.mode !== "pull") return { kind: "refused", code: "UNSUPPORTED" };
496
- const connection = await catalogueConnection(ctx, input.clientId, input.provider);
497
- if ("problem" in connection) return { kind: "refused", code: "NOT_CONNECTED" };
503
+ const externalMerchantId = await connectionMerchantId(ctx, input.clientId, input.provider);
504
+ if (externalMerchantId === null) return { kind: "refused", code: "NOT_CONNECTED" };
498
505
  const rows = await table.findMany({
499
506
  where: { clientId: input.clientId, provider: input.provider, status: { in: IN_MENU } },
500
507
  select: CATALOGUE_ROW_SELECT,
@@ -507,7 +514,7 @@ async function readCatalogueDocument(ctx, input) {
507
514
  if (!item || priceCents === null) return [];
508
515
  return [{ ...item, priceCents, ref: row.id, productId: row.productId, externalId: row.externalId, storedImage: null }];
509
516
  });
510
- const document = catalogue.menuDocument({ externalMerchantId: connection.externalMerchantId, entries, options: input.options });
517
+ const document = catalogue.menuDocument({ externalMerchantId, entries, options: input.options });
511
518
  return { kind: "document", document };
512
519
  }
513
520
  __name(readCatalogueDocument, "readCatalogueDocument");
@@ -548,6 +555,30 @@ var engineRefusal = /* @__PURE__ */ __name((code) => ({
548
555
  message: null
549
556
  }), "engineRefusal");
550
557
 
558
+ // src/core/status-inform.ts
559
+ async function informStatus(ctx, input) {
560
+ const payload = { clientId: input.clientId, orderId: input.orderId, action: "status", step: input.step, claimKey: String(input.at.getTime()) };
561
+ const options = {
562
+ dedupeKey: statusDedupeKey(input.step, input.orderId, input.at),
563
+ ...input.delayMs === void 0 ? {} : { delayMs: input.delayMs }
564
+ };
565
+ try {
566
+ const outcome = await ctx.ports.enqueue.enqueue(MARKETPLACE_OUTBOUND_JOB, payload, options);
567
+ if (!outcome.enqueued) {
568
+ ctx.ports.logger.warn(
569
+ `${MARKETPLACE_OUTBOUND_JOB}: status ${input.step} for order ${input.orderId} was not queued` + (outcome.reason ? ` (${outcome.reason})` : "")
570
+ );
571
+ }
572
+ return outcome.enqueued;
573
+ } catch (error) {
574
+ ctx.ports.logger.warn(
575
+ `${MARKETPLACE_OUTBOUND_JOB}: status ${input.step} for order ${input.orderId} was not queued: ` + (error instanceof Error ? error.message : String(error))
576
+ );
577
+ return false;
578
+ }
579
+ }
580
+ __name(informStatus, "informStatus");
581
+
551
582
  // src/core/pause.ts
552
583
  async function liveConnections(ctx, clientId) {
553
584
  const db = await ctx.ports.db();
@@ -826,6 +857,68 @@ async function sweepAvailability(ctx, now) {
826
857
  }
827
858
  __name(sweepAvailability, "sweepAvailability");
828
859
 
860
+ // src/core/lifecycle.ts
861
+ var ECHO_OF = {
862
+ DISPATCHED: "DISPATCHED",
863
+ CONCLUDED: "HANDED_OVER"
864
+ };
865
+ async function ignored(ctx, inboxId, reason) {
866
+ await settleInboxEvent(ctx, inboxId, "IGNORED", reason);
867
+ return { kind: "ignored", reason };
868
+ }
869
+ __name(ignored, "ignored");
870
+ function refusal(held) {
871
+ if (held.status !== "ACCEPTED") return `the order is ${held.status}, not ACCEPTED`;
872
+ if (held.cancellationStatus === "PENDING" || held.cancellationStatus === "REQUESTED") {
873
+ return `a cancellation request is ${held.cancellationStatus}`;
874
+ }
875
+ return null;
876
+ }
877
+ __name(refusal, "refusal");
878
+ function deliveredByOf(held) {
879
+ return held.deliveredBy === "MERCHANT" || held.deliveredBy === "MARKETPLACE" ? held.deliveredBy : null;
880
+ }
881
+ __name(deliveredByOf, "deliveredByOf");
882
+ function following(ctx, event, settings) {
883
+ const hook = ctx.ports.order.onLifecycle;
884
+ if (!event.lifecycle || !event.externalOrderId || !settings.followStatus || !hook) return null;
885
+ return { lifecycle: event.lifecycle, externalOrderId: event.externalOrderId, hook };
886
+ }
887
+ __name(following, "following");
888
+ async function followLifecycle(ctx, inboxId, clientId, event, settings) {
889
+ const follow = following(ctx, event, settings);
890
+ if (!follow) return null;
891
+ const { lifecycle, hook } = follow;
892
+ const db = await ctx.ports.db();
893
+ const table = statusTable(db);
894
+ if (!table) return null;
895
+ const held = await db.marketplaceAcceptance.findFirst({
896
+ where: { clientId, provider: event.provider, externalOrderId: follow.externalOrderId },
897
+ select: { orderId: true, status: true, deliveredBy: true, cancellationStatus: true }
898
+ });
899
+ if (!held) {
900
+ const reason2 = "lifecycle before its order";
901
+ await deferInboxEvent(ctx, inboxId, reason2);
902
+ return { kind: "ignored", reason: reason2 };
903
+ }
904
+ const stopped = refusal(held);
905
+ if (stopped) return ignored(ctx, inboxId, stopped);
906
+ const echo = ECHO_OF[lifecycle];
907
+ const ours = (await readStatusRows(table, held.orderId, [echo])).some((row) => row.source === "STORE");
908
+ if (ours) return ignored(ctx, inboxId, `echo of our ${echo}`);
909
+ const answer = await hook.call(ctx.ports.order, clientId, held.orderId, event, { deliveredBy: deliveredByOf(held) });
910
+ await noteMarketplaceStep(ctx, { clientId, orderId: held.orderId, provider: event.provider, step: lifecycle });
911
+ if (answer === "moved") {
912
+ await settleInboxEvent(ctx, inboxId, "PROCESSED");
913
+ ctx.ports.publishChanged(clientId);
914
+ return { kind: "moved", orderId: held.orderId, lifecycle };
915
+ }
916
+ const reason = answer === "unchanged" ? `already at or past ${lifecycle}` : answer.reason;
917
+ if (answer !== "unchanged") ctx.ports.logger.warn(`marketplace.lifecycle: ${event.provider} order ${held.orderId}: ${reason}`);
918
+ return ignored(ctx, inboxId, reason);
919
+ }
920
+ __name(followLifecycle, "followLifecycle");
921
+
829
922
  // src/core/pipeline.ts
830
923
  function normalizeEvent(ctx, provider, payload) {
831
924
  return ctx.registry.get(provider)?.normalize(payload) ?? null;
@@ -880,6 +973,12 @@ async function applyEvent(ctx, inboxId, clientId, event, settings, now) {
880
973
  await awaitOrderBody(ctx, clientId, inboxId);
881
974
  return { kind: "awaitingOrder" };
882
975
  }
976
+ const followed = await followLifecycle(ctx, inboxId, clientId, event, settings);
977
+ if (followed) return followed;
978
+ return ingestOrIgnore(ctx, inboxId, clientId, event, settings, now);
979
+ }
980
+ __name(applyEvent, "applyEvent");
981
+ async function ingestOrIgnore(ctx, inboxId, clientId, event, settings, now) {
883
982
  if (!event.order) {
884
983
  const reason = `no order body on ${event.eventType}`;
885
984
  await settleInboxEvent(ctx, inboxId, "IGNORED", reason);
@@ -887,7 +986,7 @@ async function applyEvent(ctx, inboxId, clientId, event, settings, now) {
887
986
  }
888
987
  return ingestCarried(ctx, inboxId, clientId, event.order, settings, now);
889
988
  }
890
- __name(applyEvent, "applyEvent");
989
+ __name(ingestOrIgnore, "ingestOrIgnore");
891
990
  async function ingestCarried(ctx, inboxId, clientId, order, settings, now) {
892
991
  const mismatch = await carriedOrderMismatch(ctx, clientId, order);
893
992
  if (mismatch) {
@@ -895,7 +994,13 @@ async function ingestCarried(ctx, inboxId, clientId, order, settings, now) {
895
994
  ctx.ports.logger.error(`marketplace.ingest: inbox row ${inboxId} refused: ${mismatch}`);
896
995
  return { kind: "failed", reason: mismatch };
897
996
  }
997
+ if (await cancelledBeforeIngest(ctx, { clientId, provider: order.provider, externalOrderId: order.externalOrderId })) {
998
+ await settleInboxEvent(ctx, inboxId, "IGNORED", CANCELLED_BEFORE_INGEST_REASON);
999
+ return { kind: "ignored", reason: CANCELLED_BEFORE_INGEST_REASON };
1000
+ }
898
1001
  const result = await ingestMarketplaceOrder(ctx, { clientId, order, settings, now });
1002
+ const ref = { clientId, provider: order.provider, externalOrderId: order.externalOrderId };
1003
+ if (await cancelledBeforeIngest(ctx, ref)) await cancelAcceptance(ctx, ref);
899
1004
  await settleInboxEvent(ctx, inboxId, "PROCESSED");
900
1005
  return { kind: "ingested", orderId: result.orderId, unmappedSkus: result.unmappedSkus };
901
1006
  }
@@ -920,7 +1025,7 @@ async function reprocessStoredDelivery(ctx, input) {
920
1025
  input.id,
921
1026
  input.clientId,
922
1027
  event,
923
- { autoAccept: target.autoAccept, ackSlaSeconds: target.ackSlaSeconds },
1028
+ { autoAccept: target.autoAccept, ackSlaSeconds: target.ackSlaSeconds, followStatus: target.followStatus },
924
1029
  void 0
925
1030
  );
926
1031
  } catch (error) {
@@ -1018,6 +1123,7 @@ function createMarketplaceEngine(config) {
1018
1123
  sweepOutbound: /* @__PURE__ */ __name((now) => sweepOutbound(ctx, now ?? ports.clock.now()), "sweepOutbound"),
1019
1124
  runOutbound: /* @__PURE__ */ __name((payload, attempt) => runOutbound(ctx, payload, attempt), "runOutbound"),
1020
1125
  runFetchOrder: /* @__PURE__ */ __name((payload, attempt) => runFetchOrder(ctx, payload, attempt), "runFetchOrder"),
1126
+ informStatus: /* @__PURE__ */ __name((input) => informStatus(ctx, input), "informStatus"),
1021
1127
  // Null too when the host bound no catalogue port: nothing could be published.
1022
1128
  catalogueMode: /* @__PURE__ */ __name((provider) => ports.catalogue ? registry.get(provider)?.catalogue?.mode ?? null : null, "catalogueMode"),
1023
1129
  publish: /* @__PURE__ */ __name((input) => publishToCatalogue(ctx, input), "publish"),
@@ -1060,6 +1166,7 @@ export {
1060
1166
  outboundResultOf,
1061
1167
  resolveReason,
1062
1168
  stableUuid,
1063
- suggestReason
1169
+ suggestReason,
1170
+ tokenGrantResultOf
1064
1171
  };
1065
1172
  //# sourceMappingURL=index.js.map