@12-apps/marketplace 1.17.0 → 1.18.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 (53) hide show
  1. package/ADOPTING.md +70 -0
  2. package/dist/99food/index.d.ts +2 -2
  3. package/dist/99food/index.js +2 -2
  4. package/dist/{adapter-CxzF-qGx.d.ts → adapter-ZpAWKoCk.d.ts} +116 -2
  5. package/dist/{chunk-7IYUO2KC.js → chunk-3B5EULS6.js} +2 -2
  6. package/dist/{chunk-EABVLQKK.js → chunk-BLHZ3MRB.js} +2 -2
  7. package/dist/{chunk-UZO2NILN.js → chunk-MDEIRGNL.js} +1067 -877
  8. package/dist/chunk-MDEIRGNL.js.map +1 -0
  9. package/dist/{chunk-KKFMDV5K.js → chunk-Q5RMVNU5.js} +1 -1
  10. package/dist/{chunk-KKFMDV5K.js.map → chunk-Q5RMVNU5.js.map} +1 -1
  11. package/dist/{chunk-CF5BSXE5.js → chunk-ZT6TZTLD.js} +1 -1
  12. package/dist/chunk-ZT6TZTLD.js.map +1 -0
  13. package/dist/{client-C-4QvVq3.d.ts → client-DpEq0yXb.d.ts} +1 -1
  14. package/dist/{engine-Y6Rpf5p7.d.ts → engine-DRRhAjZK.d.ts} +171 -143
  15. package/dist/hono/index.d.ts +2 -2
  16. package/dist/hono/index.js +3 -3
  17. package/dist/ifood/index.d.ts +7 -2
  18. package/dist/ifood/index.js +138 -100
  19. package/dist/ifood/index.js.map +1 -1
  20. package/dist/index.d.ts +3 -3
  21. package/dist/index.js +76 -5
  22. package/dist/index.js.map +1 -1
  23. package/dist/keeta/index.d.ts +2 -2
  24. package/dist/keeta/index.js +2 -2
  25. package/dist/manifest/server.d.ts +2 -2
  26. package/dist/manifest/server.js +3 -3
  27. package/dist/rappi/index.d.ts +1 -1
  28. package/dist/rappi/index.js +1 -1
  29. package/dist/server/index.d.ts +2 -2
  30. package/dist/server/index.js +3 -3
  31. package/package.json +1 -1
  32. package/prisma/marketplace.prisma +1 -1
  33. package/prisma/migrations/20261203200000_marketplace_status_logistics_steps/migration.sql +12 -0
  34. package/src/core/adapter.ts +2 -1
  35. package/src/core/delivery-code.ts +119 -0
  36. package/src/core/engine.ts +10 -2
  37. package/src/core/logistics.ts +388 -0
  38. package/src/core/outbound-queue.ts +6 -1
  39. package/src/core/outbound-sweep.ts +10 -5
  40. package/src/core/outbound.ts +4 -5
  41. package/src/core/pipeline.ts +6 -1
  42. package/src/core/ports.ts +5 -2
  43. package/src/core/status-job.ts +3 -0
  44. package/src/core/status-store.ts +2 -1
  45. package/src/core/types.ts +2 -0
  46. package/src/ifood/index.ts +3 -0
  47. package/src/ifood/logistics.ts +38 -0
  48. package/src/ifood/normalize.ts +11 -1
  49. package/src/index.ts +10 -0
  50. package/dist/chunk-CF5BSXE5.js.map +0 -1
  51. package/dist/chunk-UZO2NILN.js.map +0 -1
  52. /package/dist/{chunk-7IYUO2KC.js.map → chunk-3B5EULS6.js.map} +0 -0
  53. /package/dist/{chunk-EABVLQKK.js.map → chunk-BLHZ3MRB.js.map} +0 -0
@@ -1,5 +1,5 @@
1
- import { A as AdapterCallContext, N as NormalizedEvent, a as NormalizedMarketplaceOrder, c as MarketplaceAdapter } from '../adapter-CxzF-qGx.js';
2
- import { a as OpenDeliveryStop, O as OpenDeliveryApiBaseUrls } from '../client-C-4QvVq3.js';
1
+ import { A as AdapterCallContext, N as NormalizedEvent, a as NormalizedMarketplaceOrder, c as MarketplaceAdapter } from '../adapter-ZpAWKoCk.js';
2
+ import { a as OpenDeliveryStop, O as OpenDeliveryApiBaseUrls } from '../client-DpEq0yXb.js';
3
3
 
4
4
  /**
5
5
  * Keeta's MERCHANT AUTHORIZATION, automated from the software (Keeta's
@@ -13,7 +13,7 @@ import {
13
13
  openDeliveryRequestCancellation,
14
14
  openDeliveryStatus,
15
15
  openDeliverySwitchAvailability
16
- } from "../chunk-7IYUO2KC.js";
16
+ } from "../chunk-3B5EULS6.js";
17
17
  import "../chunk-M2V4RFMA.js";
18
18
  import {
19
19
  asString,
@@ -23,7 +23,7 @@ import {
23
23
  } from "../chunk-E4XJXHUK.js";
24
24
  import {
25
25
  outboundResultOf
26
- } from "../chunk-CF5BSXE5.js";
26
+ } from "../chunk-ZT6TZTLD.js";
27
27
  import {
28
28
  __name
29
29
  } from "../chunk-7QVYU63E.js";
@@ -1,7 +1,7 @@
1
- import { aH as OutboundJobPayload, V as FetchOrderJobPayload, b as AcknowledgeJobPayload, l as CancellationReasonsJobPayload, u as CatalogueJobPayload, f as AvailabilityJobPayload, N as DisputeAnswerJobPayload } from '../engine-Y6Rpf5p7.js';
1
+ import { aI as OutboundJobPayload, V as FetchOrderJobPayload, b as AcknowledgeJobPayload, l as CancellationReasonsJobPayload, u as CatalogueJobPayload, f as AvailabilityJobPayload, N as DisputeAnswerJobPayload } from '../engine-DRRhAjZK.js';
2
2
  import * as _12_apps_wiring from '@12-apps/wiring';
3
3
  import { createApiMarketplace, MarketplaceJobDeps } from '../server/index.js';
4
- import '../adapter-CxzF-qGx.js';
4
+ import '../adapter-ZpAWKoCk.js';
5
5
  import 'zod';
6
6
 
7
7
  declare const marketplaceServerManifest: {
@@ -1,10 +1,10 @@
1
1
  import {
2
2
  MARKETPLACE_JOBS,
3
3
  createApiMarketplace
4
- } from "../chunk-EABVLQKK.js";
4
+ } from "../chunk-BLHZ3MRB.js";
5
5
  import "../chunk-LO7OKO6V.js";
6
- import "../chunk-UZO2NILN.js";
7
- import "../chunk-KKFMDV5K.js";
6
+ import "../chunk-MDEIRGNL.js";
7
+ import "../chunk-Q5RMVNU5.js";
8
8
  import "../chunk-7QVYU63E.js";
9
9
 
10
10
  // src/manifest/server.ts
@@ -1,4 +1,4 @@
1
- import { N as NormalizedEvent, a as NormalizedMarketplaceOrder, M as MarketplaceAvailability, A as AdapterCallContext, O as OutboundResult, c as MarketplaceAdapter } from '../adapter-CxzF-qGx.js';
1
+ import { N as NormalizedEvent, a as NormalizedMarketplaceOrder, M as MarketplaceAvailability, A as AdapterCallContext, O as OutboundResult, c as MarketplaceAdapter } from '../adapter-ZpAWKoCk.js';
2
2
 
3
3
  /**
4
4
  * The words the Rappi adapter needs from its host, and nothing else.
@@ -9,7 +9,7 @@ import {
9
9
  answered,
10
10
  outboundResultOf,
11
11
  tokenGrantResultOf
12
- } from "../chunk-CF5BSXE5.js";
12
+ } from "../chunk-ZT6TZTLD.js";
13
13
  import {
14
14
  __name
15
15
  } from "../chunk-7QVYU63E.js";
@@ -1,7 +1,7 @@
1
- import { J as DecisionProblemCode, j as BulkProblemCode, a4 as LinkRefusalCode, ao as MarketplaceEngine, au as MarketplaceLogger, aH as OutboundJobPayload, V as FetchOrderJobPayload, b as AcknowledgeJobPayload, l as CancellationReasonsJobPayload, u as CatalogueJobPayload, f as AvailabilityJobPayload, N as DisputeAnswerJobPayload } from '../engine-Y6Rpf5p7.js';
1
+ import { J as DecisionProblemCode, j as BulkProblemCode, a4 as LinkRefusalCode, ap as MarketplaceEngine, av as MarketplaceLogger, aI as OutboundJobPayload, V as FetchOrderJobPayload, b as AcknowledgeJobPayload, l as CancellationReasonsJobPayload, u as CatalogueJobPayload, f as AvailabilityJobPayload, N as DisputeAnswerJobPayload } from '../engine-DRRhAjZK.js';
2
2
  import { WireRoute, WireJobBlueprint } from '@12-apps/wiring';
3
3
  import { z } from 'zod';
4
- import '../adapter-CxzF-qGx.js';
4
+ import '../adapter-ZpAWKoCk.js';
5
5
 
6
6
  /**
7
7
  * Every problem code a route words from `problems`, decisions and bulk writes
@@ -6,7 +6,7 @@ import {
6
6
  MARKETPLACE_SWEEP_QUEUE,
7
7
  ORDER_NOT_PENDING_CODE,
8
8
  createApiMarketplace
9
- } from "../chunk-EABVLQKK.js";
9
+ } from "../chunk-BLHZ3MRB.js";
10
10
  import {
11
11
  MARKETPLACE_DECIDE,
12
12
  MARKETPLACE_MANAGE,
@@ -20,8 +20,8 @@ import {
20
20
  upsertMarketplaceIntegrationsBody,
21
21
  upsertMarketplaceMenuLinksBody
22
22
  } from "../chunk-LO7OKO6V.js";
23
- import "../chunk-UZO2NILN.js";
24
- import "../chunk-KKFMDV5K.js";
23
+ import "../chunk-MDEIRGNL.js";
24
+ import "../chunk-Q5RMVNU5.js";
25
25
  import "../chunk-7QVYU63E.js";
26
26
  export {
27
27
  MARKETPLACE_AVAILABILITY_ATTEMPTS,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@12-apps/marketplace",
3
- "version": "1.17.0",
3
+ "version": "1.18.0",
4
4
  "type": "module",
5
5
  "sideEffects": false,
6
6
  "description": "One marketplace integration engine for every marketplace: the durable inbox, the ingest pipeline, the acceptance clock and its sweep, the SKU map, the integration settings with sealed credentials, and the outbound engine (confirm, cancellation request, retries), behind an adapter port. Each marketplace is an adapter at its own subpath (./ifood, ./99food and ./keeta on Open Delivery, ./rappi). Framework-neutral wiring route descriptors (./server) with a Hono adapter (./hono), dep-free job blueprints, MCP tools and the package-owned Prisma partial + migrations.",
@@ -458,7 +458,7 @@ model MarketplaceStatusUpdate {
458
458
  orderId String @map("order_id")
459
459
  provider String
460
460
 
461
- /// READY | DISPATCHED | HANDED_OVER | CONCLUDED (CHECK).
461
+ /// READY | DISPATCHED | HANDED_OVER | CONCLUDED, or LOGISTICS_<STEP> (FUT-6166) (CHECK).
462
462
  step String
463
463
  /// STORE | MARKETPLACE (CHECK).
464
464
  source String
@@ -0,0 +1,12 @@
1
+ -- @domains: marketplace
2
+ -- @12-apps/marketplace: the store's own courier reported to a marketplace (FUT-6166).
3
+ -- Additive and replay-safe: the step CHECK of `marketplace_status_updates` is
4
+ -- widened with the five Logistics steps, each a claim row of the `logistics`
5
+ -- outbound job. The previous release never writes them, so it is unaffected.
6
+ ALTER TABLE "marketplace_status_updates" DROP CONSTRAINT IF EXISTS "marketplace_status_updates_step_check";
7
+ ALTER TABLE "marketplace_status_updates" ADD CONSTRAINT "marketplace_status_updates_step_check"
8
+ CHECK ("step" IN (
9
+ 'READY', 'DISPATCHED', 'HANDED_OVER', 'CONCLUDED',
10
+ 'LOGISTICS_ASSIGNED', 'LOGISTICS_GOING_TO_ORIGIN', 'LOGISTICS_ARRIVED_AT_ORIGIN',
11
+ 'LOGISTICS_DISPATCHED', 'LOGISTICS_ARRIVED_AT_DESTINATION'
12
+ ));
@@ -3,6 +3,7 @@ import type { MarketplaceCourierApi } from "./courier-port";
3
3
  import type { MarketplaceDisputeApi } from "./dispute-port";
4
4
  import type { CatalogueReader } from "./catalogue-read-port";
5
5
  import type { MarketplaceAppLinking, MarketplaceLinking } from "./linking-port";
6
+ import type { LogisticsAdapter } from "./logistics";
6
7
  import type { MarketplaceAvailability } from "./pause-port";
7
8
  import type {
8
9
  MarketplaceDeliveredBy,
@@ -242,7 +243,7 @@ export interface CancellationRequestInput {
242
243
  }
243
244
 
244
245
  /** One marketplace, in and out. */
245
- export interface MarketplaceAdapter {
246
+ export interface MarketplaceAdapter extends LogisticsAdapter {
246
247
  readonly provider: MarketplaceProvider;
247
248
  readonly capabilities: MarketplaceCapabilities;
248
249
  /** Inbound: one delivery into the canonical shape, or null when it carries no order id. */
@@ -0,0 +1,119 @@
1
+ import type { OutboundOrderRef } from "./adapter";
2
+ import { deferInboxEvent, settleInboxEvent } from "./inbox";
3
+ import { logisticsCallContext, type DeliveryCodeAnswer } from "./logistics";
4
+ import type { DeliveryOutcome } from "./pipeline";
5
+ import type { EngineContext } from "./ports";
6
+ import type { NormalizedEvent } from "./types";
7
+
8
+ /**
9
+ * The customer's delivery code (FUT-6167), the engine's half: the marketplace
10
+ * asks for it (iFood's `DELIVERY_DROP_CODE_REQUESTED`), the host stores that
11
+ * through `order.onDeliveryCodeRequested`, and the courier's close checks the
12
+ * typed code with {@link MarketplaceDeliveryCodeEngine.verifyDeliveryCode}.
13
+ *
14
+ * The check is a call the courier waits on at the door, so it runs in the
15
+ * request, never in a job, and never throws: a marketplace that cannot answer
16
+ * is UNAVAILABLE, and the host closes anyway and records it (a close is never
17
+ * refused, ADOPTING §17).
18
+ */
19
+
20
+ const CODE_BEFORE_ORDER = "delivery code request before its order";
21
+
22
+ interface HeldOrder {
23
+ orderId: string;
24
+ provider: string;
25
+ status: string;
26
+ deliveredBy: string | null;
27
+ externalOrderId: string;
28
+ externalDisplayId: string | null;
29
+ createdAt: Date;
30
+ }
31
+
32
+ const HELD_SELECT = {
33
+ orderId: true,
34
+ provider: true,
35
+ status: true,
36
+ deliveredBy: true,
37
+ externalOrderId: true,
38
+ externalDisplayId: true,
39
+ createdAt: true,
40
+ } as const;
41
+
42
+ /** A code request reaches a host that binds the hook; null for any other event. */
43
+ export async function applyDeliveryCodeRequest(
44
+ ctx: EngineContext,
45
+ inboxId: string,
46
+ clientId: string,
47
+ event: NormalizedEvent,
48
+ ): Promise<DeliveryOutcome | null> {
49
+ const hook = ctx.ports.order.onDeliveryCodeRequested;
50
+ if (!event.deliveryCodeRequested || !hook || !event.externalOrderId) return null;
51
+ const db = await ctx.ports.db();
52
+ const held = (await db.marketplaceAcceptance.findFirst({
53
+ where: { clientId, provider: event.provider, externalOrderId: event.externalOrderId },
54
+ select: HELD_SELECT,
55
+ })) as HeldOrder | null;
56
+ if (!held) {
57
+ await deferInboxEvent(ctx, inboxId, CODE_BEFORE_ORDER);
58
+ return { kind: "ignored", reason: CODE_BEFORE_ORDER };
59
+ }
60
+ if (held.status !== "ACCEPTED") {
61
+ const reason = `the order is ${held.status}, not ACCEPTED`;
62
+ await settleInboxEvent(ctx, inboxId, "IGNORED", reason);
63
+ return { kind: "ignored", reason };
64
+ }
65
+ await hook.call(ctx.ports.order, clientId, held.orderId);
66
+ await settleInboxEvent(ctx, inboxId, "PROCESSED");
67
+ ctx.ports.publishChanged(clientId);
68
+ return { kind: "deliveryCodeRequested", orderId: held.orderId };
69
+ }
70
+
71
+ /** What the courier typed, for one of the host's orders. */
72
+ export interface VerifyDeliveryCodeInput {
73
+ clientId: string;
74
+ /** The host's own order id. */
75
+ orderId: string;
76
+ code: string;
77
+ }
78
+
79
+ /** The engine's delivery-code half (`MarketplaceEngine` extends it). */
80
+ export interface MarketplaceDeliveryCodeEngine {
81
+ /**
82
+ * Check the customer's delivery code with the marketplace (FUT-6167). Never
83
+ * throws; UNAVAILABLE says why nothing could be checked (no such order, the
84
+ * store does not carry it, no Logistics app, the marketplace did not answer).
85
+ */
86
+ verifyDeliveryCode(input: VerifyDeliveryCodeInput): Promise<DeliveryCodeAnswer>;
87
+ }
88
+
89
+ async function verifyDeliveryCode(ctx: EngineContext, input: VerifyDeliveryCodeInput): Promise<DeliveryCodeAnswer> {
90
+ try {
91
+ const db = await ctx.ports.db();
92
+ const held = (await db.marketplaceAcceptance.findFirst({
93
+ where: { clientId: input.clientId, orderId: input.orderId },
94
+ select: HELD_SELECT,
95
+ })) as HeldOrder | null;
96
+ if (!held) return { kind: "UNAVAILABLE", reason: "no marketplace order" };
97
+ if (held.deliveredBy !== "MERCHANT") return { kind: "UNAVAILABLE", reason: "the store does not carry this order" };
98
+ const logistics = ctx.registry.get(held.provider)?.logistics;
99
+ if (!logistics?.verifyDeliveryCode) return { kind: "UNAVAILABLE", reason: "the marketplace checks no delivery code" };
100
+ const call = await logisticsCallContext(ctx, input.clientId, held.provider);
101
+ if (typeof call === "string") return { kind: "UNAVAILABLE", reason: call };
102
+ const ref: OutboundOrderRef = {
103
+ externalOrderId: held.externalOrderId,
104
+ externalDisplayId: held.externalDisplayId,
105
+ hostOrderCode: held.orderId,
106
+ placedAt: held.createdAt,
107
+ };
108
+ return await logistics.verifyDeliveryCode(call, ref, input.code);
109
+ } catch (error) {
110
+ // The code is in no message: only that the check failed, and why.
111
+ ctx.ports.logger.warn(`delivery code for order ${input.orderId} not checked: ${error instanceof Error ? error.message : String(error)}`);
112
+ return { kind: "UNAVAILABLE", reason: "the check failed" };
113
+ }
114
+ }
115
+
116
+ /** Bind the delivery-code half to the engine's context. */
117
+ export function deliveryCodeEngine(ctx: EngineContext): MarketplaceDeliveryCodeEngine {
118
+ return { verifyDeliveryCode: (input) => verifyDeliveryCode(ctx, input) };
119
+ }
@@ -1,3 +1,4 @@
1
+ import { deliveryCodeEngine, type MarketplaceDeliveryCodeEngine } from "./delivery-code";
1
2
  import {
2
3
  cancelAcceptance,
3
4
  expireLapsedAcceptances,
@@ -66,6 +67,7 @@ import { guardMerchantClaim, resolveTenantByMerchant } from "./merchant-claim";
66
67
  import { listMarketplaceMenuLinks, type MarketplaceMenuLinkView } from "./menu-links";
67
68
  import { runOutbound, type OutboundAttempt, type OutboundOutcome } from "./outbound";
68
69
  import { informStatus, type InformStatusInput } from "./status-inform";
70
+ import { logisticsEngine, type MarketplaceLogisticsEngine } from "./logistics";
69
71
  import {
70
72
  availabilityState,
71
73
  pauseStore,
@@ -91,7 +93,13 @@ import { readOrderStates, type MarketplaceOrderState } from "./state";
91
93
  import type { MarketplaceProvider, NormalizedEvent, NormalizedMarketplaceOrder } from "./types";
92
94
 
93
95
  /** Everything the mounted engine can do, with the ports and adapters bound in. */
94
- export interface MarketplaceEngine extends MarketplaceCourierEngine, MarketplaceConformityEngine, MarketplaceLinkEngine, MarketplaceDisputeEngine {
96
+ export interface MarketplaceEngine
97
+ extends MarketplaceCourierEngine,
98
+ MarketplaceConformityEngine,
99
+ MarketplaceLinkEngine,
100
+ MarketplaceDisputeEngine,
101
+ MarketplaceLogisticsEngine,
102
+ MarketplaceDeliveryCodeEngine {
95
103
  /** Every registered marketplace, in registration order. */
96
104
  providers(): readonly MarketplaceProvider[];
97
105
  /** Whether a provider id (a URL segment, a body field) names a registered adapter. */
@@ -327,7 +335,7 @@ export function createMarketplaceEngine(config: MarketplaceEngineConfig): Market
327
335
  runOutbound: (payload, attempt) => runOutbound(ctx, payload, attempt),
328
336
  runFetchOrder: (payload, attempt) => runFetchOrder(ctx, payload, attempt),
329
337
  informStatus: (input) => informStatus(ctx, input),
330
- ...{ ...courierEngine(ctx), ...conformityEngine(ctx), ...linkEngine(ctx), ...disputeEngine(ctx) },
338
+ ...{ ...courierEngine(ctx), ...conformityEngine(ctx), ...linkEngine(ctx), ...disputeEngine(ctx), ...logisticsEngine(ctx), ...deliveryCodeEngine(ctx) },
331
339
 
332
340
  // Null too when the host bound no catalogue port: nothing could be published.
333
341
  catalogueMode: (provider) => (ports.catalogue ? (registry.get(provider)?.catalogue?.mode ?? null) : null),