@12-apps/marketplace 1.17.0 → 1.18.1
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 +70 -0
- package/README.md +7 -7
- package/dist/99food/index.d.ts +2 -2
- package/dist/99food/index.js +2 -2
- package/dist/{adapter-CxzF-qGx.d.ts → adapter-ZpAWKoCk.d.ts} +116 -2
- package/dist/{chunk-7IYUO2KC.js → chunk-3B5EULS6.js} +2 -2
- package/dist/{chunk-EABVLQKK.js → chunk-BLHZ3MRB.js} +2 -2
- package/dist/{chunk-UZO2NILN.js → chunk-MDEIRGNL.js} +1067 -877
- package/dist/chunk-MDEIRGNL.js.map +1 -0
- package/dist/{chunk-KKFMDV5K.js → chunk-Q5RMVNU5.js} +1 -1
- package/dist/{chunk-KKFMDV5K.js.map → chunk-Q5RMVNU5.js.map} +1 -1
- package/dist/{chunk-CF5BSXE5.js → chunk-ZT6TZTLD.js} +1 -1
- package/dist/chunk-ZT6TZTLD.js.map +1 -0
- package/dist/{client-C-4QvVq3.d.ts → client-DpEq0yXb.d.ts} +1 -1
- package/dist/{engine-Y6Rpf5p7.d.ts → engine-DRRhAjZK.d.ts} +171 -143
- package/dist/hono/index.d.ts +2 -2
- package/dist/hono/index.js +3 -3
- package/dist/ifood/index.d.ts +7 -2
- package/dist/ifood/index.js +189 -107
- package/dist/ifood/index.js.map +1 -1
- package/dist/index.d.ts +3 -3
- package/dist/index.js +76 -5
- package/dist/index.js.map +1 -1
- package/dist/keeta/index.d.ts +2 -2
- package/dist/keeta/index.js +2 -2
- package/dist/manifest/server.d.ts +2 -2
- package/dist/manifest/server.js +3 -3
- package/dist/rappi/index.d.ts +1 -1
- package/dist/rappi/index.js +1 -1
- package/dist/server/index.d.ts +2 -2
- package/dist/server/index.js +3 -3
- package/package.json +1 -1
- package/prisma/marketplace.prisma +1 -1
- package/prisma/migrations/20261203200000_marketplace_status_logistics_steps/migration.sql +12 -0
- package/src/core/adapter.ts +2 -1
- package/src/core/delivery-code.ts +119 -0
- package/src/core/engine.ts +10 -2
- package/src/core/logistics.ts +388 -0
- package/src/core/outbound-queue.ts +6 -1
- package/src/core/outbound-sweep.ts +10 -5
- package/src/core/outbound.ts +4 -5
- package/src/core/pipeline.ts +6 -1
- package/src/core/ports.ts +5 -2
- package/src/core/status-job.ts +3 -0
- package/src/core/status-store.ts +2 -1
- package/src/core/types.ts +2 -0
- package/src/ifood/catalogs.ts +34 -0
- package/src/ifood/catalogue-read.ts +30 -10
- package/src/ifood/catalogue.ts +34 -9
- package/src/ifood/index.ts +3 -0
- package/src/ifood/logistics.ts +38 -0
- package/src/ifood/normalize.ts +11 -1
- package/src/index.ts +10 -0
- package/dist/chunk-CF5BSXE5.js.map +0 -1
- package/dist/chunk-UZO2NILN.js.map +0 -1
- /package/dist/{chunk-7IYUO2KC.js.map → chunk-3B5EULS6.js.map} +0 -0
- /package/dist/{chunk-EABVLQKK.js.map → chunk-BLHZ3MRB.js.map} +0 -0
package/dist/ifood/index.js
CHANGED
|
@@ -3,7 +3,7 @@ import {
|
|
|
3
3
|
} from "../chunk-WN2EK4KX.js";
|
|
4
4
|
import {
|
|
5
5
|
normalizedLineTotalCents
|
|
6
|
-
} from "../chunk-
|
|
6
|
+
} from "../chunk-Q5RMVNU5.js";
|
|
7
7
|
import {
|
|
8
8
|
stableUuid
|
|
9
9
|
} from "../chunk-M2V4RFMA.js";
|
|
@@ -21,7 +21,7 @@ import {
|
|
|
21
21
|
answered,
|
|
22
22
|
outboundResultOf,
|
|
23
23
|
tokenGrantResultOf
|
|
24
|
-
} from "../chunk-
|
|
24
|
+
} from "../chunk-ZT6TZTLD.js";
|
|
25
25
|
import {
|
|
26
26
|
__name
|
|
27
27
|
} from "../chunk-7QVYU63E.js";
|
|
@@ -42,6 +42,25 @@ var IFOOD_REASON_CODES = [
|
|
|
42
42
|
"513"
|
|
43
43
|
];
|
|
44
44
|
|
|
45
|
+
// src/ifood/catalogs.ts
|
|
46
|
+
function defaultCatalogIdIn(body) {
|
|
47
|
+
let parsed2;
|
|
48
|
+
try {
|
|
49
|
+
parsed2 = JSON.parse(body);
|
|
50
|
+
} catch {
|
|
51
|
+
return null;
|
|
52
|
+
}
|
|
53
|
+
if (!Array.isArray(parsed2)) return null;
|
|
54
|
+
for (const entry of parsed2) {
|
|
55
|
+
if (typeof entry !== "object" || entry === null) continue;
|
|
56
|
+
const { catalogId, context } = entry;
|
|
57
|
+
if (typeof catalogId === "string" && catalogId !== "" && Array.isArray(context) && context.includes("DEFAULT")) return catalogId;
|
|
58
|
+
}
|
|
59
|
+
return null;
|
|
60
|
+
}
|
|
61
|
+
__name(defaultCatalogIdIn, "defaultCatalogIdIn");
|
|
62
|
+
var categoriesPath = /* @__PURE__ */ __name((catalogId) => `catalogs/${encodeURIComponent(catalogId)}/categories`, "categoriesPath");
|
|
63
|
+
|
|
45
64
|
// src/ifood/outbound.ts
|
|
46
65
|
var IFOOD_API = "https://merchant-api.ifood.com.br";
|
|
47
66
|
var TOKEN_URL = `${IFOOD_API}/authentication/v1.0/oauth/token`;
|
|
@@ -240,23 +259,38 @@ function categoryIdIn(body, name) {
|
|
|
240
259
|
return typeof found?.id === "string" ? found.id : null;
|
|
241
260
|
}
|
|
242
261
|
__name(categoryIdIn, "categoryIdIn");
|
|
243
|
-
async function
|
|
244
|
-
const listed = await call(ctx, "GET", "
|
|
262
|
+
async function catalogOf(ctx) {
|
|
263
|
+
const listed = await call(ctx, "GET", "catalogs");
|
|
264
|
+
if ("kind" in listed) return listed;
|
|
265
|
+
if (!ok(listed)) return failureOf(listed);
|
|
266
|
+
const catalogId = defaultCatalogIdIn(listed.body);
|
|
267
|
+
return catalogId === null ? { kind: "refused", status: listed.status, message: `no DEFAULT catalog: ${listed.body.slice(0, 200)}` } : { catalogId };
|
|
268
|
+
}
|
|
269
|
+
__name(catalogOf, "catalogOf");
|
|
270
|
+
async function findCategoryIn(ctx, catalogId, name) {
|
|
271
|
+
const listed = await call(ctx, "GET", categoriesPath(catalogId));
|
|
245
272
|
if ("kind" in listed) return listed;
|
|
246
273
|
if (!ok(listed)) return failureOf(listed);
|
|
247
274
|
return { id: categoryIdIn(listed.body, name) };
|
|
248
275
|
}
|
|
276
|
+
__name(findCategoryIn, "findCategoryIn");
|
|
277
|
+
async function findCategory(ctx, name) {
|
|
278
|
+
const catalog = await catalogOf(ctx);
|
|
279
|
+
return "kind" in catalog ? catalog : findCategoryIn(ctx, catalog.catalogId, name);
|
|
280
|
+
}
|
|
249
281
|
__name(findCategory, "findCategory");
|
|
250
282
|
async function findOrCreateCategory(ctx, name) {
|
|
251
|
-
const
|
|
283
|
+
const catalog = await catalogOf(ctx);
|
|
284
|
+
if ("kind" in catalog) return catalog;
|
|
285
|
+
const found = await findCategoryIn(ctx, catalog.catalogId, name);
|
|
252
286
|
if ("kind" in found) return found;
|
|
253
287
|
if (found.id) return { id: found.id };
|
|
254
|
-
const created = await call(ctx, "POST",
|
|
288
|
+
const created = await call(ctx, "POST", categoriesPath(catalog.catalogId), { name, status: "AVAILABLE", template: "DEFAULT" });
|
|
255
289
|
if ("kind" in created) return created;
|
|
256
290
|
if (!ok(created)) return failureOf(created);
|
|
257
291
|
const id = parse(created.body)?.id;
|
|
258
292
|
if (typeof id !== "string") return { kind: "retry", reason: "iFood created the category without an id" };
|
|
259
|
-
const again = await
|
|
293
|
+
const again = await findCategoryIn(ctx, catalog.catalogId, name);
|
|
260
294
|
return "id" in again && again.id ? { id: again.id } : { id };
|
|
261
295
|
}
|
|
262
296
|
__name(findOrCreateCategory, "findOrCreateCategory");
|
|
@@ -524,7 +558,7 @@ __name(tokenFailure, "tokenFailure");
|
|
|
524
558
|
function failureOf2(response) {
|
|
525
559
|
const result = outboundResultOf(response);
|
|
526
560
|
if (result.kind !== "refused") return { kind: "retry", reason: result.kind === "retry" ? result.reason : "unexpected answer" };
|
|
527
|
-
const code = response.status === 403
|
|
561
|
+
const code = response.status === 403 ? "NOT_AUTHORIZED" : "REFUSED";
|
|
528
562
|
return { kind: "refused", code, status: result.status, message: result.message };
|
|
529
563
|
}
|
|
530
564
|
__name(failureOf2, "failureOf");
|
|
@@ -602,8 +636,18 @@ function itemsOf(rawBody, categoryId2, notices) {
|
|
|
602
636
|
return items;
|
|
603
637
|
}
|
|
604
638
|
__name(itemsOf, "itemsOf");
|
|
639
|
+
async function catalogOf2(ctx) {
|
|
640
|
+
const listed = await get(ctx, "catalogs");
|
|
641
|
+
if ("kind" in listed) return listed;
|
|
642
|
+
if (!ok2(listed)) return failureOf2(listed);
|
|
643
|
+
const catalogId = defaultCatalogIdIn(listed.body);
|
|
644
|
+
return catalogId === null ? unexpected("catalog list (no DEFAULT catalog)", listed.body) : { catalogId };
|
|
645
|
+
}
|
|
646
|
+
__name(catalogOf2, "catalogOf");
|
|
605
647
|
async function read(ctx) {
|
|
606
|
-
const
|
|
648
|
+
const catalog = await catalogOf2(ctx);
|
|
649
|
+
if ("kind" in catalog) return catalog;
|
|
650
|
+
const listed = await get(ctx, categoriesPath(catalog.catalogId));
|
|
607
651
|
if ("kind" in listed) return listed;
|
|
608
652
|
if (!ok2(listed)) return failureOf2(listed);
|
|
609
653
|
const parsed2 = parse2(listed.body);
|
|
@@ -928,6 +972,131 @@ async function answerIfoodDispute(ctx, target, answer) {
|
|
|
928
972
|
__name(answerIfoodDispute, "answerIfoodDispute");
|
|
929
973
|
var IFOOD_DISPUTES = { answer: answerIfoodDispute };
|
|
930
974
|
|
|
975
|
+
// src/ifood/logistics.ts
|
|
976
|
+
var logisticsUrl = /* @__PURE__ */ __name((externalOrderId, action) => `${IFOOD_API}/logistics/v1.0/orders/${encodeURIComponent(externalOrderId)}/${action}`, "logisticsUrl");
|
|
977
|
+
var IFOOD_LOGISTICS_VEHICLES = [
|
|
978
|
+
"BICYCLE",
|
|
979
|
+
"ONFOOT",
|
|
980
|
+
"PATINETE",
|
|
981
|
+
"EBIKE",
|
|
982
|
+
"SUPERBIKE",
|
|
983
|
+
"CAR",
|
|
984
|
+
"MOTORCYCLE",
|
|
985
|
+
"MOTORBIKE"
|
|
986
|
+
];
|
|
987
|
+
var IFOOD_LOGISTICS_STEPS = ["GOING_TO_ORIGIN", "ARRIVED_AT_ORIGIN", "DISPATCHED", "ARRIVED_AT_DESTINATION"];
|
|
988
|
+
var STEP_ACTION = {
|
|
989
|
+
GOING_TO_ORIGIN: "goingToOrigin",
|
|
990
|
+
ARRIVED_AT_ORIGIN: "arrivedAtOrigin",
|
|
991
|
+
DISPATCHED: "dispatch",
|
|
992
|
+
ARRIVED_AT_DESTINATION: "arrivedAtDestination"
|
|
993
|
+
};
|
|
994
|
+
function ifoodLogisticsStepsTo(lastSent, target) {
|
|
995
|
+
const from = lastSent === null ? 0 : IFOOD_LOGISTICS_STEPS.indexOf(lastSent) + 1;
|
|
996
|
+
const to = IFOOD_LOGISTICS_STEPS.indexOf(target);
|
|
997
|
+
return to < from ? [] : IFOOD_LOGISTICS_STEPS.slice(from, to + 1);
|
|
998
|
+
}
|
|
999
|
+
__name(ifoodLogisticsStepsTo, "ifoodLogisticsStepsTo");
|
|
1000
|
+
function logisticsApp(ctx) {
|
|
1001
|
+
return { http: ctx.http, credentials: ctx.credentials, environment: ctx.environment, tokens: ctx.tokens, now: ctx.now };
|
|
1002
|
+
}
|
|
1003
|
+
__name(logisticsApp, "logisticsApp");
|
|
1004
|
+
function redacted(result, status) {
|
|
1005
|
+
if (result.kind === "refused") return { ...result, message: `iFood Logistics answered ${status}` };
|
|
1006
|
+
if (result.kind === "retry") return { kind: "retry", reason: `iFood Logistics answered ${status}` };
|
|
1007
|
+
return result;
|
|
1008
|
+
}
|
|
1009
|
+
__name(redacted, "redacted");
|
|
1010
|
+
function post(ctx, url, body) {
|
|
1011
|
+
let answer = "";
|
|
1012
|
+
return answered(async () => {
|
|
1013
|
+
const response = await withIfoodToken(
|
|
1014
|
+
logisticsApp(ctx),
|
|
1015
|
+
(token) => ctx.http({
|
|
1016
|
+
method: "POST",
|
|
1017
|
+
url,
|
|
1018
|
+
headers: { Authorization: `Bearer ${token}`, ...body === void 0 ? {} : { "Content-Type": "application/json" } },
|
|
1019
|
+
...body === void 0 ? {} : { body: JSON.stringify(body) }
|
|
1020
|
+
})
|
|
1021
|
+
);
|
|
1022
|
+
if (response === null) return { kind: "retry", reason: "iFood refused the token" };
|
|
1023
|
+
if ("kind" in response) return response;
|
|
1024
|
+
answer = response.body;
|
|
1025
|
+
return redacted(outboundResultOf(response), response.status);
|
|
1026
|
+
}).then((result) => ({ result, body: answer }));
|
|
1027
|
+
}
|
|
1028
|
+
__name(post, "post");
|
|
1029
|
+
async function ifoodLogisticsAssignDriver(ctx, externalOrderId, driver) {
|
|
1030
|
+
const name = driver.name.trim();
|
|
1031
|
+
const phone = driver.phone.replace(/\D/g, "");
|
|
1032
|
+
if (name === "") return { kind: "invalid", reason: "DRIVER_NAME" };
|
|
1033
|
+
if (phone.length < 10 || phone.length > 13) return { kind: "invalid", reason: "DRIVER_PHONE" };
|
|
1034
|
+
if (!IFOOD_LOGISTICS_VEHICLES.includes(driver.vehicle)) return { kind: "invalid", reason: "DRIVER_VEHICLE" };
|
|
1035
|
+
const { result } = await post(ctx, logisticsUrl(externalOrderId, "assignDriver"), {
|
|
1036
|
+
workerName: name,
|
|
1037
|
+
workerPhone: phone,
|
|
1038
|
+
workerVehicleType: driver.vehicle
|
|
1039
|
+
});
|
|
1040
|
+
return result;
|
|
1041
|
+
}
|
|
1042
|
+
__name(ifoodLogisticsAssignDriver, "ifoodLogisticsAssignDriver");
|
|
1043
|
+
async function ifoodLogisticsStep(ctx, externalOrderId, step) {
|
|
1044
|
+
const { result } = await post(ctx, logisticsUrl(externalOrderId, STEP_ACTION[step]));
|
|
1045
|
+
return result;
|
|
1046
|
+
}
|
|
1047
|
+
__name(ifoodLogisticsStep, "ifoodLogisticsStep");
|
|
1048
|
+
function successOf(body) {
|
|
1049
|
+
try {
|
|
1050
|
+
const parsed2 = JSON.parse(body);
|
|
1051
|
+
return typeof parsed2?.success === "boolean" ? parsed2.success : null;
|
|
1052
|
+
} catch {
|
|
1053
|
+
return null;
|
|
1054
|
+
}
|
|
1055
|
+
}
|
|
1056
|
+
__name(successOf, "successOf");
|
|
1057
|
+
async function ifoodLogisticsVerifyDeliveryCode(ctx, externalOrderId, code) {
|
|
1058
|
+
const typed = code.replace(/\s/g, "");
|
|
1059
|
+
if (!/^[0-9A-Za-z]{1,12}$/.test(typed)) return { kind: "invalid", reason: "CODE" };
|
|
1060
|
+
const { result, body } = await post(ctx, logisticsUrl(externalOrderId, "verifyDeliveryCode"), { code: typed });
|
|
1061
|
+
if (result.kind === "accepted") {
|
|
1062
|
+
const success = successOf(body);
|
|
1063
|
+
if (success === null) return { kind: "retry", reason: "iFood answered the delivery code without `success`" };
|
|
1064
|
+
return success ? { kind: "verified" } : { kind: "wrong-code" };
|
|
1065
|
+
}
|
|
1066
|
+
if (result.kind === "refused" && result.status === 422) return { kind: "wrong-code" };
|
|
1067
|
+
if (result.kind === "refused" && result.status === 412) return { kind: "not-requested" };
|
|
1068
|
+
return result;
|
|
1069
|
+
}
|
|
1070
|
+
__name(ifoodLogisticsVerifyDeliveryCode, "ifoodLogisticsVerifyDeliveryCode");
|
|
1071
|
+
var VEHICLE = {
|
|
1072
|
+
MOTORCYCLE: "MOTORCYCLE",
|
|
1073
|
+
CAR: "CAR",
|
|
1074
|
+
BICYCLE: "BICYCLE",
|
|
1075
|
+
ON_FOOT: "ONFOOT",
|
|
1076
|
+
SCOOTER: "PATINETE"
|
|
1077
|
+
};
|
|
1078
|
+
var IFOOD_LOGISTICS = {
|
|
1079
|
+
assignDriver: /* @__PURE__ */ __name((ctx, order, driver) => ifoodLogisticsAssignDriver(ctx, order.externalOrderId, { name: driver.name, phone: driver.phone, vehicle: VEHICLE[driver.vehicle] }), "assignDriver"),
|
|
1080
|
+
step: /* @__PURE__ */ __name((ctx, order, step) => ifoodLogisticsStep(ctx, order.externalOrderId, step), "step"),
|
|
1081
|
+
verifyDeliveryCode: /* @__PURE__ */ __name(async (ctx, order, code) => deliveryCodeAnswerOf(await ifoodLogisticsVerifyDeliveryCode(ctx, order.externalOrderId, code)), "verifyDeliveryCode")
|
|
1082
|
+
};
|
|
1083
|
+
function deliveryCodeAnswerOf(result) {
|
|
1084
|
+
switch (result.kind) {
|
|
1085
|
+
case "verified":
|
|
1086
|
+
return { kind: "VERIFIED" };
|
|
1087
|
+
case "wrong-code":
|
|
1088
|
+
case "invalid":
|
|
1089
|
+
return { kind: "WRONG_CODE" };
|
|
1090
|
+
case "not-requested":
|
|
1091
|
+
return { kind: "NOT_REQUESTED" };
|
|
1092
|
+
case "refused":
|
|
1093
|
+
return { kind: "UNAVAILABLE", reason: `refused ${result.status}` };
|
|
1094
|
+
default:
|
|
1095
|
+
return { kind: "UNAVAILABLE", reason: result.reason };
|
|
1096
|
+
}
|
|
1097
|
+
}
|
|
1098
|
+
__name(deliveryCodeAnswerOf, "deliveryCodeAnswerOf");
|
|
1099
|
+
|
|
931
1100
|
// src/ifood/order-details.ts
|
|
932
1101
|
function countOf(source) {
|
|
933
1102
|
const stated = Number(pick(source, "quantity") ?? 1);
|
|
@@ -1020,6 +1189,14 @@ __name(readIfoodBuyerDocument, "readIfoodBuyerDocument");
|
|
|
1020
1189
|
var ORDER_PLACED = /* @__PURE__ */ new Set(["PLC", "PLACED", "ORDER_PLACED"]);
|
|
1021
1190
|
var ORDER_CANCELLED = /* @__PURE__ */ new Set(["CAN", "CANCELLED", "ORDER_CANCELLED"]);
|
|
1022
1191
|
var ORDER_CONFIRMED = /* @__PURE__ */ new Set(["CFM", "CONFIRMED"]);
|
|
1192
|
+
var DELIVERY_CODE_REQUESTED = /* @__PURE__ */ new Set(["DDCR", "DELIVERY_DROP_CODE_REQUESTED"]);
|
|
1193
|
+
function flagsOf(code) {
|
|
1194
|
+
return {
|
|
1195
|
+
...ORDER_CONFIRMED.has(code) ? { marketplaceConfirmed: true } : {},
|
|
1196
|
+
...DELIVERY_CODE_REQUESTED.has(code) ? { deliveryCodeRequested: true } : {}
|
|
1197
|
+
};
|
|
1198
|
+
}
|
|
1199
|
+
__name(flagsOf, "flagsOf");
|
|
1023
1200
|
var KEEPALIVE = "KEEPALIVE";
|
|
1024
1201
|
var CANCELLATION_REQUEST_FAILED = /* @__PURE__ */ new Set(["CARF", "CANCELLATION_REQUEST_FAILED"]);
|
|
1025
1202
|
var LIFECYCLE_OF = {
|
|
@@ -1186,7 +1363,7 @@ function normalizeIfoodEvent(payload, options) {
|
|
|
1186
1363
|
cancellationOutcome: cancelled ? "CONFIRMED" : refusedRequest ? "FAILED" : null,
|
|
1187
1364
|
...LIFECYCLE_OF[eventType.toUpperCase()] ? { lifecycle: LIFECYCLE_OF[eventType.toUpperCase()] } : {},
|
|
1188
1365
|
...optional("courier", readIfoodCourierEvent(payload, eventType) ?? void 0),
|
|
1189
|
-
...
|
|
1366
|
+
...flagsOf(eventType.toUpperCase()),
|
|
1190
1367
|
// The Negotiation Platform's HSD / HSS (FUT-6099): a dispute opened or closed, never an order body.
|
|
1191
1368
|
...optional("dispute", readIfoodDispute(payload, eventType) ?? void 0)
|
|
1192
1369
|
};
|
|
@@ -1613,103 +1790,6 @@ var IFOOD_AVAILABILITY = {
|
|
|
1613
1790
|
resume: ifoodResume
|
|
1614
1791
|
};
|
|
1615
1792
|
|
|
1616
|
-
// src/ifood/logistics.ts
|
|
1617
|
-
var logisticsUrl = /* @__PURE__ */ __name((externalOrderId, action) => `${IFOOD_API}/logistics/v1.0/orders/${encodeURIComponent(externalOrderId)}/${action}`, "logisticsUrl");
|
|
1618
|
-
var IFOOD_LOGISTICS_VEHICLES = [
|
|
1619
|
-
"BICYCLE",
|
|
1620
|
-
"ONFOOT",
|
|
1621
|
-
"PATINETE",
|
|
1622
|
-
"EBIKE",
|
|
1623
|
-
"SUPERBIKE",
|
|
1624
|
-
"CAR",
|
|
1625
|
-
"MOTORCYCLE",
|
|
1626
|
-
"MOTORBIKE"
|
|
1627
|
-
];
|
|
1628
|
-
var IFOOD_LOGISTICS_STEPS = ["GOING_TO_ORIGIN", "ARRIVED_AT_ORIGIN", "DISPATCHED", "ARRIVED_AT_DESTINATION"];
|
|
1629
|
-
var STEP_ACTION = {
|
|
1630
|
-
GOING_TO_ORIGIN: "goingToOrigin",
|
|
1631
|
-
ARRIVED_AT_ORIGIN: "arrivedAtOrigin",
|
|
1632
|
-
DISPATCHED: "dispatch",
|
|
1633
|
-
ARRIVED_AT_DESTINATION: "arrivedAtDestination"
|
|
1634
|
-
};
|
|
1635
|
-
function ifoodLogisticsStepsTo(lastSent, target) {
|
|
1636
|
-
const from = lastSent === null ? 0 : IFOOD_LOGISTICS_STEPS.indexOf(lastSent) + 1;
|
|
1637
|
-
const to = IFOOD_LOGISTICS_STEPS.indexOf(target);
|
|
1638
|
-
return to < from ? [] : IFOOD_LOGISTICS_STEPS.slice(from, to + 1);
|
|
1639
|
-
}
|
|
1640
|
-
__name(ifoodLogisticsStepsTo, "ifoodLogisticsStepsTo");
|
|
1641
|
-
function logisticsApp(ctx) {
|
|
1642
|
-
return { http: ctx.http, credentials: ctx.credentials, environment: ctx.environment, tokens: ctx.tokens, now: ctx.now };
|
|
1643
|
-
}
|
|
1644
|
-
__name(logisticsApp, "logisticsApp");
|
|
1645
|
-
function redacted(result, status) {
|
|
1646
|
-
if (result.kind === "refused") return { ...result, message: `iFood Logistics answered ${status}` };
|
|
1647
|
-
if (result.kind === "retry") return { kind: "retry", reason: `iFood Logistics answered ${status}` };
|
|
1648
|
-
return result;
|
|
1649
|
-
}
|
|
1650
|
-
__name(redacted, "redacted");
|
|
1651
|
-
function post(ctx, url, body) {
|
|
1652
|
-
let answer = "";
|
|
1653
|
-
return answered(async () => {
|
|
1654
|
-
const response = await withIfoodToken(
|
|
1655
|
-
logisticsApp(ctx),
|
|
1656
|
-
(token) => ctx.http({
|
|
1657
|
-
method: "POST",
|
|
1658
|
-
url,
|
|
1659
|
-
headers: { Authorization: `Bearer ${token}`, ...body === void 0 ? {} : { "Content-Type": "application/json" } },
|
|
1660
|
-
...body === void 0 ? {} : { body: JSON.stringify(body) }
|
|
1661
|
-
})
|
|
1662
|
-
);
|
|
1663
|
-
if (response === null) return { kind: "retry", reason: "iFood refused the token" };
|
|
1664
|
-
if ("kind" in response) return response;
|
|
1665
|
-
answer = response.body;
|
|
1666
|
-
return redacted(outboundResultOf(response), response.status);
|
|
1667
|
-
}).then((result) => ({ result, body: answer }));
|
|
1668
|
-
}
|
|
1669
|
-
__name(post, "post");
|
|
1670
|
-
async function ifoodLogisticsAssignDriver(ctx, externalOrderId, driver) {
|
|
1671
|
-
const name = driver.name.trim();
|
|
1672
|
-
const phone = driver.phone.replace(/\D/g, "");
|
|
1673
|
-
if (name === "") return { kind: "invalid", reason: "DRIVER_NAME" };
|
|
1674
|
-
if (phone.length < 10 || phone.length > 13) return { kind: "invalid", reason: "DRIVER_PHONE" };
|
|
1675
|
-
if (!IFOOD_LOGISTICS_VEHICLES.includes(driver.vehicle)) return { kind: "invalid", reason: "DRIVER_VEHICLE" };
|
|
1676
|
-
const { result } = await post(ctx, logisticsUrl(externalOrderId, "assignDriver"), {
|
|
1677
|
-
workerName: name,
|
|
1678
|
-
workerPhone: phone,
|
|
1679
|
-
workerVehicleType: driver.vehicle
|
|
1680
|
-
});
|
|
1681
|
-
return result;
|
|
1682
|
-
}
|
|
1683
|
-
__name(ifoodLogisticsAssignDriver, "ifoodLogisticsAssignDriver");
|
|
1684
|
-
async function ifoodLogisticsStep(ctx, externalOrderId, step) {
|
|
1685
|
-
const { result } = await post(ctx, logisticsUrl(externalOrderId, STEP_ACTION[step]));
|
|
1686
|
-
return result;
|
|
1687
|
-
}
|
|
1688
|
-
__name(ifoodLogisticsStep, "ifoodLogisticsStep");
|
|
1689
|
-
function successOf(body) {
|
|
1690
|
-
try {
|
|
1691
|
-
const parsed2 = JSON.parse(body);
|
|
1692
|
-
return typeof parsed2?.success === "boolean" ? parsed2.success : null;
|
|
1693
|
-
} catch {
|
|
1694
|
-
return null;
|
|
1695
|
-
}
|
|
1696
|
-
}
|
|
1697
|
-
__name(successOf, "successOf");
|
|
1698
|
-
async function ifoodLogisticsVerifyDeliveryCode(ctx, externalOrderId, code) {
|
|
1699
|
-
const typed = code.replace(/\s/g, "");
|
|
1700
|
-
if (!/^[0-9A-Za-z]{1,12}$/.test(typed)) return { kind: "invalid", reason: "CODE" };
|
|
1701
|
-
const { result, body } = await post(ctx, logisticsUrl(externalOrderId, "verifyDeliveryCode"), { code: typed });
|
|
1702
|
-
if (result.kind === "accepted") {
|
|
1703
|
-
const success = successOf(body);
|
|
1704
|
-
if (success === null) return { kind: "retry", reason: "iFood answered the delivery code without `success`" };
|
|
1705
|
-
return success ? { kind: "verified" } : { kind: "wrong-code" };
|
|
1706
|
-
}
|
|
1707
|
-
if (result.kind === "refused" && result.status === 422) return { kind: "wrong-code" };
|
|
1708
|
-
if (result.kind === "refused" && result.status === 412) return { kind: "not-requested" };
|
|
1709
|
-
return result;
|
|
1710
|
-
}
|
|
1711
|
-
__name(ifoodLogisticsVerifyDeliveryCode, "ifoodLogisticsVerifyDeliveryCode");
|
|
1712
|
-
|
|
1713
1793
|
// src/ifood/index.ts
|
|
1714
1794
|
var IFOOD_PROVIDER = "IFOOD";
|
|
1715
1795
|
var ITEM_UNAVAILABLE = "503";
|
|
@@ -1781,6 +1861,7 @@ function createIfoodAdapter(config) {
|
|
|
1781
1861
|
courier: IFOOD_COURIER,
|
|
1782
1862
|
// The buyer's disputes: accept, reject with iFood's reason, or one of its alternatives (FUT-6099).
|
|
1783
1863
|
disputes: IFOOD_DISPUTES,
|
|
1864
|
+
logistics: IFOOD_LOGISTICS,
|
|
1784
1865
|
// No per-order deep link is known, so the portal, where a live order is
|
|
1785
1866
|
// on the first screen.
|
|
1786
1867
|
appLink: /* @__PURE__ */ __name(() => IFOOD_PORTAL_URL, "appLink")
|
|
@@ -1797,6 +1878,7 @@ export {
|
|
|
1797
1878
|
IFOOD_COURIER,
|
|
1798
1879
|
IFOOD_IMAGE_BASE_URL,
|
|
1799
1880
|
IFOOD_LINKING,
|
|
1881
|
+
IFOOD_LOGISTICS,
|
|
1800
1882
|
IFOOD_LOGISTICS_STEPS,
|
|
1801
1883
|
IFOOD_LOGISTICS_VEHICLES,
|
|
1802
1884
|
IFOOD_PROVIDER,
|