@12-apps/marketplace 1.8.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 +103 -9
- package/dist/99food/index.d.ts +30 -54
- package/dist/99food/index.js +60 -418
- package/dist/99food/index.js.map +1 -1
- package/dist/{chunk-BMCERPQX.js → chunk-2BA2EHNF.js} +16 -6
- package/dist/chunk-2BA2EHNF.js.map +1 -0
- package/dist/chunk-PYHNM47K.js +478 -0
- package/dist/chunk-PYHNM47K.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/client-B4EQCRD_.d.ts +20 -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/keeta/en-US.d.ts +19 -0
- package/dist/keeta/en-US.js +23 -0
- package/dist/keeta/en-US.js.map +1 -0
- package/dist/keeta/index.d.ts +253 -0
- package/dist/keeta/index.js +388 -0
- package/dist/keeta/index.js.map +1 -0
- package/dist/keeta/pt-BR.d.ts +23 -0
- package/dist/keeta/pt-BR.js +23 -0
- package/dist/keeta/pt-BR.js.map +1 -0
- 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 +14 -2
- package/prisma/marketplace.prisma +6 -0
- package/prisma/migrations/20261126230000_marketplace_menu_link_variation/migration.sql +11 -0
- package/src/99food/catalogue.ts +19 -89
- package/src/99food/copy.ts +4 -19
- package/src/99food/index.ts +28 -33
- package/src/99food/normalize.ts +11 -253
- package/src/99food/outbound.ts +29 -270
- package/src/99food/pause.ts +7 -22
- 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/index.ts +2 -1
- package/src/keeta/authorization.ts +226 -0
- package/src/keeta/copy.ts +27 -0
- package/src/keeta/en-US.ts +19 -0
- package/src/keeta/index.ts +162 -0
- package/src/keeta/merchant.ts +70 -0
- package/src/keeta/normalize.ts +131 -0
- package/src/keeta/outbound.ts +55 -0
- package/src/keeta/pt-BR.ts +23 -0
- package/src/keeta/signature.ts +85 -0
- package/src/open-delivery/client.ts +185 -0
- package/src/open-delivery/merchant.ts +126 -0
- package/src/open-delivery/normalize.ts +269 -0
- package/src/open-delivery/orders.ts +241 -0
- package/src/open-delivery/reasons.ts +26 -0
- 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
|
@@ -0,0 +1,241 @@
|
|
|
1
|
+
import {
|
|
2
|
+
answered,
|
|
3
|
+
outboundResultOf,
|
|
4
|
+
type AdapterCallContext,
|
|
5
|
+
type CancellationRequestInput,
|
|
6
|
+
type FetchOrderResult,
|
|
7
|
+
type MarketplaceHttpResponse,
|
|
8
|
+
type OutboundOrderRef,
|
|
9
|
+
type OutboundResult,
|
|
10
|
+
type StatusFacts,
|
|
11
|
+
} from "../core/adapter";
|
|
12
|
+
import type { NormalizedMarketplaceOrder } from "../core/types";
|
|
13
|
+
import { isStop, openDeliveryCall, type OpenDeliveryDialect, type OpenDeliveryStop } from "./client";
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* The ORDER calls every Open Delivery adapter shares: the order fetch,
|
|
17
|
+
* confirm, requestCancellation and the status steps. Paths, verbs, bodies and
|
|
18
|
+
* answers are the standard's (v1.7.1 and v1.5.0 agree on all of them); the
|
|
19
|
+
* job that calls these, its retries and what a failure shows are the
|
|
20
|
+
* engine's. What a marketplace does differently is an option here, set by its
|
|
21
|
+
* adapter folder. Tests never reach a marketplace: the HTTP client is the
|
|
22
|
+
* host's port, answered by a fake.
|
|
23
|
+
*/
|
|
24
|
+
|
|
25
|
+
/** One authenticated call to an order's resource. */
|
|
26
|
+
function callOrder(
|
|
27
|
+
ctx: AdapterCallContext,
|
|
28
|
+
dialect: OpenDeliveryDialect,
|
|
29
|
+
request: { method: "GET" | "POST"; externalOrderId: string; action: string | null; body?: unknown },
|
|
30
|
+
): Promise<MarketplaceHttpResponse | OpenDeliveryStop> {
|
|
31
|
+
const suffix = request.action === null ? "" : `/${request.action}`;
|
|
32
|
+
return openDeliveryCall(ctx, dialect, {
|
|
33
|
+
method: request.method,
|
|
34
|
+
path: `/v1/orders/${encodeURIComponent(request.externalOrderId)}${suffix}`,
|
|
35
|
+
...(request.body === undefined ? {} : { body: request.body }),
|
|
36
|
+
});
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
/** Whether a 422 answer's `CancellationError.cancellationStatus` says the order is already cancelled. */
|
|
40
|
+
function alreadyCancelled(response: MarketplaceHttpResponse): boolean {
|
|
41
|
+
if (response.status !== 422) return false;
|
|
42
|
+
try {
|
|
43
|
+
return (JSON.parse(response.body) as { cancellationStatus?: unknown }).cancellationStatus === "CANCELLED";
|
|
44
|
+
} catch {
|
|
45
|
+
return false;
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
/** {@link answered} for a fetch: a call that never got an answer is a retry. */
|
|
50
|
+
async function fetched(call: () => Promise<FetchOrderResult>): Promise<FetchOrderResult> {
|
|
51
|
+
try {
|
|
52
|
+
return await call();
|
|
53
|
+
} catch (error) {
|
|
54
|
+
return { kind: "retry", reason: error instanceof Error ? error.message : String(error) };
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
/** How a fetched `Order` becomes the engine's order. */
|
|
59
|
+
interface OpenDeliveryFetchOptions {
|
|
60
|
+
normalize(order: unknown): NormalizedMarketplaceOrder | null;
|
|
61
|
+
/**
|
|
62
|
+
* OPTIONAL. What the adapter does to the parsed `Order` before it is
|
|
63
|
+
* normalized (Keeta's encrypted fields). Never fails the fetch.
|
|
64
|
+
*/
|
|
65
|
+
prepare?(ctx: AdapterCallContext, order: unknown): Promise<unknown>;
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
/** `GET /v1/orders/{orderId}`: the order an event announced, normalized. A 404 is an expired order: refused. */
|
|
69
|
+
export function openDeliveryFetchOrder(
|
|
70
|
+
ctx: AdapterCallContext,
|
|
71
|
+
dialect: OpenDeliveryDialect,
|
|
72
|
+
order: { externalOrderId: string },
|
|
73
|
+
options: OpenDeliveryFetchOptions,
|
|
74
|
+
): Promise<FetchOrderResult> {
|
|
75
|
+
return fetched(async () => {
|
|
76
|
+
const response = await callOrder(ctx, dialect, { method: "GET", externalOrderId: order.externalOrderId, action: null });
|
|
77
|
+
if (isStop(response)) return response;
|
|
78
|
+
const result = outboundResultOf(response);
|
|
79
|
+
if (result.kind !== "accepted") return result;
|
|
80
|
+
let body: unknown;
|
|
81
|
+
try {
|
|
82
|
+
body = JSON.parse(response.body);
|
|
83
|
+
} catch {
|
|
84
|
+
return { kind: "refused", status: response.status, message: "the order is not JSON" };
|
|
85
|
+
}
|
|
86
|
+
const prepared = options.prepare ? await options.prepare(ctx, body) : body;
|
|
87
|
+
const normalized = options.normalize(prepared);
|
|
88
|
+
return normalized
|
|
89
|
+
? { kind: "order", order: normalized }
|
|
90
|
+
: { kind: "refused", status: response.status, message: "the order carries no id or no line" };
|
|
91
|
+
});
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
/**
|
|
95
|
+
* `POST /v1/orders/{orderId}/confirm` with the standard's `OrderConfirmed`:
|
|
96
|
+
* `orderExternalCode` (the host's own order id) and `createdAt`, plus the
|
|
97
|
+
* `preparationTime` in minutes when the store stated one. `createdAt` is sent
|
|
98
|
+
* as the moment of the confirmation: ESTIMATED (the standard describes it
|
|
99
|
+
* with the ORDER's creation text, a copy of another schema's).
|
|
100
|
+
*
|
|
101
|
+
* Every 422 is a refusal. It is the standard's generic `UnprocessableEntity`
|
|
102
|
+
* ("check if the instruction has not been sent before"), and its body cannot
|
|
103
|
+
* tell a duplicate from a cancelled order. So a retried confirm whose first
|
|
104
|
+
* answer was lost reads "not confirmed" with the app link (a false alarm a
|
|
105
|
+
* person dismisses on the marketplace), and a confirm the marketplace truly
|
|
106
|
+
* refused is never stamped confirmed.
|
|
107
|
+
*/
|
|
108
|
+
export function openDeliveryConfirm(
|
|
109
|
+
ctx: AdapterCallContext,
|
|
110
|
+
dialect: OpenDeliveryDialect,
|
|
111
|
+
order: OutboundOrderRef,
|
|
112
|
+
input: { preparationMinutes: number | null },
|
|
113
|
+
): Promise<OutboundResult> {
|
|
114
|
+
return answered(async () => {
|
|
115
|
+
const response = await callOrder(ctx, dialect, {
|
|
116
|
+
method: "POST",
|
|
117
|
+
externalOrderId: order.externalOrderId,
|
|
118
|
+
action: "confirm",
|
|
119
|
+
body: {
|
|
120
|
+
orderExternalCode: order.hostOrderCode,
|
|
121
|
+
createdAt: ctx.now.toISOString(),
|
|
122
|
+
...(input.preparationMinutes === null ? {} : { preparationTime: input.preparationMinutes }),
|
|
123
|
+
},
|
|
124
|
+
});
|
|
125
|
+
return isStop(response) ? response : outboundResultOf(response);
|
|
126
|
+
});
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
/** The standard's limit on `outOfStockItems`. */
|
|
130
|
+
const MAX_OUT_OF_STOCK_ITEMS = 100;
|
|
131
|
+
|
|
132
|
+
/** How a marketplace reads `RequestCancelled.outOfStockItems`. */
|
|
133
|
+
interface OpenDeliveryCancellationOptions {
|
|
134
|
+
/**
|
|
135
|
+
* The offending SKUs (`externalCode`) are sent as `outOfStockItems` with an
|
|
136
|
+
* item-unavailable reason. False where the marketplace wants its own item
|
|
137
|
+
* uuids there, which the engine does not keep (99Food, v1.7.1).
|
|
138
|
+
*/
|
|
139
|
+
outOfStockAsSkus: boolean;
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
/**
|
|
143
|
+
* `POST /v1/orders/{orderId}/requestCancellation` with the standard's
|
|
144
|
+
* `RequestCancelled`: `reason` (free text: the store's, else the label),
|
|
145
|
+
* `code` (one of the standard's codes) and `mode` (`MANUAL` or `AUTO`). 202
|
|
146
|
+
* means taken; the outcome is a later event. A 422 whose `cancellationStatus`
|
|
147
|
+
* is `CANCELLED` means the order is cancelled already, which is what the
|
|
148
|
+
* request wanted. A 400 is a malformed request (a bad id or body: ours); a
|
|
149
|
+
* 422 is the business answer and stays a refusal.
|
|
150
|
+
*/
|
|
151
|
+
export function openDeliveryRequestCancellation(
|
|
152
|
+
ctx: AdapterCallContext,
|
|
153
|
+
dialect: OpenDeliveryDialect,
|
|
154
|
+
order: OutboundOrderRef,
|
|
155
|
+
input: CancellationRequestInput,
|
|
156
|
+
options: OpenDeliveryCancellationOptions,
|
|
157
|
+
): Promise<OutboundResult> {
|
|
158
|
+
const skus =
|
|
159
|
+
options.outOfStockAsSkus && input.reason.itemUnavailable && input.offendingSkus.length > 0
|
|
160
|
+
? { outOfStockItems: input.offendingSkus.slice(0, MAX_OUT_OF_STOCK_ITEMS) }
|
|
161
|
+
: {};
|
|
162
|
+
return answered(async () => {
|
|
163
|
+
const response = await callOrder(ctx, dialect, {
|
|
164
|
+
method: "POST",
|
|
165
|
+
externalOrderId: order.externalOrderId,
|
|
166
|
+
action: "requestCancellation",
|
|
167
|
+
body: {
|
|
168
|
+
reason: input.text === "" ? input.reason.label : input.text,
|
|
169
|
+
code: input.reason.code,
|
|
170
|
+
mode: input.mode,
|
|
171
|
+
...skus,
|
|
172
|
+
},
|
|
173
|
+
});
|
|
174
|
+
if (isStop(response)) return response;
|
|
175
|
+
if (alreadyCancelled(response)) return { kind: "accepted" };
|
|
176
|
+
const result = outboundResultOf(response);
|
|
177
|
+
return result.kind === "refused" && result.status === 400 ? { ...result, own: true } : result;
|
|
178
|
+
});
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
/** Which standard status call a step takes, for which kind of order. */
|
|
182
|
+
type StatusPath = (facts: StatusFacts) => { action: string; body?: unknown } | null;
|
|
183
|
+
|
|
184
|
+
/** The status calls a marketplace serves. */
|
|
185
|
+
interface OpenDeliveryStatusOptions {
|
|
186
|
+
/** A take-out's HANDED_OVER is `POST /v1/orders/{id}/pickedUp`. False where the marketplace has no take-out. */
|
|
187
|
+
takeoutPickedUp: boolean;
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
/**
|
|
191
|
+
* The standard's status calls (FUT-4653):
|
|
192
|
+
*
|
|
193
|
+
* - READY: `POST /v1/orders/{id}/readyForPickup`, no body; 202. Every kind of order.
|
|
194
|
+
* - DISPATCHED: `POST /v1/orders/{id}/dispatch`, body `OrderDispatched`
|
|
195
|
+
* (`deliveryTrackingInfo` optional, sent empty). Only where the logistics
|
|
196
|
+
* is the merchant's.
|
|
197
|
+
* - HANDED_OVER: the merchant's bag `POST /v1/orders/{id}/delivered`; a
|
|
198
|
+
* take-out `POST /v1/orders/{id}/pickedUp` where the marketplace serves it.
|
|
199
|
+
* The marketplace's own fleet: none.
|
|
200
|
+
*
|
|
201
|
+
* The kind of order is `deliveredBy` (null: a take-out; ESTIMATED: an order
|
|
202
|
+
* that merely omits it reads as a take-out here).
|
|
203
|
+
*/
|
|
204
|
+
function statusPaths(options: OpenDeliveryStatusOptions): Readonly<Record<string, StatusPath>> {
|
|
205
|
+
return {
|
|
206
|
+
READY: () => ({ action: "readyForPickup" }),
|
|
207
|
+
DISPATCHED: (facts) => (facts.deliveredBy === "MERCHANT" ? { action: "dispatch", body: {} } : null),
|
|
208
|
+
HANDED_OVER: (facts) =>
|
|
209
|
+
facts.deliveredBy === "MERCHANT"
|
|
210
|
+
? { action: "delivered", body: {} }
|
|
211
|
+
: facts.deliveredBy === null && options.takeoutPickedUp
|
|
212
|
+
? { action: "pickedUp", body: {} }
|
|
213
|
+
: null,
|
|
214
|
+
};
|
|
215
|
+
}
|
|
216
|
+
|
|
217
|
+
/** A marketplace's status half: whether a step is taken (offline), and the call that tells it. */
|
|
218
|
+
export function openDeliveryStatus(options: OpenDeliveryStatusOptions) {
|
|
219
|
+
const paths = statusPaths(options);
|
|
220
|
+
return {
|
|
221
|
+
supported: (facts: StatusFacts): boolean => paths[facts.step]?.(facts) != null,
|
|
222
|
+
inform(
|
|
223
|
+
ctx: AdapterCallContext,
|
|
224
|
+
dialect: OpenDeliveryDialect,
|
|
225
|
+
order: OutboundOrderRef,
|
|
226
|
+
facts: StatusFacts,
|
|
227
|
+
): Promise<OutboundResult | { kind: "unsupported" }> {
|
|
228
|
+
const path = paths[facts.step]?.(facts) ?? null;
|
|
229
|
+
if (path === null) return Promise.resolve({ kind: "unsupported" });
|
|
230
|
+
return answered(async () => {
|
|
231
|
+
const response = await callOrder(ctx, dialect, {
|
|
232
|
+
method: "POST",
|
|
233
|
+
externalOrderId: order.externalOrderId,
|
|
234
|
+
action: path.action,
|
|
235
|
+
...(path.body === undefined ? {} : { body: path.body }),
|
|
236
|
+
});
|
|
237
|
+
return isStop(response) ? response : outboundResultOf(response);
|
|
238
|
+
});
|
|
239
|
+
},
|
|
240
|
+
};
|
|
241
|
+
}
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The cancellation codes a merchant may send, verbatim from Open Delivery's
|
|
3
|
+
* `RequestCancelled.code`: the same eleven in v1.7.1 and in Keeta's v1.5.0
|
|
4
|
+
* (its "códigos de cancelamento (v1.0.7+)"). Pinned by the standard, not
|
|
5
|
+
* estimated. The two codes only the marketplace sends
|
|
6
|
+
* (`CONSUMER_CANCELLATION_REQUESTED`, `OTHER_CANCELLATION_REASON`) are not a
|
|
7
|
+
* store's to choose and are left out.
|
|
8
|
+
*/
|
|
9
|
+
export const OPEN_DELIVERY_MERCHANT_REASON_CODES = [
|
|
10
|
+
"SYSTEMIC_ISSUES",
|
|
11
|
+
"DUPLICATE_APPLICATION",
|
|
12
|
+
"UNAVAILABLE_ITEM",
|
|
13
|
+
"RESTAURANT_WITHOUT_DELIVERY_PERSON",
|
|
14
|
+
"OUTDATED_MENU",
|
|
15
|
+
"ORDER_OUTSIDE_THE_DELIVERY_AREA",
|
|
16
|
+
"BLOCKED_CUSTOMER",
|
|
17
|
+
"OUTSIDE_DELIVERY_HOURS",
|
|
18
|
+
"INTERNAL_DIFFICULTIES_OF_THE_RESTAURANT",
|
|
19
|
+
"RISK_AREA",
|
|
20
|
+
"DELIVERY_PROBLEM",
|
|
21
|
+
] as const;
|
|
22
|
+
|
|
23
|
+
type OpenDeliveryMerchantReasonCode = (typeof OPEN_DELIVERY_MERCHANT_REASON_CODES)[number];
|
|
24
|
+
|
|
25
|
+
/** The one reason that means "an item is unavailable". */
|
|
26
|
+
export const OPEN_DELIVERY_ITEM_UNAVAILABLE: OpenDeliveryMerchantReasonCode = "UNAVAILABLE_ITEM";
|
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,
|