@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
package/dist/99food/index.js
CHANGED
|
@@ -1,464 +1,106 @@
|
|
|
1
1
|
import {
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
2
|
+
OPEN_DELIVERY_ITEM_UNAVAILABLE,
|
|
3
|
+
OPEN_DELIVERY_MERCHANT_REASON_CODES,
|
|
4
|
+
assertHttpsUrl,
|
|
5
|
+
normalizeOpenDeliveryEventAs,
|
|
6
|
+
normalizeOpenDeliveryOrderAs,
|
|
7
|
+
openDeliveryConfirm,
|
|
8
|
+
openDeliveryFetchOrder,
|
|
9
|
+
openDeliveryMenuDocument,
|
|
10
|
+
openDeliveryMerchantUpdate,
|
|
11
|
+
openDeliveryRequestCancellation,
|
|
12
|
+
openDeliveryStatus,
|
|
13
|
+
openDeliverySwitchAvailability,
|
|
14
|
+
verifyOpenDeliverySignature
|
|
15
|
+
} from "../chunk-PYHNM47K.js";
|
|
16
|
+
import "../chunk-M2V4RFMA.js";
|
|
17
|
+
import "../chunk-BD7DS3XM.js";
|
|
18
|
+
import "../chunk-HEXLCTOG.js";
|
|
18
19
|
import {
|
|
19
20
|
__name
|
|
20
21
|
} from "../chunk-7QVYU63E.js";
|
|
21
22
|
|
|
22
23
|
// src/99food/copy.ts
|
|
23
|
-
var NINETY_NINE_FOOD_REASON_CODES =
|
|
24
|
-
"SYSTEMIC_ISSUES",
|
|
25
|
-
"DUPLICATE_APPLICATION",
|
|
26
|
-
"UNAVAILABLE_ITEM",
|
|
27
|
-
"RESTAURANT_WITHOUT_DELIVERY_PERSON",
|
|
28
|
-
"OUTDATED_MENU",
|
|
29
|
-
"ORDER_OUTSIDE_THE_DELIVERY_AREA",
|
|
30
|
-
"BLOCKED_CUSTOMER",
|
|
31
|
-
"OUTSIDE_DELIVERY_HOURS",
|
|
32
|
-
"INTERNAL_DIFFICULTIES_OF_THE_RESTAURANT",
|
|
33
|
-
"RISK_AREA",
|
|
34
|
-
"DELIVERY_PROBLEM"
|
|
35
|
-
];
|
|
24
|
+
var NINETY_NINE_FOOD_REASON_CODES = OPEN_DELIVERY_MERCHANT_REASON_CODES;
|
|
36
25
|
|
|
37
26
|
// src/99food/normalize.ts
|
|
38
27
|
var NINETY_NINE_FOOD_PROVIDER = "NINETY_NINE_FOOD";
|
|
39
|
-
var CREATED = "CREATED";
|
|
40
|
-
var CANCELLED = "CANCELLED";
|
|
41
|
-
var LIFECYCLE_OF = { DISPATCHED: "DISPATCHED", CONCLUDED: "CONCLUDED" };
|
|
42
|
-
var CANCELLATION_REQUEST_DENIED = "CANCELLATION_REQUEST_DENIED";
|
|
43
|
-
function verifyOpenDeliverySignature(rawBody, signature, webhookSecret) {
|
|
44
|
-
if (!webhookSecret) return false;
|
|
45
|
-
return verifyHmacSha256Hex(rawBody, signature, webhookSecret);
|
|
46
|
-
}
|
|
47
|
-
__name(verifyOpenDeliverySignature, "verifyOpenDeliverySignature");
|
|
48
28
|
function normalizeOpenDeliveryEvent(payload) {
|
|
49
|
-
|
|
50
|
-
if (!externalOrderId) return null;
|
|
51
|
-
const eventType = (firstString(payload, [["eventType"]]) ?? "UNKNOWN").toUpperCase();
|
|
52
|
-
const cancelled = eventType === CANCELLED;
|
|
53
|
-
const denied = eventType === CANCELLATION_REQUEST_DENIED;
|
|
54
|
-
return {
|
|
55
|
-
provider: NINETY_NINE_FOOD_PROVIDER,
|
|
56
|
-
// The spec's own event id. The fallback is deterministic so a redelivery
|
|
57
|
-
// still collides on the inbox's unique index.
|
|
58
|
-
eventId: firstString(payload, [["eventId"]]) ?? `${externalOrderId}:${eventType}`,
|
|
59
|
-
eventType,
|
|
60
|
-
externalMerchantId: null,
|
|
61
|
-
externalOrderId,
|
|
62
|
-
order: null,
|
|
63
|
-
orderToFetch: eventType === CREATED,
|
|
64
|
-
cancelled,
|
|
65
|
-
cancellationOutcome: cancelled ? "CONFIRMED" : denied ? "FAILED" : null,
|
|
66
|
-
...LIFECYCLE_OF[eventType] ? { lifecycle: LIFECYCLE_OF[eventType] } : {}
|
|
67
|
-
};
|
|
29
|
+
return normalizeOpenDeliveryEventAs(NINETY_NINE_FOOD_PROVIDER, payload);
|
|
68
30
|
}
|
|
69
31
|
__name(normalizeOpenDeliveryEvent, "normalizeOpenDeliveryEvent");
|
|
70
|
-
function priceCents(source, ...path) {
|
|
71
|
-
return toCents(pick(source, ...path, "value"));
|
|
72
|
-
}
|
|
73
|
-
__name(priceCents, "priceCents");
|
|
74
|
-
function lineNote(item, fraction) {
|
|
75
|
-
const rawOptions = pick(item, "options");
|
|
76
|
-
const options = (Array.isArray(rawOptions) ? rawOptions : []).map((option) => {
|
|
77
|
-
const name = asString(pick(option, "name"));
|
|
78
|
-
const quantity = Number(pick(option, "quantity") ?? 1);
|
|
79
|
-
return name && quantity > 1 ? `${quantity}x ${name}` : name;
|
|
80
|
-
}).filter((name) => name !== null);
|
|
81
|
-
const parts = [fraction, options.join(", ") || null, firstString(item, [["specialInstructions"]])];
|
|
82
|
-
const note = parts.filter((part) => part !== null).join(" \xB7 ");
|
|
83
|
-
return note === "" ? null : note;
|
|
84
|
-
}
|
|
85
|
-
__name(lineNote, "lineNote");
|
|
86
|
-
function lineTotalCents(item, quantity) {
|
|
87
|
-
const total = priceCents(item, "totalPrice");
|
|
88
|
-
if (total !== null) return total;
|
|
89
|
-
const unit = priceCents(item, "unitPrice") ?? 0;
|
|
90
|
-
return Math.round(unit * quantity) + (priceCents(item, "optionsPrice") ?? 0);
|
|
91
|
-
}
|
|
92
|
-
__name(lineTotalCents, "lineTotalCents");
|
|
93
|
-
function quantityOf(item) {
|
|
94
|
-
const stated = Number(pick(item, "quantity") ?? 1);
|
|
95
|
-
return Number.isFinite(stated) && stated > 0 ? stated : 1;
|
|
96
|
-
}
|
|
97
|
-
__name(quantityOf, "quantityOf");
|
|
98
|
-
function splitWhole(total, quantity) {
|
|
99
|
-
const floor = Math.floor(total / quantity);
|
|
100
|
-
const remainder = total - floor * quantity;
|
|
101
|
-
if (remainder === 0) return [{ quantity, unitPriceCents: floor }];
|
|
102
|
-
const rest = quantity === 1 ? [] : [{ quantity: quantity - 1, unitPriceCents: floor }];
|
|
103
|
-
return [...rest, { quantity: 1, unitPriceCents: floor + remainder }];
|
|
104
|
-
}
|
|
105
|
-
__name(splitWhole, "splitWhole");
|
|
106
|
-
function readLines(item) {
|
|
107
|
-
const name = firstString(item, [["name"]]);
|
|
108
|
-
const sku = firstString(item, [["externalCode"], ["id"]]) ?? name;
|
|
109
|
-
if (!sku) return [];
|
|
110
|
-
const quantity = quantityOf(item);
|
|
111
|
-
const whole = Number.isInteger(quantity);
|
|
112
|
-
const fraction = whole ? null : `${quantity} ${firstString(item, [["unit"]]) ?? ""}`.trim();
|
|
113
|
-
const total = lineTotalCents(item, quantity);
|
|
114
|
-
const parts = whole ? splitWhole(total, quantity) : [{ quantity: 1, unitPriceCents: total }];
|
|
115
|
-
const note = lineNote(item, fraction);
|
|
116
|
-
return parts.map((part) => ({ externalSku: sku, name: name ?? sku, ...part, note }));
|
|
117
|
-
}
|
|
118
|
-
__name(readLines, "readLines");
|
|
119
|
-
function readOrderType(order) {
|
|
120
|
-
const stated = firstString(order, [["type"]])?.toUpperCase();
|
|
121
|
-
return stated === "DELIVERY" || stated === "TAKEOUT" ? stated : null;
|
|
122
|
-
}
|
|
123
|
-
__name(readOrderType, "readOrderType");
|
|
124
|
-
function readDeliveredBy(order) {
|
|
125
|
-
const stated = firstString(order, [["delivery", "deliveredBy"]])?.toUpperCase();
|
|
126
|
-
return stated === "MERCHANT" || stated === "MARKETPLACE" ? stated : null;
|
|
127
|
-
}
|
|
128
|
-
__name(readDeliveredBy, "readDeliveredBy");
|
|
129
|
-
function readAddress(order, missingStreetNumber) {
|
|
130
|
-
const raw = pick(order, "delivery", "deliveryAddress");
|
|
131
|
-
const street = firstString(raw, [["street"], ["streetName"]]);
|
|
132
|
-
if (street === null) return null;
|
|
133
|
-
return {
|
|
134
|
-
street,
|
|
135
|
-
number: firstString(raw, [["number"], ["streetNumber"]]) ?? missingStreetNumber,
|
|
136
|
-
neighborhood: firstString(raw, [["district"], ["neighborhood"]]) ?? "",
|
|
137
|
-
complement: firstString(raw, [["complement"]]),
|
|
138
|
-
reference: firstString(raw, [["reference"]]),
|
|
139
|
-
postalCode: firstString(raw, [["postalCode"]])?.replace(/\D/g, "") || null,
|
|
140
|
-
city: firstString(raw, [["city"]]),
|
|
141
|
-
state: firstString(raw, [["state"]])?.replace(/^[A-Z]{2}-/u, "") ?? null
|
|
142
|
-
};
|
|
143
|
-
}
|
|
144
|
-
__name(readAddress, "readAddress");
|
|
145
32
|
function normalizeOpenDeliveryOrder(order, options) {
|
|
146
|
-
|
|
147
|
-
const rawItems = pick(order, "items");
|
|
148
|
-
const lines = (Array.isArray(rawItems) ? rawItems : []).flatMap(readLines);
|
|
149
|
-
if (!externalOrderId || lines.length === 0) return null;
|
|
150
|
-
return {
|
|
151
|
-
provider: NINETY_NINE_FOOD_PROVIDER,
|
|
152
|
-
externalOrderId,
|
|
153
|
-
externalDisplayId: firstString(order, [["displayId"]]),
|
|
154
|
-
externalMerchantId: firstString(order, [["merchant", "id"]]),
|
|
155
|
-
buyer: {
|
|
156
|
-
externalId: firstString(order, [["customer", "id"]]),
|
|
157
|
-
name: firstString(order, [["customer", "name"]]),
|
|
158
|
-
phone: firstString(order, [["customer", "phone", "number"]])
|
|
159
|
-
},
|
|
160
|
-
lines,
|
|
161
|
-
// The line sum is the FALLBACK: the stated total carries the fees and
|
|
162
|
-
// discounts the lines do not, and it is the number on the receipt.
|
|
163
|
-
totalCents: firstCents(order, [["total", "orderAmount", "value"]]) ?? lines.reduce((sum, line) => sum + line.unitPriceCents * line.quantity, 0),
|
|
164
|
-
commissionCents: null,
|
|
165
|
-
deliveryDueAt: firstDate(order, [
|
|
166
|
-
["delivery", "estimatedDeliveryDateTime"],
|
|
167
|
-
["takeout", "takeoutDateTime"],
|
|
168
|
-
["schedule", "scheduledDateTimeEnd"]
|
|
169
|
-
]),
|
|
170
|
-
placedAt: firstDate(order, [["createdAt"]]) ?? /* @__PURE__ */ new Date(),
|
|
171
|
-
orderType: readOrderType(order),
|
|
172
|
-
deliveredBy: readDeliveredBy(order),
|
|
173
|
-
deliveryAddress: readAddress(order, options.missingStreetNumber)
|
|
174
|
-
};
|
|
33
|
+
return normalizeOpenDeliveryOrderAs(NINETY_NINE_FOOD_PROVIDER, order, options);
|
|
175
34
|
}
|
|
176
35
|
__name(normalizeOpenDeliveryOrder, "normalizeOpenDeliveryOrder");
|
|
177
36
|
|
|
178
37
|
// src/99food/outbound.ts
|
|
179
|
-
|
|
180
|
-
var DEFAULT_TOKEN_SECONDS = 3600;
|
|
181
|
-
function baseOf(ctx, bases) {
|
|
182
|
-
const base = bases[ctx.environment];
|
|
183
|
-
let end = base.length;
|
|
184
|
-
while (end > 0 && base.charCodeAt(end - 1) === 47) end -= 1;
|
|
185
|
-
return base.slice(0, end);
|
|
186
|
-
}
|
|
187
|
-
__name(baseOf, "baseOf");
|
|
188
|
-
var tokenKey = /* @__PURE__ */ __name((ctx) => `${NINETY_NINE_FOOD_PROVIDER}:${ctx.environment}:${ctx.credentials.clientId}`, "tokenKey");
|
|
189
|
-
function tokenOf(body) {
|
|
190
|
-
try {
|
|
191
|
-
const parsed = JSON.parse(body);
|
|
192
|
-
if (typeof parsed.access_token !== "string" || parsed.access_token === "") return null;
|
|
193
|
-
const expires = typeof parsed.expires_in === "number" && parsed.expires_in > 0 ? parsed.expires_in : DEFAULT_TOKEN_SECONDS;
|
|
194
|
-
return { token: parsed.access_token, expiresInSeconds: expires };
|
|
195
|
-
} catch {
|
|
196
|
-
return null;
|
|
197
|
-
}
|
|
198
|
-
}
|
|
199
|
-
__name(tokenOf, "tokenOf");
|
|
200
|
-
async function bearer(ctx, bases) {
|
|
201
|
-
const cached = ctx.tokens.get(tokenKey(ctx));
|
|
202
|
-
if (cached && cached.expiresAt > ctx.now.getTime() + TOKEN_MARGIN_MS) return { token: cached.token };
|
|
203
|
-
const response = await ctx.http({
|
|
204
|
-
method: "POST",
|
|
205
|
-
url: `${baseOf(ctx, bases)}/oauth/token`,
|
|
206
|
-
headers: { "Content-Type": "application/x-www-form-urlencoded" },
|
|
207
|
-
body: new URLSearchParams({
|
|
208
|
-
client_id: ctx.credentials.clientId,
|
|
209
|
-
client_secret: ctx.credentials.clientSecret,
|
|
210
|
-
grant_type: "client_credentials"
|
|
211
|
-
}).toString()
|
|
212
|
-
});
|
|
213
|
-
if (response.status < 200 || response.status >= 300) {
|
|
214
|
-
const result = tokenGrantResultOf(response);
|
|
215
|
-
return result.kind === "accepted" ? { kind: "retry", reason: "unexpected token answer" } : result;
|
|
216
|
-
}
|
|
217
|
-
const token = tokenOf(response.body);
|
|
218
|
-
if (!token) return { kind: "retry", reason: "99Food answered the token grant without a token" };
|
|
219
|
-
ctx.tokens.set(tokenKey(ctx), { token: token.token, expiresAt: ctx.now.getTime() + token.expiresInSeconds * 1e3 });
|
|
220
|
-
return { token: token.token };
|
|
221
|
-
}
|
|
222
|
-
__name(bearer, "bearer");
|
|
223
|
-
async function callOrder(ctx, bases, request) {
|
|
224
|
-
const auth = await bearer(ctx, bases);
|
|
225
|
-
if (!("token" in auth)) return auth;
|
|
226
|
-
const suffix = request.action === null ? "" : `/${request.action}`;
|
|
227
|
-
const response = await ctx.http({
|
|
228
|
-
method: request.method,
|
|
229
|
-
url: `${baseOf(ctx, bases)}/v1/orders/${encodeURIComponent(request.externalOrderId)}${suffix}`,
|
|
230
|
-
headers: {
|
|
231
|
-
Authorization: `Bearer ${auth.token}`,
|
|
232
|
-
...request.body === void 0 ? {} : { "Content-Type": "application/json" }
|
|
233
|
-
},
|
|
234
|
-
...request.body === void 0 ? {} : { body: JSON.stringify(request.body) }
|
|
235
|
-
});
|
|
236
|
-
if (response.status !== 401) return response;
|
|
237
|
-
ctx.tokens.delete(tokenKey(ctx));
|
|
238
|
-
return { kind: "retry", reason: "99Food refused the token" };
|
|
239
|
-
}
|
|
240
|
-
__name(callOrder, "callOrder");
|
|
241
|
-
function alreadyCancelled(response) {
|
|
242
|
-
if (response.status !== 422) return false;
|
|
243
|
-
try {
|
|
244
|
-
return JSON.parse(response.body).cancellationStatus === "CANCELLED";
|
|
245
|
-
} catch {
|
|
246
|
-
return false;
|
|
247
|
-
}
|
|
248
|
-
}
|
|
249
|
-
__name(alreadyCancelled, "alreadyCancelled");
|
|
250
|
-
async function fetched(call) {
|
|
251
|
-
try {
|
|
252
|
-
return await call();
|
|
253
|
-
} catch (error) {
|
|
254
|
-
return { kind: "retry", reason: error instanceof Error ? error.message : String(error) };
|
|
255
|
-
}
|
|
256
|
-
}
|
|
257
|
-
__name(fetched, "fetched");
|
|
258
|
-
function openDeliveryFetchOrder(ctx, bases, order, options) {
|
|
259
|
-
return fetched(async () => {
|
|
260
|
-
const response = await callOrder(ctx, bases, { method: "GET", externalOrderId: order.externalOrderId, action: null });
|
|
261
|
-
if ("kind" in response) return response;
|
|
262
|
-
const result = outboundResultOf(response);
|
|
263
|
-
if (result.kind !== "accepted") return result;
|
|
264
|
-
let body;
|
|
265
|
-
try {
|
|
266
|
-
body = JSON.parse(response.body);
|
|
267
|
-
} catch {
|
|
268
|
-
return { kind: "refused", status: response.status, message: "the order is not JSON" };
|
|
269
|
-
}
|
|
270
|
-
const normalized = normalizeOpenDeliveryOrder(body, options);
|
|
271
|
-
return normalized ? { kind: "order", order: normalized } : { kind: "refused", status: response.status, message: "the order carries no id or no line" };
|
|
272
|
-
});
|
|
273
|
-
}
|
|
274
|
-
__name(openDeliveryFetchOrder, "openDeliveryFetchOrder");
|
|
275
|
-
function openDeliveryConfirm(ctx, bases, order, input) {
|
|
276
|
-
return answered(async () => {
|
|
277
|
-
const response = await callOrder(ctx, bases, {
|
|
278
|
-
method: "POST",
|
|
279
|
-
externalOrderId: order.externalOrderId,
|
|
280
|
-
action: "confirm",
|
|
281
|
-
body: {
|
|
282
|
-
orderExternalCode: order.hostOrderCode,
|
|
283
|
-
createdAt: ctx.now.toISOString(),
|
|
284
|
-
...input.preparationMinutes === null ? {} : { preparationTime: input.preparationMinutes }
|
|
285
|
-
}
|
|
286
|
-
});
|
|
287
|
-
if ("kind" in response) return response;
|
|
288
|
-
return outboundResultOf(response);
|
|
289
|
-
});
|
|
290
|
-
}
|
|
291
|
-
__name(openDeliveryConfirm, "openDeliveryConfirm");
|
|
292
|
-
function openDeliveryRequestCancellation(ctx, bases, order, input) {
|
|
293
|
-
return answered(async () => {
|
|
294
|
-
const response = await callOrder(ctx, bases, {
|
|
295
|
-
method: "POST",
|
|
296
|
-
externalOrderId: order.externalOrderId,
|
|
297
|
-
action: "requestCancellation",
|
|
298
|
-
body: { reason: input.text === "" ? input.reason.label : input.text, code: input.reason.code, mode: input.mode }
|
|
299
|
-
});
|
|
300
|
-
if ("kind" in response) return response;
|
|
301
|
-
if (alreadyCancelled(response)) return { kind: "accepted" };
|
|
302
|
-
const result = outboundResultOf(response);
|
|
303
|
-
return result.kind === "refused" && result.status === 400 ? { ...result, own: true } : result;
|
|
304
|
-
});
|
|
305
|
-
}
|
|
306
|
-
__name(openDeliveryRequestCancellation, "openDeliveryRequestCancellation");
|
|
307
|
-
var STATUS_PATH = {
|
|
308
|
-
READY: /* @__PURE__ */ __name(() => ({ action: "readyForPickup" }), "READY"),
|
|
309
|
-
DISPATCHED: /* @__PURE__ */ __name((facts) => facts.deliveredBy === "MERCHANT" ? { action: "dispatch", body: {} } : null, "DISPATCHED"),
|
|
310
|
-
HANDED_OVER: /* @__PURE__ */ __name((facts) => facts.deliveredBy === "MERCHANT" ? { action: "delivered", body: {} } : facts.deliveredBy === null ? { action: "pickedUp", body: {} } : null, "HANDED_OVER")
|
|
311
|
-
};
|
|
312
|
-
function openDeliveryStatusSupported(facts) {
|
|
313
|
-
return STATUS_PATH[facts.step]?.(facts) != null;
|
|
314
|
-
}
|
|
315
|
-
__name(openDeliveryStatusSupported, "openDeliveryStatusSupported");
|
|
316
|
-
function openDeliveryInformStatus(ctx, bases, order, facts) {
|
|
317
|
-
const path = STATUS_PATH[facts.step]?.(facts) ?? null;
|
|
318
|
-
if (path === null) return Promise.resolve({ kind: "unsupported" });
|
|
319
|
-
return answered(async () => {
|
|
320
|
-
const response = await callOrder(ctx, bases, {
|
|
321
|
-
method: "POST",
|
|
322
|
-
externalOrderId: order.externalOrderId,
|
|
323
|
-
action: path.action,
|
|
324
|
-
...path.body === void 0 ? {} : { body: path.body }
|
|
325
|
-
});
|
|
326
|
-
return "kind" in response ? response : outboundResultOf(response);
|
|
327
|
-
});
|
|
328
|
-
}
|
|
329
|
-
__name(openDeliveryInformStatus, "openDeliveryInformStatus");
|
|
330
|
-
|
|
331
|
-
// src/99food/catalogue.ts
|
|
332
|
-
function categoriesOf(merchantId, entries) {
|
|
333
|
-
const byName = /* @__PURE__ */ new Map();
|
|
334
|
-
for (const entry of entries) {
|
|
335
|
-
const name = entry.categoryName.trim();
|
|
336
|
-
const category = byName.get(name) ?? { id: stableUuid(`${merchantId}:category:${name}`), name, offers: [] };
|
|
337
|
-
category.offers.push(stableUuid(`${entry.ref}:offer`));
|
|
338
|
-
byName.set(name, category);
|
|
339
|
-
}
|
|
340
|
-
return [...byName.values()];
|
|
341
|
-
}
|
|
342
|
-
__name(categoriesOf, "categoriesOf");
|
|
343
|
-
function openDeliveryMenuDocument(input) {
|
|
344
|
-
const { externalMerchantId: merchantId, entries, options } = input;
|
|
345
|
-
const categories = categoriesOf(merchantId, entries);
|
|
38
|
+
function ninetyNineFoodDialect(bases) {
|
|
346
39
|
return {
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
externalCode: category.name,
|
|
360
|
-
status: "AVAILABLE",
|
|
361
|
-
itemOfferId: category.offers
|
|
362
|
-
})),
|
|
363
|
-
items: entries.map((entry) => ({
|
|
364
|
-
id: stableUuid(`${entry.ref}:item`),
|
|
365
|
-
name: entry.name,
|
|
366
|
-
description: entry.description ?? "",
|
|
367
|
-
externalCode: entry.productId,
|
|
368
|
-
status: "AVAILABLE",
|
|
369
|
-
unit: "UN",
|
|
370
|
-
...entry.imageUrl === null ? {} : { image: { URL: entry.imageUrl } }
|
|
371
|
-
})),
|
|
372
|
-
itemOffers: entries.map((entry, index) => {
|
|
373
|
-
const value = Math.round(entry.priceCents) / 100;
|
|
374
|
-
return {
|
|
375
|
-
id: stableUuid(`${entry.ref}:offer`),
|
|
376
|
-
itemId: stableUuid(`${entry.ref}:item`),
|
|
377
|
-
index,
|
|
378
|
-
status: "AVAILABLE",
|
|
379
|
-
price: { value, originalValue: value, currency: options.currency }
|
|
380
|
-
};
|
|
381
|
-
})
|
|
40
|
+
provider: NINETY_NINE_FOOD_PROVIDER,
|
|
41
|
+
name: "99Food",
|
|
42
|
+
bases,
|
|
43
|
+
tokenGrant: /* @__PURE__ */ __name((ctx) => ({
|
|
44
|
+
contentType: "application/x-www-form-urlencoded",
|
|
45
|
+
body: new URLSearchParams({
|
|
46
|
+
client_id: ctx.credentials.clientId,
|
|
47
|
+
client_secret: ctx.credentials.clientSecret,
|
|
48
|
+
grant_type: "client_credentials"
|
|
49
|
+
}).toString()
|
|
50
|
+
}), "tokenGrant"),
|
|
51
|
+
serialize: /* @__PURE__ */ __name((body) => JSON.stringify(body), "serialize")
|
|
382
52
|
};
|
|
383
53
|
}
|
|
384
|
-
__name(
|
|
385
|
-
|
|
386
|
-
|
|
387
|
-
if (!("token" in auth)) return auth;
|
|
388
|
-
const response = await ctx.http({
|
|
389
|
-
method: "POST",
|
|
390
|
-
url: `${baseOf(ctx, bases)}/v1/merchantUpdate`,
|
|
391
|
-
headers: { Authorization: `Bearer ${auth.token}`, "X-App-MerchantId": ctx.externalMerchantId }
|
|
392
|
-
});
|
|
393
|
-
if (response.status !== 401) return outboundResultOf(response);
|
|
394
|
-
ctx.tokens.delete(tokenKey(ctx));
|
|
395
|
-
return { kind: "retry", reason: "99Food refused the token" };
|
|
396
|
-
}
|
|
397
|
-
__name(notify, "notify");
|
|
54
|
+
__name(ninetyNineFoodDialect, "ninetyNineFoodDialect");
|
|
55
|
+
|
|
56
|
+
// src/99food/catalogue.ts
|
|
398
57
|
function openDeliveryCatalogue(bases) {
|
|
58
|
+
const dialect = ninetyNineFoodDialect(bases);
|
|
399
59
|
return {
|
|
400
60
|
mode: "pull",
|
|
401
61
|
menuDocument: openDeliveryMenuDocument,
|
|
402
|
-
|
|
62
|
+
// No body: 99Food pulls the whole `Merchant` again. 200 or 204.
|
|
63
|
+
notifyMenuChanged: /* @__PURE__ */ __name((ctx) => openDeliveryMerchantUpdate(ctx, dialect, {
|
|
64
|
+
path: "/v1/merchantUpdate",
|
|
65
|
+
headers: { "X-App-MerchantId": ctx.externalMerchantId }
|
|
66
|
+
}), "notifyMenuChanged")
|
|
403
67
|
};
|
|
404
68
|
}
|
|
405
69
|
__name(openDeliveryCatalogue, "openDeliveryCatalogue");
|
|
406
70
|
|
|
407
71
|
// src/99food/pause.ts
|
|
408
72
|
function openDeliverySetMerchantStatus(ctx, bases, status) {
|
|
409
|
-
return
|
|
410
|
-
|
|
411
|
-
|
|
412
|
-
const response = await ctx.http({
|
|
413
|
-
method: "POST",
|
|
414
|
-
url: `${baseOf(ctx, bases)}/v1/merchantUpdate`,
|
|
415
|
-
headers: { Authorization: `Bearer ${auth.token}`, "Content-Type": "application/json" },
|
|
416
|
-
body: JSON.stringify({ merchantStatus: status })
|
|
417
|
-
});
|
|
418
|
-
if (response.status !== 401) return outboundResultOf(response);
|
|
419
|
-
ctx.tokens.delete(tokenKey(ctx));
|
|
420
|
-
return { kind: "retry", reason: "99Food refused the token" };
|
|
73
|
+
return openDeliveryMerchantUpdate(ctx, ninetyNineFoodDialect(bases), {
|
|
74
|
+
path: "/v1/merchantUpdate",
|
|
75
|
+
body: { merchantStatus: status }
|
|
421
76
|
});
|
|
422
77
|
}
|
|
423
78
|
__name(openDeliverySetMerchantStatus, "openDeliverySetMerchantStatus");
|
|
424
79
|
function openDeliveryAvailability(bases) {
|
|
425
|
-
return
|
|
426
|
-
kind: "switch",
|
|
427
|
-
pause: /* @__PURE__ */ __name(async (ctx, request) => {
|
|
428
|
-
const result = await openDeliverySetMerchantStatus(ctx, bases, "UNAVAILABLE");
|
|
429
|
-
return result.kind === "accepted" ? { kind: "paused", externalId: null, until: request.until } : result;
|
|
430
|
-
}, "pause"),
|
|
431
|
-
resume: /* @__PURE__ */ __name((ctx) => openDeliverySetMerchantStatus(ctx, bases, "AVAILABLE"), "resume")
|
|
432
|
-
};
|
|
80
|
+
return openDeliverySwitchAvailability((ctx, _merchantId, status) => openDeliverySetMerchantStatus(ctx, bases, status));
|
|
433
81
|
}
|
|
434
82
|
__name(openDeliveryAvailability, "openDeliveryAvailability");
|
|
435
83
|
|
|
436
84
|
// src/99food/index.ts
|
|
437
85
|
var NINETY_NINE_FOOD_CANCELLATION_ANSWER_WINDOW_MS = 5 * 6e4;
|
|
438
|
-
var
|
|
439
|
-
|
|
440
|
-
let parsed;
|
|
441
|
-
try {
|
|
442
|
-
parsed = new URL(url);
|
|
443
|
-
} catch {
|
|
444
|
-
throw new Error(`@12-apps/marketplace/99food: ${name} is not a URL.`);
|
|
445
|
-
}
|
|
446
|
-
if (parsed.protocol !== "https:") throw new Error(`@12-apps/marketplace/99food: ${name} must be https.`);
|
|
447
|
-
}
|
|
448
|
-
__name(assertHttps, "assertHttps");
|
|
86
|
+
var STATUS = openDeliveryStatus({ takeoutPickedUp: true });
|
|
87
|
+
var assertHttps = /* @__PURE__ */ __name((name, url) => assertHttpsUrl("@12-apps/marketplace/99food", name, url), "assertHttps");
|
|
449
88
|
function createNinetyNineFoodAdapter(config) {
|
|
450
89
|
assertHttps("apiBaseUrl.SANDBOX", config.apiBaseUrl.SANDBOX);
|
|
451
90
|
assertHttps("apiBaseUrl.PRODUCTION", config.apiBaseUrl.PRODUCTION);
|
|
452
91
|
assertHttps("portalUrl", config.portalUrl);
|
|
453
|
-
const
|
|
92
|
+
const dialect = ninetyNineFoodDialect({ ...config.apiBaseUrl });
|
|
93
|
+
const bases = dialect.bases;
|
|
454
94
|
const reasons = NINETY_NINE_FOOD_REASON_CODES.map((code) => ({
|
|
455
95
|
code,
|
|
456
96
|
label: config.copy.reasonLabels[code],
|
|
457
|
-
itemUnavailable: code ===
|
|
458
|
-
// `outOfStockItems` is optional in the spec (see
|
|
97
|
+
itemUnavailable: code === OPEN_DELIVERY_ITEM_UNAVAILABLE,
|
|
98
|
+
// `outOfStockItems` is optional in the spec, and not sent (see `cancellation` below).
|
|
459
99
|
needsItems: false
|
|
460
100
|
}));
|
|
461
101
|
const normalizeOptions = { missingStreetNumber: config.copy.missingStreetNumber };
|
|
102
|
+
const fetchOptions = { normalize: /* @__PURE__ */ __name((order) => normalizeOpenDeliveryOrder(order, normalizeOptions), "normalize") };
|
|
103
|
+
const cancellation = { outOfStockAsSkus: false };
|
|
462
104
|
return {
|
|
463
105
|
provider: NINETY_NINE_FOOD_PROVIDER,
|
|
464
106
|
capabilities: {
|
|
@@ -473,7 +115,7 @@ function createNinetyNineFoodAdapter(config) {
|
|
|
473
115
|
// `OrderConfirmed.preparationTime`, in minutes.
|
|
474
116
|
confirmTakesPreparationTime: true,
|
|
475
117
|
cancellationAnswerWindowMs: NINETY_NINE_FOOD_CANCELLATION_ANSWER_WINDOW_MS,
|
|
476
|
-
// Open Delivery's readyForPickup, dispatch and delivered / pickedUp (
|
|
118
|
+
// Open Delivery's readyForPickup, dispatch and delivered / pickedUp (`../open-delivery/orders.ts`).
|
|
477
119
|
statusSteps: ["READY", "DISPATCHED", "HANDED_OVER"]
|
|
478
120
|
},
|
|
479
121
|
normalize: normalizeOpenDeliveryEvent,
|
|
@@ -482,11 +124,11 @@ function createNinetyNineFoodAdapter(config) {
|
|
|
482
124
|
cancellationReasons: /* @__PURE__ */ __name(() => reasons, "cancellationReasons"),
|
|
483
125
|
// PULL: 99Food reads the menu the host serves (`GET /v1/merchant`) and is told when it changed (FUT-4768).
|
|
484
126
|
catalogue: openDeliveryCatalogue(bases),
|
|
485
|
-
fetchOrder: /* @__PURE__ */ __name((ctx, order) => openDeliveryFetchOrder(ctx,
|
|
486
|
-
confirm: /* @__PURE__ */ __name((ctx, order, input) => openDeliveryConfirm(ctx,
|
|
487
|
-
requestCancellation: /* @__PURE__ */ __name((ctx, order, input) => openDeliveryRequestCancellation(ctx,
|
|
488
|
-
statusSupported:
|
|
489
|
-
informStatus: /* @__PURE__ */ __name((ctx, order, facts) =>
|
|
127
|
+
fetchOrder: /* @__PURE__ */ __name((ctx, order) => openDeliveryFetchOrder(ctx, dialect, order, fetchOptions), "fetchOrder"),
|
|
128
|
+
confirm: /* @__PURE__ */ __name((ctx, order, input) => openDeliveryConfirm(ctx, dialect, order, input), "confirm"),
|
|
129
|
+
requestCancellation: /* @__PURE__ */ __name((ctx, order, input) => openDeliveryRequestCancellation(ctx, dialect, order, input, cancellation), "requestCancellation"),
|
|
130
|
+
statusSupported: STATUS.supported,
|
|
131
|
+
informStatus: /* @__PURE__ */ __name((ctx, order, facts) => STATUS.inform(ctx, dialect, order, facts), "informStatus"),
|
|
490
132
|
appLink: /* @__PURE__ */ __name(() => config.portalUrl, "appLink"),
|
|
491
133
|
...config.pauseByStoreSwitch ? { availability: openDeliveryAvailability(bases) } : {}
|
|
492
134
|
};
|