@12-apps/marketplace 1.1.0 → 1.3.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 +37 -7
- package/README.md +238 -4
- package/dist/99food/en-US.d.ts +19 -0
- package/dist/99food/en-US.js +23 -0
- package/dist/99food/en-US.js.map +1 -0
- package/dist/99food/index.d.ts +162 -0
- package/dist/99food/index.js +444 -0
- package/dist/99food/index.js.map +1 -0
- package/dist/99food/pt-BR.d.ts +23 -0
- package/dist/99food/pt-BR.js +23 -0
- package/dist/99food/pt-BR.js.map +1 -0
- package/dist/{adapter-BggtVFf9.d.ts → adapter-0l_EmuJA.d.ts} +206 -2
- package/dist/{chunk-NGNEL4HS.js → chunk-AYYBRQJV.js} +52 -4
- package/dist/chunk-AYYBRQJV.js.map +1 -0
- package/dist/chunk-BD7DS3XM.js +78 -0
- package/dist/chunk-BD7DS3XM.js.map +1 -0
- package/dist/chunk-M2V4RFMA.js +23 -0
- package/dist/chunk-M2V4RFMA.js.map +1 -0
- package/dist/{chunk-7BYVGNU5.js → chunk-RVBTARIY.js} +1 -1
- package/dist/chunk-RVBTARIY.js.map +1 -0
- package/dist/chunk-XUAFXNOH.js +1604 -0
- package/dist/chunk-XUAFXNOH.js.map +1 -0
- package/dist/{engine-KrgZ4pA4.d.ts → engine-CZsP8KWd.d.ts} +278 -2
- package/dist/hono/index.d.ts +2 -2
- package/dist/hono/index.js +2 -2
- package/dist/ifood/index.d.ts +16 -12
- package/dist/ifood/index.js +248 -121
- package/dist/ifood/index.js.map +1 -1
- package/dist/index.d.ts +30 -4
- package/dist/index.js +189 -346
- 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/en-US.d.ts +14 -0
- package/dist/rappi/en-US.js +18 -0
- package/dist/rappi/en-US.js.map +1 -0
- package/dist/rappi/index.d.ts +190 -0
- package/dist/rappi/index.js +359 -0
- package/dist/rappi/index.js.map +1 -0
- package/dist/rappi/pt-BR.d.ts +18 -0
- package/dist/rappi/pt-BR.js +18 -0
- package/dist/rappi/pt-BR.js.map +1 -0
- package/dist/server/index.d.ts +19 -8
- package/dist/server/index.js +4 -2
- package/package.json +26 -2
- package/prisma/marketplace.prisma +97 -2
- package/prisma/migrations/20261113120000_marketplace_catalogue/migration.sql +97 -0
- package/src/99food/catalogue.ts +117 -0
- package/src/99food/copy.ts +36 -0
- package/src/99food/en-US.ts +19 -0
- package/src/99food/index.ts +125 -0
- package/src/99food/normalize.ts +263 -0
- package/src/99food/outbound.ts +220 -0
- package/src/99food/pt-BR.ts +23 -0
- package/src/core/adapter.ts +27 -2
- package/src/core/catalogue-category.ts +119 -0
- package/src/core/catalogue-ids.ts +23 -0
- package/src/core/catalogue-job.ts +332 -0
- package/src/core/catalogue-port.ts +173 -0
- package/src/core/catalogue-store.ts +172 -0
- package/src/core/catalogue-sweep.ts +43 -0
- package/src/core/catalogue.ts +226 -0
- package/src/core/decisions.ts +16 -4
- package/src/core/engine.ts +54 -0
- package/src/core/fetch-order.ts +253 -0
- package/src/core/inbox.ts +8 -2
- package/src/core/ingest.ts +10 -2
- package/src/core/integration.ts +30 -1
- package/src/core/order-fetch-marks.ts +63 -0
- package/src/core/outbound-sweep.ts +7 -2
- package/src/core/outbound.ts +22 -2
- package/src/core/payload-read.ts +101 -0
- package/src/core/pipeline.ts +64 -15
- package/src/core/ports.ts +36 -0
- package/src/core/types.ts +14 -0
- package/src/ifood/catalogue.ts +273 -0
- package/src/ifood/index.ts +4 -0
- package/src/ifood/normalize.ts +15 -88
- package/src/ifood/outbound.ts +9 -6
- package/src/index.ts +49 -1
- package/src/rappi/copy.ts +39 -0
- package/src/rappi/en-US.ts +14 -0
- package/src/rappi/index.ts +161 -0
- package/src/rappi/normalize.ts +314 -0
- package/src/rappi/outbound.ts +182 -0
- package/src/rappi/pt-BR.ts +18 -0
- package/src/server/index.ts +1 -0
- package/src/server/jobs.ts +71 -8
- package/dist/chunk-7BYVGNU5.js.map +0 -1
- package/dist/chunk-DVIRN42O.js +0 -707
- package/dist/chunk-DVIRN42O.js.map +0 -1
- package/dist/chunk-NGNEL4HS.js.map +0 -1
package/dist/index.js
CHANGED
|
@@ -1,222 +1,72 @@
|
|
|
1
1
|
import {
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
import {
|
|
2
|
+
CATALOGUE_GIVE_UP_MS,
|
|
3
|
+
CATALOGUE_ROW_SELECT,
|
|
4
|
+
CATALOGUE_STALE_MS,
|
|
6
5
|
CLAIM_SELECT,
|
|
6
|
+
CatalogueRetryError,
|
|
7
|
+
FetchOrderRetryError,
|
|
8
|
+
MARKETPLACE_CATALOGUE_JOB,
|
|
9
|
+
MARKETPLACE_FETCH_ORDER_JOB,
|
|
7
10
|
MARKETPLACE_OUTBOUND_JOB,
|
|
8
11
|
MarketplaceAdapterRegistrationError,
|
|
12
|
+
NO_FAILURE,
|
|
9
13
|
NO_STANDING_REQUEST,
|
|
10
14
|
OUTBOUND_GIVE_UP_MS,
|
|
11
15
|
OUTBOUND_STALE_MS,
|
|
12
16
|
OutboundRetryError,
|
|
13
17
|
PROVIDER_ID_PATTERN,
|
|
18
|
+
awaitOrderBody,
|
|
19
|
+
cancelAcceptance,
|
|
20
|
+
catalogueConnection,
|
|
21
|
+
catalogueDedupeKey,
|
|
22
|
+
catalogueTable,
|
|
23
|
+
connectionMerchantId,
|
|
14
24
|
createAdapterRegistry,
|
|
25
|
+
deferInboxEvent,
|
|
26
|
+
enqueueCatalogue,
|
|
15
27
|
enqueueOutbound,
|
|
28
|
+
expireLapsedAcceptances,
|
|
16
29
|
failCancellationRequest,
|
|
30
|
+
ingestMarketplaceOrder,
|
|
17
31
|
listMarketplaceIntegrations,
|
|
18
|
-
|
|
32
|
+
listMarketplaceMenuLinks,
|
|
33
|
+
listPendingAcceptances,
|
|
34
|
+
listRetryableInboxEvents,
|
|
35
|
+
markCancelledBeforeIngest,
|
|
36
|
+
merchantMismatch,
|
|
19
37
|
notPendingProblem,
|
|
20
38
|
outboundDedupeKey,
|
|
21
39
|
planCancellation,
|
|
40
|
+
recordInboxEvent,
|
|
22
41
|
refusalData,
|
|
23
|
-
rejectConfirmed,
|
|
24
42
|
requestData,
|
|
25
43
|
requestsFirst,
|
|
44
|
+
requeueOwedFetch,
|
|
26
45
|
resolveIngestionTarget,
|
|
27
46
|
resolveReason,
|
|
28
47
|
resolveTenantByMerchant,
|
|
48
|
+
runCatalogue,
|
|
49
|
+
runFetchOrder,
|
|
29
50
|
runOutbound,
|
|
30
51
|
saveMarketplaceIntegration,
|
|
52
|
+
settleInboxEvent,
|
|
31
53
|
suggestReason,
|
|
32
54
|
sweepOutbound,
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
55
|
+
tellCancellationFailed,
|
|
56
|
+
upsertLink,
|
|
57
|
+
viewOf
|
|
58
|
+
} from "./chunk-XUAFXNOH.js";
|
|
59
|
+
import {
|
|
60
|
+
stableUuid
|
|
61
|
+
} from "./chunk-M2V4RFMA.js";
|
|
62
|
+
import {
|
|
63
|
+
answered,
|
|
64
|
+
outboundResultOf
|
|
65
|
+
} from "./chunk-RVBTARIY.js";
|
|
36
66
|
import {
|
|
37
67
|
__name
|
|
38
68
|
} from "./chunk-7QVYU63E.js";
|
|
39
69
|
|
|
40
|
-
// src/core/acceptance.ts
|
|
41
|
-
async function listPendingAcceptances(ctx, clientId, orderIds) {
|
|
42
|
-
const db = await ctx.ports.db();
|
|
43
|
-
const rows = await db.marketplaceAcceptance.findMany({
|
|
44
|
-
where: { clientId, status: "AWAITING", ...orderIds ? { orderId: { in: [...orderIds] } } : {} },
|
|
45
|
-
orderBy: { acknowledgeBy: "asc" },
|
|
46
|
-
select: {
|
|
47
|
-
id: true,
|
|
48
|
-
orderId: true,
|
|
49
|
-
provider: true,
|
|
50
|
-
externalOrderId: true,
|
|
51
|
-
externalDisplayId: true,
|
|
52
|
-
acknowledgeBy: true,
|
|
53
|
-
externalDueAt: true,
|
|
54
|
-
commissionCents: true
|
|
55
|
-
}
|
|
56
|
-
});
|
|
57
|
-
if (rows.length === 0) return [];
|
|
58
|
-
const summaries = await ctx.ports.order.summarize(
|
|
59
|
-
clientId,
|
|
60
|
-
rows.map((row) => row.orderId)
|
|
61
|
-
);
|
|
62
|
-
return rows.map((row) => {
|
|
63
|
-
const summary = summaries.get(row.orderId);
|
|
64
|
-
return {
|
|
65
|
-
...row,
|
|
66
|
-
buyerName: summary?.buyerName ?? null,
|
|
67
|
-
totalCents: summary?.totalCents ?? 0,
|
|
68
|
-
unmappedLines: summary?.unmappedLines ?? 0,
|
|
69
|
-
createdAt: summary?.createdAt ?? null
|
|
70
|
-
};
|
|
71
|
-
});
|
|
72
|
-
}
|
|
73
|
-
__name(listPendingAcceptances, "listPendingAcceptances");
|
|
74
|
-
async function expireLapsedAcceptances(ctx, now) {
|
|
75
|
-
const db = await ctx.ports.db();
|
|
76
|
-
const lapsed = await db.marketplaceAcceptance.findMany({
|
|
77
|
-
// An order whose cancellation request waits is left to that request: its
|
|
78
|
-
// answer, or its window's TIMEOUT, ends it first (FUT-4758), so the host
|
|
79
|
-
// never hears EXPIRED and then the confirmation for one order.
|
|
80
|
-
where: { status: "AWAITING", acknowledgeBy: { lt: now }, ...NO_STANDING_REQUEST },
|
|
81
|
-
select: { id: true, clientId: true, orderId: true }
|
|
82
|
-
});
|
|
83
|
-
if (lapsed.length === 0) return { expired: 0, tenants: 0 };
|
|
84
|
-
const result = await db.marketplaceAcceptance.updateMany({
|
|
85
|
-
where: { id: { in: lapsed.map((row) => row.id) }, status: "AWAITING", ...NO_STANDING_REQUEST },
|
|
86
|
-
data: { status: "EXPIRED", acknowledgedAt: now }
|
|
87
|
-
});
|
|
88
|
-
for (const row of lapsed) {
|
|
89
|
-
try {
|
|
90
|
-
await ctx.ports.order.onRefused(row.clientId, row.orderId, "EXPIRED");
|
|
91
|
-
} catch (error) {
|
|
92
|
-
ctx.ports.logger.error(
|
|
93
|
-
`marketplace.expire-acceptances: order ${row.orderId} expired but the host's refusal failed: ` + (error instanceof Error ? error.message : String(error))
|
|
94
|
-
);
|
|
95
|
-
}
|
|
96
|
-
}
|
|
97
|
-
const tenants = new Set(lapsed.map((row) => row.clientId));
|
|
98
|
-
for (const clientId of tenants) ctx.ports.publishChanged(clientId);
|
|
99
|
-
return { expired: result.count, tenants: tenants.size };
|
|
100
|
-
}
|
|
101
|
-
__name(expireLapsedAcceptances, "expireLapsedAcceptances");
|
|
102
|
-
function hasConfirmHook(ctx) {
|
|
103
|
-
return ctx.ports.order.onCancellationConfirmed !== void 0;
|
|
104
|
-
}
|
|
105
|
-
__name(hasConfirmHook, "hasConfirmHook");
|
|
106
|
-
async function cancelAcceptance(ctx, input) {
|
|
107
|
-
const db = await ctx.ports.db();
|
|
108
|
-
const row = await db.marketplaceAcceptance.findFirst({
|
|
109
|
-
where: {
|
|
110
|
-
clientId: input.clientId,
|
|
111
|
-
provider: input.provider,
|
|
112
|
-
externalOrderId: input.externalOrderId
|
|
113
|
-
},
|
|
114
|
-
select: { orderId: true, cancellationStatus: true }
|
|
115
|
-
});
|
|
116
|
-
if (!row) return false;
|
|
117
|
-
const answered2 = await ctx.ports.transaction(async (tx) => {
|
|
118
|
-
await rejectConfirmed(tx, row.orderId, input.now ?? ctx.ports.clock.now());
|
|
119
|
-
const confirmed = await tx.marketplaceAcceptance.updateMany({
|
|
120
|
-
where: { orderId: row.orderId, cancellationStatus: { in: ["PENDING", "REQUESTED", "FAILED"] } },
|
|
121
|
-
data: { cancellationStatus: "CONFIRMED", cancellationFailureReason: null }
|
|
122
|
-
});
|
|
123
|
-
return confirmed.count > 0;
|
|
124
|
-
});
|
|
125
|
-
const storeAsked = answered2 || row.cancellationStatus === "CONFIRMED";
|
|
126
|
-
const told = storeAsked && (answered2 ? await tellCancellationConfirmed(ctx, input.clientId, row.orderId) : hasConfirmHook(ctx));
|
|
127
|
-
if (!told) await ctx.ports.order.onRefused(input.clientId, row.orderId, "CANCELLED");
|
|
128
|
-
ctx.ports.publishChanged(input.clientId);
|
|
129
|
-
return true;
|
|
130
|
-
}
|
|
131
|
-
__name(cancelAcceptance, "cancelAcceptance");
|
|
132
|
-
|
|
133
|
-
// src/core/menu-links.ts
|
|
134
|
-
var LINK_SELECT = {
|
|
135
|
-
id: true,
|
|
136
|
-
provider: true,
|
|
137
|
-
externalSku: true,
|
|
138
|
-
externalName: true,
|
|
139
|
-
menuItemId: true,
|
|
140
|
-
lastSeenAt: true
|
|
141
|
-
};
|
|
142
|
-
function toView(row) {
|
|
143
|
-
return {
|
|
144
|
-
id: row.id,
|
|
145
|
-
provider: row.provider,
|
|
146
|
-
externalSku: row.externalSku,
|
|
147
|
-
externalName: row.externalName ?? row.externalSku,
|
|
148
|
-
menuItemId: row.menuItemId,
|
|
149
|
-
lastSeenAt: row.lastSeenAt
|
|
150
|
-
};
|
|
151
|
-
}
|
|
152
|
-
__name(toView, "toView");
|
|
153
|
-
async function listMarketplaceMenuLinks(ctx, input) {
|
|
154
|
-
const db = await ctx.ports.db();
|
|
155
|
-
const rows = await db.marketplaceMenuLink.findMany({
|
|
156
|
-
where: {
|
|
157
|
-
clientId: input.clientId,
|
|
158
|
-
...input.ids ? { id: { in: [...input.ids] } } : {},
|
|
159
|
-
...input.provider ? { provider: input.provider } : {},
|
|
160
|
-
...input.unmappedOnly ? { menuItemId: null } : {}
|
|
161
|
-
},
|
|
162
|
-
select: LINK_SELECT,
|
|
163
|
-
orderBy: { lastSeenAt: "desc" }
|
|
164
|
-
});
|
|
165
|
-
return rows.map(toView);
|
|
166
|
-
}
|
|
167
|
-
__name(listMarketplaceMenuLinks, "listMarketplaceMenuLinks");
|
|
168
|
-
async function upsertLink(db, input) {
|
|
169
|
-
const row = await db.marketplaceMenuLink.upsert({
|
|
170
|
-
where: {
|
|
171
|
-
clientId_provider_externalSku: {
|
|
172
|
-
clientId: input.clientId,
|
|
173
|
-
provider: input.provider,
|
|
174
|
-
externalSku: input.externalSku
|
|
175
|
-
}
|
|
176
|
-
},
|
|
177
|
-
create: {
|
|
178
|
-
clientId: input.clientId,
|
|
179
|
-
provider: input.provider,
|
|
180
|
-
externalSku: input.externalSku,
|
|
181
|
-
externalName: input.externalName ?? input.externalSku,
|
|
182
|
-
menuItemId: input.menuItemId
|
|
183
|
-
},
|
|
184
|
-
// `externalName` is NOT rewritten here. It is the marketplace's snapshot of
|
|
185
|
-
// what it called the item, and an operator's typing is not a better record
|
|
186
|
-
// of that than the marketplace's own text.
|
|
187
|
-
update: { menuItemId: input.menuItemId },
|
|
188
|
-
select: LINK_SELECT
|
|
189
|
-
});
|
|
190
|
-
return toView(row);
|
|
191
|
-
}
|
|
192
|
-
__name(upsertLink, "upsertLink");
|
|
193
|
-
async function resolveSkus(db, input) {
|
|
194
|
-
const resolved = /* @__PURE__ */ new Map();
|
|
195
|
-
for (const sku of input.skus) {
|
|
196
|
-
const row = await db.marketplaceMenuLink.upsert({
|
|
197
|
-
where: {
|
|
198
|
-
clientId_provider_externalSku: {
|
|
199
|
-
clientId: input.clientId,
|
|
200
|
-
provider: input.provider,
|
|
201
|
-
externalSku: sku.externalSku
|
|
202
|
-
}
|
|
203
|
-
},
|
|
204
|
-
create: {
|
|
205
|
-
clientId: input.clientId,
|
|
206
|
-
provider: input.provider,
|
|
207
|
-
externalSku: sku.externalSku,
|
|
208
|
-
externalName: sku.name,
|
|
209
|
-
lastSeenAt: input.seenAt
|
|
210
|
-
},
|
|
211
|
-
update: { lastSeenAt: input.seenAt },
|
|
212
|
-
select: { menuItemId: true }
|
|
213
|
-
});
|
|
214
|
-
resolved.set(sku.externalSku, row.menuItemId);
|
|
215
|
-
}
|
|
216
|
-
return resolved;
|
|
217
|
-
}
|
|
218
|
-
__name(resolveSkus, "resolveSkus");
|
|
219
|
-
|
|
220
70
|
// src/core/bulk.ts
|
|
221
71
|
function duplicates(rows, keyOf, list, field) {
|
|
222
72
|
const seen = /* @__PURE__ */ new Set();
|
|
@@ -374,7 +224,6 @@ var CLEARED_REQUEST = {
|
|
|
374
224
|
cancellationStatus: null,
|
|
375
225
|
cancellationReasonCode: null,
|
|
376
226
|
cancellationOffendingSkus: null,
|
|
377
|
-
cancellationRequestedAt: null,
|
|
378
227
|
cancellationFailureReason: null
|
|
379
228
|
};
|
|
380
229
|
function planOne(ctx, input, index, row, now) {
|
|
@@ -404,7 +253,7 @@ function requestOnly(ctx, plan) {
|
|
|
404
253
|
__name(requestOnly, "requestOnly");
|
|
405
254
|
async function admitAndClaim(ctx, clientId, planned, validateOnly) {
|
|
406
255
|
await ctx.ports.transaction(async (tx) => {
|
|
407
|
-
const
|
|
256
|
+
const refused2 = [];
|
|
408
257
|
for (const one of planned) {
|
|
409
258
|
const admission = one.input.decision === "ACCEPT" ? one.input.admission ?? {} : {};
|
|
410
259
|
const answer = await ctx.ports.order.admit({
|
|
@@ -416,18 +265,18 @@ async function admitAndClaim(ctx, clientId, planned, validateOnly) {
|
|
|
416
265
|
});
|
|
417
266
|
if (!answer.admitted) {
|
|
418
267
|
const host = { code: answer.code, message: answer.message, details: answer.details };
|
|
419
|
-
|
|
268
|
+
refused2.push({ path: ["decisions", one.index], code: "HOST_REFUSED", host });
|
|
420
269
|
}
|
|
421
270
|
}
|
|
422
|
-
if (
|
|
271
|
+
if (refused2.length > 0) throw new Rollback(refused2);
|
|
423
272
|
for (const one of planned) {
|
|
424
273
|
const claimed = await tx.marketplaceAcceptance.updateMany({
|
|
425
274
|
where: { orderId: one.input.orderId, clientId, status: "AWAITING", ...NO_STANDING_REQUEST },
|
|
426
275
|
data: one.data
|
|
427
276
|
});
|
|
428
|
-
if (claimed.count === 0)
|
|
277
|
+
if (claimed.count === 0) refused2.push({ path: ["decisions", one.index, "orderId"], code: "CONFLICT" });
|
|
429
278
|
}
|
|
430
|
-
if (
|
|
279
|
+
if (refused2.length > 0) throw new Rollback(refused2);
|
|
431
280
|
if (validateOnly) throw new Rollback(null);
|
|
432
281
|
});
|
|
433
282
|
}
|
|
@@ -538,153 +387,116 @@ async function claimStoreCancellation(ctx, input, row, plan) {
|
|
|
538
387
|
}
|
|
539
388
|
__name(claimStoreCancellation, "claimStoreCancellation");
|
|
540
389
|
|
|
541
|
-
// src/core/
|
|
542
|
-
|
|
543
|
-
|
|
544
|
-
|
|
545
|
-
|
|
546
|
-
|
|
547
|
-
|
|
548
|
-
|
|
549
|
-
|
|
550
|
-
|
|
551
|
-
|
|
552
|
-
data: {
|
|
553
|
-
clientId: input.clientId,
|
|
554
|
-
provider: input.event.provider,
|
|
555
|
-
eventId: input.event.eventId,
|
|
556
|
-
eventType: input.event.eventType,
|
|
557
|
-
externalOrderId: input.event.externalOrderId,
|
|
558
|
-
payload: input.payload
|
|
559
|
-
},
|
|
560
|
-
select: { id: true }
|
|
561
|
-
});
|
|
562
|
-
return { id: row.id, fresh: true };
|
|
563
|
-
} catch (error) {
|
|
564
|
-
if (error.code !== "P2002") throw error;
|
|
565
|
-
const raced = await db.marketplaceInboxEvent.findUnique({
|
|
566
|
-
where: key,
|
|
567
|
-
select: { id: true }
|
|
568
|
-
});
|
|
569
|
-
return { id: raced?.id ?? "", fresh: false };
|
|
570
|
-
}
|
|
571
|
-
}
|
|
572
|
-
__name(recordInboxEvent, "recordInboxEvent");
|
|
573
|
-
async function settleInboxEvent(ctx, id, outcome, error) {
|
|
574
|
-
if (!id) return;
|
|
575
|
-
const db = await ctx.ports.db();
|
|
576
|
-
await db.marketplaceInboxEvent.update({
|
|
577
|
-
where: { id },
|
|
578
|
-
data: {
|
|
579
|
-
status: outcome,
|
|
580
|
-
// Truncated: a provider's error body can be a page of HTML, and the
|
|
581
|
-
// column exists to be read by a human in a list.
|
|
582
|
-
error: error ? error.slice(0, 500) : null,
|
|
583
|
-
processedAt: ctx.ports.clock.now(),
|
|
584
|
-
attempts: { increment: 1 }
|
|
585
|
-
}
|
|
390
|
+
// src/core/catalogue.ts
|
|
391
|
+
var refused = /* @__PURE__ */ __name((code) => ({ kind: "refused", code }), "refused");
|
|
392
|
+
async function usableTable(ctx, provider) {
|
|
393
|
+
if (!ctx.ports.catalogue || !ctx.registry.get(provider)?.catalogue) return null;
|
|
394
|
+
return catalogueTable(ctx);
|
|
395
|
+
}
|
|
396
|
+
__name(usableTable, "usableTable");
|
|
397
|
+
async function findRow(table, target) {
|
|
398
|
+
return await table.findFirst({
|
|
399
|
+
where: { clientId: target.clientId, provider: target.provider, productId: target.productId },
|
|
400
|
+
select: CATALOGUE_ROW_SELECT
|
|
586
401
|
});
|
|
587
402
|
}
|
|
588
|
-
__name(
|
|
589
|
-
async function
|
|
590
|
-
|
|
591
|
-
|
|
592
|
-
|
|
593
|
-
|
|
594
|
-
|
|
403
|
+
__name(findRow, "findRow");
|
|
404
|
+
async function queue(ctx, row, action) {
|
|
405
|
+
const payload = { clientId: row.clientId, publicationId: row.id, action };
|
|
406
|
+
await enqueueCatalogue(ctx, payload, catalogueDedupeKey(payload, row.requestedAt));
|
|
407
|
+
ctx.ports.publishChanged(row.clientId);
|
|
408
|
+
return { kind: "queued", publication: viewOf(row) };
|
|
409
|
+
}
|
|
410
|
+
__name(queue, "queue");
|
|
411
|
+
var validPrice = /* @__PURE__ */ __name((price) => price === void 0 || price === null || Number.isInteger(price) && price > 0, "validPrice");
|
|
412
|
+
async function publishRefusal(ctx, input) {
|
|
413
|
+
if (!validPrice(input.priceCents)) return "INVALID_PRICE";
|
|
414
|
+
if ("problem" in await catalogueConnection(ctx, input.clientId, input.provider)) return "NOT_CONNECTED";
|
|
415
|
+
const items = await ctx.ports.catalogue?.items(input.clientId, [input.productId]);
|
|
416
|
+
return items?.has(input.productId) ? null : "UNKNOWN_PRODUCT";
|
|
417
|
+
}
|
|
418
|
+
__name(publishRefusal, "publishRefusal");
|
|
419
|
+
async function publishToCatalogue(ctx, input) {
|
|
420
|
+
const table = await usableTable(ctx, input.provider);
|
|
421
|
+
if (!table) return refused("UNSUPPORTED");
|
|
422
|
+
const refusal = await publishRefusal(ctx, input);
|
|
423
|
+
if (refusal) return refused(refusal);
|
|
424
|
+
const existing = await findRow(table, input);
|
|
425
|
+
const now = ctx.ports.clock.now();
|
|
426
|
+
const price = input.priceCents === void 0 ? {} : { priceCents: input.priceCents };
|
|
427
|
+
const samePrice = input.priceCents === void 0 || input.priceCents === existing?.priceCents;
|
|
428
|
+
const asked = existing?.status === "PUBLISHING" && samePrice ? {} : { requestedAt: now };
|
|
429
|
+
const row = await table.upsert({
|
|
430
|
+
where: { clientId_provider_productId: { clientId: input.clientId, provider: input.provider, productId: input.productId } },
|
|
431
|
+
create: { clientId: input.clientId, provider: input.provider, productId: input.productId, status: "PUBLISHING", requestedAt: now, ...price },
|
|
432
|
+
update: { status: "PUBLISHING", ...NO_FAILURE, ...asked, ...price },
|
|
433
|
+
select: CATALOGUE_ROW_SELECT
|
|
595
434
|
});
|
|
435
|
+
return queue(ctx, row, "publish");
|
|
436
|
+
}
|
|
437
|
+
__name(publishToCatalogue, "publishToCatalogue");
|
|
438
|
+
async function withdrawFromCatalogue(ctx, target) {
|
|
439
|
+
const table = await usableTable(ctx, target.provider);
|
|
440
|
+
if (!table) return refused("UNSUPPORTED");
|
|
441
|
+
const existing = await findRow(table, target);
|
|
442
|
+
if (!existing || existing.status === "WITHDRAWN") return { kind: "unchanged", publication: existing && viewOf(existing) };
|
|
443
|
+
if (existing.status === "WITHDRAWING") return queue(ctx, existing, "withdraw");
|
|
444
|
+
const now = ctx.ports.clock.now();
|
|
445
|
+
const local = await connectionMerchantId(ctx, target.clientId, target.provider) === null;
|
|
446
|
+
const data = local ? { status: "WITHDRAWN", withdrawnAt: now, requestedAt: now, ...NO_FAILURE } : { status: "WITHDRAWING", requestedAt: now, ...NO_FAILURE };
|
|
447
|
+
const moved = await table.updateMany({ where: { id: existing.id, status: existing.status }, data });
|
|
448
|
+
const row = await findRow(table, target) ?? existing;
|
|
449
|
+
if (moved.count === 0 || local) {
|
|
450
|
+
ctx.ports.publishChanged(target.clientId);
|
|
451
|
+
return { kind: "unchanged", publication: viewOf(row) };
|
|
452
|
+
}
|
|
453
|
+
return queue(ctx, row, "withdraw");
|
|
596
454
|
}
|
|
597
|
-
__name(
|
|
598
|
-
async function
|
|
599
|
-
const
|
|
600
|
-
|
|
455
|
+
__name(withdrawFromCatalogue, "withdrawFromCatalogue");
|
|
456
|
+
async function readCatalogueState(ctx, input) {
|
|
457
|
+
const table = await catalogueTable(ctx);
|
|
458
|
+
if (!table || input.productIds.length === 0) return [];
|
|
459
|
+
const rows = await table.findMany({
|
|
601
460
|
where: {
|
|
602
|
-
|
|
603
|
-
|
|
604
|
-
|
|
461
|
+
clientId: input.clientId,
|
|
462
|
+
productId: { in: [...input.productIds] },
|
|
463
|
+
...input.providers ? { provider: { in: [...input.providers] } } : {}
|
|
605
464
|
},
|
|
606
|
-
|
|
607
|
-
take: ctx.ports.reconcileBatchSize,
|
|
608
|
-
select: { id: true, clientId: true, provider: true, payload: true }
|
|
465
|
+
select: CATALOGUE_ROW_SELECT
|
|
609
466
|
});
|
|
610
|
-
return rows.map(
|
|
611
|
-
|
|
612
|
-
|
|
613
|
-
|
|
614
|
-
|
|
615
|
-
|
|
616
|
-
|
|
617
|
-
|
|
618
|
-
|
|
619
|
-
|
|
620
|
-
|
|
621
|
-
|
|
622
|
-
|
|
623
|
-
|
|
624
|
-
|
|
625
|
-
|
|
626
|
-
}
|
|
627
|
-
|
|
628
|
-
|
|
629
|
-
|
|
630
|
-
|
|
631
|
-
// was shown is the marketplace's menu — a mapped line renamed to the host's
|
|
632
|
-
// own product name would quietly disagree with the receipt in the bag.
|
|
633
|
-
name: line.name,
|
|
634
|
-
quantity: line.quantity,
|
|
635
|
-
unitPriceCents: line.unitPriceCents,
|
|
636
|
-
note: line.note,
|
|
637
|
-
subjectRef: mapping.get(line.externalSku) ?? null
|
|
638
|
-
}));
|
|
639
|
-
}
|
|
640
|
-
__name(composeLines, "composeLines");
|
|
641
|
-
function acceptanceData(clientId, orderId, order, settings, now) {
|
|
642
|
-
const accepted = settings.autoAccept;
|
|
643
|
-
return {
|
|
644
|
-
clientId,
|
|
645
|
-
orderId,
|
|
646
|
-
provider: order.provider,
|
|
647
|
-
externalOrderId: order.externalOrderId,
|
|
648
|
-
externalDisplayId: order.externalDisplayId,
|
|
649
|
-
// Who carries a delivery: the merchant's courier or the marketplace's fleet.
|
|
650
|
-
deliveredBy: order.deliveredBy ?? null,
|
|
651
|
-
status: accepted ? "ACCEPTED" : "AWAITING",
|
|
652
|
-
acknowledgeBy: new Date(now.getTime() + settings.ackSlaSeconds * 1e3),
|
|
653
|
-
acknowledgedAt: accepted ? now : null,
|
|
654
|
-
externalDueAt: order.deliveryDueAt,
|
|
655
|
-
commissionCents: order.commissionCents,
|
|
656
|
-
// The order's SKUs: what a cancellation's offending SKUs are checked against.
|
|
657
|
-
lineSkus: [...new Set(order.lines.map((line) => line.externalSku))]
|
|
658
|
-
};
|
|
659
|
-
}
|
|
660
|
-
__name(acceptanceData, "acceptanceData");
|
|
661
|
-
async function ingestMarketplaceOrder(ctx, input) {
|
|
662
|
-
const { clientId, order, settings } = input;
|
|
663
|
-
const now = input.now ?? ctx.ports.clock.now();
|
|
664
|
-
const db = await ctx.ports.db();
|
|
665
|
-
const existing = await db.marketplaceAcceptance.findFirst({
|
|
666
|
-
where: { provider: order.provider, externalOrderId: order.externalOrderId },
|
|
667
|
-
select: { orderId: true }
|
|
467
|
+
return rows.map(viewOf);
|
|
468
|
+
}
|
|
469
|
+
__name(readCatalogueState, "readCatalogueState");
|
|
470
|
+
var IN_MENU = ["PUBLISHING", "PUBLISHED", "FAILED"];
|
|
471
|
+
function menuPrice(row, storePrice) {
|
|
472
|
+
if (row.status !== "FAILED") return row.priceCents ?? storePrice;
|
|
473
|
+
const live = row.publishedAt !== null && (row.withdrawnAt === null || row.withdrawnAt < row.publishedAt);
|
|
474
|
+
const republishFailed = row.failedAction === "PUBLISH" && live;
|
|
475
|
+
return republishFailed ? row.sentPriceCents ?? row.priceCents ?? storePrice : null;
|
|
476
|
+
}
|
|
477
|
+
__name(menuPrice, "menuPrice");
|
|
478
|
+
async function readCatalogueDocument(ctx, input) {
|
|
479
|
+
const catalogue = ctx.registry.get(input.provider)?.catalogue;
|
|
480
|
+
const table = await usableTable(ctx, input.provider);
|
|
481
|
+
if (!table || catalogue?.mode !== "pull") return { kind: "refused", code: "UNSUPPORTED" };
|
|
482
|
+
const connection = await catalogueConnection(ctx, input.clientId, input.provider);
|
|
483
|
+
if ("problem" in connection) return { kind: "refused", code: "NOT_CONNECTED" };
|
|
484
|
+
const rows = await table.findMany({
|
|
485
|
+
where: { clientId: input.clientId, provider: input.provider, status: { in: IN_MENU } },
|
|
486
|
+
select: CATALOGUE_ROW_SELECT,
|
|
487
|
+
orderBy: { createdAt: "asc" }
|
|
668
488
|
});
|
|
669
|
-
|
|
670
|
-
|
|
671
|
-
|
|
672
|
-
|
|
673
|
-
|
|
674
|
-
|
|
675
|
-
const created = await ctx.ports.order.create(clientId, order, lines, tx);
|
|
676
|
-
await tx.marketplaceAcceptance.create({ data: acceptanceData(clientId, created, order, settings, now) });
|
|
677
|
-
await markMarketplaceEventSeen(tx, clientId, order.provider, now);
|
|
678
|
-
return { orderId: created, unmappedSkus: lines.filter((l) => l.subjectRef === null).map((l) => l.externalSku) };
|
|
489
|
+
const items = await ctx.ports.catalogue?.items(input.clientId, rows.map((row) => row.productId)) ?? /* @__PURE__ */ new Map();
|
|
490
|
+
const entries = rows.flatMap((row) => {
|
|
491
|
+
const item = items.get(row.productId);
|
|
492
|
+
const priceCents = item ? menuPrice(row, item.priceCents) : null;
|
|
493
|
+
if (!item || priceCents === null) return [];
|
|
494
|
+
return [{ ...item, priceCents, ref: row.id, productId: row.productId, externalId: row.externalId, storedImage: null }];
|
|
679
495
|
});
|
|
680
|
-
|
|
681
|
-
|
|
682
|
-
await ctx.ports.order.onAccepted(clientId, orderId);
|
|
683
|
-
}
|
|
684
|
-
ctx.ports.publishChanged(clientId);
|
|
685
|
-
return { orderId, created: true, unmappedSkus, autoAccepted: accepted };
|
|
496
|
+
const document = catalogue.menuDocument({ externalMerchantId: connection.externalMerchantId, entries, options: input.options });
|
|
497
|
+
return { kind: "document", document };
|
|
686
498
|
}
|
|
687
|
-
__name(
|
|
499
|
+
__name(readCatalogueDocument, "readCatalogueDocument");
|
|
688
500
|
|
|
689
501
|
// src/core/pipeline.ts
|
|
690
502
|
function normalizeEvent(ctx, provider, payload) {
|
|
@@ -710,6 +522,15 @@ async function processDelivery(ctx, input) {
|
|
|
710
522
|
}
|
|
711
523
|
}
|
|
712
524
|
__name(processDelivery, "processDelivery");
|
|
525
|
+
function announcesOrderToFetch(event) {
|
|
526
|
+
return event.order === null && event.orderToFetch === true && event.externalOrderId !== null;
|
|
527
|
+
}
|
|
528
|
+
__name(announcesOrderToFetch, "announcesOrderToFetch");
|
|
529
|
+
async function carriedOrderMismatch(ctx, clientId, order) {
|
|
530
|
+
if (!order.externalMerchantId) return null;
|
|
531
|
+
return merchantMismatch(order, await connectionMerchantId(ctx, clientId, order.provider));
|
|
532
|
+
}
|
|
533
|
+
__name(carriedOrderMismatch, "carriedOrderMismatch");
|
|
713
534
|
async function applyEvent(ctx, inboxId, clientId, event, settings, now) {
|
|
714
535
|
const outcome = event.cancellationOutcome ?? (event.cancelled ? "CONFIRMED" : null);
|
|
715
536
|
if (outcome === "FAILED" && event.externalOrderId) {
|
|
@@ -722,30 +543,35 @@ async function applyEvent(ctx, inboxId, clientId, event, settings, now) {
|
|
|
722
543
|
return { kind: "cancellationFailed" };
|
|
723
544
|
}
|
|
724
545
|
if (event.cancelled && event.externalOrderId) {
|
|
725
|
-
|
|
726
|
-
|
|
727
|
-
provider: event.provider,
|
|
728
|
-
externalOrderId: event.externalOrderId,
|
|
729
|
-
now
|
|
730
|
-
});
|
|
546
|
+
const order = { clientId, provider: event.provider, externalOrderId: event.externalOrderId };
|
|
547
|
+
if (!await cancelAcceptance(ctx, { ...order, now })) await markCancelledBeforeIngest(ctx, order);
|
|
731
548
|
await settleInboxEvent(ctx, inboxId, "PROCESSED");
|
|
732
549
|
return { kind: "cancelled" };
|
|
733
550
|
}
|
|
551
|
+
if (announcesOrderToFetch(event)) {
|
|
552
|
+
await awaitOrderBody(ctx, clientId, inboxId);
|
|
553
|
+
return { kind: "awaitingOrder" };
|
|
554
|
+
}
|
|
734
555
|
if (!event.order) {
|
|
735
556
|
const reason = `no order body on ${event.eventType}`;
|
|
736
557
|
await settleInboxEvent(ctx, inboxId, "IGNORED", reason);
|
|
737
558
|
return { kind: "ignored", reason };
|
|
738
559
|
}
|
|
739
|
-
|
|
740
|
-
|
|
741
|
-
|
|
742
|
-
|
|
743
|
-
|
|
744
|
-
|
|
560
|
+
return ingestCarried(ctx, inboxId, clientId, event.order, settings, now);
|
|
561
|
+
}
|
|
562
|
+
__name(applyEvent, "applyEvent");
|
|
563
|
+
async function ingestCarried(ctx, inboxId, clientId, order, settings, now) {
|
|
564
|
+
const mismatch = await carriedOrderMismatch(ctx, clientId, order);
|
|
565
|
+
if (mismatch) {
|
|
566
|
+
await settleInboxEvent(ctx, inboxId, "FAILED", mismatch);
|
|
567
|
+
ctx.ports.logger.error(`marketplace.ingest: inbox row ${inboxId} refused: ${mismatch}`);
|
|
568
|
+
return { kind: "failed", reason: mismatch };
|
|
569
|
+
}
|
|
570
|
+
const result = await ingestMarketplaceOrder(ctx, { clientId, order, settings, now });
|
|
745
571
|
await settleInboxEvent(ctx, inboxId, "PROCESSED");
|
|
746
572
|
return { kind: "ingested", orderId: result.orderId, unmappedSkus: result.unmappedSkus };
|
|
747
573
|
}
|
|
748
|
-
__name(
|
|
574
|
+
__name(ingestCarried, "ingestCarried");
|
|
749
575
|
async function reprocessStoredDelivery(ctx, input) {
|
|
750
576
|
const event = normalizeEvent(ctx, input.provider, input.payload);
|
|
751
577
|
if (!event) {
|
|
@@ -759,6 +585,7 @@ async function reprocessStoredDelivery(ctx, input) {
|
|
|
759
585
|
await deferInboxEvent(ctx, input.id, reason);
|
|
760
586
|
return { kind: "ignored", reason };
|
|
761
587
|
}
|
|
588
|
+
if (announcesOrderToFetch(event)) return requeueOwedFetch(ctx, input);
|
|
762
589
|
try {
|
|
763
590
|
return await applyEvent(
|
|
764
591
|
ctx,
|
|
@@ -861,11 +688,25 @@ function createMarketplaceEngine(config) {
|
|
|
861
688
|
retryable: /* @__PURE__ */ __name(() => listRetryableInboxEvents(ctx), "retryable"),
|
|
862
689
|
reprocess: /* @__PURE__ */ __name((event) => reprocessStoredDelivery(ctx, event), "reprocess"),
|
|
863
690
|
sweepOutbound: /* @__PURE__ */ __name((now) => sweepOutbound(ctx, now ?? ports.clock.now()), "sweepOutbound"),
|
|
864
|
-
runOutbound: /* @__PURE__ */ __name((payload, attempt) => runOutbound(ctx, payload, attempt), "runOutbound")
|
|
691
|
+
runOutbound: /* @__PURE__ */ __name((payload, attempt) => runOutbound(ctx, payload, attempt), "runOutbound"),
|
|
692
|
+
runFetchOrder: /* @__PURE__ */ __name((payload, attempt) => runFetchOrder(ctx, payload, attempt), "runFetchOrder"),
|
|
693
|
+
// Null too when the host bound no catalogue port: nothing could be published.
|
|
694
|
+
catalogueMode: /* @__PURE__ */ __name((provider) => ports.catalogue ? registry.get(provider)?.catalogue?.mode ?? null : null, "catalogueMode"),
|
|
695
|
+
publish: /* @__PURE__ */ __name((input) => publishToCatalogue(ctx, input), "publish"),
|
|
696
|
+
withdraw: /* @__PURE__ */ __name((input) => withdrawFromCatalogue(ctx, input), "withdraw"),
|
|
697
|
+
catalogueState: /* @__PURE__ */ __name((input) => readCatalogueState(ctx, input), "catalogueState"),
|
|
698
|
+
catalogueDocument: /* @__PURE__ */ __name((input) => readCatalogueDocument(ctx, input), "catalogueDocument"),
|
|
699
|
+
runCatalogue: /* @__PURE__ */ __name((payload, attempt) => runCatalogue(ctx, payload, attempt), "runCatalogue")
|
|
865
700
|
};
|
|
866
701
|
}
|
|
867
702
|
__name(createMarketplaceEngine, "createMarketplaceEngine");
|
|
868
703
|
export {
|
|
704
|
+
CATALOGUE_GIVE_UP_MS,
|
|
705
|
+
CATALOGUE_STALE_MS,
|
|
706
|
+
CatalogueRetryError,
|
|
707
|
+
FetchOrderRetryError,
|
|
708
|
+
MARKETPLACE_CATALOGUE_JOB,
|
|
709
|
+
MARKETPLACE_FETCH_ORDER_JOB,
|
|
869
710
|
MARKETPLACE_OUTBOUND_JOB,
|
|
870
711
|
MarketplaceAdapterRegistrationError,
|
|
871
712
|
OUTBOUND_GIVE_UP_MS,
|
|
@@ -873,11 +714,13 @@ export {
|
|
|
873
714
|
OutboundRetryError,
|
|
874
715
|
PROVIDER_ID_PATTERN,
|
|
875
716
|
answered,
|
|
717
|
+
catalogueDedupeKey,
|
|
876
718
|
createAdapterRegistry,
|
|
877
719
|
createMarketplaceEngine,
|
|
878
720
|
outboundDedupeKey,
|
|
879
721
|
outboundResultOf,
|
|
880
722
|
resolveReason,
|
|
723
|
+
stableUuid,
|
|
881
724
|
suggestReason
|
|
882
725
|
};
|
|
883
726
|
//# sourceMappingURL=index.js.map
|