@12-apps/marketplace 1.19.0 → 1.19.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/README.md +1 -1
- package/dist/ifood/index.js +28 -23
- package/dist/ifood/index.js.map +1 -1
- package/package.json +1 -1
- package/src/ifood/catalogs.ts +17 -8
- package/src/ifood/catalogue.ts +31 -22
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@12-apps/marketplace",
|
|
3
|
-
"version": "1.19.
|
|
3
|
+
"version": "1.19.1",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"sideEffects": false,
|
|
6
6
|
"description": "One marketplace integration engine for every marketplace: the durable inbox, the ingest pipeline, the acceptance clock and its sweep, the SKU map, the integration settings with sealed credentials, and the outbound engine (confirm, cancellation request, retries), behind an adapter port. Each marketplace is an adapter at its own subpath (./ifood, ./99food and ./keeta on Open Delivery, ./rappi). Framework-neutral wiring route descriptors (./server) with a Hono adapter (./hono), dep-free job blueprints, MCP tools and the package-owned Prisma partial + migrations.",
|
package/src/ifood/catalogs.ts
CHANGED
|
@@ -7,13 +7,18 @@
|
|
|
7
7
|
* "no Route matched with those values").
|
|
8
8
|
*
|
|
9
9
|
* The catalog is the one whose `context` holds `DEFAULT` (delivery), as the
|
|
10
|
-
* Fundamentals page names the contexts (DEFAULT, WHITELABEL, INDOOR).
|
|
10
|
+
* Fundamentals page names the contexts (DEFAULT, WHITELABEL, INDOOR). When
|
|
11
|
+
* more than one does, the first with `status: "AVAILABLE"` wins, else the
|
|
12
|
+
* first of them (FUT-6257; no answer with two has been seen, this is hardening).
|
|
11
13
|
* Reproduced live against iFood's sandbox on 2026-12-03 (FUT-6146): the list
|
|
12
14
|
* answered `[{ catalogId, context: ["DEFAULT"], status: "AVAILABLE", … }]`
|
|
13
15
|
* and `…/catalogs/{catalogId}/categories` answered the categories.
|
|
14
16
|
*/
|
|
15
17
|
|
|
16
|
-
/**
|
|
18
|
+
/**
|
|
19
|
+
* The DEFAULT catalog's id in a `GET /catalogs` answer: the first AVAILABLE
|
|
20
|
+
* one, else the first one; null when the answer names none.
|
|
21
|
+
*/
|
|
17
22
|
export function defaultCatalogIdIn(body: string): string | null {
|
|
18
23
|
let parsed: unknown;
|
|
19
24
|
try {
|
|
@@ -22,12 +27,16 @@ export function defaultCatalogIdIn(body: string): string | null {
|
|
|
22
27
|
return null;
|
|
23
28
|
}
|
|
24
29
|
if (!Array.isArray(parsed)) return null;
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
30
|
+
const defaults = (parsed as unknown[]).map(defaultEntryOf).filter((entry) => entry !== null);
|
|
31
|
+
return (defaults.find((entry) => entry.available) ?? defaults[0])?.catalogId ?? null;
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
/** A `GET /catalogs` entry whose `context` holds DEFAULT, with whether iFood answers it AVAILABLE; null otherwise. */
|
|
35
|
+
function defaultEntryOf(entry: unknown): { catalogId: string; available: boolean } | null {
|
|
36
|
+
if (typeof entry !== "object" || entry === null) return null;
|
|
37
|
+
const { catalogId, context, status } = entry as { catalogId?: unknown; context?: unknown; status?: unknown };
|
|
38
|
+
if (typeof catalogId !== "string" || catalogId === "" || !Array.isArray(context) || !context.includes("DEFAULT")) return null;
|
|
39
|
+
return { catalogId, available: status === "AVAILABLE" };
|
|
31
40
|
}
|
|
32
41
|
|
|
33
42
|
/** The categories path of a catalog, relative to the merchant's base. */
|
package/src/ifood/catalogue.ts
CHANGED
|
@@ -135,36 +135,35 @@ async function findCategoryIn(ctx: CatalogueCallContext, catalogId: string, name
|
|
|
135
135
|
return { id: categoryIdIn(listed.body, name) };
|
|
136
136
|
}
|
|
137
137
|
|
|
138
|
-
/** The category named `name` in the merchant's DEFAULT catalog, or null; the failure otherwise. */
|
|
139
|
-
async function findCategory(ctx: CatalogueCallContext, name: string): Promise<{ id: string | null } | Failure> {
|
|
140
|
-
const catalog = await catalogOf(ctx);
|
|
141
|
-
return "kind" in catalog ? catalog : findCategoryIn(ctx, catalog.catalogId, name);
|
|
142
|
-
}
|
|
143
|
-
|
|
144
138
|
/**
|
|
145
139
|
* Find the category by name, else create it, then read the list again and
|
|
146
140
|
* adopt the first of that name: a POST whose answer was lost and was retried
|
|
147
141
|
* is found rather than created twice. The engine runs this once per
|
|
148
|
-
* (connection, name) at a time and stores the id (`ctx.category`).
|
|
142
|
+
* (connection, name) at a time and stores the id (`ctx.category`). A
|
|
143
|
+
* `catalogId` this publish already read is reused rather than read again.
|
|
149
144
|
*/
|
|
150
|
-
async function findOrCreateCategory(ctx: CatalogueCallContext, name: string): Promise<{ id: string } | Failure> {
|
|
151
|
-
const catalog = await catalogOf(ctx);
|
|
145
|
+
async function findOrCreateCategory(ctx: CatalogueCallContext, name: string, catalogId?: string): Promise<{ id: string } | Failure> {
|
|
146
|
+
const catalog = catalogId === undefined ? await catalogOf(ctx) : { catalogId };
|
|
152
147
|
if ("kind" in catalog) return catalog;
|
|
153
148
|
const found = await findCategoryIn(ctx, catalog.catalogId, name);
|
|
154
149
|
if ("kind" in found) return found;
|
|
155
|
-
|
|
156
|
-
|
|
150
|
+
return found.id ? { id: found.id } : createCategoryIn(ctx, catalog.catalogId, name);
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
/** Create the category in the catalog, then adopt the first of that name the list answers. */
|
|
154
|
+
async function createCategoryIn(ctx: CatalogueCallContext, catalogId: string, name: string): Promise<{ id: string } | Failure> {
|
|
155
|
+
const created = await call(ctx, "POST", categoriesPath(catalogId), { name, status: "AVAILABLE", template: "DEFAULT" });
|
|
157
156
|
if ("kind" in created) return created;
|
|
158
157
|
if (!ok(created)) return failureOf(created);
|
|
159
158
|
const id = (parse(created.body) as { id?: unknown } | null)?.id;
|
|
160
159
|
if (typeof id !== "string") return { kind: "retry", reason: "iFood created the category without an id" };
|
|
161
160
|
// ESTIMATED: the list answers categories in creation order, so the first match is the oldest.
|
|
162
|
-
const again = await findCategoryIn(ctx,
|
|
161
|
+
const again = await findCategoryIn(ctx, catalogId, name);
|
|
163
162
|
return "id" in again && again.id ? { id: again.id } : { id };
|
|
164
163
|
}
|
|
165
164
|
|
|
166
|
-
const categoryId = (ctx: CatalogueCallContext, name: string): Promise<{ id: string } | Failure> =>
|
|
167
|
-
ctx.category(name, () => findOrCreateCategory(ctx, name));
|
|
165
|
+
const categoryId = (ctx: CatalogueCallContext, name: string, catalogId?: string): Promise<{ id: string } | Failure> =>
|
|
166
|
+
ctx.category(name, () => findOrCreateCategory(ctx, name, catalogId));
|
|
168
167
|
|
|
169
168
|
/** The photo the item goes out with, and what the publish could not do about it. */
|
|
170
169
|
interface PhotoOutcome {
|
|
@@ -239,13 +238,21 @@ export function ifoodItemBody(item: CatalogueItemOut, category: string, image: s
|
|
|
239
238
|
* Whether a refused PUT was refused because the stored category is gone (the
|
|
240
239
|
* store deleted it on iFood's portal): iFood's 400 and 404 do not say which id
|
|
241
240
|
* was unknown, so the list is read again, and a category of that name with
|
|
242
|
-
* another id, or none at all, means the stored one is stale.
|
|
243
|
-
*
|
|
241
|
+
* another id, or none at all, means the stored one is stale. Answers the
|
|
242
|
+
* catalog it read when it is, so the find again reads no second `/catalogs`
|
|
243
|
+
* (FUT-6257); null otherwise. ESTIMATED that a deleted category answers 400 or 404.
|
|
244
244
|
*/
|
|
245
|
-
async function categoryWentStale(
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
245
|
+
async function categoryWentStale(
|
|
246
|
+
ctx: CatalogueCallContext,
|
|
247
|
+
put: MarketplaceHttpResponse,
|
|
248
|
+
name: string,
|
|
249
|
+
used: string,
|
|
250
|
+
): Promise<{ catalogId: string } | null> {
|
|
251
|
+
if (put.status !== 400 && put.status !== 404) return null;
|
|
252
|
+
const catalog = await catalogOf(ctx);
|
|
253
|
+
if ("kind" in catalog) return null;
|
|
254
|
+
const found = await findCategoryIn(ctx, catalog.catalogId, name);
|
|
255
|
+
return !("kind" in found) && found.id !== used ? catalog : null;
|
|
249
256
|
}
|
|
250
257
|
|
|
251
258
|
/** `PUT /items`, once more with the category found or created again when the stored one went stale. */
|
|
@@ -253,9 +260,11 @@ async function putItem(ctx: CatalogueCallContext, item: CatalogueItemOut, image:
|
|
|
253
260
|
const category = await categoryId(ctx, item.categoryName);
|
|
254
261
|
if ("kind" in category) return category;
|
|
255
262
|
const put = await call(ctx, "PUT", "items", ifoodItemBody(item, category.id, image));
|
|
256
|
-
if ("kind" in put || ok(put)
|
|
263
|
+
if ("kind" in put || ok(put)) return put;
|
|
264
|
+
const stale = await categoryWentStale(ctx, put, item.categoryName, category.id);
|
|
265
|
+
if (stale === null) return put;
|
|
257
266
|
await ctx.forgetCategory(item.categoryName);
|
|
258
|
-
const again = await categoryId(ctx, item.categoryName);
|
|
267
|
+
const again = await categoryId(ctx, item.categoryName, stale.catalogId);
|
|
259
268
|
if ("kind" in again) return again;
|
|
260
269
|
return call(ctx, "PUT", "items", ifoodItemBody(item, again.id, image));
|
|
261
270
|
}
|