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