@12-apps/marketplace 1.8.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/README.md +102 -8
- package/dist/99food/index.d.ts +30 -54
- package/dist/99food/index.js +60 -418
- package/dist/99food/index.js.map +1 -1
- package/dist/chunk-PYHNM47K.js +478 -0
- package/dist/chunk-PYHNM47K.js.map +1 -0
- package/dist/client-B4EQCRD_.d.ts +20 -0
- 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/package.json +14 -2
- package/src/99food/catalogue.ts +19 -89
- package/src/99food/copy.ts +4 -19
- package/src/99food/index.ts +28 -33
- package/src/99food/normalize.ts +11 -253
- package/src/99food/outbound.ts +29 -270
- package/src/99food/pause.ts +7 -22
- package/src/index.ts +2 -1
- package/src/keeta/authorization.ts +226 -0
- package/src/keeta/copy.ts +27 -0
- package/src/keeta/en-US.ts +19 -0
- package/src/keeta/index.ts +162 -0
- package/src/keeta/merchant.ts +70 -0
- package/src/keeta/normalize.ts +131 -0
- package/src/keeta/outbound.ts +55 -0
- package/src/keeta/pt-BR.ts +23 -0
- package/src/keeta/signature.ts +85 -0
- package/src/open-delivery/client.ts +185 -0
- package/src/open-delivery/merchant.ts +126 -0
- package/src/open-delivery/normalize.ts +269 -0
- package/src/open-delivery/orders.ts +241 -0
- package/src/open-delivery/reasons.ts +26 -0
|
@@ -0,0 +1,241 @@
|
|
|
1
|
+
import {
|
|
2
|
+
answered,
|
|
3
|
+
outboundResultOf,
|
|
4
|
+
type AdapterCallContext,
|
|
5
|
+
type CancellationRequestInput,
|
|
6
|
+
type FetchOrderResult,
|
|
7
|
+
type MarketplaceHttpResponse,
|
|
8
|
+
type OutboundOrderRef,
|
|
9
|
+
type OutboundResult,
|
|
10
|
+
type StatusFacts,
|
|
11
|
+
} from "../core/adapter";
|
|
12
|
+
import type { NormalizedMarketplaceOrder } from "../core/types";
|
|
13
|
+
import { isStop, openDeliveryCall, type OpenDeliveryDialect, type OpenDeliveryStop } from "./client";
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* The ORDER calls every Open Delivery adapter shares: the order fetch,
|
|
17
|
+
* confirm, requestCancellation and the status steps. Paths, verbs, bodies and
|
|
18
|
+
* answers are the standard's (v1.7.1 and v1.5.0 agree on all of them); the
|
|
19
|
+
* job that calls these, its retries and what a failure shows are the
|
|
20
|
+
* engine's. What a marketplace does differently is an option here, set by its
|
|
21
|
+
* adapter folder. Tests never reach a marketplace: the HTTP client is the
|
|
22
|
+
* host's port, answered by a fake.
|
|
23
|
+
*/
|
|
24
|
+
|
|
25
|
+
/** One authenticated call to an order's resource. */
|
|
26
|
+
function callOrder(
|
|
27
|
+
ctx: AdapterCallContext,
|
|
28
|
+
dialect: OpenDeliveryDialect,
|
|
29
|
+
request: { method: "GET" | "POST"; externalOrderId: string; action: string | null; body?: unknown },
|
|
30
|
+
): Promise<MarketplaceHttpResponse | OpenDeliveryStop> {
|
|
31
|
+
const suffix = request.action === null ? "" : `/${request.action}`;
|
|
32
|
+
return openDeliveryCall(ctx, dialect, {
|
|
33
|
+
method: request.method,
|
|
34
|
+
path: `/v1/orders/${encodeURIComponent(request.externalOrderId)}${suffix}`,
|
|
35
|
+
...(request.body === undefined ? {} : { body: request.body }),
|
|
36
|
+
});
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
/** Whether a 422 answer's `CancellationError.cancellationStatus` says the order is already cancelled. */
|
|
40
|
+
function alreadyCancelled(response: MarketplaceHttpResponse): boolean {
|
|
41
|
+
if (response.status !== 422) return false;
|
|
42
|
+
try {
|
|
43
|
+
return (JSON.parse(response.body) as { cancellationStatus?: unknown }).cancellationStatus === "CANCELLED";
|
|
44
|
+
} catch {
|
|
45
|
+
return false;
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
/** {@link answered} for a fetch: a call that never got an answer is a retry. */
|
|
50
|
+
async function fetched(call: () => Promise<FetchOrderResult>): Promise<FetchOrderResult> {
|
|
51
|
+
try {
|
|
52
|
+
return await call();
|
|
53
|
+
} catch (error) {
|
|
54
|
+
return { kind: "retry", reason: error instanceof Error ? error.message : String(error) };
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
/** How a fetched `Order` becomes the engine's order. */
|
|
59
|
+
interface OpenDeliveryFetchOptions {
|
|
60
|
+
normalize(order: unknown): NormalizedMarketplaceOrder | null;
|
|
61
|
+
/**
|
|
62
|
+
* OPTIONAL. What the adapter does to the parsed `Order` before it is
|
|
63
|
+
* normalized (Keeta's encrypted fields). Never fails the fetch.
|
|
64
|
+
*/
|
|
65
|
+
prepare?(ctx: AdapterCallContext, order: unknown): Promise<unknown>;
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
/** `GET /v1/orders/{orderId}`: the order an event announced, normalized. A 404 is an expired order: refused. */
|
|
69
|
+
export function openDeliveryFetchOrder(
|
|
70
|
+
ctx: AdapterCallContext,
|
|
71
|
+
dialect: OpenDeliveryDialect,
|
|
72
|
+
order: { externalOrderId: string },
|
|
73
|
+
options: OpenDeliveryFetchOptions,
|
|
74
|
+
): Promise<FetchOrderResult> {
|
|
75
|
+
return fetched(async () => {
|
|
76
|
+
const response = await callOrder(ctx, dialect, { method: "GET", externalOrderId: order.externalOrderId, action: null });
|
|
77
|
+
if (isStop(response)) return response;
|
|
78
|
+
const result = outboundResultOf(response);
|
|
79
|
+
if (result.kind !== "accepted") return result;
|
|
80
|
+
let body: unknown;
|
|
81
|
+
try {
|
|
82
|
+
body = JSON.parse(response.body);
|
|
83
|
+
} catch {
|
|
84
|
+
return { kind: "refused", status: response.status, message: "the order is not JSON" };
|
|
85
|
+
}
|
|
86
|
+
const prepared = options.prepare ? await options.prepare(ctx, body) : body;
|
|
87
|
+
const normalized = options.normalize(prepared);
|
|
88
|
+
return normalized
|
|
89
|
+
? { kind: "order", order: normalized }
|
|
90
|
+
: { kind: "refused", status: response.status, message: "the order carries no id or no line" };
|
|
91
|
+
});
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
/**
|
|
95
|
+
* `POST /v1/orders/{orderId}/confirm` with the standard's `OrderConfirmed`:
|
|
96
|
+
* `orderExternalCode` (the host's own order id) and `createdAt`, plus the
|
|
97
|
+
* `preparationTime` in minutes when the store stated one. `createdAt` is sent
|
|
98
|
+
* as the moment of the confirmation: ESTIMATED (the standard describes it
|
|
99
|
+
* with the ORDER's creation text, a copy of another schema's).
|
|
100
|
+
*
|
|
101
|
+
* Every 422 is a refusal. It is the standard's generic `UnprocessableEntity`
|
|
102
|
+
* ("check if the instruction has not been sent before"), and its body cannot
|
|
103
|
+
* tell a duplicate from a cancelled order. So a retried confirm whose first
|
|
104
|
+
* answer was lost reads "not confirmed" with the app link (a false alarm a
|
|
105
|
+
* person dismisses on the marketplace), and a confirm the marketplace truly
|
|
106
|
+
* refused is never stamped confirmed.
|
|
107
|
+
*/
|
|
108
|
+
export function openDeliveryConfirm(
|
|
109
|
+
ctx: AdapterCallContext,
|
|
110
|
+
dialect: OpenDeliveryDialect,
|
|
111
|
+
order: OutboundOrderRef,
|
|
112
|
+
input: { preparationMinutes: number | null },
|
|
113
|
+
): Promise<OutboundResult> {
|
|
114
|
+
return answered(async () => {
|
|
115
|
+
const response = await callOrder(ctx, dialect, {
|
|
116
|
+
method: "POST",
|
|
117
|
+
externalOrderId: order.externalOrderId,
|
|
118
|
+
action: "confirm",
|
|
119
|
+
body: {
|
|
120
|
+
orderExternalCode: order.hostOrderCode,
|
|
121
|
+
createdAt: ctx.now.toISOString(),
|
|
122
|
+
...(input.preparationMinutes === null ? {} : { preparationTime: input.preparationMinutes }),
|
|
123
|
+
},
|
|
124
|
+
});
|
|
125
|
+
return isStop(response) ? response : outboundResultOf(response);
|
|
126
|
+
});
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
/** The standard's limit on `outOfStockItems`. */
|
|
130
|
+
const MAX_OUT_OF_STOCK_ITEMS = 100;
|
|
131
|
+
|
|
132
|
+
/** How a marketplace reads `RequestCancelled.outOfStockItems`. */
|
|
133
|
+
interface OpenDeliveryCancellationOptions {
|
|
134
|
+
/**
|
|
135
|
+
* The offending SKUs (`externalCode`) are sent as `outOfStockItems` with an
|
|
136
|
+
* item-unavailable reason. False where the marketplace wants its own item
|
|
137
|
+
* uuids there, which the engine does not keep (99Food, v1.7.1).
|
|
138
|
+
*/
|
|
139
|
+
outOfStockAsSkus: boolean;
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
/**
|
|
143
|
+
* `POST /v1/orders/{orderId}/requestCancellation` with the standard's
|
|
144
|
+
* `RequestCancelled`: `reason` (free text: the store's, else the label),
|
|
145
|
+
* `code` (one of the standard's codes) and `mode` (`MANUAL` or `AUTO`). 202
|
|
146
|
+
* means taken; the outcome is a later event. A 422 whose `cancellationStatus`
|
|
147
|
+
* is `CANCELLED` means the order is cancelled already, which is what the
|
|
148
|
+
* request wanted. A 400 is a malformed request (a bad id or body: ours); a
|
|
149
|
+
* 422 is the business answer and stays a refusal.
|
|
150
|
+
*/
|
|
151
|
+
export function openDeliveryRequestCancellation(
|
|
152
|
+
ctx: AdapterCallContext,
|
|
153
|
+
dialect: OpenDeliveryDialect,
|
|
154
|
+
order: OutboundOrderRef,
|
|
155
|
+
input: CancellationRequestInput,
|
|
156
|
+
options: OpenDeliveryCancellationOptions,
|
|
157
|
+
): Promise<OutboundResult> {
|
|
158
|
+
const skus =
|
|
159
|
+
options.outOfStockAsSkus && input.reason.itemUnavailable && input.offendingSkus.length > 0
|
|
160
|
+
? { outOfStockItems: input.offendingSkus.slice(0, MAX_OUT_OF_STOCK_ITEMS) }
|
|
161
|
+
: {};
|
|
162
|
+
return answered(async () => {
|
|
163
|
+
const response = await callOrder(ctx, dialect, {
|
|
164
|
+
method: "POST",
|
|
165
|
+
externalOrderId: order.externalOrderId,
|
|
166
|
+
action: "requestCancellation",
|
|
167
|
+
body: {
|
|
168
|
+
reason: input.text === "" ? input.reason.label : input.text,
|
|
169
|
+
code: input.reason.code,
|
|
170
|
+
mode: input.mode,
|
|
171
|
+
...skus,
|
|
172
|
+
},
|
|
173
|
+
});
|
|
174
|
+
if (isStop(response)) return response;
|
|
175
|
+
if (alreadyCancelled(response)) return { kind: "accepted" };
|
|
176
|
+
const result = outboundResultOf(response);
|
|
177
|
+
return result.kind === "refused" && result.status === 400 ? { ...result, own: true } : result;
|
|
178
|
+
});
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
/** Which standard status call a step takes, for which kind of order. */
|
|
182
|
+
type StatusPath = (facts: StatusFacts) => { action: string; body?: unknown } | null;
|
|
183
|
+
|
|
184
|
+
/** The status calls a marketplace serves. */
|
|
185
|
+
interface OpenDeliveryStatusOptions {
|
|
186
|
+
/** A take-out's HANDED_OVER is `POST /v1/orders/{id}/pickedUp`. False where the marketplace has no take-out. */
|
|
187
|
+
takeoutPickedUp: boolean;
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
/**
|
|
191
|
+
* The standard's status calls (FUT-4653):
|
|
192
|
+
*
|
|
193
|
+
* - READY: `POST /v1/orders/{id}/readyForPickup`, no body; 202. Every kind of order.
|
|
194
|
+
* - DISPATCHED: `POST /v1/orders/{id}/dispatch`, body `OrderDispatched`
|
|
195
|
+
* (`deliveryTrackingInfo` optional, sent empty). Only where the logistics
|
|
196
|
+
* is the merchant's.
|
|
197
|
+
* - HANDED_OVER: the merchant's bag `POST /v1/orders/{id}/delivered`; a
|
|
198
|
+
* take-out `POST /v1/orders/{id}/pickedUp` where the marketplace serves it.
|
|
199
|
+
* The marketplace's own fleet: none.
|
|
200
|
+
*
|
|
201
|
+
* The kind of order is `deliveredBy` (null: a take-out; ESTIMATED: an order
|
|
202
|
+
* that merely omits it reads as a take-out here).
|
|
203
|
+
*/
|
|
204
|
+
function statusPaths(options: OpenDeliveryStatusOptions): Readonly<Record<string, StatusPath>> {
|
|
205
|
+
return {
|
|
206
|
+
READY: () => ({ action: "readyForPickup" }),
|
|
207
|
+
DISPATCHED: (facts) => (facts.deliveredBy === "MERCHANT" ? { action: "dispatch", body: {} } : null),
|
|
208
|
+
HANDED_OVER: (facts) =>
|
|
209
|
+
facts.deliveredBy === "MERCHANT"
|
|
210
|
+
? { action: "delivered", body: {} }
|
|
211
|
+
: facts.deliveredBy === null && options.takeoutPickedUp
|
|
212
|
+
? { action: "pickedUp", body: {} }
|
|
213
|
+
: null,
|
|
214
|
+
};
|
|
215
|
+
}
|
|
216
|
+
|
|
217
|
+
/** A marketplace's status half: whether a step is taken (offline), and the call that tells it. */
|
|
218
|
+
export function openDeliveryStatus(options: OpenDeliveryStatusOptions) {
|
|
219
|
+
const paths = statusPaths(options);
|
|
220
|
+
return {
|
|
221
|
+
supported: (facts: StatusFacts): boolean => paths[facts.step]?.(facts) != null,
|
|
222
|
+
inform(
|
|
223
|
+
ctx: AdapterCallContext,
|
|
224
|
+
dialect: OpenDeliveryDialect,
|
|
225
|
+
order: OutboundOrderRef,
|
|
226
|
+
facts: StatusFacts,
|
|
227
|
+
): Promise<OutboundResult | { kind: "unsupported" }> {
|
|
228
|
+
const path = paths[facts.step]?.(facts) ?? null;
|
|
229
|
+
if (path === null) return Promise.resolve({ kind: "unsupported" });
|
|
230
|
+
return answered(async () => {
|
|
231
|
+
const response = await callOrder(ctx, dialect, {
|
|
232
|
+
method: "POST",
|
|
233
|
+
externalOrderId: order.externalOrderId,
|
|
234
|
+
action: path.action,
|
|
235
|
+
...(path.body === undefined ? {} : { body: path.body }),
|
|
236
|
+
});
|
|
237
|
+
return isStop(response) ? response : outboundResultOf(response);
|
|
238
|
+
});
|
|
239
|
+
},
|
|
240
|
+
};
|
|
241
|
+
}
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The cancellation codes a merchant may send, verbatim from Open Delivery's
|
|
3
|
+
* `RequestCancelled.code`: the same eleven in v1.7.1 and in Keeta's v1.5.0
|
|
4
|
+
* (its "códigos de cancelamento (v1.0.7+)"). Pinned by the standard, not
|
|
5
|
+
* estimated. The two codes only the marketplace sends
|
|
6
|
+
* (`CONSUMER_CANCELLATION_REQUESTED`, `OTHER_CANCELLATION_REASON`) are not a
|
|
7
|
+
* store's to choose and are left out.
|
|
8
|
+
*/
|
|
9
|
+
export const OPEN_DELIVERY_MERCHANT_REASON_CODES = [
|
|
10
|
+
"SYSTEMIC_ISSUES",
|
|
11
|
+
"DUPLICATE_APPLICATION",
|
|
12
|
+
"UNAVAILABLE_ITEM",
|
|
13
|
+
"RESTAURANT_WITHOUT_DELIVERY_PERSON",
|
|
14
|
+
"OUTDATED_MENU",
|
|
15
|
+
"ORDER_OUTSIDE_THE_DELIVERY_AREA",
|
|
16
|
+
"BLOCKED_CUSTOMER",
|
|
17
|
+
"OUTSIDE_DELIVERY_HOURS",
|
|
18
|
+
"INTERNAL_DIFFICULTIES_OF_THE_RESTAURANT",
|
|
19
|
+
"RISK_AREA",
|
|
20
|
+
"DELIVERY_PROBLEM",
|
|
21
|
+
] as const;
|
|
22
|
+
|
|
23
|
+
type OpenDeliveryMerchantReasonCode = (typeof OPEN_DELIVERY_MERCHANT_REASON_CODES)[number];
|
|
24
|
+
|
|
25
|
+
/** The one reason that means "an item is unavailable". */
|
|
26
|
+
export const OPEN_DELIVERY_ITEM_UNAVAILABLE: OpenDeliveryMerchantReasonCode = "UNAVAILABLE_ITEM";
|