@12-apps/marketplace 1.9.0 → 1.10.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.
- package/ADOPTING.md +27 -1
- package/README.md +1 -1
- package/dist/{chunk-BMCERPQX.js → chunk-2BA2EHNF.js} +16 -6
- package/dist/chunk-2BA2EHNF.js.map +1 -0
- package/dist/{chunk-6N2HEV3U.js → chunk-WVZ2IXFH.js} +3 -3
- package/dist/{chunk-NP4EYKPO.js → chunk-ZO4S3LF4.js} +5 -3
- package/dist/chunk-ZO4S3LF4.js.map +1 -0
- package/dist/{engine-wmW-VuBL.d.ts → engine-DK3iV_Kk.d.ts} +91 -60
- package/dist/hono/index.d.ts +1 -1
- package/dist/hono/index.js +3 -3
- package/dist/index.d.ts +1 -1
- package/dist/index.js +17 -6
- package/dist/index.js.map +1 -1
- package/dist/manifest/index.d.ts +4 -1
- package/dist/manifest/index.js +1 -1
- package/dist/manifest/server.d.ts +1 -1
- package/dist/manifest/server.js +3 -3
- package/dist/server/index.d.ts +6 -2
- package/dist/server/index.js +3 -3
- package/package.json +1 -1
- package/prisma/marketplace.prisma +6 -0
- package/prisma/migrations/20261126230000_marketplace_menu_link_variation/migration.sql +11 -0
- package/src/core/bulk.ts +34 -7
- package/src/core/ingest.ts +2 -1
- package/src/core/menu-links.ts +36 -6
- package/src/core/order-types.ts +74 -0
- package/src/core/ports.ts +23 -65
- package/src/server/mcp.ts +7 -2
- package/dist/chunk-BMCERPQX.js.map +0 -1
- package/dist/chunk-NP4EYKPO.js.map +0 -1
- /package/dist/{chunk-6N2HEV3U.js.map → chunk-WVZ2IXFH.js.map} +0 -0
package/dist/manifest/index.d.ts
CHANGED
|
@@ -205,6 +205,7 @@ declare const marketplaceManifest: {
|
|
|
205
205
|
externalSku: zod.ZodString;
|
|
206
206
|
externalName: zod.ZodString;
|
|
207
207
|
menuItemId: zod.ZodNullable<zod.ZodString>;
|
|
208
|
+
variationId: zod.ZodNullable<zod.ZodString>;
|
|
208
209
|
lastSeenAt: zod.ZodString;
|
|
209
210
|
}, zod_v4_core.$strip>>;
|
|
210
211
|
}, zod_v4_core.$strip>;
|
|
@@ -215,7 +216,7 @@ declare const marketplaceManifest: {
|
|
|
215
216
|
readonly operationId: "upsertMarketplaceMenuLinks";
|
|
216
217
|
readonly method: "PUT";
|
|
217
218
|
readonly path: "/admin/{tenantSlug}/marketplace/menu-links";
|
|
218
|
-
readonly summary: "Map one or many external marketplace SKUs to the store's catalog ids, or unmap them with `menuItemId: null
|
|
219
|
+
readonly summary: "Map one or many external marketplace SKUs to the store's catalog ids, or unmap them with `menuItemId: null`; `variationId` maps a SKU to one variation of its catalog id instead of the item alone, in one all-or-nothing call keyed by (`provider`, `externalSku`). A SKU never seen is created, so a store can map ahead of the first order. Applies to orders ingested from now on. Any problem (a catalog id that is not the store's, a variation that is not one of its catalog id's, a repeated SKU) writes nothing; `validateOnly: true` checks without writing.";
|
|
219
220
|
readonly tags: readonly ["marketplace"];
|
|
220
221
|
readonly params: zod.ZodObject<{
|
|
221
222
|
tenantSlug: zod.ZodString;
|
|
@@ -227,6 +228,7 @@ declare const marketplaceManifest: {
|
|
|
227
228
|
externalSku: zod.ZodString;
|
|
228
229
|
externalName: zod.ZodOptional<zod.ZodString>;
|
|
229
230
|
menuItemId: zod.ZodNullable<zod.ZodString>;
|
|
231
|
+
variationId: zod.ZodOptional<zod.ZodNullable<zod.ZodString>>;
|
|
230
232
|
}, zod_v4_core.$strict>>;
|
|
231
233
|
}, zod_v4_core.$strict>;
|
|
232
234
|
readonly response: zod.ZodObject<{
|
|
@@ -238,6 +240,7 @@ declare const marketplaceManifest: {
|
|
|
238
240
|
externalSku: zod.ZodString;
|
|
239
241
|
externalName: zod.ZodString;
|
|
240
242
|
menuItemId: zod.ZodNullable<zod.ZodString>;
|
|
243
|
+
variationId: zod.ZodNullable<zod.ZodString>;
|
|
241
244
|
lastSeenAt: zod.ZodString;
|
|
242
245
|
}, zod_v4_core.$strip>>;
|
|
243
246
|
}, zod_v4_core.$strip>;
|
package/dist/manifest/index.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { ah as OutboundJobPayload, G as FetchOrderJobPayload, n as CatalogueJobPayload, b as AvailabilityJobPayload } from '../engine-
|
|
1
|
+
import { ah as OutboundJobPayload, G as FetchOrderJobPayload, n as CatalogueJobPayload, b as AvailabilityJobPayload } from '../engine-DK3iV_Kk.js';
|
|
2
2
|
import * as _12_apps_wiring from '@12-apps/wiring';
|
|
3
3
|
import { createApiMarketplace, MarketplaceJobDeps } from '../server/index.js';
|
|
4
4
|
import '../adapter-CYJzu1c9.js';
|
package/dist/manifest/server.js
CHANGED
|
@@ -1,9 +1,9 @@
|
|
|
1
1
|
import {
|
|
2
2
|
MARKETPLACE_JOBS,
|
|
3
3
|
createApiMarketplace
|
|
4
|
-
} from "../chunk-
|
|
5
|
-
import "../chunk-
|
|
6
|
-
import "../chunk-
|
|
4
|
+
} from "../chunk-WVZ2IXFH.js";
|
|
5
|
+
import "../chunk-ZO4S3LF4.js";
|
|
6
|
+
import "../chunk-2BA2EHNF.js";
|
|
7
7
|
import "../chunk-7QVYU63E.js";
|
|
8
8
|
|
|
9
9
|
// src/manifest/server.ts
|
package/dist/server/index.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { y as DecisionProblemCode, f as BulkProblemCode, _ as MarketplaceEngine, a4 as MarketplaceLogger, ah as OutboundJobPayload, G as FetchOrderJobPayload, n as CatalogueJobPayload, b as AvailabilityJobPayload } from '../engine-
|
|
1
|
+
import { y as DecisionProblemCode, f as BulkProblemCode, _ as MarketplaceEngine, a4 as MarketplaceLogger, ah as OutboundJobPayload, G as FetchOrderJobPayload, n as CatalogueJobPayload, b as AvailabilityJobPayload } from '../engine-DK3iV_Kk.js';
|
|
2
2
|
import { WireRoute, WireJobBlueprint } from '@12-apps/wiring';
|
|
3
3
|
import { z } from 'zod';
|
|
4
4
|
import '../adapter-CYJzu1c9.js';
|
|
@@ -225,6 +225,7 @@ declare const upsertMarketplaceMenuLinksBody: z.ZodObject<{
|
|
|
225
225
|
externalSku: z.ZodString;
|
|
226
226
|
externalName: z.ZodOptional<z.ZodString>;
|
|
227
227
|
menuItemId: z.ZodNullable<z.ZodString>;
|
|
228
|
+
variationId: z.ZodOptional<z.ZodNullable<z.ZodString>>;
|
|
228
229
|
}, z.core.$strict>>;
|
|
229
230
|
}, z.core.$strict>;
|
|
230
231
|
/** Which queued orders to read. */
|
|
@@ -410,6 +411,7 @@ declare const MARKETPLACE_MCP_TOOLS: readonly [{
|
|
|
410
411
|
externalSku: z.ZodString;
|
|
411
412
|
externalName: z.ZodString;
|
|
412
413
|
menuItemId: z.ZodNullable<z.ZodString>;
|
|
414
|
+
variationId: z.ZodNullable<z.ZodString>;
|
|
413
415
|
lastSeenAt: z.ZodString;
|
|
414
416
|
}, z.core.$strip>>;
|
|
415
417
|
}, z.core.$strip>;
|
|
@@ -420,7 +422,7 @@ declare const MARKETPLACE_MCP_TOOLS: readonly [{
|
|
|
420
422
|
readonly operationId: "upsertMarketplaceMenuLinks";
|
|
421
423
|
readonly method: "PUT";
|
|
422
424
|
readonly path: "/admin/{tenantSlug}/marketplace/menu-links";
|
|
423
|
-
readonly summary: "Map one or many external marketplace SKUs to the store's catalog ids, or unmap them with `menuItemId: null
|
|
425
|
+
readonly summary: "Map one or many external marketplace SKUs to the store's catalog ids, or unmap them with `menuItemId: null`; `variationId` maps a SKU to one variation of its catalog id instead of the item alone, in one all-or-nothing call keyed by (`provider`, `externalSku`). A SKU never seen is created, so a store can map ahead of the first order. Applies to orders ingested from now on. Any problem (a catalog id that is not the store's, a variation that is not one of its catalog id's, a repeated SKU) writes nothing; `validateOnly: true` checks without writing.";
|
|
424
426
|
readonly tags: readonly ["marketplace"];
|
|
425
427
|
readonly params: z.ZodObject<{
|
|
426
428
|
tenantSlug: z.ZodString;
|
|
@@ -432,6 +434,7 @@ declare const MARKETPLACE_MCP_TOOLS: readonly [{
|
|
|
432
434
|
externalSku: z.ZodString;
|
|
433
435
|
externalName: z.ZodOptional<z.ZodString>;
|
|
434
436
|
menuItemId: z.ZodNullable<z.ZodString>;
|
|
437
|
+
variationId: z.ZodOptional<z.ZodNullable<z.ZodString>>;
|
|
435
438
|
}, z.core.$strict>>;
|
|
436
439
|
}, z.core.$strict>;
|
|
437
440
|
readonly response: z.ZodObject<{
|
|
@@ -443,6 +446,7 @@ declare const MARKETPLACE_MCP_TOOLS: readonly [{
|
|
|
443
446
|
externalSku: z.ZodString;
|
|
444
447
|
externalName: z.ZodString;
|
|
445
448
|
menuItemId: z.ZodNullable<z.ZodString>;
|
|
449
|
+
variationId: z.ZodNullable<z.ZodString>;
|
|
446
450
|
lastSeenAt: z.ZodString;
|
|
447
451
|
}, z.core.$strip>>;
|
|
448
452
|
}, z.core.$strip>;
|
package/dist/server/index.js
CHANGED
|
@@ -6,7 +6,7 @@ import {
|
|
|
6
6
|
MARKETPLACE_SWEEP_QUEUE,
|
|
7
7
|
ORDER_NOT_PENDING_CODE,
|
|
8
8
|
createApiMarketplace
|
|
9
|
-
} from "../chunk-
|
|
9
|
+
} from "../chunk-WVZ2IXFH.js";
|
|
10
10
|
import {
|
|
11
11
|
MARKETPLACE_DECIDE,
|
|
12
12
|
MARKETPLACE_MANAGE,
|
|
@@ -19,8 +19,8 @@ import {
|
|
|
19
19
|
marketplaceParams,
|
|
20
20
|
upsertMarketplaceIntegrationsBody,
|
|
21
21
|
upsertMarketplaceMenuLinksBody
|
|
22
|
-
} from "../chunk-
|
|
23
|
-
import "../chunk-
|
|
22
|
+
} from "../chunk-ZO4S3LF4.js";
|
|
23
|
+
import "../chunk-2BA2EHNF.js";
|
|
24
24
|
import "../chunk-7QVYU63E.js";
|
|
25
25
|
export {
|
|
26
26
|
MARKETPLACE_AVAILABILITY_ATTEMPTS,
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@12-apps/marketplace",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.10.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.",
|
|
@@ -93,6 +93,9 @@ model MarketplaceIntegration {
|
|
|
93
93
|
///
|
|
94
94
|
/// `lastSeenAt` is what the list sorts by, so a store fixing five mappings
|
|
95
95
|
/// fixes the five costing them money rather than the five added first.
|
|
96
|
+
///
|
|
97
|
+
/// `variationId` narrows the mapping to one variation of the item: the host
|
|
98
|
+
/// asks its own catalog whether it belongs (`order.catalogHasVariation`).
|
|
96
99
|
model MarketplaceMenuLink {
|
|
97
100
|
id String @id @default(uuid())
|
|
98
101
|
clientId String @map("client_id")
|
|
@@ -105,6 +108,9 @@ model MarketplaceMenuLink {
|
|
|
105
108
|
|
|
106
109
|
/// The host catalog row this SKU means, or NULL while nothing does.
|
|
107
110
|
menuItemId String? @map("menu_item_id")
|
|
111
|
+
/// The variation of `menuItemId` this SKU names, or NULL for the parent
|
|
112
|
+
/// item alone. Never set without `menuItemId` (FUT-4927).
|
|
113
|
+
variationId String? @map("variation_id")
|
|
108
114
|
|
|
109
115
|
lastSeenAt DateTime @default(now()) @map("last_seen_at")
|
|
110
116
|
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
-- @domains: marketplace
|
|
2
|
+
-- @12-apps/marketplace: a SKU may map to one variation of a catalog item (FUT-4927).
|
|
3
|
+
-- Additive and replay-safe, like the engine's earlier migrations: one nullable
|
|
4
|
+
-- column. No foreign key into a host table, and nothing the previous release
|
|
5
|
+
-- reads is changed: every existing link keeps `variation_id` NULL, which is
|
|
6
|
+
-- the parent-only mapping it already was.
|
|
7
|
+
--
|
|
8
|
+
-- `variation_id` is the host's id of the variation the SKU names, beside the
|
|
9
|
+
-- `menu_item_id` it belongs to. NULL maps the SKU to the parent item alone.
|
|
10
|
+
|
|
11
|
+
ALTER TABLE "marketplace_menu_links" ADD COLUMN IF NOT EXISTS "variation_id" TEXT;
|
package/src/core/bulk.ts
CHANGED
|
@@ -40,12 +40,17 @@ export type IntegrationRowInput =
|
|
|
40
40
|
| ({ operation?: "upsert" } & Omit<SaveMarketplaceIntegrationInput, "clientId">)
|
|
41
41
|
| { operation: "archive" | "unarchive"; provider: string; reason?: string | null };
|
|
42
42
|
|
|
43
|
-
/**
|
|
43
|
+
/**
|
|
44
|
+
* One SKU link: map it to a catalog id, or unmap it with `menuItemId: null`.
|
|
45
|
+
* `variationId` narrows the mapping to one variation of that id (FUT-4927);
|
|
46
|
+
* absent or null maps the item alone, and it may not be set on an unmap.
|
|
47
|
+
*/
|
|
44
48
|
export interface MenuLinkRowInput {
|
|
45
49
|
provider: string;
|
|
46
50
|
externalSku: string;
|
|
47
51
|
externalName?: string;
|
|
48
52
|
menuItemId: string | null;
|
|
53
|
+
variationId?: string | null;
|
|
49
54
|
}
|
|
50
55
|
|
|
51
56
|
/** The key of each row seen twice, as problems. */
|
|
@@ -177,7 +182,33 @@ async function auditArchives(
|
|
|
177
182
|
}
|
|
178
183
|
}
|
|
179
184
|
|
|
180
|
-
/**
|
|
185
|
+
/**
|
|
186
|
+
* The catalog half of one link row: the item must be the store's, and a named
|
|
187
|
+
* variation must be one of that item's, as the host answers. A variation on an
|
|
188
|
+
* unmap, or with a host that cannot answer for variations, is refused at the
|
|
189
|
+
* variation, and the item is not asked about it.
|
|
190
|
+
*/
|
|
191
|
+
async function linkRowProblems(
|
|
192
|
+
ctx: EngineContext,
|
|
193
|
+
clientId: string,
|
|
194
|
+
row: MenuLinkRowInput,
|
|
195
|
+
index: number,
|
|
196
|
+
): Promise<BulkProblem[]> {
|
|
197
|
+
const variationPath = ["links", index, "variationId"];
|
|
198
|
+
const variationId = row.variationId ?? null;
|
|
199
|
+
if (row.menuItemId === null) {
|
|
200
|
+
return variationId === null ? [] : [{ path: variationPath, code: "CATALOG_ITEM_NOT_FOUND" }];
|
|
201
|
+
}
|
|
202
|
+
if (!(await ctx.ports.order.catalogHas(clientId, row.menuItemId))) {
|
|
203
|
+
return [{ path: ["links", index, "menuItemId"], code: "CATALOG_ITEM_NOT_FOUND" }];
|
|
204
|
+
}
|
|
205
|
+
if (variationId === null) return [];
|
|
206
|
+
const order = ctx.ports.order;
|
|
207
|
+
const belongs = order.catalogHasVariation ? await order.catalogHasVariation(clientId, row.menuItemId, variationId) : false;
|
|
208
|
+
return belongs ? [] : [{ path: variationPath, code: "CATALOG_ITEM_NOT_FOUND" }];
|
|
209
|
+
}
|
|
210
|
+
|
|
211
|
+
/** Map or unmap many SKUs at once; every catalog id (and variation) must be the store's. */
|
|
181
212
|
export async function upsertMenuLinks(
|
|
182
213
|
ctx: EngineContext,
|
|
183
214
|
input: { clientId: string; rows: readonly MenuLinkRowInput[]; validateOnly?: boolean },
|
|
@@ -186,11 +217,7 @@ export async function upsertMenuLinks(
|
|
|
186
217
|
...duplicates(input.rows, (row) => `${row.provider}\u0000${row.externalSku}`, "links", "externalSku"),
|
|
187
218
|
...unknownProviders(ctx, input.rows, "links"),
|
|
188
219
|
];
|
|
189
|
-
for (const [index, row] of input.rows.entries())
|
|
190
|
-
if (row.menuItemId !== null && !(await ctx.ports.order.catalogHas(input.clientId, row.menuItemId))) {
|
|
191
|
-
problems.push({ path: ["links", index, "menuItemId"], code: "CATALOG_ITEM_NOT_FOUND" });
|
|
192
|
-
}
|
|
193
|
-
}
|
|
220
|
+
for (const [index, row] of input.rows.entries()) problems.push(...(await linkRowProblems(ctx, input.clientId, row, index)));
|
|
194
221
|
if (problems.length > 0) return { kind: "refused", problems };
|
|
195
222
|
|
|
196
223
|
const validateOnly = input.validateOnly === true;
|
package/src/core/ingest.ts
CHANGED
|
@@ -78,7 +78,8 @@ async function composeLines(
|
|
|
78
78
|
quantity: line.quantity,
|
|
79
79
|
unitPriceCents: line.unitPriceCents,
|
|
80
80
|
note: line.note,
|
|
81
|
-
subjectRef: mapping.get(line.externalSku) ?? null,
|
|
81
|
+
subjectRef: mapping.get(line.externalSku)?.menuItemId ?? null,
|
|
82
|
+
variationRef: mapping.get(line.externalSku)?.variationId ?? null,
|
|
82
83
|
}));
|
|
83
84
|
}
|
|
84
85
|
|
package/src/core/menu-links.ts
CHANGED
|
@@ -14,6 +14,13 @@ import type { MarketplaceProvider } from "./types";
|
|
|
14
14
|
* The package never dereferences `menuItemId`. It asks the host whether the id
|
|
15
15
|
* is theirs (`order.catalogHas`, in `./bulk.ts`) and stores it; what the id
|
|
16
16
|
* MEANS is the host's.
|
|
17
|
+
*
|
|
18
|
+
* A link may narrow the mapping to ONE variation of that id (`variationId`,
|
|
19
|
+
* FUT-4927): a marketplace SKU usually names one flavour or size, and the
|
|
20
|
+
* host's order line is then the item plus that variation. `variationId` null
|
|
21
|
+
* is the item alone, exactly as before it existed; it is never set without
|
|
22
|
+
* `menuItemId`, and the host answers whether it belongs to the item
|
|
23
|
+
* (`order.catalogHasVariation`).
|
|
17
24
|
*/
|
|
18
25
|
|
|
19
26
|
/** A link as every read surface sees it. */
|
|
@@ -24,15 +31,24 @@ export interface MarketplaceMenuLinkView {
|
|
|
24
31
|
externalName: string;
|
|
25
32
|
/** The host catalog id, or null while the SKU is unmapped. */
|
|
26
33
|
menuItemId: string | null;
|
|
34
|
+
/** The variation of `menuItemId` the SKU names, or null for the item alone. */
|
|
35
|
+
variationId: string | null;
|
|
27
36
|
lastSeenAt: Date;
|
|
28
37
|
}
|
|
29
38
|
|
|
39
|
+
/** What one SKU resolves to on the ingest path; both null while it is unmapped. */
|
|
40
|
+
interface ResolvedSku {
|
|
41
|
+
menuItemId: string | null;
|
|
42
|
+
variationId: string | null;
|
|
43
|
+
}
|
|
44
|
+
|
|
30
45
|
const LINK_SELECT = {
|
|
31
46
|
id: true,
|
|
32
47
|
provider: true,
|
|
33
48
|
externalSku: true,
|
|
34
49
|
externalName: true,
|
|
35
50
|
menuItemId: true,
|
|
51
|
+
variationId: true,
|
|
36
52
|
lastSeenAt: true,
|
|
37
53
|
} as const;
|
|
38
54
|
|
|
@@ -42,6 +58,7 @@ interface LinkRow {
|
|
|
42
58
|
externalSku: string;
|
|
43
59
|
externalName: string | null;
|
|
44
60
|
menuItemId: string | null;
|
|
61
|
+
variationId?: string | null;
|
|
45
62
|
lastSeenAt: Date;
|
|
46
63
|
}
|
|
47
64
|
|
|
@@ -52,6 +69,9 @@ function toView(row: LinkRow): MarketplaceMenuLinkView {
|
|
|
52
69
|
externalSku: row.externalSku,
|
|
53
70
|
externalName: row.externalName ?? row.externalSku,
|
|
54
71
|
menuItemId: row.menuItemId,
|
|
72
|
+
// A row with no variation key (a fake db) reads as the item alone. A Prisma client
|
|
73
|
+
// generated before the migration throws on `variationId` instead: sync, generate, migrate.
|
|
74
|
+
variationId: row.menuItemId === null ? null : (row.variationId ?? null),
|
|
55
75
|
lastSeenAt: row.lastSeenAt,
|
|
56
76
|
};
|
|
57
77
|
}
|
|
@@ -86,6 +106,10 @@ export async function listMarketplaceMenuLinks(
|
|
|
86
106
|
* transaction. The caller has already checked the id is this tenant's
|
|
87
107
|
* (`order.catalogHas`). Creates the link if the SKU has never been seen: an
|
|
88
108
|
* operator mapping ahead of the first order is doing the right thing.
|
|
109
|
+
*
|
|
110
|
+
* Every write sets BOTH halves: a mapping to the item alone (`variationId`
|
|
111
|
+
* absent or null) clears a variation an earlier mapping named, and an unmap
|
|
112
|
+
* clears both.
|
|
89
113
|
*/
|
|
90
114
|
export async function upsertLink(
|
|
91
115
|
db: MarketplaceDb,
|
|
@@ -95,8 +119,10 @@ export async function upsertLink(
|
|
|
95
119
|
externalSku: string;
|
|
96
120
|
externalName?: string;
|
|
97
121
|
menuItemId: string | null;
|
|
122
|
+
variationId?: string | null;
|
|
98
123
|
},
|
|
99
124
|
): Promise<MarketplaceMenuLinkView> {
|
|
125
|
+
const variationId = input.menuItemId === null ? null : (input.variationId ?? null);
|
|
100
126
|
const row = (await db.marketplaceMenuLink.upsert({
|
|
101
127
|
where: {
|
|
102
128
|
clientId_provider_externalSku: {
|
|
@@ -111,11 +137,12 @@ export async function upsertLink(
|
|
|
111
137
|
externalSku: input.externalSku,
|
|
112
138
|
externalName: input.externalName ?? input.externalSku,
|
|
113
139
|
menuItemId: input.menuItemId,
|
|
140
|
+
variationId,
|
|
114
141
|
},
|
|
115
142
|
// `externalName` is NOT rewritten here. It is the marketplace's snapshot of
|
|
116
143
|
// what it called the item, and an operator's typing is not a better record
|
|
117
144
|
// of that than the marketplace's own text.
|
|
118
|
-
update: { menuItemId: input.menuItemId },
|
|
145
|
+
update: { menuItemId: input.menuItemId, variationId },
|
|
119
146
|
select: LINK_SELECT,
|
|
120
147
|
})) as LinkRow;
|
|
121
148
|
return toView(row);
|
|
@@ -139,8 +166,8 @@ export async function resolveSkus(
|
|
|
139
166
|
skus: { externalSku: string; name: string }[];
|
|
140
167
|
seenAt: Date;
|
|
141
168
|
},
|
|
142
|
-
): Promise<Map<string,
|
|
143
|
-
const resolved = new Map<string,
|
|
169
|
+
): Promise<Map<string, ResolvedSku>> {
|
|
170
|
+
const resolved = new Map<string, ResolvedSku>();
|
|
144
171
|
for (const sku of input.skus) {
|
|
145
172
|
const row = (await db.marketplaceMenuLink.upsert({
|
|
146
173
|
where: {
|
|
@@ -158,9 +185,12 @@ export async function resolveSkus(
|
|
|
158
185
|
lastSeenAt: input.seenAt,
|
|
159
186
|
},
|
|
160
187
|
update: { lastSeenAt: input.seenAt },
|
|
161
|
-
select: { menuItemId: true },
|
|
162
|
-
})) as { menuItemId: string | null };
|
|
163
|
-
resolved.set(sku.externalSku,
|
|
188
|
+
select: { menuItemId: true, variationId: true },
|
|
189
|
+
})) as { menuItemId: string | null; variationId?: string | null };
|
|
190
|
+
resolved.set(sku.externalSku, {
|
|
191
|
+
menuItemId: row.menuItemId,
|
|
192
|
+
variationId: row.menuItemId === null ? null : (row.variationId ?? null),
|
|
193
|
+
});
|
|
164
194
|
}
|
|
165
195
|
return resolved;
|
|
166
196
|
}
|
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The order-side vocabulary the host's order port speaks: what a resolved line
|
|
3
|
+
* is, what the queue shows, what a person stated, and why an order or a
|
|
4
|
+
* cancellation ended. Plain data with no engine dependency; `./ports`
|
|
5
|
+
* re-exports every name, so a host imports them from there as before.
|
|
6
|
+
*/
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* One order line after the engine has matched it against the SKU map.
|
|
10
|
+
* `subjectRef` is the host catalog id, or null when the SKU was never mapped —
|
|
11
|
+
* never an error, and never a reason to refuse the order. `variationRef` is
|
|
12
|
+
* the variation of `subjectRef` the SKU was mapped to (FUT-4927), or null for
|
|
13
|
+
* the item alone and for every unmapped line.
|
|
14
|
+
*/
|
|
15
|
+
export interface ResolvedOrderLine {
|
|
16
|
+
externalSku: string;
|
|
17
|
+
name: string;
|
|
18
|
+
quantity: number;
|
|
19
|
+
unitPriceCents: number;
|
|
20
|
+
note: string | null;
|
|
21
|
+
subjectRef: string | null;
|
|
22
|
+
variationRef: string | null;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
/** What the acceptance queue shows about an order, resolved by the host. */
|
|
26
|
+
export interface MarketplaceOrderSummary {
|
|
27
|
+
buyerName: string | null;
|
|
28
|
+
totalCents: number;
|
|
29
|
+
unmappedLines: number;
|
|
30
|
+
createdAt: string | null;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
/**
|
|
34
|
+
* What a person stated beside an accept, for the host's admission to read.
|
|
35
|
+
* Every field is optional and the host decides what it means.
|
|
36
|
+
*/
|
|
37
|
+
export interface MarketplaceAdmission {
|
|
38
|
+
/** "Accept anyway": the person was told the order is short and confirmed. */
|
|
39
|
+
admitStockPastZero?: boolean;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
/** The host's answer: proceed, or refuse with its own coded reason. */
|
|
43
|
+
export type MarketplaceAdmissionResult =
|
|
44
|
+
| { admitted: true }
|
|
45
|
+
| {
|
|
46
|
+
admitted: false;
|
|
47
|
+
/** The host's machine code (`MARKETPLACE_STOCK_ASK`). The route answers 409 with it. */
|
|
48
|
+
code: string;
|
|
49
|
+
/** The host's sentence, in the caller's language. */
|
|
50
|
+
message: string;
|
|
51
|
+
/** Anything the client needs to ask the person (the short lines). */
|
|
52
|
+
details?: unknown;
|
|
53
|
+
};
|
|
54
|
+
|
|
55
|
+
/** Why the engine tells the host to refuse an order. Vocabulary, not copy. */
|
|
56
|
+
export type MarketplaceRefusal = "REJECTED" | "EXPIRED" | "CANCELLED";
|
|
57
|
+
|
|
58
|
+
/**
|
|
59
|
+
* Why the store's own cancellation request FAILED. Vocabulary, not copy.
|
|
60
|
+
*
|
|
61
|
+
* - `REFUSED`: the marketplace judged the request and denied it: its denial
|
|
62
|
+
* event, an answer that is its business refusal, or an adapter that has no
|
|
63
|
+
* cancellation API to call. An adapter that cannot tell a business 4xx from
|
|
64
|
+
* a malformed request (iFood, Rappi) reports every final 4xx as REFUSED.
|
|
65
|
+
* - `UNREACHABLE`: our own request never got a judgement: no credentials or
|
|
66
|
+
* ones that do not open, a token the marketplace did not grant, a request
|
|
67
|
+
* the marketplace calls malformed (99Food's 400), or every attempt failed
|
|
68
|
+
* on the network or a 5xx. Retrying, or fixing the connection, can help.
|
|
69
|
+
* - `TIMEOUT`: no answer inside the marketplace's cancellation window.
|
|
70
|
+
*
|
|
71
|
+
* A host written before `UNREACHABLE` existed treats an unknown value as it
|
|
72
|
+
* treats REFUSED: the order stays and a person finishes in the marketplace's app.
|
|
73
|
+
*/
|
|
74
|
+
export type CancellationFailureReason = "REFUSED" | "UNREACHABLE" | "TIMEOUT";
|
package/src/core/ports.ts
CHANGED
|
@@ -2,9 +2,25 @@ import type { AdapterRegistry } from "./registry";
|
|
|
2
2
|
import type { AdapterTokenCache, MarketplaceAdapter, MarketplaceHttp, StatusStep } from "./adapter";
|
|
3
3
|
import type { CatalogueImage, CatalogueItem } from "./catalogue-port";
|
|
4
4
|
import type { MarketplaceLifecycleAnswer, MarketplaceLifecycleFacts } from "./lifecycle-port";
|
|
5
|
+
import type {
|
|
6
|
+
CancellationFailureReason,
|
|
7
|
+
MarketplaceAdmission,
|
|
8
|
+
MarketplaceAdmissionResult,
|
|
9
|
+
MarketplaceOrderSummary,
|
|
10
|
+
MarketplaceRefusal,
|
|
11
|
+
ResolvedOrderLine,
|
|
12
|
+
} from "./order-types";
|
|
5
13
|
import type { NormalizedEvent, NormalizedMarketplaceOrder } from "./types";
|
|
6
14
|
|
|
7
15
|
export type { MarketplaceLifecycleAnswer, MarketplaceLifecycleFacts } from "./lifecycle-port";
|
|
16
|
+
export type {
|
|
17
|
+
CancellationFailureReason,
|
|
18
|
+
MarketplaceAdmission,
|
|
19
|
+
MarketplaceAdmissionResult,
|
|
20
|
+
MarketplaceOrderSummary,
|
|
21
|
+
MarketplaceRefusal,
|
|
22
|
+
ResolvedOrderLine,
|
|
23
|
+
} from "./order-types";
|
|
8
24
|
|
|
9
25
|
/**
|
|
10
26
|
* What the engine needs FROM its host, and nothing more.
|
|
@@ -96,37 +112,6 @@ export interface MarketplaceDb {
|
|
|
96
112
|
};
|
|
97
113
|
}
|
|
98
114
|
|
|
99
|
-
/**
|
|
100
|
-
* One order line after the engine has matched it against the SKU map.
|
|
101
|
-
* `subjectRef` is the host catalog id, or null when the SKU was never mapped —
|
|
102
|
-
* never an error, and never a reason to refuse the order.
|
|
103
|
-
*/
|
|
104
|
-
export interface ResolvedOrderLine {
|
|
105
|
-
externalSku: string;
|
|
106
|
-
name: string;
|
|
107
|
-
quantity: number;
|
|
108
|
-
unitPriceCents: number;
|
|
109
|
-
note: string | null;
|
|
110
|
-
subjectRef: string | null;
|
|
111
|
-
}
|
|
112
|
-
|
|
113
|
-
/** What the acceptance queue shows about an order, resolved by the host. */
|
|
114
|
-
export interface MarketplaceOrderSummary {
|
|
115
|
-
buyerName: string | null;
|
|
116
|
-
totalCents: number;
|
|
117
|
-
unmappedLines: number;
|
|
118
|
-
createdAt: string | null;
|
|
119
|
-
}
|
|
120
|
-
|
|
121
|
-
/**
|
|
122
|
-
* What a person stated beside an accept, for the host's admission to read.
|
|
123
|
-
* Every field is optional and the host decides what it means.
|
|
124
|
-
*/
|
|
125
|
-
export interface MarketplaceAdmission {
|
|
126
|
-
/** "Accept anyway": the person was told the order is short and confirmed. */
|
|
127
|
-
admitStockPastZero?: boolean;
|
|
128
|
-
}
|
|
129
|
-
|
|
130
115
|
/** One decision as the host's admission sees it, BEFORE the claim commits. */
|
|
131
116
|
export interface MarketplaceAdmissionRequest {
|
|
132
117
|
clientId: string;
|
|
@@ -142,40 +127,6 @@ export interface MarketplaceAdmissionRequest {
|
|
|
142
127
|
db: MarketplaceDb;
|
|
143
128
|
}
|
|
144
129
|
|
|
145
|
-
/** The host's answer: proceed, or refuse with its own coded reason. */
|
|
146
|
-
export type MarketplaceAdmissionResult =
|
|
147
|
-
| { admitted: true }
|
|
148
|
-
| {
|
|
149
|
-
admitted: false;
|
|
150
|
-
/** The host's machine code (`MARKETPLACE_STOCK_ASK`). The route answers 409 with it. */
|
|
151
|
-
code: string;
|
|
152
|
-
/** The host's sentence, in the caller's language. */
|
|
153
|
-
message: string;
|
|
154
|
-
/** Anything the client needs to ask the person (the short lines). */
|
|
155
|
-
details?: unknown;
|
|
156
|
-
};
|
|
157
|
-
|
|
158
|
-
/** Why the engine tells the host to refuse an order. Vocabulary, not copy. */
|
|
159
|
-
export type MarketplaceRefusal = "REJECTED" | "EXPIRED" | "CANCELLED";
|
|
160
|
-
|
|
161
|
-
/**
|
|
162
|
-
* Why the store's own cancellation request FAILED. Vocabulary, not copy.
|
|
163
|
-
*
|
|
164
|
-
* - `REFUSED`: the marketplace judged the request and denied it: its denial
|
|
165
|
-
* event, an answer that is its business refusal, or an adapter that has no
|
|
166
|
-
* cancellation API to call. An adapter that cannot tell a business 4xx from
|
|
167
|
-
* a malformed request (iFood, Rappi) reports every final 4xx as REFUSED.
|
|
168
|
-
* - `UNREACHABLE`: our own request never got a judgement: no credentials or
|
|
169
|
-
* ones that do not open, a token the marketplace did not grant, a request
|
|
170
|
-
* the marketplace calls malformed (99Food's 400), or every attempt failed
|
|
171
|
-
* on the network or a 5xx. Retrying, or fixing the connection, can help.
|
|
172
|
-
* - `TIMEOUT`: no answer inside the marketplace's cancellation window.
|
|
173
|
-
*
|
|
174
|
-
* A host written before `UNREACHABLE` existed treats an unknown value as it
|
|
175
|
-
* treats REFUSED: the order stays and a person finishes in the marketplace's app.
|
|
176
|
-
*/
|
|
177
|
-
export type CancellationFailureReason = "REFUSED" | "UNREACHABLE" | "TIMEOUT";
|
|
178
|
-
|
|
179
130
|
/**
|
|
180
131
|
* The host's domain, as five questions the engine asks and never answers.
|
|
181
132
|
*
|
|
@@ -207,6 +158,13 @@ export interface MarketplaceOrderPort {
|
|
|
207
158
|
): Promise<string>;
|
|
208
159
|
/** Does this catalog id exist and belong to this tenant? */
|
|
209
160
|
catalogHas(clientId: string, subjectRef: string): Promise<boolean>;
|
|
161
|
+
/**
|
|
162
|
+
* OPTIONAL. Is `variationRef` a variation of the catalog id `subjectRef`,
|
|
163
|
+
* and this tenant's? Asked before a SKU is mapped to a variation (FUT-4927);
|
|
164
|
+
* `subjectRef` has already passed `catalogHas`. Absent: a mapping that names
|
|
165
|
+
* a variation is refused, and mapping to the item alone works as before.
|
|
166
|
+
*/
|
|
167
|
+
catalogHasVariation?(clientId: string, subjectRef: string, variationRef: string): Promise<boolean>;
|
|
210
168
|
/** The store said yes, or the connection auto-accepts. */
|
|
211
169
|
onAccepted(clientId: string, orderId: string): Promise<void>;
|
|
212
170
|
/**
|
package/src/server/mcp.ts
CHANGED
|
@@ -104,12 +104,16 @@ export const listMarketplaceMenuLinksQuery = z.strictObject({
|
|
|
104
104
|
ids: idsText.optional(),
|
|
105
105
|
});
|
|
106
106
|
|
|
107
|
-
/**
|
|
107
|
+
/**
|
|
108
|
+
* Map one external SKU to a catalog id, or unmap it with `menuItemId: null`.
|
|
109
|
+
* `variationId` narrows it to one variation of that id; absent or null maps the item alone.
|
|
110
|
+
*/
|
|
108
111
|
const menuLinkInput = z.strictObject({
|
|
109
112
|
provider: providerSchema,
|
|
110
113
|
externalSku: z.string().trim().min(1),
|
|
111
114
|
externalName: z.string().trim().min(1).optional(),
|
|
112
115
|
menuItemId: z.string().min(1).nullable(),
|
|
116
|
+
variationId: z.string().min(1).nullable().optional(),
|
|
113
117
|
});
|
|
114
118
|
|
|
115
119
|
/** Many SKU links, all-or-nothing; idempotent on (provider, externalSku). */
|
|
@@ -203,6 +207,7 @@ const menuLinkSchema = z.object({
|
|
|
203
207
|
externalSku: z.string(),
|
|
204
208
|
externalName: z.string(),
|
|
205
209
|
menuItemId: z.string().nullable(),
|
|
210
|
+
variationId: z.string().nullable(),
|
|
206
211
|
lastSeenAt: z.string(),
|
|
207
212
|
});
|
|
208
213
|
|
|
@@ -274,7 +279,7 @@ export const MARKETPLACE_MCP_TOOLS = [
|
|
|
274
279
|
method: "PUT",
|
|
275
280
|
path: `${BASE}/menu-links`,
|
|
276
281
|
summary:
|
|
277
|
-
"Map one or many external marketplace SKUs to the store's catalog ids, or unmap them with `menuItemId: null
|
|
282
|
+
"Map one or many external marketplace SKUs to the store's catalog ids, or unmap them with `menuItemId: null`; `variationId` maps a SKU to one variation of its catalog id instead of the item alone, in one all-or-nothing call keyed by (`provider`, `externalSku`). A SKU never seen is created, so a store can map ahead of the first order. Applies to orders ingested from now on. Any problem (a catalog id that is not the store's, a variation that is not one of its catalog id's, a repeated SKU) writes nothing; `validateOnly: true` checks without writing.",
|
|
278
283
|
tags: ["marketplace"],
|
|
279
284
|
params: marketplaceParams,
|
|
280
285
|
body: upsertMarketplaceMenuLinksBody,
|