@12-apps/marketplace 1.15.0 → 1.16.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.
@@ -938,6 +938,13 @@ declare class AcknowledgeRetryError extends Error {
938
938
  * kept on the acceptance row (`cancellation_reasons`) for the order's life.
939
939
  * The adapter's static list is the fallback while, or when, that read did not
940
940
  * land: a refusal is never held on the marketplace.
941
+ *
942
+ * The same job, for an order the store did not auto-accept, first applies a
943
+ * confirmation on the marketplace that arrived before the order (FUT-5653's
944
+ * C5). It runs here, a few seconds after the ingest, rather than inside it, so
945
+ * the ingest pays no inbox read that almost always finds nothing (FUT-5984).
946
+ * A job that is late or lost loses nothing: the expiry sweep applies a deferred
947
+ * confirmation before it expires the order (`applyOwedConfirmations`).
941
948
  */
942
949
  /** The wire name, `<namespace>.<blueprint>`. */
943
950
  declare const MARKETPLACE_CANCELLATION_REASONS_JOB = "marketplace.cancellation-reasons";
@@ -945,6 +952,8 @@ declare const MARKETPLACE_CANCELLATION_REASONS_JOB = "marketplace.cancellation-r
945
952
  interface CancellationReasonsJobPayload {
946
953
  clientId: string;
947
954
  orderId: string;
955
+ /** Set for an order not auto-accepted: apply a confirmation that outran it first. */
956
+ deferredConfirmations?: true;
948
957
  }
949
958
  /** What one run did. */
950
959
  type CancellationReasonsOutcome = {
@@ -1,6 +1,6 @@
1
1
  import { Hono, Context } from 'hono';
2
2
  import { ApiMarketplace, MarketplaceApiConfig, MarketplaceActor } from '../server/index.js';
3
- import '../engine-BSLdhAGC.js';
3
+ import '../engine-DSRj7qnC.js';
4
4
  import '../adapter-CjVdMhZ2.js';
5
5
  import '@12-apps/wiring';
6
6
  import 'zod';
@@ -1,8 +1,8 @@
1
1
  import {
2
2
  createApiMarketplace
3
- } from "../chunk-FQI56NGC.js";
3
+ } from "../chunk-DJIMUEHT.js";
4
4
  import "../chunk-SGOTN5PX.js";
5
- import "../chunk-B4DYPQFE.js";
5
+ import "../chunk-TICWLBSA.js";
6
6
  import "../chunk-H2VQRD3B.js";
7
7
  import {
8
8
  __name
@@ -120,6 +120,62 @@ declare function ifoodResume(ctx: AdapterCallContext, ref: StorePauseRef): Promi
120
120
  /** iFood's availability capability: interruptions, no extend. */
121
121
  declare const IFOOD_AVAILABILITY: MarketplaceAvailability;
122
122
 
123
+ /** The vehicles iFood takes in `workerVehicleType`. */
124
+ declare const IFOOD_LOGISTICS_VEHICLES: readonly ["BICYCLE", "ONFOOT", "PATINETE", "EBIKE", "SUPERBIKE", "CAR", "MOTORCYCLE", "MOTORBIKE"];
125
+ type IfoodLogisticsVehicle = (typeof IFOOD_LOGISTICS_VEHICLES)[number];
126
+ /** The steps after the driver is assigned, in the one order iFood accepts them. */
127
+ declare const IFOOD_LOGISTICS_STEPS: readonly ["GOING_TO_ORIGIN", "ARRIVED_AT_ORIGIN", "DISPATCHED", "ARRIVED_AT_DESTINATION"];
128
+ type IfoodLogisticsStep = (typeof IFOOD_LOGISTICS_STEPS)[number];
129
+ /**
130
+ * The steps still owed, in order, to reach `target` when `lastSent` is the
131
+ * last one iFood took (null: only the driver was assigned). Empty when the
132
+ * target was already passed: iFood answers a repeated or earlier step with
133
+ * 409, so it is never sent.
134
+ */
135
+ declare function ifoodLogisticsStepsTo(lastSent: IfoodLogisticsStep | null, target: IfoodLogisticsStep): IfoodLogisticsStep[];
136
+ /** Who carries the order, as iFood shows them to the customer. */
137
+ interface IfoodLogisticsDriver {
138
+ name: string;
139
+ /**
140
+ * Digits only once sent; anything else in it (spaces, `+`, dashes) is
141
+ * dropped. 10 to 13 digits is this client's own bound, not iFood's.
142
+ */
143
+ phone: string;
144
+ vehicle: IfoodLogisticsVehicle;
145
+ }
146
+ /** A call refused here, before reaching iFood: nothing was sent. */
147
+ interface IfoodLogisticsInvalid {
148
+ kind: "invalid";
149
+ reason: "DRIVER_NAME" | "DRIVER_PHONE" | "DRIVER_VEHICLE" | "CODE";
150
+ }
151
+ /**
152
+ * `assignDriver`: the store's motoboy takes the order. A second assignment of
153
+ * the same order is iFood's 409, answered as `refused` like any final 4xx.
154
+ */
155
+ declare function ifoodLogisticsAssignDriver(ctx: AdapterCallContext, externalOrderId: string, driver: IfoodLogisticsDriver): Promise<OutboundResult | IfoodLogisticsInvalid>;
156
+ /** One step of the delivery. iFood takes it asynchronously: `accepted` means the request was taken. */
157
+ declare function ifoodLogisticsStep(ctx: AdapterCallContext, externalOrderId: string, step: IfoodLogisticsStep): Promise<OutboundResult>;
158
+ /** What iFood said of the customer's delivery code. */
159
+ type IfoodDeliveryCodeResult = {
160
+ kind: "verified";
161
+ }
162
+ /** iFood's `success: false`, or its 422: the motoboy may type it again. */
163
+ | {
164
+ kind: "wrong-code";
165
+ }
166
+ /** iFood's 412: the order never asked for a code (no `DELIVERY_DROP_CODE_REQUESTED`). */
167
+ | {
168
+ kind: "not-requested";
169
+ } | Exclude<OutboundResult, {
170
+ kind: "accepted";
171
+ }> | IfoodLogisticsInvalid;
172
+ /**
173
+ * `verifyDeliveryCode`: the code the customer reads from the iFood app,
174
+ * typed by the motoboy at the door. The code is in the request body only, and
175
+ * no result carries iFood's answer body, so nothing here puts it in a log.
176
+ */
177
+ declare function ifoodLogisticsVerifyDeliveryCode(ctx: AdapterCallContext, externalOrderId: string, code: string): Promise<IfoodDeliveryCodeResult>;
178
+
123
179
  /**
124
180
  * `@12-apps/marketplace/ifood` — iFood, as one adapter on the engine.
125
181
  *
@@ -149,4 +205,4 @@ interface IfoodAdapterConfig {
149
205
  */
150
206
  declare function createIfoodAdapter(config: IfoodAdapterConfig): MarketplaceAdapter;
151
207
 
152
- export { IFOOD_AVAILABILITY, IFOOD_CANCELLATION_ANSWER_WINDOW_MS, IFOOD_CATALOGUE, IFOOD_CATALOGUE_READER, IFOOD_COURIER, IFOOD_IMAGE_BASE_URL, IFOOD_LINKING, IFOOD_PROVIDER, IFOOD_REASON_CODES, type IfoodAdapterConfig, type IfoodAdapterCopy, type IfoodReasonCode, createIfoodAdapter, ifoodFetchOrder, ifoodItemBody, ifoodItemId, ifoodLocalTime, ifoodPause, ifoodProductId, ifoodResume, normalizeIfoodEvent, readIfoodCourierEvent, readIfoodOrder, readIfoodPresence, verifyIfoodSignature };
208
+ export { IFOOD_AVAILABILITY, IFOOD_CANCELLATION_ANSWER_WINDOW_MS, IFOOD_CATALOGUE, IFOOD_CATALOGUE_READER, IFOOD_COURIER, IFOOD_IMAGE_BASE_URL, IFOOD_LINKING, IFOOD_LOGISTICS_STEPS, IFOOD_LOGISTICS_VEHICLES, IFOOD_PROVIDER, IFOOD_REASON_CODES, type IfoodAdapterConfig, type IfoodAdapterCopy, type IfoodDeliveryCodeResult, type IfoodLogisticsDriver, type IfoodLogisticsInvalid, type IfoodLogisticsStep, type IfoodLogisticsVehicle, type IfoodReasonCode, createIfoodAdapter, ifoodFetchOrder, ifoodItemBody, ifoodItemId, ifoodLocalTime, ifoodLogisticsAssignDriver, ifoodLogisticsStep, ifoodLogisticsStepsTo, ifoodLogisticsVerifyDeliveryCode, ifoodPause, ifoodProductId, ifoodResume, normalizeIfoodEvent, readIfoodCourierEvent, readIfoodOrder, readIfoodPresence, verifyIfoodSignature };
@@ -1376,6 +1376,103 @@ var IFOOD_AVAILABILITY = {
1376
1376
  resume: ifoodResume
1377
1377
  };
1378
1378
 
1379
+ // src/ifood/logistics.ts
1380
+ var logisticsUrl = /* @__PURE__ */ __name((externalOrderId, action) => `${IFOOD_API}/logistics/v1.0/orders/${encodeURIComponent(externalOrderId)}/${action}`, "logisticsUrl");
1381
+ var IFOOD_LOGISTICS_VEHICLES = [
1382
+ "BICYCLE",
1383
+ "ONFOOT",
1384
+ "PATINETE",
1385
+ "EBIKE",
1386
+ "SUPERBIKE",
1387
+ "CAR",
1388
+ "MOTORCYCLE",
1389
+ "MOTORBIKE"
1390
+ ];
1391
+ var IFOOD_LOGISTICS_STEPS = ["GOING_TO_ORIGIN", "ARRIVED_AT_ORIGIN", "DISPATCHED", "ARRIVED_AT_DESTINATION"];
1392
+ var STEP_ACTION = {
1393
+ GOING_TO_ORIGIN: "goingToOrigin",
1394
+ ARRIVED_AT_ORIGIN: "arrivedAtOrigin",
1395
+ DISPATCHED: "dispatch",
1396
+ ARRIVED_AT_DESTINATION: "arrivedAtDestination"
1397
+ };
1398
+ function ifoodLogisticsStepsTo(lastSent, target) {
1399
+ const from = lastSent === null ? 0 : IFOOD_LOGISTICS_STEPS.indexOf(lastSent) + 1;
1400
+ const to = IFOOD_LOGISTICS_STEPS.indexOf(target);
1401
+ return to < from ? [] : IFOOD_LOGISTICS_STEPS.slice(from, to + 1);
1402
+ }
1403
+ __name(ifoodLogisticsStepsTo, "ifoodLogisticsStepsTo");
1404
+ function logisticsApp(ctx) {
1405
+ return { http: ctx.http, credentials: ctx.credentials, environment: ctx.environment, tokens: ctx.tokens, now: ctx.now };
1406
+ }
1407
+ __name(logisticsApp, "logisticsApp");
1408
+ function redacted(result, status) {
1409
+ if (result.kind === "refused") return { ...result, message: `iFood Logistics answered ${status}` };
1410
+ if (result.kind === "retry") return { kind: "retry", reason: `iFood Logistics answered ${status}` };
1411
+ return result;
1412
+ }
1413
+ __name(redacted, "redacted");
1414
+ function post(ctx, url, body) {
1415
+ let answer = "";
1416
+ return answered(async () => {
1417
+ const response = await withIfoodToken(
1418
+ logisticsApp(ctx),
1419
+ (token) => ctx.http({
1420
+ method: "POST",
1421
+ url,
1422
+ headers: { Authorization: `Bearer ${token}`, ...body === void 0 ? {} : { "Content-Type": "application/json" } },
1423
+ ...body === void 0 ? {} : { body: JSON.stringify(body) }
1424
+ })
1425
+ );
1426
+ if (response === null) return { kind: "retry", reason: "iFood refused the token" };
1427
+ if ("kind" in response) return response;
1428
+ answer = response.body;
1429
+ return redacted(outboundResultOf(response), response.status);
1430
+ }).then((result) => ({ result, body: answer }));
1431
+ }
1432
+ __name(post, "post");
1433
+ async function ifoodLogisticsAssignDriver(ctx, externalOrderId, driver) {
1434
+ const name = driver.name.trim();
1435
+ const phone = driver.phone.replace(/\D/g, "");
1436
+ if (name === "") return { kind: "invalid", reason: "DRIVER_NAME" };
1437
+ if (phone.length < 10 || phone.length > 13) return { kind: "invalid", reason: "DRIVER_PHONE" };
1438
+ if (!IFOOD_LOGISTICS_VEHICLES.includes(driver.vehicle)) return { kind: "invalid", reason: "DRIVER_VEHICLE" };
1439
+ const { result } = await post(ctx, logisticsUrl(externalOrderId, "assignDriver"), {
1440
+ workerName: name,
1441
+ workerPhone: phone,
1442
+ workerVehicleType: driver.vehicle
1443
+ });
1444
+ return result;
1445
+ }
1446
+ __name(ifoodLogisticsAssignDriver, "ifoodLogisticsAssignDriver");
1447
+ async function ifoodLogisticsStep(ctx, externalOrderId, step) {
1448
+ const { result } = await post(ctx, logisticsUrl(externalOrderId, STEP_ACTION[step]));
1449
+ return result;
1450
+ }
1451
+ __name(ifoodLogisticsStep, "ifoodLogisticsStep");
1452
+ function successOf(body) {
1453
+ try {
1454
+ const parsed2 = JSON.parse(body);
1455
+ return typeof parsed2?.success === "boolean" ? parsed2.success : null;
1456
+ } catch {
1457
+ return null;
1458
+ }
1459
+ }
1460
+ __name(successOf, "successOf");
1461
+ async function ifoodLogisticsVerifyDeliveryCode(ctx, externalOrderId, code) {
1462
+ const typed = code.replace(/\s/g, "");
1463
+ if (!/^[0-9A-Za-z]{1,12}$/.test(typed)) return { kind: "invalid", reason: "CODE" };
1464
+ const { result, body } = await post(ctx, logisticsUrl(externalOrderId, "verifyDeliveryCode"), { code: typed });
1465
+ if (result.kind === "accepted") {
1466
+ const success = successOf(body);
1467
+ if (success === null) return { kind: "retry", reason: "iFood answered the delivery code without `success`" };
1468
+ return success ? { kind: "verified" } : { kind: "wrong-code" };
1469
+ }
1470
+ if (result.kind === "refused" && result.status === 422) return { kind: "wrong-code" };
1471
+ if (result.kind === "refused" && result.status === 412) return { kind: "not-requested" };
1472
+ return result;
1473
+ }
1474
+ __name(ifoodLogisticsVerifyDeliveryCode, "ifoodLogisticsVerifyDeliveryCode");
1475
+
1379
1476
  // src/ifood/index.ts
1380
1477
  var IFOOD_PROVIDER = "IFOOD";
1381
1478
  var ITEM_UNAVAILABLE = "503";
@@ -1453,6 +1550,8 @@ export {
1453
1550
  IFOOD_COURIER,
1454
1551
  IFOOD_IMAGE_BASE_URL,
1455
1552
  IFOOD_LINKING,
1553
+ IFOOD_LOGISTICS_STEPS,
1554
+ IFOOD_LOGISTICS_VEHICLES,
1456
1555
  IFOOD_PROVIDER,
1457
1556
  IFOOD_REASON_CODES,
1458
1557
  createIfoodAdapter,
@@ -1460,6 +1559,10 @@ export {
1460
1559
  ifoodItemBody,
1461
1560
  ifoodItemId,
1462
1561
  ifoodLocalTime,
1562
+ ifoodLogisticsAssignDriver,
1563
+ ifoodLogisticsStep,
1564
+ ifoodLogisticsStepsTo,
1565
+ ifoodLogisticsVerifyDeliveryCode,
1463
1566
  ifoodPause,
1464
1567
  ifoodProductId,
1465
1568
  ifoodResume,