@12-apps/marketplace 1.2.0 → 1.4.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 +76 -4
- package/README.md +163 -3
- package/dist/99food/index.d.ts +79 -2
- package/dist/99food/index.js +117 -2
- package/dist/99food/index.js.map +1 -1
- package/dist/adapter-B8QtQ2RA.d.ts +612 -0
- package/dist/{chunk-CRYFYDQ2.js → chunk-DPCZVN2C.js} +69 -5
- package/dist/chunk-DPCZVN2C.js.map +1 -0
- package/dist/{chunk-D2X7TOR6.js → chunk-EBAIZQPB.js} +841 -86
- package/dist/chunk-EBAIZQPB.js.map +1 -0
- package/dist/chunk-M2V4RFMA.js +23 -0
- package/dist/chunk-M2V4RFMA.js.map +1 -0
- package/dist/{chunk-24RQZPSV.js → chunk-SZRZTLVU.js} +1 -1
- package/dist/chunk-SZRZTLVU.js.map +1 -0
- package/dist/{engine-D-a4mttc.d.ts → engine-o5cm54vC.d.ts} +462 -2
- package/dist/hono/index.d.ts +2 -2
- package/dist/hono/index.js +2 -2
- package/dist/ifood/index.d.ts +22 -2
- package/dist/ifood/index.js +396 -56
- package/dist/ifood/index.js.map +1 -1
- package/dist/index.d.ts +30 -4
- package/dist/index.js +445 -9
- package/dist/index.js.map +1 -1
- package/dist/manifest/server.d.ts +5 -2
- package/dist/manifest/server.js +2 -2
- package/dist/rappi/index.d.ts +15 -2
- package/dist/rappi/index.js +62 -2
- package/dist/rappi/index.js.map +1 -1
- package/dist/server/index.d.ts +25 -10
- package/dist/server/index.js +4 -2
- package/package.json +1 -1
- package/prisma/marketplace.prisma +158 -1
- package/prisma/migrations/20261113120000_marketplace_catalogue/migration.sql +97 -0
- package/prisma/migrations/20261113230000_marketplace_store_pause/migration.sql +70 -0
- package/src/99food/catalogue.ts +117 -0
- package/src/99food/index.ts +14 -0
- package/src/99food/outbound.ts +3 -3
- package/src/99food/pause.ts +56 -0
- package/src/core/adapter.ts +20 -1
- package/src/core/catalogue-category.ts +119 -0
- package/src/core/catalogue-ids.ts +23 -0
- package/src/core/catalogue-job.ts +332 -0
- package/src/core/catalogue-port.ts +173 -0
- package/src/core/catalogue-store.ts +172 -0
- package/src/core/catalogue-sweep.ts +43 -0
- package/src/core/catalogue.ts +226 -0
- package/src/core/engine.ts +83 -0
- package/src/core/outbound-sweep.ts +7 -2
- package/src/core/pause-job.ts +381 -0
- package/src/core/pause-port.ts +110 -0
- package/src/core/pause-retired.ts +139 -0
- package/src/core/pause-store.ts +151 -0
- package/src/core/pause-sweep.ts +148 -0
- package/src/core/pause.ts +316 -0
- package/src/core/ports.ts +47 -0
- package/src/ifood/catalogue.ts +273 -0
- package/src/ifood/index.ts +8 -0
- package/src/ifood/outbound.ts +9 -6
- package/src/ifood/pause.ts +257 -0
- package/src/index.ts +64 -0
- package/src/rappi/index.ts +17 -0
- package/src/rappi/outbound.ts +2 -2
- package/src/rappi/pause.ts +98 -0
- package/src/server/index.ts +1 -0
- package/src/server/jobs.ts +100 -10
- package/dist/adapter-_iuSxvVD.d.ts +0 -319
- package/dist/chunk-24RQZPSV.js.map +0 -1
- package/dist/chunk-CRYFYDQ2.js.map +0 -1
- package/dist/chunk-D2X7TOR6.js.map +0 -1
package/dist/ifood/index.js
CHANGED
|
@@ -1,3 +1,6 @@
|
|
|
1
|
+
import {
|
|
2
|
+
stableUuid
|
|
3
|
+
} from "../chunk-M2V4RFMA.js";
|
|
1
4
|
import {
|
|
2
5
|
asString,
|
|
3
6
|
firstCents,
|
|
@@ -9,7 +12,7 @@ import {
|
|
|
9
12
|
import {
|
|
10
13
|
answered,
|
|
11
14
|
outboundResultOf
|
|
12
|
-
} from "../chunk-
|
|
15
|
+
} from "../chunk-SZRZTLVU.js";
|
|
13
16
|
import {
|
|
14
17
|
__name
|
|
15
18
|
} from "../chunk-7QVYU63E.js";
|
|
@@ -30,6 +33,242 @@ var IFOOD_REASON_CODES = [
|
|
|
30
33
|
"513"
|
|
31
34
|
];
|
|
32
35
|
|
|
36
|
+
// src/ifood/outbound.ts
|
|
37
|
+
var IFOOD_API = "https://merchant-api.ifood.com.br";
|
|
38
|
+
var TOKEN_URL = `${IFOOD_API}/authentication/v1.0/oauth/token`;
|
|
39
|
+
var orderUrl = /* @__PURE__ */ __name((externalOrderId, action) => `${IFOOD_API}/order/v1.0/orders/${encodeURIComponent(externalOrderId)}/${action}`, "orderUrl");
|
|
40
|
+
var IFOOD_PORTAL_URL = "https://portal.ifood.com.br/";
|
|
41
|
+
var TOKEN_MARGIN_MS = 6e4;
|
|
42
|
+
var ifoodTokenKey = /* @__PURE__ */ __name((ctx) => `IFOOD:${ctx.environment}:${ctx.credentials.clientId}`, "ifoodTokenKey");
|
|
43
|
+
function tokenOf(body) {
|
|
44
|
+
try {
|
|
45
|
+
const parsed = JSON.parse(body);
|
|
46
|
+
if (typeof parsed.accessToken !== "string" || parsed.accessToken === "") return null;
|
|
47
|
+
const expires = typeof parsed.expiresIn === "number" && parsed.expiresIn > 0 ? parsed.expiresIn : 3600;
|
|
48
|
+
return { token: parsed.accessToken, expiresInSeconds: expires };
|
|
49
|
+
} catch {
|
|
50
|
+
return null;
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
__name(tokenOf, "tokenOf");
|
|
54
|
+
async function bearer(ctx) {
|
|
55
|
+
const key = ifoodTokenKey(ctx);
|
|
56
|
+
const cached = ctx.tokens.get(key);
|
|
57
|
+
if (cached && cached.expiresAt > ctx.now.getTime() + TOKEN_MARGIN_MS) return { token: cached.token };
|
|
58
|
+
const response = await ctx.http({
|
|
59
|
+
method: "POST",
|
|
60
|
+
url: TOKEN_URL,
|
|
61
|
+
headers: { "Content-Type": "application/x-www-form-urlencoded" },
|
|
62
|
+
body: new URLSearchParams({
|
|
63
|
+
grantType: "client_credentials",
|
|
64
|
+
clientId: ctx.credentials.clientId,
|
|
65
|
+
clientSecret: ctx.credentials.clientSecret
|
|
66
|
+
}).toString()
|
|
67
|
+
});
|
|
68
|
+
if (response.status < 200 || response.status >= 300) return outboundResultOf(response);
|
|
69
|
+
const token = tokenOf(response.body);
|
|
70
|
+
if (!token) return { kind: "retry", reason: "iFood answered the token grant without a token" };
|
|
71
|
+
ctx.tokens.set(key, { token: token.token, expiresAt: ctx.now.getTime() + token.expiresInSeconds * 1e3 });
|
|
72
|
+
return { token: token.token };
|
|
73
|
+
}
|
|
74
|
+
__name(bearer, "bearer");
|
|
75
|
+
function postOrder(ctx, externalOrderId, action, body) {
|
|
76
|
+
return answered(async () => {
|
|
77
|
+
const auth = await bearer(ctx);
|
|
78
|
+
if (!("token" in auth)) return auth;
|
|
79
|
+
const response = await ctx.http({
|
|
80
|
+
method: "POST",
|
|
81
|
+
url: orderUrl(externalOrderId, action),
|
|
82
|
+
headers: {
|
|
83
|
+
Authorization: `Bearer ${auth.token}`,
|
|
84
|
+
...body === void 0 ? {} : { "Content-Type": "application/json" }
|
|
85
|
+
},
|
|
86
|
+
...body === void 0 ? {} : { body: JSON.stringify(body) }
|
|
87
|
+
});
|
|
88
|
+
if (response.status === 401) ctx.tokens.delete(ifoodTokenKey(ctx));
|
|
89
|
+
return response.status === 401 ? { kind: "retry", reason: "iFood refused the token" } : outboundResultOf(response);
|
|
90
|
+
});
|
|
91
|
+
}
|
|
92
|
+
__name(postOrder, "postOrder");
|
|
93
|
+
function ifoodConfirm(ctx, order) {
|
|
94
|
+
return postOrder(ctx, order.externalOrderId, "confirm");
|
|
95
|
+
}
|
|
96
|
+
__name(ifoodConfirm, "ifoodConfirm");
|
|
97
|
+
function ifoodRequestCancellation(ctx, order, input) {
|
|
98
|
+
return postOrder(ctx, order.externalOrderId, "requestCancellation", {
|
|
99
|
+
reason: input.text === "" ? input.reason.label : input.text,
|
|
100
|
+
cancellationCode: input.reason.code
|
|
101
|
+
});
|
|
102
|
+
}
|
|
103
|
+
__name(ifoodRequestCancellation, "ifoodRequestCancellation");
|
|
104
|
+
|
|
105
|
+
// src/ifood/catalogue.ts
|
|
106
|
+
var catalogUrl = /* @__PURE__ */ __name((merchantId, path) => `${IFOOD_API}/catalog/v2.0/merchants/${encodeURIComponent(merchantId)}/${path}`, "catalogUrl");
|
|
107
|
+
var ifoodItemId = /* @__PURE__ */ __name((ref) => stableUuid(`${ref}:item`), "ifoodItemId");
|
|
108
|
+
var ifoodProductId = /* @__PURE__ */ __name((ref) => stableUuid(`${ref}:product`), "ifoodProductId");
|
|
109
|
+
var priceOf = /* @__PURE__ */ __name((cents) => ({ value: Math.round(cents) / 100 }), "priceOf");
|
|
110
|
+
async function call(ctx, method, path, body) {
|
|
111
|
+
const auth = await bearer(ctx);
|
|
112
|
+
if (!("token" in auth)) return auth.kind === "accepted" ? { kind: "retry", reason: "unexpected token answer" } : auth;
|
|
113
|
+
const response = await ctx.http({
|
|
114
|
+
method,
|
|
115
|
+
url: catalogUrl(ctx.externalMerchantId, path),
|
|
116
|
+
headers: {
|
|
117
|
+
Authorization: `Bearer ${auth.token}`,
|
|
118
|
+
...body === void 0 ? {} : { "Content-Type": "application/json" }
|
|
119
|
+
},
|
|
120
|
+
...body === void 0 ? {} : { body: JSON.stringify(body) }
|
|
121
|
+
});
|
|
122
|
+
if (response.status !== 401) return response;
|
|
123
|
+
ctx.tokens.delete(ifoodTokenKey(ctx));
|
|
124
|
+
return { kind: "retry", reason: "iFood refused the token" };
|
|
125
|
+
}
|
|
126
|
+
__name(call, "call");
|
|
127
|
+
function failureOf(response) {
|
|
128
|
+
const result = outboundResultOf(response);
|
|
129
|
+
return result.kind === "accepted" ? { kind: "retry", reason: "unexpected answer" } : result;
|
|
130
|
+
}
|
|
131
|
+
__name(failureOf, "failureOf");
|
|
132
|
+
var ok = /* @__PURE__ */ __name((response) => response.status >= 200 && response.status < 300, "ok");
|
|
133
|
+
function parse(body) {
|
|
134
|
+
try {
|
|
135
|
+
return JSON.parse(body);
|
|
136
|
+
} catch {
|
|
137
|
+
return null;
|
|
138
|
+
}
|
|
139
|
+
}
|
|
140
|
+
__name(parse, "parse");
|
|
141
|
+
function categoryIdIn(body, name) {
|
|
142
|
+
const parsed = parse(body);
|
|
143
|
+
if (!Array.isArray(parsed)) return null;
|
|
144
|
+
const wanted = name.trim().toLowerCase();
|
|
145
|
+
const found = parsed.find(
|
|
146
|
+
(entry) => typeof entry === "object" && entry !== null && String(entry.name ?? "").trim().toLowerCase() === wanted
|
|
147
|
+
);
|
|
148
|
+
return typeof found?.id === "string" ? found.id : null;
|
|
149
|
+
}
|
|
150
|
+
__name(categoryIdIn, "categoryIdIn");
|
|
151
|
+
async function findCategory(ctx, name) {
|
|
152
|
+
const listed = await call(ctx, "GET", "categories");
|
|
153
|
+
if ("kind" in listed) return listed;
|
|
154
|
+
if (!ok(listed)) return failureOf(listed);
|
|
155
|
+
return { id: categoryIdIn(listed.body, name) };
|
|
156
|
+
}
|
|
157
|
+
__name(findCategory, "findCategory");
|
|
158
|
+
async function findOrCreateCategory(ctx, name) {
|
|
159
|
+
const found = await findCategory(ctx, name);
|
|
160
|
+
if ("kind" in found) return found;
|
|
161
|
+
if (found.id) return { id: found.id };
|
|
162
|
+
const created = await call(ctx, "POST", "categories", { name, status: "AVAILABLE", template: "DEFAULT" });
|
|
163
|
+
if ("kind" in created) return created;
|
|
164
|
+
if (!ok(created)) return failureOf(created);
|
|
165
|
+
const id = parse(created.body)?.id;
|
|
166
|
+
if (typeof id !== "string") return { kind: "retry", reason: "iFood created the category without an id" };
|
|
167
|
+
const again = await findCategory(ctx, name);
|
|
168
|
+
return "id" in again && again.id ? { id: again.id } : { id };
|
|
169
|
+
}
|
|
170
|
+
__name(findOrCreateCategory, "findOrCreateCategory");
|
|
171
|
+
var categoryId = /* @__PURE__ */ __name((ctx, name) => ctx.category(name, () => findOrCreateCategory(ctx, name)), "categoryId");
|
|
172
|
+
async function photoFor(ctx, item) {
|
|
173
|
+
const stored = item.storedImage;
|
|
174
|
+
if (item.imageUrl === null) return { image: null };
|
|
175
|
+
if (stored && stored.sourceUrl === item.imageUrl) return { image: stored };
|
|
176
|
+
const bytes = await ctx.loadImage(item.imageUrl);
|
|
177
|
+
return bytes ? upload(ctx, bytes, item.imageUrl) : { image: stored };
|
|
178
|
+
}
|
|
179
|
+
__name(photoFor, "photoFor");
|
|
180
|
+
async function upload(ctx, bytes, sourceUrl) {
|
|
181
|
+
const uploaded = await call(ctx, "POST", "image/upload", { image: `data:${bytes.contentType};base64,${bytes.base64}` });
|
|
182
|
+
if ("kind" in uploaded) return uploaded;
|
|
183
|
+
if (!ok(uploaded)) {
|
|
184
|
+
const failure = failureOf(uploaded);
|
|
185
|
+
if (failure.kind === "retry") return failure;
|
|
186
|
+
return { image: null, warning: uploaded.status === 413 ? "IMAGE_TOO_LARGE" : "IMAGE_REFUSED" };
|
|
187
|
+
}
|
|
188
|
+
const path = parse(uploaded.body)?.imagePath;
|
|
189
|
+
if (typeof path !== "string") return { kind: "retry", reason: "iFood answered the upload without an imagePath" };
|
|
190
|
+
return { image: { ref: path, sourceUrl } };
|
|
191
|
+
}
|
|
192
|
+
__name(upload, "upload");
|
|
193
|
+
function ifoodItemBody(item, category, image) {
|
|
194
|
+
const itemId = ifoodItemId(item.ref);
|
|
195
|
+
const productId = ifoodProductId(item.ref);
|
|
196
|
+
return {
|
|
197
|
+
item: {
|
|
198
|
+
id: itemId,
|
|
199
|
+
type: "DEFAULT",
|
|
200
|
+
categoryId: category,
|
|
201
|
+
status: "AVAILABLE",
|
|
202
|
+
price: priceOf(item.priceCents),
|
|
203
|
+
externalCode: item.productId,
|
|
204
|
+
// ESTIMATED: the guide's examples link item and product implicitly; the
|
|
205
|
+
// field naming the product is not shown on the pages read.
|
|
206
|
+
productId
|
|
207
|
+
},
|
|
208
|
+
products: [
|
|
209
|
+
{
|
|
210
|
+
id: productId,
|
|
211
|
+
name: item.name,
|
|
212
|
+
description: item.description ?? "",
|
|
213
|
+
externalCode: `${item.productId}-product`,
|
|
214
|
+
// ESTIMATED field name: the Errors page names `imagePath` as what the upload returns and the item carries.
|
|
215
|
+
...image === null ? {} : { imagePath: image }
|
|
216
|
+
}
|
|
217
|
+
],
|
|
218
|
+
optionGroups: [],
|
|
219
|
+
options: []
|
|
220
|
+
};
|
|
221
|
+
}
|
|
222
|
+
__name(ifoodItemBody, "ifoodItemBody");
|
|
223
|
+
async function categoryWentStale(ctx, put, name, used) {
|
|
224
|
+
if (put.status !== 400 && put.status !== 404) return false;
|
|
225
|
+
const found = await findCategory(ctx, name);
|
|
226
|
+
return !("kind" in found) && found.id !== used;
|
|
227
|
+
}
|
|
228
|
+
__name(categoryWentStale, "categoryWentStale");
|
|
229
|
+
async function putItem(ctx, item, image) {
|
|
230
|
+
const category = await categoryId(ctx, item.categoryName);
|
|
231
|
+
if ("kind" in category) return category;
|
|
232
|
+
const put = await call(ctx, "PUT", "items", ifoodItemBody(item, category.id, image));
|
|
233
|
+
if ("kind" in put || ok(put) || !await categoryWentStale(ctx, put, item.categoryName, category.id)) return put;
|
|
234
|
+
await ctx.forgetCategory(item.categoryName);
|
|
235
|
+
const again = await categoryId(ctx, item.categoryName);
|
|
236
|
+
if ("kind" in again) return again;
|
|
237
|
+
return call(ctx, "PUT", "items", ifoodItemBody(item, again.id, image));
|
|
238
|
+
}
|
|
239
|
+
__name(putItem, "putItem");
|
|
240
|
+
async function publish(ctx, item) {
|
|
241
|
+
const photo = await photoFor(ctx, item);
|
|
242
|
+
if ("kind" in photo) return photo;
|
|
243
|
+
const put = await putItem(ctx, item, photo.image?.ref ?? null);
|
|
244
|
+
if ("kind" in put) return put;
|
|
245
|
+
if (!ok(put)) return failureOf(put);
|
|
246
|
+
const published = { kind: "published", externalId: ifoodItemId(item.ref), sku: item.productId, image: photo.image };
|
|
247
|
+
return photo.warning ? { ...published, warning: photo.warning } : published;
|
|
248
|
+
}
|
|
249
|
+
__name(publish, "publish");
|
|
250
|
+
async function withdraw(ctx, item) {
|
|
251
|
+
const itemId = item.externalId ?? ifoodItemId(item.ref);
|
|
252
|
+
const patched = await call(ctx, "PATCH", "items/status", { itemId, status: "UNAVAILABLE" });
|
|
253
|
+
if ("kind" in patched) return patched;
|
|
254
|
+
if (patched.status === 404) return { kind: "accepted" };
|
|
255
|
+
return outboundResultOf(patched);
|
|
256
|
+
}
|
|
257
|
+
__name(withdraw, "withdraw");
|
|
258
|
+
async function publishedOrRetry(run) {
|
|
259
|
+
try {
|
|
260
|
+
return await run();
|
|
261
|
+
} catch (error) {
|
|
262
|
+
return { kind: "retry", reason: error instanceof Error ? error.message : String(error) };
|
|
263
|
+
}
|
|
264
|
+
}
|
|
265
|
+
__name(publishedOrRetry, "publishedOrRetry");
|
|
266
|
+
var IFOOD_CATALOGUE = {
|
|
267
|
+
mode: "push",
|
|
268
|
+
publishItem: /* @__PURE__ */ __name((ctx, item) => publishedOrRetry(() => publish(ctx, item)), "publishItem"),
|
|
269
|
+
withdrawItem: /* @__PURE__ */ __name((ctx, item) => answered(() => withdraw(ctx, item)), "withdrawItem")
|
|
270
|
+
};
|
|
271
|
+
|
|
33
272
|
// src/ifood/normalize.ts
|
|
34
273
|
var ORDER_PLACED = /* @__PURE__ */ new Set(["PLC", "PLACED", "ORDER_PLACED"]);
|
|
35
274
|
var ORDER_CANCELLED = /* @__PURE__ */ new Set(["CAN", "CANCELLED", "ORDER_CANCELLED"]);
|
|
@@ -144,73 +383,162 @@ function normalizeIfoodEvent(payload, options) {
|
|
|
144
383
|
}
|
|
145
384
|
__name(normalizeIfoodEvent, "normalizeIfoodEvent");
|
|
146
385
|
|
|
147
|
-
// src/ifood/
|
|
148
|
-
var
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
386
|
+
// src/ifood/pause.ts
|
|
387
|
+
var interruptionsUrl = /* @__PURE__ */ __name((merchantId) => `${IFOOD_API}/merchant/v1.0/merchants/${encodeURIComponent(merchantId)}/interruptions`, "interruptionsUrl");
|
|
388
|
+
async function call2(ctx, request) {
|
|
389
|
+
const auth = await bearer(ctx);
|
|
390
|
+
if (!("token" in auth)) return auth.kind === "accepted" ? { kind: "retry", reason: "unexpected token answer" } : auth;
|
|
391
|
+
const response = await ctx.http({
|
|
392
|
+
method: request.method,
|
|
393
|
+
url: request.url,
|
|
394
|
+
headers: {
|
|
395
|
+
Authorization: `Bearer ${auth.token}`,
|
|
396
|
+
...request.json === void 0 ? {} : { "Content-Type": "application/json" }
|
|
397
|
+
},
|
|
398
|
+
...request.json === void 0 ? {} : { body: JSON.stringify(request.json) }
|
|
399
|
+
});
|
|
400
|
+
if (response.status !== 401) return response;
|
|
401
|
+
ctx.tokens.delete(ifoodTokenKey(ctx));
|
|
402
|
+
return { kind: "retry", reason: "iFood refused the token" };
|
|
403
|
+
}
|
|
404
|
+
__name(call2, "call");
|
|
405
|
+
var settled = /* @__PURE__ */ __name((value) => "kind" in value, "settled");
|
|
406
|
+
function notOk(response) {
|
|
407
|
+
const result = outboundResultOf(response);
|
|
408
|
+
return result.kind === "accepted" ? null : result;
|
|
409
|
+
}
|
|
410
|
+
__name(notOk, "notOk");
|
|
411
|
+
async function listInterruptions(ctx, merchantId) {
|
|
412
|
+
const response = await call2(ctx, { method: "GET", url: interruptionsUrl(merchantId) });
|
|
413
|
+
if (settled(response)) return response;
|
|
414
|
+
const failure = notOk(response);
|
|
415
|
+
if (failure) return failure;
|
|
154
416
|
try {
|
|
155
|
-
const parsed = JSON.parse(body);
|
|
156
|
-
if (
|
|
157
|
-
|
|
158
|
-
|
|
417
|
+
const parsed = JSON.parse(response.body === "" ? "[]" : response.body);
|
|
418
|
+
if (!Array.isArray(parsed)) return [];
|
|
419
|
+
return parsed.flatMap((entry) => {
|
|
420
|
+
const value = entry ?? {};
|
|
421
|
+
if (typeof value.id !== "string") return [];
|
|
422
|
+
return [
|
|
423
|
+
{
|
|
424
|
+
id: value.id,
|
|
425
|
+
end: typeof value.end === "string" ? value.end : null,
|
|
426
|
+
description: typeof value.description === "string" ? value.description : null
|
|
427
|
+
}
|
|
428
|
+
];
|
|
429
|
+
});
|
|
159
430
|
} catch {
|
|
160
|
-
return
|
|
431
|
+
return { kind: "retry", reason: "iFood answered the interruption list with something that is not JSON" };
|
|
161
432
|
}
|
|
162
433
|
}
|
|
163
|
-
__name(
|
|
164
|
-
|
|
165
|
-
const
|
|
166
|
-
|
|
167
|
-
if (
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
434
|
+
__name(listInterruptions, "listInterruptions");
|
|
435
|
+
function sameEnd(end, until, timeZone) {
|
|
436
|
+
const sent = ifoodLocalTime(until, timeZone);
|
|
437
|
+
if (end === sent) return true;
|
|
438
|
+
if (/(?:[zZ]|[+-]\d{2}:?\d{2})$/u.test(end)) {
|
|
439
|
+
const parsed = Date.parse(end);
|
|
440
|
+
return !Number.isNaN(parsed) && Math.floor(parsed / 1e3) === Math.floor(until.getTime() / 1e3);
|
|
441
|
+
}
|
|
442
|
+
return end.slice(0, 19) === sent.slice(0, 19);
|
|
443
|
+
}
|
|
444
|
+
__name(sameEnd, "sameEnd");
|
|
445
|
+
function ours(list, key, sent, keep = []) {
|
|
446
|
+
const candidates = list.filter((entry) => !keep.includes(entry.id));
|
|
447
|
+
const byKey = candidates.find((entry) => entry.id === key);
|
|
448
|
+
if (byKey || !sent) return byKey;
|
|
449
|
+
return candidates.find(
|
|
450
|
+
(entry) => entry.description === sent.reason && entry.end !== null && sameEnd(entry.end, sent.until, sent.timeZone)
|
|
451
|
+
);
|
|
452
|
+
}
|
|
453
|
+
__name(ours, "ours");
|
|
454
|
+
function wallClock(date, timeZone) {
|
|
455
|
+
const format = new Intl.DateTimeFormat("en-US", {
|
|
456
|
+
timeZone,
|
|
457
|
+
hourCycle: "h23",
|
|
458
|
+
year: "numeric",
|
|
459
|
+
month: "2-digit",
|
|
460
|
+
day: "2-digit",
|
|
461
|
+
hour: "2-digit",
|
|
462
|
+
minute: "2-digit",
|
|
463
|
+
second: "2-digit"
|
|
177
464
|
});
|
|
178
|
-
|
|
179
|
-
const
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
return
|
|
465
|
+
const clock = { year: 0, month: 1, day: 1, hour: 0, minute: 0, second: 0 };
|
|
466
|
+
for (const part of format.formatToParts(date)) {
|
|
467
|
+
if (part.type in clock) clock[part.type] = Number(part.value);
|
|
468
|
+
}
|
|
469
|
+
return clock;
|
|
183
470
|
}
|
|
184
|
-
__name(
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
471
|
+
__name(wallClock, "wallClock");
|
|
472
|
+
var pad = /* @__PURE__ */ __name((value, width = 2) => String(value).padStart(width, "0"), "pad");
|
|
473
|
+
function ifoodLocalTime(date, timeZone) {
|
|
474
|
+
const local = wallClock(date, timeZone);
|
|
475
|
+
const asUtc = Date.UTC(local.year, local.month - 1, local.day, local.hour, local.minute, local.second);
|
|
476
|
+
const offset = Math.round((asUtc - Math.floor(date.getTime() / 1e3) * 1e3) / 6e4);
|
|
477
|
+
const abs = Math.abs(offset);
|
|
478
|
+
return `${pad(local.year, 4)}-${pad(local.month)}-${pad(local.day)}T${pad(local.hour)}:${pad(local.minute)}:${pad(local.second)}${offset < 0 ? "-" : "+"}${pad(Math.floor(abs / 60))}:${pad(abs % 60)}`;
|
|
479
|
+
}
|
|
480
|
+
__name(ifoodLocalTime, "ifoodLocalTime");
|
|
481
|
+
function createdId(body, sent) {
|
|
482
|
+
try {
|
|
483
|
+
const id = JSON.parse(body).id;
|
|
484
|
+
return typeof id === "string" && id !== "" ? id : sent;
|
|
485
|
+
} catch {
|
|
486
|
+
return sent;
|
|
487
|
+
}
|
|
488
|
+
}
|
|
489
|
+
__name(createdId, "createdId");
|
|
490
|
+
function ifoodPause(ctx, request) {
|
|
491
|
+
return answeredPause(async () => {
|
|
492
|
+
const list = await listInterruptions(ctx, request.merchantId);
|
|
493
|
+
if (!Array.isArray(list)) return list;
|
|
494
|
+
const landed = ours(list, request.key, { until: request.until, reason: request.reason, timeZone: request.timeZone }, request.keep);
|
|
495
|
+
if (landed) return { kind: "paused", externalId: landed.id, until: request.until };
|
|
496
|
+
const response = await call2(ctx, {
|
|
190
497
|
method: "POST",
|
|
191
|
-
url:
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
498
|
+
url: interruptionsUrl(request.merchantId),
|
|
499
|
+
json: {
|
|
500
|
+
id: request.key,
|
|
501
|
+
description: request.reason,
|
|
502
|
+
start: ifoodLocalTime(ctx.now, request.timeZone),
|
|
503
|
+
end: ifoodLocalTime(request.until, request.timeZone)
|
|
504
|
+
}
|
|
197
505
|
});
|
|
198
|
-
if (response
|
|
199
|
-
|
|
506
|
+
if (settled(response)) return response;
|
|
507
|
+
const failure = notOk(response);
|
|
508
|
+
if (failure) return failure;
|
|
509
|
+
return { kind: "paused", externalId: createdId(response.body, request.key), until: request.until };
|
|
200
510
|
});
|
|
201
511
|
}
|
|
202
|
-
__name(
|
|
203
|
-
function
|
|
204
|
-
return
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
512
|
+
__name(ifoodPause, "ifoodPause");
|
|
513
|
+
function ifoodResume(ctx, ref) {
|
|
514
|
+
return answered(async () => {
|
|
515
|
+
let id = ref.externalId;
|
|
516
|
+
if (id === null && ref.key !== null) {
|
|
517
|
+
const list = await listInterruptions(ctx, ref.merchantId);
|
|
518
|
+
if (!Array.isArray(list)) return list;
|
|
519
|
+
id = ours(list, ref.key, ref.sent, ref.keep)?.id ?? null;
|
|
520
|
+
}
|
|
521
|
+
if (id === null) return { kind: "accepted" };
|
|
522
|
+
const response = await call2(ctx, { method: "DELETE", url: `${interruptionsUrl(ref.merchantId)}/${encodeURIComponent(id)}` });
|
|
523
|
+
if (settled(response)) return response;
|
|
524
|
+
if (response.status === 404) return { kind: "accepted" };
|
|
525
|
+
return outboundResultOf(response);
|
|
211
526
|
});
|
|
212
527
|
}
|
|
213
|
-
__name(
|
|
528
|
+
__name(ifoodResume, "ifoodResume");
|
|
529
|
+
async function answeredPause(run) {
|
|
530
|
+
try {
|
|
531
|
+
return await run();
|
|
532
|
+
} catch (error) {
|
|
533
|
+
return { kind: "retry", reason: error instanceof Error ? error.message : String(error) };
|
|
534
|
+
}
|
|
535
|
+
}
|
|
536
|
+
__name(answeredPause, "answeredPause");
|
|
537
|
+
var IFOOD_AVAILABILITY = {
|
|
538
|
+
kind: "interruption",
|
|
539
|
+
pause: ifoodPause,
|
|
540
|
+
resume: ifoodResume
|
|
541
|
+
};
|
|
214
542
|
|
|
215
543
|
// src/ifood/index.ts
|
|
216
544
|
var IFOOD_PROVIDER = "IFOOD";
|
|
@@ -244,8 +572,12 @@ function createIfoodAdapter(config) {
|
|
|
244
572
|
readSignature: /* @__PURE__ */ __name((header) => header("x-ifood-signature") ?? header("x-signature"), "readSignature"),
|
|
245
573
|
verifySignature: verifyIfoodSignature,
|
|
246
574
|
cancellationReasons: /* @__PURE__ */ __name(() => reasons, "cancellationReasons"),
|
|
575
|
+
// The Catalog API v2.0: each item is written and paused through the API (FUT-4768).
|
|
576
|
+
catalogue: IFOOD_CATALOGUE,
|
|
247
577
|
confirm: /* @__PURE__ */ __name((ctx, order) => ifoodConfirm(ctx, order), "confirm"),
|
|
248
578
|
requestCancellation: ifoodRequestCancellation,
|
|
579
|
+
// The Merchant API's interruptions: an id and an end per closure (FUT-4851).
|
|
580
|
+
availability: IFOOD_AVAILABILITY,
|
|
249
581
|
// No per-order deep link is known, so the portal, where a live order is
|
|
250
582
|
// on the first screen.
|
|
251
583
|
appLink: /* @__PURE__ */ __name(() => IFOOD_PORTAL_URL, "appLink")
|
|
@@ -253,10 +585,18 @@ function createIfoodAdapter(config) {
|
|
|
253
585
|
}
|
|
254
586
|
__name(createIfoodAdapter, "createIfoodAdapter");
|
|
255
587
|
export {
|
|
588
|
+
IFOOD_AVAILABILITY,
|
|
256
589
|
IFOOD_CANCELLATION_ANSWER_WINDOW_MS,
|
|
590
|
+
IFOOD_CATALOGUE,
|
|
257
591
|
IFOOD_PROVIDER,
|
|
258
592
|
IFOOD_REASON_CODES,
|
|
259
593
|
createIfoodAdapter,
|
|
594
|
+
ifoodItemBody,
|
|
595
|
+
ifoodItemId,
|
|
596
|
+
ifoodLocalTime,
|
|
597
|
+
ifoodPause,
|
|
598
|
+
ifoodProductId,
|
|
599
|
+
ifoodResume,
|
|
260
600
|
normalizeIfoodEvent,
|
|
261
601
|
verifyIfoodSignature
|
|
262
602
|
};
|