@12-apps/marketplace 1.8.0 → 1.10.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 +27 -1
- package/README.md +103 -9
- 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-BMCERPQX.js → chunk-2BA2EHNF.js} +16 -6
- package/dist/chunk-2BA2EHNF.js.map +1 -0
- package/dist/chunk-PYHNM47K.js +478 -0
- package/dist/chunk-PYHNM47K.js.map +1 -0
- package/dist/{chunk-6N2HEV3U.js → chunk-WVZ2IXFH.js} +3 -3
- package/dist/{chunk-NP4EYKPO.js → chunk-ZO4S3LF4.js} +5 -3
- package/dist/chunk-ZO4S3LF4.js.map +1 -0
- package/dist/client-B4EQCRD_.d.ts +20 -0
- package/dist/{engine-wmW-VuBL.d.ts → engine-DK3iV_Kk.d.ts} +91 -60
- package/dist/hono/index.d.ts +1 -1
- package/dist/hono/index.js +3 -3
- package/dist/index.d.ts +1 -1
- package/dist/index.js +17 -6
- package/dist/index.js.map +1 -1
- 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/dist/manifest/index.d.ts +4 -1
- package/dist/manifest/index.js +1 -1
- package/dist/manifest/server.d.ts +1 -1
- package/dist/manifest/server.js +3 -3
- package/dist/server/index.d.ts +6 -2
- package/dist/server/index.js +3 -3
- package/package.json +14 -2
- package/prisma/marketplace.prisma +6 -0
- package/prisma/migrations/20261126230000_marketplace_menu_link_variation/migration.sql +11 -0
- 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/core/bulk.ts +34 -7
- package/src/core/ingest.ts +2 -1
- package/src/core/menu-links.ts +36 -6
- package/src/core/order-types.ts +74 -0
- package/src/core/ports.ts +23 -65
- 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
- package/src/server/mcp.ts +7 -2
- package/dist/chunk-BMCERPQX.js.map +0 -1
- package/dist/chunk-NP4EYKPO.js.map +0 -1
- /package/dist/{chunk-6N2HEV3U.js.map → chunk-WVZ2IXFH.js.map} +0 -0
package/src/core/bulk.ts
CHANGED
|
@@ -40,12 +40,17 @@ export type IntegrationRowInput =
|
|
|
40
40
|
| ({ operation?: "upsert" } & Omit<SaveMarketplaceIntegrationInput, "clientId">)
|
|
41
41
|
| { operation: "archive" | "unarchive"; provider: string; reason?: string | null };
|
|
42
42
|
|
|
43
|
-
/**
|
|
43
|
+
/**
|
|
44
|
+
* One SKU link: map it to a catalog id, or unmap it with `menuItemId: null`.
|
|
45
|
+
* `variationId` narrows the mapping to one variation of that id (FUT-4927);
|
|
46
|
+
* absent or null maps the item alone, and it may not be set on an unmap.
|
|
47
|
+
*/
|
|
44
48
|
export interface MenuLinkRowInput {
|
|
45
49
|
provider: string;
|
|
46
50
|
externalSku: string;
|
|
47
51
|
externalName?: string;
|
|
48
52
|
menuItemId: string | null;
|
|
53
|
+
variationId?: string | null;
|
|
49
54
|
}
|
|
50
55
|
|
|
51
56
|
/** The key of each row seen twice, as problems. */
|
|
@@ -177,7 +182,33 @@ async function auditArchives(
|
|
|
177
182
|
}
|
|
178
183
|
}
|
|
179
184
|
|
|
180
|
-
/**
|
|
185
|
+
/**
|
|
186
|
+
* The catalog half of one link row: the item must be the store's, and a named
|
|
187
|
+
* variation must be one of that item's, as the host answers. A variation on an
|
|
188
|
+
* unmap, or with a host that cannot answer for variations, is refused at the
|
|
189
|
+
* variation, and the item is not asked about it.
|
|
190
|
+
*/
|
|
191
|
+
async function linkRowProblems(
|
|
192
|
+
ctx: EngineContext,
|
|
193
|
+
clientId: string,
|
|
194
|
+
row: MenuLinkRowInput,
|
|
195
|
+
index: number,
|
|
196
|
+
): Promise<BulkProblem[]> {
|
|
197
|
+
const variationPath = ["links", index, "variationId"];
|
|
198
|
+
const variationId = row.variationId ?? null;
|
|
199
|
+
if (row.menuItemId === null) {
|
|
200
|
+
return variationId === null ? [] : [{ path: variationPath, code: "CATALOG_ITEM_NOT_FOUND" }];
|
|
201
|
+
}
|
|
202
|
+
if (!(await ctx.ports.order.catalogHas(clientId, row.menuItemId))) {
|
|
203
|
+
return [{ path: ["links", index, "menuItemId"], code: "CATALOG_ITEM_NOT_FOUND" }];
|
|
204
|
+
}
|
|
205
|
+
if (variationId === null) return [];
|
|
206
|
+
const order = ctx.ports.order;
|
|
207
|
+
const belongs = order.catalogHasVariation ? await order.catalogHasVariation(clientId, row.menuItemId, variationId) : false;
|
|
208
|
+
return belongs ? [] : [{ path: variationPath, code: "CATALOG_ITEM_NOT_FOUND" }];
|
|
209
|
+
}
|
|
210
|
+
|
|
211
|
+
/** Map or unmap many SKUs at once; every catalog id (and variation) must be the store's. */
|
|
181
212
|
export async function upsertMenuLinks(
|
|
182
213
|
ctx: EngineContext,
|
|
183
214
|
input: { clientId: string; rows: readonly MenuLinkRowInput[]; validateOnly?: boolean },
|
|
@@ -186,11 +217,7 @@ export async function upsertMenuLinks(
|
|
|
186
217
|
...duplicates(input.rows, (row) => `${row.provider}\u0000${row.externalSku}`, "links", "externalSku"),
|
|
187
218
|
...unknownProviders(ctx, input.rows, "links"),
|
|
188
219
|
];
|
|
189
|
-
for (const [index, row] of input.rows.entries())
|
|
190
|
-
if (row.menuItemId !== null && !(await ctx.ports.order.catalogHas(input.clientId, row.menuItemId))) {
|
|
191
|
-
problems.push({ path: ["links", index, "menuItemId"], code: "CATALOG_ITEM_NOT_FOUND" });
|
|
192
|
-
}
|
|
193
|
-
}
|
|
220
|
+
for (const [index, row] of input.rows.entries()) problems.push(...(await linkRowProblems(ctx, input.clientId, row, index)));
|
|
194
221
|
if (problems.length > 0) return { kind: "refused", problems };
|
|
195
222
|
|
|
196
223
|
const validateOnly = input.validateOnly === true;
|
package/src/core/ingest.ts
CHANGED
|
@@ -78,7 +78,8 @@ async function composeLines(
|
|
|
78
78
|
quantity: line.quantity,
|
|
79
79
|
unitPriceCents: line.unitPriceCents,
|
|
80
80
|
note: line.note,
|
|
81
|
-
subjectRef: mapping.get(line.externalSku) ?? null,
|
|
81
|
+
subjectRef: mapping.get(line.externalSku)?.menuItemId ?? null,
|
|
82
|
+
variationRef: mapping.get(line.externalSku)?.variationId ?? null,
|
|
82
83
|
}));
|
|
83
84
|
}
|
|
84
85
|
|
package/src/core/menu-links.ts
CHANGED
|
@@ -14,6 +14,13 @@ import type { MarketplaceProvider } from "./types";
|
|
|
14
14
|
* The package never dereferences `menuItemId`. It asks the host whether the id
|
|
15
15
|
* is theirs (`order.catalogHas`, in `./bulk.ts`) and stores it; what the id
|
|
16
16
|
* MEANS is the host's.
|
|
17
|
+
*
|
|
18
|
+
* A link may narrow the mapping to ONE variation of that id (`variationId`,
|
|
19
|
+
* FUT-4927): a marketplace SKU usually names one flavour or size, and the
|
|
20
|
+
* host's order line is then the item plus that variation. `variationId` null
|
|
21
|
+
* is the item alone, exactly as before it existed; it is never set without
|
|
22
|
+
* `menuItemId`, and the host answers whether it belongs to the item
|
|
23
|
+
* (`order.catalogHasVariation`).
|
|
17
24
|
*/
|
|
18
25
|
|
|
19
26
|
/** A link as every read surface sees it. */
|
|
@@ -24,15 +31,24 @@ export interface MarketplaceMenuLinkView {
|
|
|
24
31
|
externalName: string;
|
|
25
32
|
/** The host catalog id, or null while the SKU is unmapped. */
|
|
26
33
|
menuItemId: string | null;
|
|
34
|
+
/** The variation of `menuItemId` the SKU names, or null for the item alone. */
|
|
35
|
+
variationId: string | null;
|
|
27
36
|
lastSeenAt: Date;
|
|
28
37
|
}
|
|
29
38
|
|
|
39
|
+
/** What one SKU resolves to on the ingest path; both null while it is unmapped. */
|
|
40
|
+
interface ResolvedSku {
|
|
41
|
+
menuItemId: string | null;
|
|
42
|
+
variationId: string | null;
|
|
43
|
+
}
|
|
44
|
+
|
|
30
45
|
const LINK_SELECT = {
|
|
31
46
|
id: true,
|
|
32
47
|
provider: true,
|
|
33
48
|
externalSku: true,
|
|
34
49
|
externalName: true,
|
|
35
50
|
menuItemId: true,
|
|
51
|
+
variationId: true,
|
|
36
52
|
lastSeenAt: true,
|
|
37
53
|
} as const;
|
|
38
54
|
|
|
@@ -42,6 +58,7 @@ interface LinkRow {
|
|
|
42
58
|
externalSku: string;
|
|
43
59
|
externalName: string | null;
|
|
44
60
|
menuItemId: string | null;
|
|
61
|
+
variationId?: string | null;
|
|
45
62
|
lastSeenAt: Date;
|
|
46
63
|
}
|
|
47
64
|
|
|
@@ -52,6 +69,9 @@ function toView(row: LinkRow): MarketplaceMenuLinkView {
|
|
|
52
69
|
externalSku: row.externalSku,
|
|
53
70
|
externalName: row.externalName ?? row.externalSku,
|
|
54
71
|
menuItemId: row.menuItemId,
|
|
72
|
+
// A row with no variation key (a fake db) reads as the item alone. A Prisma client
|
|
73
|
+
// generated before the migration throws on `variationId` instead: sync, generate, migrate.
|
|
74
|
+
variationId: row.menuItemId === null ? null : (row.variationId ?? null),
|
|
55
75
|
lastSeenAt: row.lastSeenAt,
|
|
56
76
|
};
|
|
57
77
|
}
|
|
@@ -86,6 +106,10 @@ export async function listMarketplaceMenuLinks(
|
|
|
86
106
|
* transaction. The caller has already checked the id is this tenant's
|
|
87
107
|
* (`order.catalogHas`). Creates the link if the SKU has never been seen: an
|
|
88
108
|
* operator mapping ahead of the first order is doing the right thing.
|
|
109
|
+
*
|
|
110
|
+
* Every write sets BOTH halves: a mapping to the item alone (`variationId`
|
|
111
|
+
* absent or null) clears a variation an earlier mapping named, and an unmap
|
|
112
|
+
* clears both.
|
|
89
113
|
*/
|
|
90
114
|
export async function upsertLink(
|
|
91
115
|
db: MarketplaceDb,
|
|
@@ -95,8 +119,10 @@ export async function upsertLink(
|
|
|
95
119
|
externalSku: string;
|
|
96
120
|
externalName?: string;
|
|
97
121
|
menuItemId: string | null;
|
|
122
|
+
variationId?: string | null;
|
|
98
123
|
},
|
|
99
124
|
): Promise<MarketplaceMenuLinkView> {
|
|
125
|
+
const variationId = input.menuItemId === null ? null : (input.variationId ?? null);
|
|
100
126
|
const row = (await db.marketplaceMenuLink.upsert({
|
|
101
127
|
where: {
|
|
102
128
|
clientId_provider_externalSku: {
|
|
@@ -111,11 +137,12 @@ export async function upsertLink(
|
|
|
111
137
|
externalSku: input.externalSku,
|
|
112
138
|
externalName: input.externalName ?? input.externalSku,
|
|
113
139
|
menuItemId: input.menuItemId,
|
|
140
|
+
variationId,
|
|
114
141
|
},
|
|
115
142
|
// `externalName` is NOT rewritten here. It is the marketplace's snapshot of
|
|
116
143
|
// what it called the item, and an operator's typing is not a better record
|
|
117
144
|
// of that than the marketplace's own text.
|
|
118
|
-
update: { menuItemId: input.menuItemId },
|
|
145
|
+
update: { menuItemId: input.menuItemId, variationId },
|
|
119
146
|
select: LINK_SELECT,
|
|
120
147
|
})) as LinkRow;
|
|
121
148
|
return toView(row);
|
|
@@ -139,8 +166,8 @@ export async function resolveSkus(
|
|
|
139
166
|
skus: { externalSku: string; name: string }[];
|
|
140
167
|
seenAt: Date;
|
|
141
168
|
},
|
|
142
|
-
): Promise<Map<string,
|
|
143
|
-
const resolved = new Map<string,
|
|
169
|
+
): Promise<Map<string, ResolvedSku>> {
|
|
170
|
+
const resolved = new Map<string, ResolvedSku>();
|
|
144
171
|
for (const sku of input.skus) {
|
|
145
172
|
const row = (await db.marketplaceMenuLink.upsert({
|
|
146
173
|
where: {
|
|
@@ -158,9 +185,12 @@ export async function resolveSkus(
|
|
|
158
185
|
lastSeenAt: input.seenAt,
|
|
159
186
|
},
|
|
160
187
|
update: { lastSeenAt: input.seenAt },
|
|
161
|
-
select: { menuItemId: true },
|
|
162
|
-
})) as { menuItemId: string | null };
|
|
163
|
-
resolved.set(sku.externalSku,
|
|
188
|
+
select: { menuItemId: true, variationId: true },
|
|
189
|
+
})) as { menuItemId: string | null; variationId?: string | null };
|
|
190
|
+
resolved.set(sku.externalSku, {
|
|
191
|
+
menuItemId: row.menuItemId,
|
|
192
|
+
variationId: row.menuItemId === null ? null : (row.variationId ?? null),
|
|
193
|
+
});
|
|
164
194
|
}
|
|
165
195
|
return resolved;
|
|
166
196
|
}
|
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The order-side vocabulary the host's order port speaks: what a resolved line
|
|
3
|
+
* is, what the queue shows, what a person stated, and why an order or a
|
|
4
|
+
* cancellation ended. Plain data with no engine dependency; `./ports`
|
|
5
|
+
* re-exports every name, so a host imports them from there as before.
|
|
6
|
+
*/
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* One order line after the engine has matched it against the SKU map.
|
|
10
|
+
* `subjectRef` is the host catalog id, or null when the SKU was never mapped —
|
|
11
|
+
* never an error, and never a reason to refuse the order. `variationRef` is
|
|
12
|
+
* the variation of `subjectRef` the SKU was mapped to (FUT-4927), or null for
|
|
13
|
+
* the item alone and for every unmapped line.
|
|
14
|
+
*/
|
|
15
|
+
export interface ResolvedOrderLine {
|
|
16
|
+
externalSku: string;
|
|
17
|
+
name: string;
|
|
18
|
+
quantity: number;
|
|
19
|
+
unitPriceCents: number;
|
|
20
|
+
note: string | null;
|
|
21
|
+
subjectRef: string | null;
|
|
22
|
+
variationRef: string | null;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
/** What the acceptance queue shows about an order, resolved by the host. */
|
|
26
|
+
export interface MarketplaceOrderSummary {
|
|
27
|
+
buyerName: string | null;
|
|
28
|
+
totalCents: number;
|
|
29
|
+
unmappedLines: number;
|
|
30
|
+
createdAt: string | null;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
/**
|
|
34
|
+
* What a person stated beside an accept, for the host's admission to read.
|
|
35
|
+
* Every field is optional and the host decides what it means.
|
|
36
|
+
*/
|
|
37
|
+
export interface MarketplaceAdmission {
|
|
38
|
+
/** "Accept anyway": the person was told the order is short and confirmed. */
|
|
39
|
+
admitStockPastZero?: boolean;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
/** The host's answer: proceed, or refuse with its own coded reason. */
|
|
43
|
+
export type MarketplaceAdmissionResult =
|
|
44
|
+
| { admitted: true }
|
|
45
|
+
| {
|
|
46
|
+
admitted: false;
|
|
47
|
+
/** The host's machine code (`MARKETPLACE_STOCK_ASK`). The route answers 409 with it. */
|
|
48
|
+
code: string;
|
|
49
|
+
/** The host's sentence, in the caller's language. */
|
|
50
|
+
message: string;
|
|
51
|
+
/** Anything the client needs to ask the person (the short lines). */
|
|
52
|
+
details?: unknown;
|
|
53
|
+
};
|
|
54
|
+
|
|
55
|
+
/** Why the engine tells the host to refuse an order. Vocabulary, not copy. */
|
|
56
|
+
export type MarketplaceRefusal = "REJECTED" | "EXPIRED" | "CANCELLED";
|
|
57
|
+
|
|
58
|
+
/**
|
|
59
|
+
* Why the store's own cancellation request FAILED. Vocabulary, not copy.
|
|
60
|
+
*
|
|
61
|
+
* - `REFUSED`: the marketplace judged the request and denied it: its denial
|
|
62
|
+
* event, an answer that is its business refusal, or an adapter that has no
|
|
63
|
+
* cancellation API to call. An adapter that cannot tell a business 4xx from
|
|
64
|
+
* a malformed request (iFood, Rappi) reports every final 4xx as REFUSED.
|
|
65
|
+
* - `UNREACHABLE`: our own request never got a judgement: no credentials or
|
|
66
|
+
* ones that do not open, a token the marketplace did not grant, a request
|
|
67
|
+
* the marketplace calls malformed (99Food's 400), or every attempt failed
|
|
68
|
+
* on the network or a 5xx. Retrying, or fixing the connection, can help.
|
|
69
|
+
* - `TIMEOUT`: no answer inside the marketplace's cancellation window.
|
|
70
|
+
*
|
|
71
|
+
* A host written before `UNREACHABLE` existed treats an unknown value as it
|
|
72
|
+
* treats REFUSED: the order stays and a person finishes in the marketplace's app.
|
|
73
|
+
*/
|
|
74
|
+
export type CancellationFailureReason = "REFUSED" | "UNREACHABLE" | "TIMEOUT";
|
package/src/core/ports.ts
CHANGED
|
@@ -2,9 +2,25 @@ import type { AdapterRegistry } from "./registry";
|
|
|
2
2
|
import type { AdapterTokenCache, MarketplaceAdapter, MarketplaceHttp, StatusStep } from "./adapter";
|
|
3
3
|
import type { CatalogueImage, CatalogueItem } from "./catalogue-port";
|
|
4
4
|
import type { MarketplaceLifecycleAnswer, MarketplaceLifecycleFacts } from "./lifecycle-port";
|
|
5
|
+
import type {
|
|
6
|
+
CancellationFailureReason,
|
|
7
|
+
MarketplaceAdmission,
|
|
8
|
+
MarketplaceAdmissionResult,
|
|
9
|
+
MarketplaceOrderSummary,
|
|
10
|
+
MarketplaceRefusal,
|
|
11
|
+
ResolvedOrderLine,
|
|
12
|
+
} from "./order-types";
|
|
5
13
|
import type { NormalizedEvent, NormalizedMarketplaceOrder } from "./types";
|
|
6
14
|
|
|
7
15
|
export type { MarketplaceLifecycleAnswer, MarketplaceLifecycleFacts } from "./lifecycle-port";
|
|
16
|
+
export type {
|
|
17
|
+
CancellationFailureReason,
|
|
18
|
+
MarketplaceAdmission,
|
|
19
|
+
MarketplaceAdmissionResult,
|
|
20
|
+
MarketplaceOrderSummary,
|
|
21
|
+
MarketplaceRefusal,
|
|
22
|
+
ResolvedOrderLine,
|
|
23
|
+
} from "./order-types";
|
|
8
24
|
|
|
9
25
|
/**
|
|
10
26
|
* What the engine needs FROM its host, and nothing more.
|
|
@@ -96,37 +112,6 @@ export interface MarketplaceDb {
|
|
|
96
112
|
};
|
|
97
113
|
}
|
|
98
114
|
|
|
99
|
-
/**
|
|
100
|
-
* One order line after the engine has matched it against the SKU map.
|
|
101
|
-
* `subjectRef` is the host catalog id, or null when the SKU was never mapped —
|
|
102
|
-
* never an error, and never a reason to refuse the order.
|
|
103
|
-
*/
|
|
104
|
-
export interface ResolvedOrderLine {
|
|
105
|
-
externalSku: string;
|
|
106
|
-
name: string;
|
|
107
|
-
quantity: number;
|
|
108
|
-
unitPriceCents: number;
|
|
109
|
-
note: string | null;
|
|
110
|
-
subjectRef: string | null;
|
|
111
|
-
}
|
|
112
|
-
|
|
113
|
-
/** What the acceptance queue shows about an order, resolved by the host. */
|
|
114
|
-
export interface MarketplaceOrderSummary {
|
|
115
|
-
buyerName: string | null;
|
|
116
|
-
totalCents: number;
|
|
117
|
-
unmappedLines: number;
|
|
118
|
-
createdAt: string | null;
|
|
119
|
-
}
|
|
120
|
-
|
|
121
|
-
/**
|
|
122
|
-
* What a person stated beside an accept, for the host's admission to read.
|
|
123
|
-
* Every field is optional and the host decides what it means.
|
|
124
|
-
*/
|
|
125
|
-
export interface MarketplaceAdmission {
|
|
126
|
-
/** "Accept anyway": the person was told the order is short and confirmed. */
|
|
127
|
-
admitStockPastZero?: boolean;
|
|
128
|
-
}
|
|
129
|
-
|
|
130
115
|
/** One decision as the host's admission sees it, BEFORE the claim commits. */
|
|
131
116
|
export interface MarketplaceAdmissionRequest {
|
|
132
117
|
clientId: string;
|
|
@@ -142,40 +127,6 @@ export interface MarketplaceAdmissionRequest {
|
|
|
142
127
|
db: MarketplaceDb;
|
|
143
128
|
}
|
|
144
129
|
|
|
145
|
-
/** The host's answer: proceed, or refuse with its own coded reason. */
|
|
146
|
-
export type MarketplaceAdmissionResult =
|
|
147
|
-
| { admitted: true }
|
|
148
|
-
| {
|
|
149
|
-
admitted: false;
|
|
150
|
-
/** The host's machine code (`MARKETPLACE_STOCK_ASK`). The route answers 409 with it. */
|
|
151
|
-
code: string;
|
|
152
|
-
/** The host's sentence, in the caller's language. */
|
|
153
|
-
message: string;
|
|
154
|
-
/** Anything the client needs to ask the person (the short lines). */
|
|
155
|
-
details?: unknown;
|
|
156
|
-
};
|
|
157
|
-
|
|
158
|
-
/** Why the engine tells the host to refuse an order. Vocabulary, not copy. */
|
|
159
|
-
export type MarketplaceRefusal = "REJECTED" | "EXPIRED" | "CANCELLED";
|
|
160
|
-
|
|
161
|
-
/**
|
|
162
|
-
* Why the store's own cancellation request FAILED. Vocabulary, not copy.
|
|
163
|
-
*
|
|
164
|
-
* - `REFUSED`: the marketplace judged the request and denied it: its denial
|
|
165
|
-
* event, an answer that is its business refusal, or an adapter that has no
|
|
166
|
-
* cancellation API to call. An adapter that cannot tell a business 4xx from
|
|
167
|
-
* a malformed request (iFood, Rappi) reports every final 4xx as REFUSED.
|
|
168
|
-
* - `UNREACHABLE`: our own request never got a judgement: no credentials or
|
|
169
|
-
* ones that do not open, a token the marketplace did not grant, a request
|
|
170
|
-
* the marketplace calls malformed (99Food's 400), or every attempt failed
|
|
171
|
-
* on the network or a 5xx. Retrying, or fixing the connection, can help.
|
|
172
|
-
* - `TIMEOUT`: no answer inside the marketplace's cancellation window.
|
|
173
|
-
*
|
|
174
|
-
* A host written before `UNREACHABLE` existed treats an unknown value as it
|
|
175
|
-
* treats REFUSED: the order stays and a person finishes in the marketplace's app.
|
|
176
|
-
*/
|
|
177
|
-
export type CancellationFailureReason = "REFUSED" | "UNREACHABLE" | "TIMEOUT";
|
|
178
|
-
|
|
179
130
|
/**
|
|
180
131
|
* The host's domain, as five questions the engine asks and never answers.
|
|
181
132
|
*
|
|
@@ -207,6 +158,13 @@ export interface MarketplaceOrderPort {
|
|
|
207
158
|
): Promise<string>;
|
|
208
159
|
/** Does this catalog id exist and belong to this tenant? */
|
|
209
160
|
catalogHas(clientId: string, subjectRef: string): Promise<boolean>;
|
|
161
|
+
/**
|
|
162
|
+
* OPTIONAL. Is `variationRef` a variation of the catalog id `subjectRef`,
|
|
163
|
+
* and this tenant's? Asked before a SKU is mapped to a variation (FUT-4927);
|
|
164
|
+
* `subjectRef` has already passed `catalogHas`. Absent: a mapping that names
|
|
165
|
+
* a variation is refused, and mapping to the item alone works as before.
|
|
166
|
+
*/
|
|
167
|
+
catalogHasVariation?(clientId: string, subjectRef: string, variationRef: string): Promise<boolean>;
|
|
210
168
|
/** The store said yes, or the connection auto-accepts. */
|
|
211
169
|
onAccepted(clientId: string, orderId: string): Promise<void>;
|
|
212
170
|
/**
|
package/src/index.ts
CHANGED
|
@@ -13,7 +13,8 @@
|
|
|
13
13
|
* acceptance clock and its sweep, the SKU map, the integration settings and
|
|
14
14
|
* the outbound engine, and knows no marketplace by name. Each marketplace is
|
|
15
15
|
* an adapter at its own subpath (`@12-apps/marketplace/ifood`,
|
|
16
|
-
* `@12-apps/marketplace/99food`, `@12-apps/marketplace/
|
|
16
|
+
* `@12-apps/marketplace/99food`, `@12-apps/marketplace/keeta`,
|
|
17
|
+
* `@12-apps/marketplace/rappi`), registered at
|
|
17
18
|
* mount. The wiring manifests are at `./manifest` and `./manifest/server`.
|
|
18
19
|
*
|
|
19
20
|
* Everything the engine needs from its host is a required port with no
|
|
@@ -0,0 +1,226 @@
|
|
|
1
|
+
import { outboundResultOf, type AdapterCallContext } from "../core/adapter";
|
|
2
|
+
import { asString, firstString, pick } from "../core/payload-read";
|
|
3
|
+
import { isStop, openDeliveryCall, type OpenDeliveryDialect, type OpenDeliveryStop } from "../open-delivery/client";
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Keeta's MERCHANT AUTHORIZATION, automated from the software (Keeta's
|
|
7
|
+
* "Autorização Self-serve do Merchant", read 2026-11-26 at
|
|
8
|
+
* https://api-docs.mykeeta.com/apis/opendelivery/merchant-self-serve-authorization).
|
|
9
|
+
* Not on the adapter port: connecting a store is the host's flow, and these
|
|
10
|
+
* are the calls it is built from.
|
|
11
|
+
*
|
|
12
|
+
* 1. {@link KeetaAuthorization.authorizationUrl}: `GET
|
|
13
|
+
* /oauth/authorization/url?clientId=&redirectUri=` (no token: a token
|
|
14
|
+
* needs at least one authorized store), the link the merchant opens.
|
|
15
|
+
* 2. The merchant authorizes; Keeta redirects to `redirectUri` with `authId`
|
|
16
|
+
* (and `state`, `appId`, `timestamp`), and POSTs the authorization webhook
|
|
17
|
+
* ({@link normalizeKeetaAuthorizationEvent}: `opType` 1 added, 2 revoked).
|
|
18
|
+
* 3. {@link KeetaAuthorization.authorizedShops}: `GET
|
|
19
|
+
* /oauth/authorized/{authId}/merchantInfo?pageNum=&pageSize=` lists the
|
|
20
|
+
* stores of that authorization (Keeta's ids, names, addresses, time zones).
|
|
21
|
+
* 4. The host maps a Keeta store to its own, then
|
|
22
|
+
* {@link KeetaAuthorization.onboard}: `PUT /v1/merchantOnboarding?merchantId=`
|
|
23
|
+
* binds the SOFTWARE's merchant id (the connection's `externalMerchantId`)
|
|
24
|
+
* to `keetaMerchantId` and registers the `GET /v1/merchant` URL and the
|
|
25
|
+
* orders webhook. "Without onboarding there are no order events"; sending
|
|
26
|
+
* it again for the same id REPLACES the mapping. Answer 201.
|
|
27
|
+
*
|
|
28
|
+
* ESTIMATED: the JSON answer the authorization webhook wants ("200 within
|
|
29
|
+
* ~5 s, with JSON containing `code` and `message`": the values are not
|
|
30
|
+
* given, {@link KEETA_AUTHORIZATION_WEBHOOK_ANSWER} sends 0 and "success").
|
|
31
|
+
*/
|
|
32
|
+
|
|
33
|
+
/** What the authorization webhook is answered with, 200, within ~5 s. ESTIMATED values. */
|
|
34
|
+
export const KEETA_AUTHORIZATION_WEBHOOK_ANSWER = { code: 0, message: "success" } as const;
|
|
35
|
+
|
|
36
|
+
/** One authorization change Keeta reported (event 1301 added, 1302 revoked). */
|
|
37
|
+
export interface KeetaAuthorizationEvent {
|
|
38
|
+
authId: string;
|
|
39
|
+
change: "AUTHORIZED" | "REVOKED";
|
|
40
|
+
/** Keeta's store id (an integer on the wire, text here). */
|
|
41
|
+
shopId: string;
|
|
42
|
+
shopName: string | null;
|
|
43
|
+
/** The application's id. */
|
|
44
|
+
clientId: string | null;
|
|
45
|
+
at: Date | null;
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
/** Read the authorization webhook's body (`WebhookNotification`). Null when it names no authorization or store. */
|
|
49
|
+
export function normalizeKeetaAuthorizationEvent(payload: unknown): KeetaAuthorizationEvent | null {
|
|
50
|
+
const authId = firstString(payload, [["authId"]]);
|
|
51
|
+
const shopId = firstString(payload, [["shopId"]]);
|
|
52
|
+
const opType = pick(payload, "opType");
|
|
53
|
+
const change = opType === 1 ? "AUTHORIZED" : opType === 2 ? "REVOKED" : null;
|
|
54
|
+
if (!authId || !shopId || !change) return null;
|
|
55
|
+
const millis = pick(payload, "createTime");
|
|
56
|
+
return {
|
|
57
|
+
authId,
|
|
58
|
+
change,
|
|
59
|
+
shopId,
|
|
60
|
+
shopName: firstString(payload, [["shopName"]]),
|
|
61
|
+
clientId: firstString(payload, [["clientId"]]),
|
|
62
|
+
at: typeof millis === "number" && Number.isFinite(millis) ? new Date(millis) : null,
|
|
63
|
+
};
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
/** One store of an authorization, as `merchantInfo` lists it. */
|
|
67
|
+
export interface KeetaAuthorizedShop {
|
|
68
|
+
/** Keeta's store id: the `keetaMerchantId` the onboarding takes. */
|
|
69
|
+
id: string;
|
|
70
|
+
name: string | null;
|
|
71
|
+
address: string | null;
|
|
72
|
+
latitude: number | null;
|
|
73
|
+
longitude: number | null;
|
|
74
|
+
timeZone: string | null;
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
/** One page of an authorization's stores. */
|
|
78
|
+
export interface KeetaAuthorizedShops {
|
|
79
|
+
brandId: string | null;
|
|
80
|
+
brandName: string | null;
|
|
81
|
+
shops: KeetaAuthorizedShop[];
|
|
82
|
+
page: { pageNum: number; totalPage: number; totalCount: number };
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
/** What the host registers for one store at onboarding. */
|
|
86
|
+
export interface KeetaOnboarding {
|
|
87
|
+
/** The SOFTWARE's merchant id: the connection's `externalMerchantId`, the `Merchant.id` the host serves. */
|
|
88
|
+
merchantId: string;
|
|
89
|
+
/** Keeta's store id, from `authorizedShops` or Keeta's portal. */
|
|
90
|
+
keetaMerchantId: number;
|
|
91
|
+
/** Where Keeta reads this store's `GET /v1/merchant` (one URL per store), and its key if it wants one. */
|
|
92
|
+
merchantDocument?: { url: string; apiKey?: string };
|
|
93
|
+
/** The engine's webhook for this store: `…/webhooks/marketplace/:tenantSlug/KEETA`. */
|
|
94
|
+
ordersWebhookUrl?: string;
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
export type KeetaAuthorizationResult<T> = { kind: "ok"; value: T } | OpenDeliveryStop;
|
|
98
|
+
|
|
99
|
+
const numberOr = (value: unknown, fallback: number | null): number | null =>
|
|
100
|
+
typeof value === "number" && Number.isFinite(value) ? value : fallback;
|
|
101
|
+
|
|
102
|
+
function shopOf(raw: unknown): KeetaAuthorizedShop | null {
|
|
103
|
+
const id = firstString(raw, [["id"]]);
|
|
104
|
+
if (!id) return null;
|
|
105
|
+
return {
|
|
106
|
+
id,
|
|
107
|
+
name: firstString(raw, [["name"]]),
|
|
108
|
+
address: firstString(raw, [["address"]]),
|
|
109
|
+
latitude: numberOr(pick(raw, "latitude"), null),
|
|
110
|
+
longitude: numberOr(pick(raw, "longitude"), null),
|
|
111
|
+
timeZone: firstString(raw, [["timeZone"]]),
|
|
112
|
+
};
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
function shopsOf(body: unknown): KeetaAuthorizedShops {
|
|
116
|
+
const raw = pick(body, "authorizedShops");
|
|
117
|
+
return {
|
|
118
|
+
brandId: asString(pick(body, "brandId")),
|
|
119
|
+
brandName: firstString(body, [["brandName"]]),
|
|
120
|
+
shops: (Array.isArray(raw) ? raw : []).map(shopOf).filter((shop): shop is KeetaAuthorizedShop => shop !== null),
|
|
121
|
+
page: {
|
|
122
|
+
pageNum: numberOr(pick(body, "page", "pageNum"), 1) ?? 1,
|
|
123
|
+
totalPage: numberOr(pick(body, "page", "totalPage"), 1) ?? 1,
|
|
124
|
+
totalCount: numberOr(pick(body, "page", "totalCount"), 0) ?? 0,
|
|
125
|
+
},
|
|
126
|
+
};
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
/** One call whose 2xx answer is read by `read`: the value, or the result that ends it. */
|
|
130
|
+
async function readCall<T>(
|
|
131
|
+
ctx: AdapterCallContext,
|
|
132
|
+
dialect: OpenDeliveryDialect,
|
|
133
|
+
call: Parameters<typeof openDeliveryCall>[2],
|
|
134
|
+
read: (body: unknown) => T | null,
|
|
135
|
+
): Promise<KeetaAuthorizationResult<T>> {
|
|
136
|
+
try {
|
|
137
|
+
const response = await openDeliveryCall(ctx, dialect, call);
|
|
138
|
+
if (isStop(response)) return response;
|
|
139
|
+
const result = outboundResultOf(response);
|
|
140
|
+
if (result.kind !== "accepted") return result;
|
|
141
|
+
let body: unknown = null;
|
|
142
|
+
try {
|
|
143
|
+
body = response.body === "" ? null : JSON.parse(response.body);
|
|
144
|
+
} catch {
|
|
145
|
+
body = null;
|
|
146
|
+
}
|
|
147
|
+
const value = read(body);
|
|
148
|
+
return value === null
|
|
149
|
+
? { kind: "refused", status: response.status, message: "Keeta's answer is not the documented shape" }
|
|
150
|
+
: { kind: "ok", value };
|
|
151
|
+
} catch (error) {
|
|
152
|
+
return { kind: "retry", reason: error instanceof Error ? error.message : String(error) };
|
|
153
|
+
}
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
/** The self-serve authorization calls, bound to Keeta's dialect. */
|
|
157
|
+
export interface KeetaAuthorization {
|
|
158
|
+
authorizationUrl(ctx: AdapterCallContext, input: { redirectUri?: string }): Promise<KeetaAuthorizationResult<string>>;
|
|
159
|
+
authorizedShops(
|
|
160
|
+
ctx: AdapterCallContext,
|
|
161
|
+
input: { authId: string; pageNum?: number; pageSize?: number },
|
|
162
|
+
): Promise<KeetaAuthorizationResult<KeetaAuthorizedShops>>;
|
|
163
|
+
onboard(ctx: AdapterCallContext, input: KeetaOnboarding): Promise<KeetaAuthorizationResult<true>>;
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
/** An https URL Keeta answered, or null. */
|
|
167
|
+
function httpsUrl(body: unknown): string | null {
|
|
168
|
+
const url = firstString(body, [["merchantAuthorizationUrl"]]);
|
|
169
|
+
if (!url) return null;
|
|
170
|
+
try {
|
|
171
|
+
return new URL(url).protocol === "https:" ? url : null;
|
|
172
|
+
} catch {
|
|
173
|
+
return null;
|
|
174
|
+
}
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
export function keetaAuthorization(dialect: OpenDeliveryDialect): KeetaAuthorization {
|
|
178
|
+
return {
|
|
179
|
+
authorizationUrl: (ctx, input) =>
|
|
180
|
+
readCall(
|
|
181
|
+
ctx,
|
|
182
|
+
dialect,
|
|
183
|
+
{
|
|
184
|
+
method: "GET",
|
|
185
|
+
path: "/oauth/authorization/url",
|
|
186
|
+
query: { clientId: ctx.credentials.clientId, ...(input.redirectUri ? { redirectUri: input.redirectUri } : {}) },
|
|
187
|
+
authenticated: false,
|
|
188
|
+
},
|
|
189
|
+
httpsUrl,
|
|
190
|
+
),
|
|
191
|
+
authorizedShops: (ctx, input) =>
|
|
192
|
+
readCall(
|
|
193
|
+
ctx,
|
|
194
|
+
dialect,
|
|
195
|
+
{
|
|
196
|
+
method: "GET",
|
|
197
|
+
path: `/oauth/authorized/${encodeURIComponent(input.authId)}/merchantInfo`,
|
|
198
|
+
query: { pageNum: String(input.pageNum ?? 1), pageSize: String(Math.min(input.pageSize ?? 100, 100)) },
|
|
199
|
+
},
|
|
200
|
+
(body) => (body === null ? null : shopsOf(body)),
|
|
201
|
+
),
|
|
202
|
+
onboard: (ctx, input) =>
|
|
203
|
+
readCall(
|
|
204
|
+
ctx,
|
|
205
|
+
dialect,
|
|
206
|
+
{
|
|
207
|
+
method: "PUT",
|
|
208
|
+
path: "/v1/merchantOnboarding",
|
|
209
|
+
query: { merchantId: input.merchantId },
|
|
210
|
+
body: {
|
|
211
|
+
keetaMerchantId: input.keetaMerchantId,
|
|
212
|
+
...(input.merchantDocument
|
|
213
|
+
? {
|
|
214
|
+
getMerchantURL: {
|
|
215
|
+
baseURL: input.merchantDocument.url,
|
|
216
|
+
...(input.merchantDocument.apiKey ? { apiKey: input.merchantDocument.apiKey } : {}),
|
|
217
|
+
},
|
|
218
|
+
}
|
|
219
|
+
: {}),
|
|
220
|
+
...(input.ordersWebhookUrl ? { ordersWebhookURL: input.ordersWebhookUrl } : {}),
|
|
221
|
+
},
|
|
222
|
+
},
|
|
223
|
+
() => true as const,
|
|
224
|
+
),
|
|
225
|
+
};
|
|
226
|
+
}
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import { OPEN_DELIVERY_MERCHANT_REASON_CODES } from "../open-delivery/reasons";
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* The words the Keeta adapter needs from its host, and nothing else.
|
|
5
|
+
*
|
|
6
|
+
* Required and defaultless, like every copy port in this repo: the adapter
|
|
7
|
+
* ships no language of its own. Two packs sit beside it (`./pt-BR`,
|
|
8
|
+
* `./en-US`); a host passes one by hand, or its own.
|
|
9
|
+
*/
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* The cancellation codes a merchant may send: Keeta's "códigos de
|
|
13
|
+
* cancelamento (v1.0.7+)", the same eleven as Open Delivery's
|
|
14
|
+
* (`../open-delivery/reasons.ts`). Keeta still accepts its legacy codes
|
|
15
|
+
* (`INSUFFICIENT_DISHES`, `SUSPENSION_OF_BUSINESS`, `INSUFFICIENT_MANPOWER`,
|
|
16
|
+
* `OTHERS`) and recommends the new ones, so only the new ones are offered.
|
|
17
|
+
*/
|
|
18
|
+
export const KEETA_REASON_CODES = OPEN_DELIVERY_MERCHANT_REASON_CODES;
|
|
19
|
+
|
|
20
|
+
export type KeetaReasonCode = (typeof KEETA_REASON_CODES)[number];
|
|
21
|
+
|
|
22
|
+
export interface KeetaAdapterCopy {
|
|
23
|
+
/** One label per reason code. Sent as the request's `reason` text when the store wrote none. */
|
|
24
|
+
reasonLabels: Readonly<Record<KeetaReasonCode, string>>;
|
|
25
|
+
/** What an address with no street number (or an encrypted one Keeta would not reveal) says instead. */
|
|
26
|
+
missingStreetNumber: string;
|
|
27
|
+
}
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import type { KeetaAdapterCopy } from "./copy";
|
|
2
|
+
|
|
3
|
+
/** The Keeta adapter's en-US pack: the same reasons, in English. */
|
|
4
|
+
export const KEETA_ADAPTER_COPY_EN_US = {
|
|
5
|
+
reasonLabels: {
|
|
6
|
+
SYSTEMIC_ISSUES: "System problems",
|
|
7
|
+
DUPLICATE_APPLICATION: "Duplicate order",
|
|
8
|
+
UNAVAILABLE_ITEM: "Item unavailable",
|
|
9
|
+
RESTAURANT_WITHOUT_DELIVERY_PERSON: "No courier available",
|
|
10
|
+
OUTDATED_MENU: "Menu out of date",
|
|
11
|
+
ORDER_OUTSIDE_THE_DELIVERY_AREA: "Outside the delivery area",
|
|
12
|
+
BLOCKED_CUSTOMER: "Blocked customer",
|
|
13
|
+
OUTSIDE_DELIVERY_HOURS: "Outside delivery hours",
|
|
14
|
+
INTERNAL_DIFFICULTIES_OF_THE_RESTAURANT: "Internal difficulties",
|
|
15
|
+
RISK_AREA: "Risk area",
|
|
16
|
+
DELIVERY_PROBLEM: "Delivery problem",
|
|
17
|
+
},
|
|
18
|
+
missingStreetNumber: "no number",
|
|
19
|
+
} as const satisfies KeetaAdapterCopy;
|