@12-apps/marketplace 1.3.0 → 1.5.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 +47 -0
- package/README.md +122 -1
- package/dist/99food/index.d.ts +33 -2
- package/dist/99food/index.js +34 -2
- package/dist/99food/index.js.map +1 -1
- package/dist/{adapter-0l_EmuJA.d.ts → adapter-CCWMqt2g.d.ts} +275 -3
- package/dist/{chunk-XUAFXNOH.js → chunk-FKUEFVVA.js} +396 -4
- package/dist/chunk-FKUEFVVA.js.map +1 -0
- package/dist/{chunk-RVBTARIY.js → chunk-RGMSD7S7.js} +1 -1
- package/dist/chunk-RGMSD7S7.js.map +1 -0
- package/dist/{chunk-AYYBRQJV.js → chunk-UONWZIBO.js} +43 -3
- package/dist/chunk-UONWZIBO.js.map +1 -0
- package/dist/chunk-WN2EK4KX.js +15 -0
- package/dist/chunk-WN2EK4KX.js.map +1 -0
- package/dist/{engine-CZsP8KWd.d.ts → engine-C3uq_BJd.d.ts} +252 -2
- package/dist/hono/index.d.ts +2 -2
- package/dist/hono/index.js +2 -2
- package/dist/ifood/index.d.ts +44 -2
- package/dist/ifood/index.js +441 -1
- package/dist/ifood/index.js.map +1 -1
- package/dist/index.d.ts +3 -3
- package/dist/index.js +342 -3
- package/dist/index.js.map +1 -1
- package/dist/manifest/server.d.ts +4 -2
- package/dist/manifest/server.js +2 -2
- package/dist/rappi/index.d.ts +15 -2
- package/dist/rappi/index.js +56 -2
- package/dist/rappi/index.js.map +1 -1
- package/dist/server/index.d.ts +18 -7
- package/dist/server/index.js +4 -2
- package/package.json +1 -1
- package/prisma/marketplace.prisma +67 -1
- package/prisma/migrations/20261113230000_marketplace_store_pause/migration.sql +70 -0
- package/src/99food/index.ts +10 -0
- package/src/99food/pause.ts +56 -0
- package/src/core/adapter.ts +19 -2
- package/src/core/catalogue-read-port.ts +151 -0
- package/src/core/catalogue-read.ts +51 -0
- package/src/core/engine.ts +46 -0
- 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 +11 -0
- package/src/ifood/catalogue-read.ts +187 -0
- package/src/ifood/catalogue-snapshot.ts +246 -0
- package/src/ifood/index.ts +9 -0
- package/src/ifood/pause.ts +257 -0
- package/src/index.ts +39 -0
- package/src/rappi/index.ts +11 -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 +64 -4
- package/dist/chunk-AYYBRQJV.js.map +0 -1
- package/dist/chunk-RVBTARIY.js.map +0 -1
- package/dist/chunk-XUAFXNOH.js.map +0 -1
|
@@ -0,0 +1,187 @@
|
|
|
1
|
+
import { outboundResultOf, type MarketplaceHttpResponse, type OutboundResult } from "../core/adapter";
|
|
2
|
+
import type {
|
|
3
|
+
CatalogueReadContext,
|
|
4
|
+
CatalogueReader,
|
|
5
|
+
CatalogueReadNotice,
|
|
6
|
+
CatalogueReadResult,
|
|
7
|
+
CatalogueSnapshotCategory,
|
|
8
|
+
CatalogueSnapshotItem,
|
|
9
|
+
} from "../core/catalogue-read-port";
|
|
10
|
+
import { availableOf, entriesOf, indexOf, itemOf, record, text } from "./catalogue-snapshot";
|
|
11
|
+
import { IFOOD_API, bearer, ifoodTokenKey } from "./outbound";
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* The iFood adapter's catalogue READ (FUT-951): the menu a store already keeps
|
|
15
|
+
* on iFood, read through the store's OWN connection (its merchant id, and the
|
|
16
|
+
* client-credentials token of its app, `bearer`), for the host to import.
|
|
17
|
+
*
|
|
18
|
+
* One read is, in order:
|
|
19
|
+
* 1. `GET /categories` — the merchant's categories (CONFIRMED, the Endpoints
|
|
20
|
+
* page as read 2026-11-13; ESTIMATED that it answers an array of
|
|
21
|
+
* `{ id, name, status, externalCode }`, the same estimate the push makes);
|
|
22
|
+
* 2. `GET /categories/{categoryId}/items` for each, in the list's order
|
|
23
|
+
* (CONFIRMED, the Introduction's step 5, read 2026-11-16), mapped by
|
|
24
|
+
* `./catalogue-snapshot.ts`.
|
|
25
|
+
*
|
|
26
|
+
* Pagination: none. The pages read show no page, size or cursor parameter on
|
|
27
|
+
* either call (ESTIMATED that there is none), so the menu is read one category
|
|
28
|
+
* per call, which bounds each answer by a category's size.
|
|
29
|
+
*
|
|
30
|
+
* Failures:
|
|
31
|
+
* - a 401 drops the cached token and asks once more with a fresh one (as the
|
|
32
|
+
* push's `call` does); a second 401 is a `retry`;
|
|
33
|
+
* - the token grant refused (400, 401, 403) is `CREDENTIALS`;
|
|
34
|
+
* - 403 (the Errors page: the app is not allowed this merchant, or the
|
|
35
|
+
* catalogue module is not enabled for it) and 404 on the category list (no
|
|
36
|
+
* such merchant for this app, ESTIMATED) are `NOT_AUTHORIZED`;
|
|
37
|
+
* - 429, 408, 425, 5xx and a call with no answer are `retry`;
|
|
38
|
+
* - any other 4xx is `REFUSED`, with iFood's words.
|
|
39
|
+
* A category whose own items answer 404 (deleted between the two calls), or
|
|
40
|
+
* answer in a shape other than `{ items: [...] }`, is left out with a
|
|
41
|
+
* CATEGORY_UNREADABLE notice rather than failing the read; an entry of a list
|
|
42
|
+
* that is not an object is a notice too. Only a category LIST that is not a
|
|
43
|
+
* list refuses the read (REFUSED): without it there is no menu to keep.
|
|
44
|
+
*/
|
|
45
|
+
|
|
46
|
+
const catalogUrl = (merchantId: string, path: string): string =>
|
|
47
|
+
`${IFOOD_API}/catalog/v2.0/merchants/${encodeURIComponent(merchantId)}/${path}`;
|
|
48
|
+
|
|
49
|
+
type Failure = Exclude<CatalogueReadResult, { kind: "snapshot" }>;
|
|
50
|
+
|
|
51
|
+
/** The token grant's failure: a refusal is the store's credentials, anything else is asked again. */
|
|
52
|
+
function tokenFailure(result: OutboundResult): Failure {
|
|
53
|
+
if (result.kind === "refused") return { kind: "refused", code: "CREDENTIALS", status: result.status, message: result.message };
|
|
54
|
+
return { kind: "retry", reason: result.kind === "retry" ? result.reason : "unexpected token answer" };
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
/** A non-2xx catalogue answer as the failure it is. */
|
|
58
|
+
function failureOf(response: MarketplaceHttpResponse): Failure {
|
|
59
|
+
const result = outboundResultOf(response);
|
|
60
|
+
if (result.kind !== "refused") return { kind: "retry", reason: result.kind === "retry" ? result.reason : "unexpected answer" };
|
|
61
|
+
const code = response.status === 403 || response.status === 404 ? "NOT_AUTHORIZED" : "REFUSED";
|
|
62
|
+
return { kind: "refused", code, status: result.status, message: result.message };
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
/** One authenticated GET: iFood's answer (any status but 401), or the failure that ends the read. */
|
|
66
|
+
async function get(ctx: CatalogueReadContext, path: string): Promise<MarketplaceHttpResponse | Failure> {
|
|
67
|
+
for (let attempt = 0; attempt < 2; attempt += 1) {
|
|
68
|
+
const auth = await bearer(ctx);
|
|
69
|
+
if (!("token" in auth)) return tokenFailure(auth);
|
|
70
|
+
const response = await ctx.http({
|
|
71
|
+
method: "GET",
|
|
72
|
+
url: catalogUrl(ctx.externalMerchantId, path),
|
|
73
|
+
headers: { Authorization: `Bearer ${auth.token}`, Accept: "application/json" },
|
|
74
|
+
});
|
|
75
|
+
if (response.status !== 401) return response;
|
|
76
|
+
// A token that went stale under us: forget it, and the next turn grants another.
|
|
77
|
+
ctx.tokens.delete(ifoodTokenKey(ctx));
|
|
78
|
+
}
|
|
79
|
+
return { kind: "retry", reason: "iFood refused a fresh token" };
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
const ok = (response: MarketplaceHttpResponse): boolean => response.status >= 200 && response.status < 300;
|
|
83
|
+
|
|
84
|
+
function parse(body: string): unknown {
|
|
85
|
+
try {
|
|
86
|
+
return JSON.parse(body) as unknown;
|
|
87
|
+
} catch {
|
|
88
|
+
return null;
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
const unexpected = (what: string, body: string): Failure => ({
|
|
93
|
+
kind: "refused",
|
|
94
|
+
code: "REFUSED",
|
|
95
|
+
status: 200,
|
|
96
|
+
message: `unexpected ${what} answer: ${body.slice(0, 200)}`,
|
|
97
|
+
});
|
|
98
|
+
|
|
99
|
+
/** One category's items, or the notice that leaves the category out, or the failure that ends the read. */
|
|
100
|
+
async function categoryOf(
|
|
101
|
+
ctx: CatalogueReadContext,
|
|
102
|
+
raw: Record<string, unknown>,
|
|
103
|
+
notices: CatalogueReadNotice[],
|
|
104
|
+
): Promise<CatalogueSnapshotCategory | null | Failure> {
|
|
105
|
+
const id = text(raw.id);
|
|
106
|
+
const name = text(raw.name);
|
|
107
|
+
if (id === null || name === null) {
|
|
108
|
+
notices.push(unreadableCategory(id, raw));
|
|
109
|
+
return null;
|
|
110
|
+
}
|
|
111
|
+
const answered = await get(ctx, `categories/${encodeURIComponent(id)}/items`);
|
|
112
|
+
if ("kind" in answered) return answered;
|
|
113
|
+
if (answered.status === 404) {
|
|
114
|
+
notices.push(unreadableCategory(id, "its items answered 404"));
|
|
115
|
+
return null;
|
|
116
|
+
}
|
|
117
|
+
if (!ok(answered)) return failureOf(answered);
|
|
118
|
+
const items = itemsOf(answered.body, id, notices);
|
|
119
|
+
return items && { externalId: id, externalCode: text(raw.externalCode), name, available: availableOf(raw.status), items };
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
const unreadableCategory = (categoryExternalId: string | null, detail: unknown): CatalogueReadNotice => ({
|
|
123
|
+
code: "CATEGORY_UNREADABLE",
|
|
124
|
+
categoryExternalId,
|
|
125
|
+
externalId: null,
|
|
126
|
+
detail: (typeof detail === "string" ? detail : JSON.stringify(detail) ?? String(detail)).slice(0, 300),
|
|
127
|
+
});
|
|
128
|
+
|
|
129
|
+
/**
|
|
130
|
+
* One category's items, each one it cannot read an ITEM_UNREADABLE notice; or
|
|
131
|
+
* null, with a CATEGORY_UNREADABLE notice, when the answer is not the expected
|
|
132
|
+
* `{ items: [...] }` (ESTIMATED: the PUT's four lists, `items` plural). A
|
|
133
|
+
* notice rather than refusing the whole read: one category answered in an odd
|
|
134
|
+
* shape keeps the rest of the menu importable, and the host shows which
|
|
135
|
+
* category did not come over; a shape iFood changed everywhere leaves a notice
|
|
136
|
+
* on every category, never a silent empty menu.
|
|
137
|
+
*/
|
|
138
|
+
function itemsOf(rawBody: string, categoryId: string, notices: CatalogueReadNotice[]): CatalogueSnapshotItem[] | null {
|
|
139
|
+
const body = record(parse(rawBody));
|
|
140
|
+
if (!body || !Array.isArray(body.items)) {
|
|
141
|
+
notices.push(unreadableCategory(categoryId, `unexpected items answer: ${rawBody}`));
|
|
142
|
+
return null;
|
|
143
|
+
}
|
|
144
|
+
const index = indexOf(body);
|
|
145
|
+
const { entries, others } = entriesOf(body.items);
|
|
146
|
+
for (const other of others) {
|
|
147
|
+
notices.push({ code: "ITEM_UNREADABLE", categoryExternalId: categoryId, externalId: null, detail: String(JSON.stringify(other)).slice(0, 300) });
|
|
148
|
+
}
|
|
149
|
+
const items: CatalogueSnapshotItem[] = [];
|
|
150
|
+
for (const entry of entries) {
|
|
151
|
+
const mapped = itemOf(entry, index, categoryId);
|
|
152
|
+
if ("code" in mapped) notices.push(mapped);
|
|
153
|
+
else items.push(mapped);
|
|
154
|
+
}
|
|
155
|
+
return items;
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
async function read(ctx: CatalogueReadContext): Promise<CatalogueReadResult> {
|
|
159
|
+
const listed = await get(ctx, "categories");
|
|
160
|
+
if ("kind" in listed) return listed;
|
|
161
|
+
if (!ok(listed)) return failureOf(listed);
|
|
162
|
+
const parsed = parse(listed.body);
|
|
163
|
+
if (!Array.isArray(parsed)) return unexpected("category list", listed.body);
|
|
164
|
+
const categories: CatalogueSnapshotCategory[] = [];
|
|
165
|
+
const notices: CatalogueReadNotice[] = [];
|
|
166
|
+
const { entries, others } = entriesOf(parsed);
|
|
167
|
+
// A category entry that is not an object is said, never skipped.
|
|
168
|
+
for (const other of others) notices.push(unreadableCategory(null, other));
|
|
169
|
+
for (const raw of entries) {
|
|
170
|
+
const category = await categoryOf(ctx, raw, notices);
|
|
171
|
+
if (category === null) continue;
|
|
172
|
+
if ("kind" in category) return category;
|
|
173
|
+
categories.push(category);
|
|
174
|
+
}
|
|
175
|
+
return { kind: "snapshot", snapshot: { categories, notices } };
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
/** iFood's catalogue read. */
|
|
179
|
+
export const IFOOD_CATALOGUE_READER: CatalogueReader = {
|
|
180
|
+
read: async (ctx) => {
|
|
181
|
+
try {
|
|
182
|
+
return await read(ctx);
|
|
183
|
+
} catch (error) {
|
|
184
|
+
return { kind: "retry", reason: error instanceof Error ? error.message : String(error) };
|
|
185
|
+
}
|
|
186
|
+
},
|
|
187
|
+
};
|
|
@@ -0,0 +1,246 @@
|
|
|
1
|
+
import {
|
|
2
|
+
centsFromMajor,
|
|
3
|
+
type CatalogueReadNotice,
|
|
4
|
+
type CatalogueReadWarning,
|
|
5
|
+
type CatalogueSnapshotItem,
|
|
6
|
+
type CatalogueSnapshotOption,
|
|
7
|
+
type CatalogueSnapshotOptionGroup,
|
|
8
|
+
} from "../core/catalogue-read-port";
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* iFood's catalogue answer mapped into the engine's {@link CatalogueSnapshotItem}
|
|
12
|
+
* (FUT-951). Pure and offline: `./catalogue-read.ts` makes the calls.
|
|
13
|
+
*
|
|
14
|
+
* The shapes are the Catalog API v2.0's `PUT /items` payload, which the
|
|
15
|
+
* portal's Introduction shows whole (read 2026-11-16): an `item` (id, type,
|
|
16
|
+
* categoryId, status, `price { value }`, externalCode), `products` (id, name,
|
|
17
|
+
* description, externalCode, and `optionGroups: [{ id, min, max }]` linking a
|
|
18
|
+
* product to its groups), `optionGroups` (id, name, status, `optionIds`) and
|
|
19
|
+
* `options` (id, `productId` naming the product that holds its name, status,
|
|
20
|
+
* `price { value }`). The package README's "Catalogue reading" table marks
|
|
21
|
+
* each fact CONFIRMED with its URL or ESTIMATED. ESTIMATED here, and said
|
|
22
|
+
* beside each use:
|
|
23
|
+
* - that `GET /categories/{categoryId}/items` answers the same four lists
|
|
24
|
+
* (`items` plural) the PUT takes;
|
|
25
|
+
* - that `item.productId` names the item's product (the field the push sends);
|
|
26
|
+
* - that `products[].imagePath` holds the photo, a path under
|
|
27
|
+
* {@link IFOOD_IMAGE_BASE_URL}.
|
|
28
|
+
*/
|
|
29
|
+
|
|
30
|
+
/**
|
|
31
|
+
* Where iFood serves a catalogue photo's `imagePath`. ESTIMATED: the Catalog
|
|
32
|
+
* pages read name `imagePath` but not its host; this is the host iFood's own
|
|
33
|
+
* app loads dish photos from. A path that is already an absolute https URL is
|
|
34
|
+
* used as it is.
|
|
35
|
+
*/
|
|
36
|
+
export const IFOOD_IMAGE_BASE_URL = "https://static-images.ifood.com.br/image/upload/t_high/pratos/";
|
|
37
|
+
|
|
38
|
+
type Json = Record<string, unknown>;
|
|
39
|
+
|
|
40
|
+
export const record = (value: unknown): Json | null =>
|
|
41
|
+
typeof value === "object" && value !== null && !Array.isArray(value) ? (value as Json) : null;
|
|
42
|
+
|
|
43
|
+
export const text = (value: unknown): string | null => (typeof value === "string" && value.trim() !== "" ? value.trim() : null);
|
|
44
|
+
|
|
45
|
+
/**
|
|
46
|
+
* The objects of a JSON array, and every other entry it held (a string, a
|
|
47
|
+
* number, a nested array), so the caller can SAY what it could not read rather
|
|
48
|
+
* than lose it. Not an array: no entries, nothing else.
|
|
49
|
+
*/
|
|
50
|
+
export function entriesOf(value: unknown): { entries: Json[]; others: unknown[] } {
|
|
51
|
+
const entries: Json[] = [];
|
|
52
|
+
const others: unknown[] = [];
|
|
53
|
+
for (const entry of Array.isArray(value) ? value : []) {
|
|
54
|
+
const object = record(entry);
|
|
55
|
+
if (object) entries.push(object);
|
|
56
|
+
else others.push(entry);
|
|
57
|
+
}
|
|
58
|
+
return { entries, others };
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
/**
|
|
62
|
+
* The lookup lists (`products`, `optionGroups`, `options`) keep only objects
|
|
63
|
+
* with an id: an entry is only ever reached through an id that names it, and a
|
|
64
|
+
* reference that finds nothing is warned about where it is made
|
|
65
|
+
* (OPTION_GROUP_UNREADABLE, OPTION_UNREADABLE, or the item's ITEM_UNREADABLE).
|
|
66
|
+
*/
|
|
67
|
+
const list = (value: unknown): Json[] => entriesOf(value).entries;
|
|
68
|
+
|
|
69
|
+
/** iFood's `status`: anything but UNAVAILABLE is on sale (an absent status included). */
|
|
70
|
+
export const availableOf = (status: unknown): boolean => status !== "UNAVAILABLE";
|
|
71
|
+
|
|
72
|
+
/** The four lists of one category's answer, each by id. */
|
|
73
|
+
interface IfoodCatalogueIndex {
|
|
74
|
+
products: Map<string, Json>;
|
|
75
|
+
optionGroups: Map<string, Json>;
|
|
76
|
+
options: Map<string, Json>;
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
const byId = (entries: Json[]): Map<string, Json> =>
|
|
80
|
+
new Map(entries.flatMap((entry) => (text(entry.id) ? [[text(entry.id) as string, entry] as const] : [])));
|
|
81
|
+
|
|
82
|
+
export function indexOf(answer: Json): IfoodCatalogueIndex {
|
|
83
|
+
return { products: byId(list(answer.products)), optionGroups: byId(list(answer.optionGroups)), options: byId(list(answer.options)) };
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
const priceCentsOf = (price: unknown): number | null => centsFromMajor(record(price)?.value);
|
|
87
|
+
|
|
88
|
+
const warning = (code: CatalogueReadWarning["code"], subject: string | null, detail: unknown = null): CatalogueReadWarning => ({
|
|
89
|
+
code,
|
|
90
|
+
subject,
|
|
91
|
+
detail: detail === null || detail === undefined ? null : JSON.stringify(detail),
|
|
92
|
+
});
|
|
93
|
+
|
|
94
|
+
/** The photo's absolute URL; a warning when a reference is there but cannot be resolved. */
|
|
95
|
+
export function photoOf(imagePath: unknown, subject: string | null): { url: string | null; warning?: CatalogueReadWarning } {
|
|
96
|
+
if (imagePath === undefined || imagePath === null || imagePath === "") return { url: null };
|
|
97
|
+
const path = text(imagePath);
|
|
98
|
+
if (path === null) return { url: null, warning: warning("PHOTO_UNREADABLE", subject, imagePath) };
|
|
99
|
+
if (/^https:\/\//i.test(path)) return { url: path };
|
|
100
|
+
if (/^[a-z][a-z0-9+.-]*:/i.test(path)) return { url: null, warning: warning("PHOTO_UNREADABLE", subject, path) };
|
|
101
|
+
return { url: `${IFOOD_IMAGE_BASE_URL}${urlSafePath(path.replace(/^\/+/, ""))}` };
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
/**
|
|
105
|
+
* The path as a URL path: kept as it is when it is already one (an encoded
|
|
106
|
+
* `%20` stays `%20`, never `%2520`); only a character a URL may not carry
|
|
107
|
+
* (a space, a quote, a non-ASCII letter) is encoded, and a `%` that starts no
|
|
108
|
+
* escape becomes `%25`.
|
|
109
|
+
*/
|
|
110
|
+
function urlSafePath(path: string): string {
|
|
111
|
+
return path
|
|
112
|
+
.replace(/%(?![0-9A-Fa-f]{2})/g, "%25")
|
|
113
|
+
.replace(/[^A-Za-z0-9\-._~!$&'()*+,;=:@/%]/g, (character) => encodeURIComponent(character));
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
/** The words and photo of an item or option: its product's (`productId`), else its own. */
|
|
117
|
+
interface Described {
|
|
118
|
+
name: string | null;
|
|
119
|
+
description: string | null;
|
|
120
|
+
externalCode: string | null;
|
|
121
|
+
imagePath: unknown;
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
function describedBy(index: IfoodCatalogueIndex, own: Json): Described {
|
|
125
|
+
const product = index.products.get(text(own.productId) ?? "") ?? {};
|
|
126
|
+
return {
|
|
127
|
+
name: text(product.name) ?? text(own.name),
|
|
128
|
+
description: text(product.description) ?? text(own.description),
|
|
129
|
+
externalCode: text(own.externalCode) ?? text(product.externalCode),
|
|
130
|
+
imagePath: product.imagePath ?? own.imagePath,
|
|
131
|
+
};
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
/** The photo's URL, its warning (if any) pushed onto `warnings`. */
|
|
135
|
+
function photoInto(imagePath: unknown, subject: string | null, warnings: CatalogueReadWarning[]): string | null {
|
|
136
|
+
const photo = photoOf(imagePath, subject);
|
|
137
|
+
if (photo.warning) warnings.push(photo.warning);
|
|
138
|
+
return photo.url;
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
/** One option of a group, or the warning that leaves it out. */
|
|
142
|
+
function optionOf(id: string, index: IfoodCatalogueIndex, warnings: CatalogueReadWarning[]): CatalogueSnapshotOption | null {
|
|
143
|
+
const option = index.options.get(id);
|
|
144
|
+
const described = option ? describedBy(index, option) : null;
|
|
145
|
+
if (!option || !described || described.name === null) {
|
|
146
|
+
warnings.push(warning("OPTION_UNREADABLE", id, option));
|
|
147
|
+
return null;
|
|
148
|
+
}
|
|
149
|
+
const priceDeltaCents = priceCentsOf(option.price);
|
|
150
|
+
if (priceDeltaCents === null) warnings.push(warning("OPTION_PRICE_MISSING", id, option.price));
|
|
151
|
+
return {
|
|
152
|
+
externalId: id,
|
|
153
|
+
externalCode: described.externalCode,
|
|
154
|
+
name: described.name,
|
|
155
|
+
description: described.description,
|
|
156
|
+
priceDeltaCents,
|
|
157
|
+
photoUrl: photoInto(described.imagePath, id, warnings),
|
|
158
|
+
available: availableOf(option.status),
|
|
159
|
+
};
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
/** A group's bounds, when they make sense: whole numbers, 0 <= min <= max, max >= 1. */
|
|
163
|
+
function boundsOf(link: Json, group: Json): { min: number; max: number } | null {
|
|
164
|
+
const min = link.min ?? group.min ?? 0;
|
|
165
|
+
const max = link.max ?? group.max;
|
|
166
|
+
if (!Number.isInteger(min) || !Number.isInteger(max)) return null;
|
|
167
|
+
const [low, high] = [min as number, max as number];
|
|
168
|
+
return low >= 0 && high >= 1 && high >= low ? { min: low, max: high } : null;
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
/** A group's option ids; an entry that is not an id is an OPTION_UNREADABLE warning, never dropped. */
|
|
172
|
+
function idsOf(groupId: string, value: unknown, warnings: CatalogueReadWarning[]): string[] {
|
|
173
|
+
const ids: string[] = [];
|
|
174
|
+
for (const entry of Array.isArray(value) ? value : []) {
|
|
175
|
+
const id = text(entry);
|
|
176
|
+
if (id === null) warnings.push(warning("OPTION_UNREADABLE", groupId, entry));
|
|
177
|
+
else ids.push(id);
|
|
178
|
+
}
|
|
179
|
+
return ids;
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
/** One group a product links to (`products[].optionGroups[]`), or the warning that leaves it out. */
|
|
183
|
+
function groupOf(link: Json, index: IfoodCatalogueIndex, warnings: CatalogueReadWarning[]): CatalogueSnapshotOptionGroup | null {
|
|
184
|
+
const id = text(link.id);
|
|
185
|
+
const group = id === null ? undefined : index.optionGroups.get(id);
|
|
186
|
+
const bounds = group ? boundsOf(link, group) : null;
|
|
187
|
+
const name = text(group?.name);
|
|
188
|
+
if (id === null || !group || name === null || bounds === null) {
|
|
189
|
+
warnings.push(warning("OPTION_GROUP_UNREADABLE", id, { link, group }));
|
|
190
|
+
return null;
|
|
191
|
+
}
|
|
192
|
+
const options = idsOf(id, group.optionIds, warnings).flatMap((optionId) => optionOf(optionId, index, warnings) ?? []);
|
|
193
|
+
if (options.length === 0) {
|
|
194
|
+
warnings.push(warning("OPTION_GROUP_UNREADABLE", id, "no readable option"));
|
|
195
|
+
return null;
|
|
196
|
+
}
|
|
197
|
+
return { externalId: id, externalCode: text(group.externalCode), name, ...bounds, available: availableOf(group.status), options };
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
/** The item's own warnings: a kind it maps only partly, and a missing price. */
|
|
201
|
+
function itemWarnings(raw: Json, priceCents: number | null): CatalogueReadWarning[] {
|
|
202
|
+
const warnings: CatalogueReadWarning[] = [];
|
|
203
|
+
const type = text(raw.type);
|
|
204
|
+
if (type !== null && type !== "DEFAULT") warnings.push(warning("ITEM_TYPE_UNSUPPORTED", null, type));
|
|
205
|
+
if (priceCents === null) warnings.push(warning("PRICE_MISSING", null, raw.price));
|
|
206
|
+
return warnings;
|
|
207
|
+
}
|
|
208
|
+
|
|
209
|
+
/**
|
|
210
|
+
* The group links of an item: on its product (CONFIRMED, the PUT example),
|
|
211
|
+
* else on the item itself. Links that are not objects, or a value that is not
|
|
212
|
+
* a list at all, are an OPTION_GROUP_UNREADABLE warning each.
|
|
213
|
+
*/
|
|
214
|
+
function linksOf(index: IfoodCatalogueIndex, raw: Json, warnings: CatalogueReadWarning[]): Json[] {
|
|
215
|
+
const product = index.products.get(text(raw.productId) ?? "");
|
|
216
|
+
const value = product?.optionGroups ?? raw.optionGroups;
|
|
217
|
+
if (value !== undefined && value !== null && !Array.isArray(value)) warnings.push(warning("OPTION_GROUP_UNREADABLE", null, value));
|
|
218
|
+
const { entries, others } = entriesOf(value);
|
|
219
|
+
for (const other of others) warnings.push(warning("OPTION_GROUP_UNREADABLE", null, other));
|
|
220
|
+
return entries;
|
|
221
|
+
}
|
|
222
|
+
|
|
223
|
+
/** One item of a category, or the notice that says why it could not be read. */
|
|
224
|
+
export function itemOf(raw: Json, index: IfoodCatalogueIndex, categoryId: string): CatalogueSnapshotItem | CatalogueReadNotice {
|
|
225
|
+
const id = text(raw.id);
|
|
226
|
+
// ESTIMATED: `item.productId` names the product that holds the item's name, description and photo.
|
|
227
|
+
const described = describedBy(index, raw);
|
|
228
|
+
if (id === null || described.name === null) {
|
|
229
|
+
return { code: "ITEM_UNREADABLE", categoryExternalId: categoryId, externalId: id, detail: JSON.stringify(raw).slice(0, 300) };
|
|
230
|
+
}
|
|
231
|
+
const priceCents = priceCentsOf(raw.price);
|
|
232
|
+
const warnings = itemWarnings(raw, priceCents);
|
|
233
|
+
const photoUrl = photoInto(described.imagePath, null, warnings);
|
|
234
|
+
const optionGroups = linksOf(index, raw, warnings).flatMap((link) => groupOf(link, index, warnings) ?? []);
|
|
235
|
+
return {
|
|
236
|
+
externalId: id,
|
|
237
|
+
externalCode: described.externalCode,
|
|
238
|
+
name: described.name,
|
|
239
|
+
description: described.description,
|
|
240
|
+
priceCents,
|
|
241
|
+
photoUrl,
|
|
242
|
+
available: availableOf(raw.status),
|
|
243
|
+
optionGroups,
|
|
244
|
+
warnings,
|
|
245
|
+
};
|
|
246
|
+
}
|
package/src/ifood/index.ts
CHANGED
|
@@ -1,8 +1,10 @@
|
|
|
1
1
|
import type { CancellationReason, MarketplaceAdapter } from "../core/adapter";
|
|
2
2
|
import { IFOOD_REASON_CODES, type IfoodAdapterCopy, type IfoodReasonCode } from "./copy";
|
|
3
3
|
import { IFOOD_CATALOGUE } from "./catalogue";
|
|
4
|
+
import { IFOOD_CATALOGUE_READER } from "./catalogue-read";
|
|
4
5
|
import { normalizeIfoodEvent, verifyIfoodSignature } from "./normalize";
|
|
5
6
|
import { IFOOD_PORTAL_URL, ifoodConfirm, ifoodRequestCancellation } from "./outbound";
|
|
7
|
+
import { IFOOD_AVAILABILITY } from "./pause";
|
|
6
8
|
|
|
7
9
|
/**
|
|
8
10
|
* `@12-apps/marketplace/ifood` — iFood, as one adapter on the engine.
|
|
@@ -65,8 +67,12 @@ export function createIfoodAdapter(config: IfoodAdapterConfig): MarketplaceAdapt
|
|
|
65
67
|
cancellationReasons: () => reasons,
|
|
66
68
|
// The Catalog API v2.0: each item is written and paused through the API (FUT-4768).
|
|
67
69
|
catalogue: IFOOD_CATALOGUE,
|
|
70
|
+
// The same API read back: the menu the store keeps on iFood, for the host to import (FUT-951).
|
|
71
|
+
catalogueReader: IFOOD_CATALOGUE_READER,
|
|
68
72
|
confirm: (ctx, order) => ifoodConfirm(ctx, order),
|
|
69
73
|
requestCancellation: ifoodRequestCancellation,
|
|
74
|
+
// The Merchant API's interruptions: an id and an end per closure (FUT-4851).
|
|
75
|
+
availability: IFOOD_AVAILABILITY,
|
|
70
76
|
// No per-order deep link is known, so the portal, where a live order is
|
|
71
77
|
// on the first screen.
|
|
72
78
|
appLink: () => IFOOD_PORTAL_URL,
|
|
@@ -76,3 +82,6 @@ export function createIfoodAdapter(config: IfoodAdapterConfig): MarketplaceAdapt
|
|
|
76
82
|
export { IFOOD_REASON_CODES, type IfoodAdapterCopy, type IfoodReasonCode } from "./copy";
|
|
77
83
|
export { normalizeIfoodEvent, verifyIfoodSignature } from "./normalize";
|
|
78
84
|
export { IFOOD_CATALOGUE, ifoodItemBody, ifoodItemId, ifoodProductId } from "./catalogue";
|
|
85
|
+
export { IFOOD_CATALOGUE_READER } from "./catalogue-read";
|
|
86
|
+
export { IFOOD_IMAGE_BASE_URL } from "./catalogue-snapshot";
|
|
87
|
+
export { IFOOD_AVAILABILITY, ifoodLocalTime, ifoodPause, ifoodResume } from "./pause";
|