@12-apps/marketplace 1.7.0 → 1.9.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 (96) hide show
  1. package/ADOPTING.md +17 -0
  2. package/README.md +153 -3
  3. package/dist/99food/index.d.ts +30 -52
  4. package/dist/99food/index.js +62 -391
  5. package/dist/99food/index.js.map +1 -1
  6. package/dist/{adapter-DOjTak1H.d.ts → adapter-CYJzu1c9.d.ts} +45 -1
  7. package/dist/{chunk-YVUZHXUF.js → chunk-6N2HEV3U.js} +4 -4
  8. package/dist/chunk-6N2HEV3U.js.map +1 -0
  9. package/dist/{chunk-SJUKBCVO.js → chunk-BMCERPQX.js} +198 -12
  10. package/dist/chunk-BMCERPQX.js.map +1 -0
  11. package/dist/{chunk-L3DCOZUU.js → chunk-HEXLCTOG.js} +1 -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/chunk-PYHNM47K.js +478 -0
  16. package/dist/chunk-PYHNM47K.js.map +1 -0
  17. package/dist/client-B4EQCRD_.d.ts +20 -0
  18. package/dist/{engine-DG1fg43Y.d.ts → engine-wmW-VuBL.d.ts} +107 -4
  19. package/dist/hono/index.d.ts +2 -2
  20. package/dist/hono/index.js +3 -3
  21. package/dist/ifood/index.d.ts +1 -1
  22. package/dist/ifood/index.js +29 -3
  23. package/dist/ifood/index.js.map +1 -1
  24. package/dist/index.d.ts +3 -3
  25. package/dist/index.js +104 -7
  26. package/dist/index.js.map +1 -1
  27. package/dist/keeta/en-US.d.ts +19 -0
  28. package/dist/keeta/en-US.js +23 -0
  29. package/dist/keeta/en-US.js.map +1 -0
  30. package/dist/keeta/index.d.ts +253 -0
  31. package/dist/keeta/index.js +388 -0
  32. package/dist/keeta/index.js.map +1 -0
  33. package/dist/keeta/pt-BR.d.ts +23 -0
  34. package/dist/keeta/pt-BR.js +23 -0
  35. package/dist/keeta/pt-BR.js.map +1 -0
  36. package/dist/manifest/index.d.ts +3 -0
  37. package/dist/manifest/index.js +1 -1
  38. package/dist/manifest/server.d.ts +2 -2
  39. package/dist/manifest/server.js +3 -3
  40. package/dist/rappi/index.d.ts +7 -1
  41. package/dist/rappi/index.js +20 -3
  42. package/dist/rappi/index.js.map +1 -1
  43. package/dist/server/index.d.ts +6 -2
  44. package/dist/server/index.js +3 -3
  45. package/package.json +14 -2
  46. package/prisma/marketplace.prisma +45 -1
  47. package/prisma/migrations/20261126120000_marketplace_status_updates/migration.sql +60 -0
  48. package/src/99food/catalogue.ts +19 -89
  49. package/src/99food/copy.ts +4 -19
  50. package/src/99food/index.ts +29 -28
  51. package/src/99food/normalize.ts +11 -247
  52. package/src/99food/outbound.ts +29 -215
  53. package/src/99food/pause.ts +7 -22
  54. package/src/core/adapter.ts +41 -1
  55. package/src/core/bulk.ts +1 -1
  56. package/src/core/engine.ts +9 -0
  57. package/src/core/ingest.ts +6 -0
  58. package/src/core/integration.ts +17 -0
  59. package/src/core/lifecycle-port.ts +16 -0
  60. package/src/core/lifecycle.ts +133 -0
  61. package/src/core/outbound-queue.ts +24 -4
  62. package/src/core/outbound-sweep.ts +49 -5
  63. package/src/core/outbound.ts +11 -0
  64. package/src/core/pipeline.ts +22 -2
  65. package/src/core/ports.ts +39 -3
  66. package/src/core/status-inform.ts +51 -0
  67. package/src/core/status-job.ts +207 -0
  68. package/src/core/status-store.ts +85 -0
  69. package/src/core/types.ts +14 -0
  70. package/src/ifood/index.ts +7 -1
  71. package/src/ifood/normalize.ts +17 -0
  72. package/src/ifood/outbound.ts +35 -0
  73. package/src/index.ts +8 -1
  74. package/src/keeta/authorization.ts +226 -0
  75. package/src/keeta/copy.ts +27 -0
  76. package/src/keeta/en-US.ts +19 -0
  77. package/src/keeta/index.ts +162 -0
  78. package/src/keeta/merchant.ts +70 -0
  79. package/src/keeta/normalize.ts +131 -0
  80. package/src/keeta/outbound.ts +55 -0
  81. package/src/keeta/pt-BR.ts +23 -0
  82. package/src/keeta/signature.ts +85 -0
  83. package/src/open-delivery/client.ts +185 -0
  84. package/src/open-delivery/merchant.ts +126 -0
  85. package/src/open-delivery/normalize.ts +269 -0
  86. package/src/open-delivery/orders.ts +241 -0
  87. package/src/open-delivery/reasons.ts +26 -0
  88. package/src/rappi/index.ts +6 -1
  89. package/src/rappi/normalize.ts +6 -0
  90. package/src/rappi/outbound.ts +31 -2
  91. package/src/server/mcp.ts +2 -0
  92. package/src/server/webhook.ts +3 -1
  93. package/dist/chunk-L3DCOZUU.js.map +0 -1
  94. package/dist/chunk-SJUKBCVO.js.map +0 -1
  95. package/dist/chunk-UYFYGGCS.js.map +0 -1
  96. package/dist/chunk-YVUZHXUF.js.map +0 -1
package/dist/index.js CHANGED
@@ -44,11 +44,13 @@ import {
44
44
  markCancelledBeforeIngest,
45
45
  merchantMismatch,
46
46
  notPendingProblem,
47
+ noteMarketplaceStep,
47
48
  outboundDedupeKey,
48
49
  pauseTable,
49
50
  planCancellation,
50
51
  readOutboundConnection,
51
52
  readPause,
53
+ readStatusRows,
52
54
  recordInboxEvent,
53
55
  refusalData,
54
56
  requestData,
@@ -63,12 +65,14 @@ import {
63
65
  runOutbound,
64
66
  saveMarketplaceIntegration,
65
67
  settleInboxEvent,
68
+ statusDedupeKey,
69
+ statusTable,
66
70
  suggestReason,
67
71
  sweepOutbound,
68
72
  tellCancellationFailed,
69
73
  upsertLink,
70
74
  viewOf
71
- } from "./chunk-SJUKBCVO.js";
75
+ } from "./chunk-BMCERPQX.js";
72
76
  import {
73
77
  centsFromMajor
74
78
  } from "./chunk-WN2EK4KX.js";
@@ -79,7 +83,7 @@ import {
79
83
  answered,
80
84
  outboundResultOf,
81
85
  tokenGrantResultOf
82
- } from "./chunk-L3DCOZUU.js";
86
+ } from "./chunk-HEXLCTOG.js";
83
87
  import {
84
88
  __name
85
89
  } from "./chunk-7QVYU63E.js";
@@ -103,7 +107,7 @@ function unknownProviders(ctx, rows, list) {
103
107
  );
104
108
  }
105
109
  __name(unknownProviders, "unknownProviders");
106
- var SETTING_KEYS = ["enabled", "environment", "externalMerchantId", "autoAccept", "ackSlaSeconds", "credentials"];
110
+ var SETTING_KEYS = ["enabled", "environment", "externalMerchantId", "autoAccept", "followStatus", "ackSlaSeconds", "credentials"];
107
111
  function integrationRowProblems(row, index, existing) {
108
112
  if (row.operation === "archive" || row.operation === "unarchive") {
109
113
  return existing.has(row.provider) ? [] : [{ path: ["integrations", index, "provider"], code: "NOT_FOUND" }];
@@ -436,8 +440,8 @@ __name(publishRefusal, "publishRefusal");
436
440
  async function publishToCatalogue(ctx, input) {
437
441
  const table = await usableTable(ctx, input.provider);
438
442
  if (!table) return refused("UNSUPPORTED");
439
- const refusal = await publishRefusal(ctx, input);
440
- if (refusal) return refused(refusal);
443
+ const refusal2 = await publishRefusal(ctx, input);
444
+ if (refusal2) return refused(refusal2);
441
445
  const existing = await findRow(table, input);
442
446
  const now = ctx.ports.clock.now();
443
447
  const price = input.priceCents === void 0 ? {} : { priceCents: input.priceCents };
@@ -551,6 +555,30 @@ var engineRefusal = /* @__PURE__ */ __name((code) => ({
551
555
  message: null
552
556
  }), "engineRefusal");
553
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
+
554
582
  // src/core/pause.ts
555
583
  async function liveConnections(ctx, clientId) {
556
584
  const db = await ctx.ports.db();
@@ -829,6 +857,68 @@ async function sweepAvailability(ctx, now) {
829
857
  }
830
858
  __name(sweepAvailability, "sweepAvailability");
831
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
+
832
922
  // src/core/pipeline.ts
833
923
  function normalizeEvent(ctx, provider, payload) {
834
924
  return ctx.registry.get(provider)?.normalize(payload) ?? null;
@@ -883,6 +973,12 @@ async function applyEvent(ctx, inboxId, clientId, event, settings, now) {
883
973
  await awaitOrderBody(ctx, clientId, inboxId);
884
974
  return { kind: "awaitingOrder" };
885
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) {
886
982
  if (!event.order) {
887
983
  const reason = `no order body on ${event.eventType}`;
888
984
  await settleInboxEvent(ctx, inboxId, "IGNORED", reason);
@@ -890,7 +986,7 @@ async function applyEvent(ctx, inboxId, clientId, event, settings, now) {
890
986
  }
891
987
  return ingestCarried(ctx, inboxId, clientId, event.order, settings, now);
892
988
  }
893
- __name(applyEvent, "applyEvent");
989
+ __name(ingestOrIgnore, "ingestOrIgnore");
894
990
  async function ingestCarried(ctx, inboxId, clientId, order, settings, now) {
895
991
  const mismatch = await carriedOrderMismatch(ctx, clientId, order);
896
992
  if (mismatch) {
@@ -929,7 +1025,7 @@ async function reprocessStoredDelivery(ctx, input) {
929
1025
  input.id,
930
1026
  input.clientId,
931
1027
  event,
932
- { autoAccept: target.autoAccept, ackSlaSeconds: target.ackSlaSeconds },
1028
+ { autoAccept: target.autoAccept, ackSlaSeconds: target.ackSlaSeconds, followStatus: target.followStatus },
933
1029
  void 0
934
1030
  );
935
1031
  } catch (error) {
@@ -1027,6 +1123,7 @@ function createMarketplaceEngine(config) {
1027
1123
  sweepOutbound: /* @__PURE__ */ __name((now) => sweepOutbound(ctx, now ?? ports.clock.now()), "sweepOutbound"),
1028
1124
  runOutbound: /* @__PURE__ */ __name((payload, attempt) => runOutbound(ctx, payload, attempt), "runOutbound"),
1029
1125
  runFetchOrder: /* @__PURE__ */ __name((payload, attempt) => runFetchOrder(ctx, payload, attempt), "runFetchOrder"),
1126
+ informStatus: /* @__PURE__ */ __name((input) => informStatus(ctx, input), "informStatus"),
1030
1127
  // Null too when the host bound no catalogue port: nothing could be published.
1031
1128
  catalogueMode: /* @__PURE__ */ __name((provider) => ports.catalogue ? registry.get(provider)?.catalogue?.mode ?? null : null, "catalogueMode"),
1032
1129
  publish: /* @__PURE__ */ __name((input) => publishToCatalogue(ctx, input), "publish"),