@12-apps/marketplace 1.2.0 → 1.4.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 +76 -4
- package/README.md +163 -3
- package/dist/99food/index.d.ts +79 -2
- package/dist/99food/index.js +117 -2
- package/dist/99food/index.js.map +1 -1
- package/dist/adapter-B8QtQ2RA.d.ts +612 -0
- package/dist/{chunk-CRYFYDQ2.js → chunk-DPCZVN2C.js} +69 -5
- package/dist/chunk-DPCZVN2C.js.map +1 -0
- package/dist/{chunk-D2X7TOR6.js → chunk-EBAIZQPB.js} +841 -86
- package/dist/chunk-EBAIZQPB.js.map +1 -0
- package/dist/chunk-M2V4RFMA.js +23 -0
- package/dist/chunk-M2V4RFMA.js.map +1 -0
- package/dist/{chunk-24RQZPSV.js → chunk-SZRZTLVU.js} +1 -1
- package/dist/chunk-SZRZTLVU.js.map +1 -0
- package/dist/{engine-D-a4mttc.d.ts → engine-o5cm54vC.d.ts} +462 -2
- package/dist/hono/index.d.ts +2 -2
- package/dist/hono/index.js +2 -2
- package/dist/ifood/index.d.ts +22 -2
- package/dist/ifood/index.js +396 -56
- package/dist/ifood/index.js.map +1 -1
- package/dist/index.d.ts +30 -4
- package/dist/index.js +445 -9
- package/dist/index.js.map +1 -1
- package/dist/manifest/server.d.ts +5 -2
- package/dist/manifest/server.js +2 -2
- package/dist/rappi/index.d.ts +15 -2
- package/dist/rappi/index.js +62 -2
- package/dist/rappi/index.js.map +1 -1
- package/dist/server/index.d.ts +25 -10
- package/dist/server/index.js +4 -2
- package/package.json +1 -1
- package/prisma/marketplace.prisma +158 -1
- package/prisma/migrations/20261113120000_marketplace_catalogue/migration.sql +97 -0
- package/prisma/migrations/20261113230000_marketplace_store_pause/migration.sql +70 -0
- package/src/99food/catalogue.ts +117 -0
- package/src/99food/index.ts +14 -0
- package/src/99food/outbound.ts +3 -3
- package/src/99food/pause.ts +56 -0
- package/src/core/adapter.ts +20 -1
- 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/engine.ts +83 -0
- package/src/core/outbound-sweep.ts +7 -2
- 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 +47 -0
- package/src/ifood/catalogue.ts +273 -0
- package/src/ifood/index.ts +8 -0
- package/src/ifood/outbound.ts +9 -6
- package/src/ifood/pause.ts +257 -0
- package/src/index.ts +64 -0
- package/src/rappi/index.ts +17 -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 +100 -10
- package/dist/adapter-_iuSxvVD.d.ts +0 -319
- package/dist/chunk-24RQZPSV.js.map +0 -1
- package/dist/chunk-CRYFYDQ2.js.map +0 -1
- package/dist/chunk-D2X7TOR6.js.map +0 -1
package/dist/index.js
CHANGED
|
@@ -1,22 +1,39 @@
|
|
|
1
1
|
import {
|
|
2
|
+
AvailabilityRetryError,
|
|
3
|
+
CATALOGUE_GIVE_UP_MS,
|
|
4
|
+
CATALOGUE_ROW_SELECT,
|
|
5
|
+
CATALOGUE_STALE_MS,
|
|
2
6
|
CLAIM_SELECT,
|
|
7
|
+
CatalogueRetryError,
|
|
3
8
|
FetchOrderRetryError,
|
|
9
|
+
MARKETPLACE_AVAILABILITY_JOB,
|
|
10
|
+
MARKETPLACE_CATALOGUE_JOB,
|
|
4
11
|
MARKETPLACE_FETCH_ORDER_JOB,
|
|
5
12
|
MARKETPLACE_OUTBOUND_JOB,
|
|
6
13
|
MarketplaceAdapterRegistrationError,
|
|
14
|
+
NO_FAILURE,
|
|
7
15
|
NO_STANDING_REQUEST,
|
|
8
16
|
OUTBOUND_GIVE_UP_MS,
|
|
9
17
|
OUTBOUND_STALE_MS,
|
|
10
18
|
OutboundRetryError,
|
|
11
19
|
PROVIDER_ID_PATTERN,
|
|
20
|
+
STORE_PAUSE_LEASE_MS,
|
|
21
|
+
availabilityDedupeKey,
|
|
12
22
|
awaitOrderBody,
|
|
13
23
|
cancelAcceptance,
|
|
24
|
+
catalogueConnection,
|
|
25
|
+
catalogueDedupeKey,
|
|
26
|
+
catalogueTable,
|
|
14
27
|
connectionMerchantId,
|
|
15
28
|
createAdapterRegistry,
|
|
16
29
|
deferInboxEvent,
|
|
30
|
+
enqueueAvailability,
|
|
31
|
+
enqueueCatalogue,
|
|
17
32
|
enqueueOutbound,
|
|
18
33
|
expireLapsedAcceptances,
|
|
19
34
|
failCancellationRequest,
|
|
35
|
+
hasPauseTable,
|
|
36
|
+
holdsSomething,
|
|
20
37
|
ingestMarketplaceOrder,
|
|
21
38
|
listMarketplaceIntegrations,
|
|
22
39
|
listMarketplaceMenuLinks,
|
|
@@ -26,7 +43,9 @@ import {
|
|
|
26
43
|
merchantMismatch,
|
|
27
44
|
notPendingProblem,
|
|
28
45
|
outboundDedupeKey,
|
|
46
|
+
pauseTable,
|
|
29
47
|
planCancellation,
|
|
48
|
+
readPause,
|
|
30
49
|
recordInboxEvent,
|
|
31
50
|
refusalData,
|
|
32
51
|
requestData,
|
|
@@ -35,6 +54,8 @@ import {
|
|
|
35
54
|
resolveIngestionTarget,
|
|
36
55
|
resolveReason,
|
|
37
56
|
resolveTenantByMerchant,
|
|
57
|
+
runAvailability,
|
|
58
|
+
runCatalogue,
|
|
38
59
|
runFetchOrder,
|
|
39
60
|
runOutbound,
|
|
40
61
|
saveMarketplaceIntegration,
|
|
@@ -42,12 +63,16 @@ import {
|
|
|
42
63
|
suggestReason,
|
|
43
64
|
sweepOutbound,
|
|
44
65
|
tellCancellationFailed,
|
|
45
|
-
upsertLink
|
|
46
|
-
|
|
66
|
+
upsertLink,
|
|
67
|
+
viewOf
|
|
68
|
+
} from "./chunk-EBAIZQPB.js";
|
|
69
|
+
import {
|
|
70
|
+
stableUuid
|
|
71
|
+
} from "./chunk-M2V4RFMA.js";
|
|
47
72
|
import {
|
|
48
73
|
answered,
|
|
49
74
|
outboundResultOf
|
|
50
|
-
} from "./chunk-
|
|
75
|
+
} from "./chunk-SZRZTLVU.js";
|
|
51
76
|
import {
|
|
52
77
|
__name
|
|
53
78
|
} from "./chunk-7QVYU63E.js";
|
|
@@ -238,7 +263,7 @@ function requestOnly(ctx, plan) {
|
|
|
238
263
|
__name(requestOnly, "requestOnly");
|
|
239
264
|
async function admitAndClaim(ctx, clientId, planned, validateOnly) {
|
|
240
265
|
await ctx.ports.transaction(async (tx) => {
|
|
241
|
-
const
|
|
266
|
+
const refused2 = [];
|
|
242
267
|
for (const one of planned) {
|
|
243
268
|
const admission = one.input.decision === "ACCEPT" ? one.input.admission ?? {} : {};
|
|
244
269
|
const answer = await ctx.ports.order.admit({
|
|
@@ -250,18 +275,18 @@ async function admitAndClaim(ctx, clientId, planned, validateOnly) {
|
|
|
250
275
|
});
|
|
251
276
|
if (!answer.admitted) {
|
|
252
277
|
const host = { code: answer.code, message: answer.message, details: answer.details };
|
|
253
|
-
|
|
278
|
+
refused2.push({ path: ["decisions", one.index], code: "HOST_REFUSED", host });
|
|
254
279
|
}
|
|
255
280
|
}
|
|
256
|
-
if (
|
|
281
|
+
if (refused2.length > 0) throw new Rollback(refused2);
|
|
257
282
|
for (const one of planned) {
|
|
258
283
|
const claimed = await tx.marketplaceAcceptance.updateMany({
|
|
259
284
|
where: { orderId: one.input.orderId, clientId, status: "AWAITING", ...NO_STANDING_REQUEST },
|
|
260
285
|
data: one.data
|
|
261
286
|
});
|
|
262
|
-
if (claimed.count === 0)
|
|
287
|
+
if (claimed.count === 0) refused2.push({ path: ["decisions", one.index, "orderId"], code: "CONFLICT" });
|
|
263
288
|
}
|
|
264
|
-
if (
|
|
289
|
+
if (refused2.length > 0) throw new Rollback(refused2);
|
|
265
290
|
if (validateOnly) throw new Rollback(null);
|
|
266
291
|
});
|
|
267
292
|
}
|
|
@@ -372,6 +397,395 @@ async function claimStoreCancellation(ctx, input, row, plan) {
|
|
|
372
397
|
}
|
|
373
398
|
__name(claimStoreCancellation, "claimStoreCancellation");
|
|
374
399
|
|
|
400
|
+
// src/core/catalogue.ts
|
|
401
|
+
var refused = /* @__PURE__ */ __name((code) => ({ kind: "refused", code }), "refused");
|
|
402
|
+
async function usableTable(ctx, provider) {
|
|
403
|
+
if (!ctx.ports.catalogue || !ctx.registry.get(provider)?.catalogue) return null;
|
|
404
|
+
return catalogueTable(ctx);
|
|
405
|
+
}
|
|
406
|
+
__name(usableTable, "usableTable");
|
|
407
|
+
async function findRow(table, target) {
|
|
408
|
+
return await table.findFirst({
|
|
409
|
+
where: { clientId: target.clientId, provider: target.provider, productId: target.productId },
|
|
410
|
+
select: CATALOGUE_ROW_SELECT
|
|
411
|
+
});
|
|
412
|
+
}
|
|
413
|
+
__name(findRow, "findRow");
|
|
414
|
+
async function queue(ctx, row, action) {
|
|
415
|
+
const payload = { clientId: row.clientId, publicationId: row.id, action };
|
|
416
|
+
await enqueueCatalogue(ctx, payload, catalogueDedupeKey(payload, row.requestedAt));
|
|
417
|
+
ctx.ports.publishChanged(row.clientId);
|
|
418
|
+
return { kind: "queued", publication: viewOf(row) };
|
|
419
|
+
}
|
|
420
|
+
__name(queue, "queue");
|
|
421
|
+
var validPrice = /* @__PURE__ */ __name((price) => price === void 0 || price === null || Number.isInteger(price) && price > 0, "validPrice");
|
|
422
|
+
async function publishRefusal(ctx, input) {
|
|
423
|
+
if (!validPrice(input.priceCents)) return "INVALID_PRICE";
|
|
424
|
+
if ("problem" in await catalogueConnection(ctx, input.clientId, input.provider)) return "NOT_CONNECTED";
|
|
425
|
+
const items = await ctx.ports.catalogue?.items(input.clientId, [input.productId]);
|
|
426
|
+
return items?.has(input.productId) ? null : "UNKNOWN_PRODUCT";
|
|
427
|
+
}
|
|
428
|
+
__name(publishRefusal, "publishRefusal");
|
|
429
|
+
async function publishToCatalogue(ctx, input) {
|
|
430
|
+
const table = await usableTable(ctx, input.provider);
|
|
431
|
+
if (!table) return refused("UNSUPPORTED");
|
|
432
|
+
const refusal = await publishRefusal(ctx, input);
|
|
433
|
+
if (refusal) return refused(refusal);
|
|
434
|
+
const existing = await findRow(table, input);
|
|
435
|
+
const now = ctx.ports.clock.now();
|
|
436
|
+
const price = input.priceCents === void 0 ? {} : { priceCents: input.priceCents };
|
|
437
|
+
const samePrice = input.priceCents === void 0 || input.priceCents === existing?.priceCents;
|
|
438
|
+
const asked = existing?.status === "PUBLISHING" && samePrice ? {} : { requestedAt: now };
|
|
439
|
+
const row = await table.upsert({
|
|
440
|
+
where: { clientId_provider_productId: { clientId: input.clientId, provider: input.provider, productId: input.productId } },
|
|
441
|
+
create: { clientId: input.clientId, provider: input.provider, productId: input.productId, status: "PUBLISHING", requestedAt: now, ...price },
|
|
442
|
+
update: { status: "PUBLISHING", ...NO_FAILURE, ...asked, ...price },
|
|
443
|
+
select: CATALOGUE_ROW_SELECT
|
|
444
|
+
});
|
|
445
|
+
return queue(ctx, row, "publish");
|
|
446
|
+
}
|
|
447
|
+
__name(publishToCatalogue, "publishToCatalogue");
|
|
448
|
+
async function withdrawFromCatalogue(ctx, target) {
|
|
449
|
+
const table = await usableTable(ctx, target.provider);
|
|
450
|
+
if (!table) return refused("UNSUPPORTED");
|
|
451
|
+
const existing = await findRow(table, target);
|
|
452
|
+
if (!existing || existing.status === "WITHDRAWN") return { kind: "unchanged", publication: existing && viewOf(existing) };
|
|
453
|
+
if (existing.status === "WITHDRAWING") return queue(ctx, existing, "withdraw");
|
|
454
|
+
const now = ctx.ports.clock.now();
|
|
455
|
+
const local = await connectionMerchantId(ctx, target.clientId, target.provider) === null;
|
|
456
|
+
const data = local ? { status: "WITHDRAWN", withdrawnAt: now, requestedAt: now, ...NO_FAILURE } : { status: "WITHDRAWING", requestedAt: now, ...NO_FAILURE };
|
|
457
|
+
const moved = await table.updateMany({ where: { id: existing.id, status: existing.status }, data });
|
|
458
|
+
const row = await findRow(table, target) ?? existing;
|
|
459
|
+
if (moved.count === 0 || local) {
|
|
460
|
+
ctx.ports.publishChanged(target.clientId);
|
|
461
|
+
return { kind: "unchanged", publication: viewOf(row) };
|
|
462
|
+
}
|
|
463
|
+
return queue(ctx, row, "withdraw");
|
|
464
|
+
}
|
|
465
|
+
__name(withdrawFromCatalogue, "withdrawFromCatalogue");
|
|
466
|
+
async function readCatalogueState(ctx, input) {
|
|
467
|
+
const table = await catalogueTable(ctx);
|
|
468
|
+
if (!table || input.productIds.length === 0) return [];
|
|
469
|
+
const rows = await table.findMany({
|
|
470
|
+
where: {
|
|
471
|
+
clientId: input.clientId,
|
|
472
|
+
productId: { in: [...input.productIds] },
|
|
473
|
+
...input.providers ? { provider: { in: [...input.providers] } } : {}
|
|
474
|
+
},
|
|
475
|
+
select: CATALOGUE_ROW_SELECT
|
|
476
|
+
});
|
|
477
|
+
return rows.map(viewOf);
|
|
478
|
+
}
|
|
479
|
+
__name(readCatalogueState, "readCatalogueState");
|
|
480
|
+
var IN_MENU = ["PUBLISHING", "PUBLISHED", "FAILED"];
|
|
481
|
+
function menuPrice(row, storePrice) {
|
|
482
|
+
if (row.status !== "FAILED") return row.priceCents ?? storePrice;
|
|
483
|
+
const live = row.publishedAt !== null && (row.withdrawnAt === null || row.withdrawnAt < row.publishedAt);
|
|
484
|
+
const republishFailed = row.failedAction === "PUBLISH" && live;
|
|
485
|
+
return republishFailed ? row.sentPriceCents ?? row.priceCents ?? storePrice : null;
|
|
486
|
+
}
|
|
487
|
+
__name(menuPrice, "menuPrice");
|
|
488
|
+
async function readCatalogueDocument(ctx, input) {
|
|
489
|
+
const catalogue = ctx.registry.get(input.provider)?.catalogue;
|
|
490
|
+
const table = await usableTable(ctx, input.provider);
|
|
491
|
+
if (!table || catalogue?.mode !== "pull") return { kind: "refused", code: "UNSUPPORTED" };
|
|
492
|
+
const connection = await catalogueConnection(ctx, input.clientId, input.provider);
|
|
493
|
+
if ("problem" in connection) return { kind: "refused", code: "NOT_CONNECTED" };
|
|
494
|
+
const rows = await table.findMany({
|
|
495
|
+
where: { clientId: input.clientId, provider: input.provider, status: { in: IN_MENU } },
|
|
496
|
+
select: CATALOGUE_ROW_SELECT,
|
|
497
|
+
orderBy: { createdAt: "asc" }
|
|
498
|
+
});
|
|
499
|
+
const items = await ctx.ports.catalogue?.items(input.clientId, rows.map((row) => row.productId)) ?? /* @__PURE__ */ new Map();
|
|
500
|
+
const entries = rows.flatMap((row) => {
|
|
501
|
+
const item = items.get(row.productId);
|
|
502
|
+
const priceCents = item ? menuPrice(row, item.priceCents) : null;
|
|
503
|
+
if (!item || priceCents === null) return [];
|
|
504
|
+
return [{ ...item, priceCents, ref: row.id, productId: row.productId, externalId: row.externalId, storedImage: null }];
|
|
505
|
+
});
|
|
506
|
+
const document = catalogue.menuDocument({ externalMerchantId: connection.externalMerchantId, entries, options: input.options });
|
|
507
|
+
return { kind: "document", document };
|
|
508
|
+
}
|
|
509
|
+
__name(readCatalogueDocument, "readCatalogueDocument");
|
|
510
|
+
|
|
511
|
+
// src/core/pause.ts
|
|
512
|
+
async function liveConnections(ctx, clientId) {
|
|
513
|
+
const db = await ctx.ports.db();
|
|
514
|
+
return await db.marketplaceIntegration.findMany({
|
|
515
|
+
where: { clientId, enabled: true, archivedAt: null },
|
|
516
|
+
select: { provider: true, externalMerchantId: true, credentials: true },
|
|
517
|
+
orderBy: { provider: "asc" }
|
|
518
|
+
});
|
|
519
|
+
}
|
|
520
|
+
__name(liveConnections, "liveConnections");
|
|
521
|
+
var isConnected = /* @__PURE__ */ __name((row) => row.credentials !== "" && Boolean(row.externalMerchantId), "isConnected");
|
|
522
|
+
function validTimeZone(timeZone) {
|
|
523
|
+
try {
|
|
524
|
+
new Intl.DateTimeFormat("en-US", { timeZone });
|
|
525
|
+
return timeZone !== "";
|
|
526
|
+
} catch {
|
|
527
|
+
return false;
|
|
528
|
+
}
|
|
529
|
+
}
|
|
530
|
+
__name(validTimeZone, "validTimeZone");
|
|
531
|
+
function alreadyAsked(row, until) {
|
|
532
|
+
if (!row || row.wanted !== "PAUSED") return false;
|
|
533
|
+
if (row.state !== "PAUSING" && row.state !== "PAUSED") return false;
|
|
534
|
+
return row.until !== null && row.until.getTime() === until.getTime();
|
|
535
|
+
}
|
|
536
|
+
__name(alreadyAsked, "alreadyAsked");
|
|
537
|
+
async function askPause(ctx, input, provider) {
|
|
538
|
+
const existing = await readPause(ctx, input.clientId, provider);
|
|
539
|
+
if (alreadyAsked(existing, input.until)) return "unchanged";
|
|
540
|
+
const table = pauseTable(await ctx.ports.db());
|
|
541
|
+
const data = {
|
|
542
|
+
wanted: "PAUSED",
|
|
543
|
+
state: "PAUSING",
|
|
544
|
+
until: input.until,
|
|
545
|
+
reason: input.reason,
|
|
546
|
+
timeZone: input.timeZone,
|
|
547
|
+
requestedAt: input.now,
|
|
548
|
+
// No key is minted here: the job writes one just before its call, so a
|
|
549
|
+
// resume that lands first knows nothing of ours can stand there. A key
|
|
550
|
+
// still in doubt stays on the row (its create may have landed).
|
|
551
|
+
failureCode: null,
|
|
552
|
+
failureDetail: null,
|
|
553
|
+
failedAt: null
|
|
554
|
+
};
|
|
555
|
+
await table.upsert({
|
|
556
|
+
where: { clientId_provider: { clientId: input.clientId, provider } },
|
|
557
|
+
create: { clientId: input.clientId, provider, ...data },
|
|
558
|
+
update: data
|
|
559
|
+
});
|
|
560
|
+
const payload = { clientId: input.clientId, provider };
|
|
561
|
+
await enqueueAvailability(ctx, payload, availabilityDedupeKey(payload, input.now));
|
|
562
|
+
return "queued";
|
|
563
|
+
}
|
|
564
|
+
__name(askPause, "askPause");
|
|
565
|
+
async function pauseStore(ctx, input) {
|
|
566
|
+
const now = input.now ?? ctx.ports.clock.now();
|
|
567
|
+
if (input.until.getTime() <= now.getTime()) return { kind: "invalid", code: "UNTIL_NOT_FUTURE" };
|
|
568
|
+
if (!validTimeZone(input.timeZone)) return { kind: "invalid", code: "TIME_ZONE_INVALID" };
|
|
569
|
+
pauseTable(await ctx.ports.db());
|
|
570
|
+
const full = { ...input, now };
|
|
571
|
+
const marketplaces = [];
|
|
572
|
+
for (const connection of await liveConnections(ctx, input.clientId)) {
|
|
573
|
+
const adapter = ctx.registry.get(connection.provider);
|
|
574
|
+
let outcome;
|
|
575
|
+
if (!adapter?.availability) outcome = "unsupported";
|
|
576
|
+
else if (!isConnected(connection)) outcome = "not-connected";
|
|
577
|
+
else outcome = await askPause(ctx, full, connection.provider);
|
|
578
|
+
marketplaces.push({ provider: connection.provider, outcome });
|
|
579
|
+
}
|
|
580
|
+
if (marketplaces.some((entry) => entry.outcome === "queued")) ctx.ports.publishChanged(input.clientId);
|
|
581
|
+
return { kind: "ok", marketplaces };
|
|
582
|
+
}
|
|
583
|
+
__name(pauseStore, "pauseStore");
|
|
584
|
+
async function askResume(ctx, read, now) {
|
|
585
|
+
let row = read;
|
|
586
|
+
for (let tries = 0; row && tries < 3; tries += 1) {
|
|
587
|
+
if (resumeAsked(row)) return "unchanged";
|
|
588
|
+
const outcome = await writeResume(ctx, row, now);
|
|
589
|
+
if (outcome !== null) return outcome;
|
|
590
|
+
row = await readPause(ctx, read.clientId, read.provider);
|
|
591
|
+
}
|
|
592
|
+
return row ? forceResume(ctx, row, now) : "unchanged";
|
|
593
|
+
}
|
|
594
|
+
__name(askResume, "askResume");
|
|
595
|
+
async function forceResume(ctx, row, now) {
|
|
596
|
+
const table = pauseTable(await ctx.ports.db());
|
|
597
|
+
await table.updateMany({
|
|
598
|
+
where: { id: row.id },
|
|
599
|
+
data: { wanted: "OPEN", state: "RESUMING", requestedAt: now, failureCode: null, failureDetail: null, failedAt: null }
|
|
600
|
+
});
|
|
601
|
+
const payload = { clientId: row.clientId, provider: row.provider };
|
|
602
|
+
await enqueueAvailability(ctx, payload, availabilityDedupeKey(payload, now));
|
|
603
|
+
return "queued";
|
|
604
|
+
}
|
|
605
|
+
__name(forceResume, "forceResume");
|
|
606
|
+
function resumeAsked(row) {
|
|
607
|
+
if (row.wanted !== "OPEN") return false;
|
|
608
|
+
return row.state === "RESUMING" || row.state === "OPEN" && !holdsSomething(row);
|
|
609
|
+
}
|
|
610
|
+
__name(resumeAsked, "resumeAsked");
|
|
611
|
+
async function writeResume(ctx, row, now) {
|
|
612
|
+
const held = holdsSomething(row) || row.state === "PAUSING";
|
|
613
|
+
const table = pauseTable(await ctx.ports.db());
|
|
614
|
+
const moved = await table.updateMany({
|
|
615
|
+
where: { id: row.id, attemptKey: row.attemptKey },
|
|
616
|
+
data: {
|
|
617
|
+
wanted: "OPEN",
|
|
618
|
+
state: held ? "RESUMING" : "OPEN",
|
|
619
|
+
requestedAt: now,
|
|
620
|
+
failureCode: null,
|
|
621
|
+
failureDetail: null,
|
|
622
|
+
failedAt: null,
|
|
623
|
+
...held ? {} : { resumedAt: now }
|
|
624
|
+
}
|
|
625
|
+
});
|
|
626
|
+
if (moved.count === 0) return null;
|
|
627
|
+
if (!held) return "open";
|
|
628
|
+
const payload = { clientId: row.clientId, provider: row.provider };
|
|
629
|
+
await enqueueAvailability(ctx, payload, availabilityDedupeKey(payload, now));
|
|
630
|
+
return "queued";
|
|
631
|
+
}
|
|
632
|
+
__name(writeResume, "writeResume");
|
|
633
|
+
async function resumeStore(ctx, input) {
|
|
634
|
+
const now = input.now ?? ctx.ports.clock.now();
|
|
635
|
+
const table = pauseTable(await ctx.ports.db());
|
|
636
|
+
const rows = await table.findMany({
|
|
637
|
+
where: { clientId: input.clientId },
|
|
638
|
+
orderBy: { provider: "asc" }
|
|
639
|
+
});
|
|
640
|
+
const marketplaces = [];
|
|
641
|
+
for (const row of rows) marketplaces.push({ provider: row.provider, outcome: await askResume(ctx, row, now) });
|
|
642
|
+
if (marketplaces.some((entry) => entry.outcome !== "unchanged")) ctx.ports.publishChanged(input.clientId);
|
|
643
|
+
return { marketplaces };
|
|
644
|
+
}
|
|
645
|
+
__name(resumeStore, "resumeStore");
|
|
646
|
+
var NEVER_ASKED = {
|
|
647
|
+
state: "OPEN",
|
|
648
|
+
wanted: "OPEN",
|
|
649
|
+
until: null,
|
|
650
|
+
sentUntil: null,
|
|
651
|
+
failureCode: null,
|
|
652
|
+
failureDetail: null,
|
|
653
|
+
pausedAt: null,
|
|
654
|
+
resumedAt: null,
|
|
655
|
+
updatedAt: null
|
|
656
|
+
};
|
|
657
|
+
function viewOf2(ctx, provider, connection, row) {
|
|
658
|
+
const availability = ctx.registry.get(provider)?.availability;
|
|
659
|
+
const recorded = row ? {
|
|
660
|
+
state: row.state,
|
|
661
|
+
wanted: row.wanted,
|
|
662
|
+
until: row.until,
|
|
663
|
+
sentUntil: row.sentUntil,
|
|
664
|
+
failureCode: row.failureCode,
|
|
665
|
+
failureDetail: row.failureDetail,
|
|
666
|
+
pausedAt: row.pausedAt,
|
|
667
|
+
resumedAt: row.resumedAt,
|
|
668
|
+
updatedAt: row.updatedAt
|
|
669
|
+
} : NEVER_ASKED;
|
|
670
|
+
return {
|
|
671
|
+
provider,
|
|
672
|
+
supported: availability !== void 0,
|
|
673
|
+
kind: availability?.kind ?? null,
|
|
674
|
+
connected: connection !== void 0 && isConnected(connection),
|
|
675
|
+
...recorded
|
|
676
|
+
};
|
|
677
|
+
}
|
|
678
|
+
__name(viewOf2, "viewOf");
|
|
679
|
+
async function availabilityState(ctx, clientId) {
|
|
680
|
+
const db = await ctx.ports.db();
|
|
681
|
+
const connections = await liveConnections(ctx, clientId);
|
|
682
|
+
const rows = db.marketplaceStorePause ? await db.marketplaceStorePause.findMany({ where: { clientId } }) : [];
|
|
683
|
+
const byProvider = new Map(rows.map((row) => [row.provider, row]));
|
|
684
|
+
const views = connections.map(
|
|
685
|
+
(connection) => viewOf2(ctx, connection.provider, connection, byProvider.get(connection.provider))
|
|
686
|
+
);
|
|
687
|
+
const live = new Set(connections.map((connection) => connection.provider));
|
|
688
|
+
for (const row of rows) {
|
|
689
|
+
if (!live.has(row.provider) && holdsSomething(row)) views.push(viewOf2(ctx, row.provider, void 0, row));
|
|
690
|
+
}
|
|
691
|
+
return views;
|
|
692
|
+
}
|
|
693
|
+
__name(availabilityState, "availabilityState");
|
|
694
|
+
|
|
695
|
+
// src/core/pause-sweep.ts
|
|
696
|
+
var sweepKey = /* @__PURE__ */ __name((row, now) => availabilityDedupeKey({ clientId: row.clientId, provider: row.provider }, row.requestedAt, `:sweep:${now.getTime()}`), "sweepKey");
|
|
697
|
+
async function requeue(ctx, rows, now) {
|
|
698
|
+
let queued = 0;
|
|
699
|
+
for (const row of rows) {
|
|
700
|
+
if (await enqueueAvailability(ctx, { clientId: row.clientId, provider: row.provider }, sweepKey(row, now))) queued += 1;
|
|
701
|
+
}
|
|
702
|
+
return queued;
|
|
703
|
+
}
|
|
704
|
+
__name(requeue, "requeue");
|
|
705
|
+
async function expire(ctx, now) {
|
|
706
|
+
const table = pauseTable(await ctx.ports.db());
|
|
707
|
+
const rows = await table.findMany({
|
|
708
|
+
where: { wanted: "PAUSED", state: { in: ["PAUSING", "PAUSED", "FAILED"] }, until: { lt: now } }
|
|
709
|
+
});
|
|
710
|
+
let expired = 0;
|
|
711
|
+
for (const row of rows) {
|
|
712
|
+
const held = holdsSomething(row);
|
|
713
|
+
const moved = await table.updateMany({
|
|
714
|
+
where: { id: row.id, wanted: "PAUSED", until: row.until },
|
|
715
|
+
data: { wanted: "OPEN", state: held ? "RESUMING" : "OPEN", requestedAt: now, failureCode: null, failureDetail: null, failedAt: null }
|
|
716
|
+
});
|
|
717
|
+
if (moved.count === 0) continue;
|
|
718
|
+
expired += 1;
|
|
719
|
+
ctx.ports.publishChanged(row.clientId);
|
|
720
|
+
if (held) await requeue(ctx, [{ ...row, requestedAt: now }], now);
|
|
721
|
+
}
|
|
722
|
+
return expired;
|
|
723
|
+
}
|
|
724
|
+
__name(expire, "expire");
|
|
725
|
+
async function giveUp(ctx, cutoff, now) {
|
|
726
|
+
const table = pauseTable(await ctx.ports.db());
|
|
727
|
+
let failed = 0;
|
|
728
|
+
for (const [state, code] of [["PAUSING", "PAUSE_UNANSWERED"], ["RESUMING", "RESUME_UNANSWERED"]]) {
|
|
729
|
+
const moved = await table.updateMany({
|
|
730
|
+
where: { state, requestedAt: { lt: cutoff } },
|
|
731
|
+
data: { state: "FAILED", failureCode: code, failureDetail: null, failedAt: now }
|
|
732
|
+
});
|
|
733
|
+
failed += moved.count;
|
|
734
|
+
}
|
|
735
|
+
return failed;
|
|
736
|
+
}
|
|
737
|
+
__name(giveUp, "giveUp");
|
|
738
|
+
async function retryFailedResumes(ctx, stale, cutoff) {
|
|
739
|
+
const table = pauseTable(await ctx.ports.db());
|
|
740
|
+
const rows = await table.findMany({
|
|
741
|
+
where: { state: "FAILED", wanted: "OPEN", failedAt: { lt: stale }, requestedAt: { gte: cutoff } }
|
|
742
|
+
});
|
|
743
|
+
const retried = [];
|
|
744
|
+
for (const row of rows.filter(holdsSomething)) {
|
|
745
|
+
const moved = await table.updateMany({ where: { id: row.id, state: "FAILED", wanted: "OPEN" }, data: { state: "RESUMING" } });
|
|
746
|
+
if (moved.count > 0) retried.push(row);
|
|
747
|
+
}
|
|
748
|
+
return retried;
|
|
749
|
+
}
|
|
750
|
+
__name(retryFailedResumes, "retryFailedResumes");
|
|
751
|
+
async function reopenOrphans(ctx, stale) {
|
|
752
|
+
const table = pauseTable(await ctx.ports.db());
|
|
753
|
+
const rows = await table.findMany({
|
|
754
|
+
where: {
|
|
755
|
+
state: "OPEN",
|
|
756
|
+
wanted: "OPEN",
|
|
757
|
+
requestedAt: { lt: stale },
|
|
758
|
+
OR: [{ attemptKey: { not: null } }, { externalId: { not: null } }, { sentUntil: { not: null } }, { retiredExternalIds: { not: null } }]
|
|
759
|
+
}
|
|
760
|
+
});
|
|
761
|
+
const reopened = [];
|
|
762
|
+
for (const row of rows) {
|
|
763
|
+
const moved = await table.updateMany({ where: { id: row.id, state: "OPEN", wanted: "OPEN" }, data: { state: "RESUMING" } });
|
|
764
|
+
if (moved.count > 0) reopened.push(row);
|
|
765
|
+
}
|
|
766
|
+
return reopened;
|
|
767
|
+
}
|
|
768
|
+
__name(reopenOrphans, "reopenOrphans");
|
|
769
|
+
async function sweepAvailability(ctx, now) {
|
|
770
|
+
const db = await ctx.ports.db();
|
|
771
|
+
if (!hasPauseTable(db)) return { expired: 0, requeued: 0, givenUp: 0 };
|
|
772
|
+
const table = pauseTable(db);
|
|
773
|
+
const stale = new Date(now.getTime() - OUTBOUND_STALE_MS);
|
|
774
|
+
const cutoff = new Date(now.getTime() - OUTBOUND_GIVE_UP_MS);
|
|
775
|
+
const expired = await expire(ctx, now);
|
|
776
|
+
const givenUp = await giveUp(ctx, cutoff, now);
|
|
777
|
+
const waiting = await table.findMany({
|
|
778
|
+
where: { state: { in: ["PAUSING", "RESUMING"] }, requestedAt: { lt: stale, gte: cutoff } }
|
|
779
|
+
});
|
|
780
|
+
const retired = await table.findMany({ where: { state: "PAUSED", retiredExternalIds: { not: null } } });
|
|
781
|
+
const failedResumes = await retryFailedResumes(ctx, stale, cutoff);
|
|
782
|
+
const orphans = await reopenOrphans(ctx, stale);
|
|
783
|
+
const requeued = await requeue(ctx, [...waiting, ...retired, ...failedResumes, ...orphans], now);
|
|
784
|
+
if (givenUp > 0) ctx.ports.logger.error(`marketplace.availability-sweep: gave up on ${givenUp} store pause ask(s).`);
|
|
785
|
+
return { expired, requeued, givenUp };
|
|
786
|
+
}
|
|
787
|
+
__name(sweepAvailability, "sweepAvailability");
|
|
788
|
+
|
|
375
789
|
// src/core/pipeline.ts
|
|
376
790
|
function normalizeEvent(ctx, provider, payload) {
|
|
377
791
|
return ctx.registry.get(provider)?.normalize(payload) ?? null;
|
|
@@ -563,12 +977,30 @@ function createMarketplaceEngine(config) {
|
|
|
563
977
|
reprocess: /* @__PURE__ */ __name((event) => reprocessStoredDelivery(ctx, event), "reprocess"),
|
|
564
978
|
sweepOutbound: /* @__PURE__ */ __name((now) => sweepOutbound(ctx, now ?? ports.clock.now()), "sweepOutbound"),
|
|
565
979
|
runOutbound: /* @__PURE__ */ __name((payload, attempt) => runOutbound(ctx, payload, attempt), "runOutbound"),
|
|
566
|
-
runFetchOrder: /* @__PURE__ */ __name((payload, attempt) => runFetchOrder(ctx, payload, attempt), "runFetchOrder")
|
|
980
|
+
runFetchOrder: /* @__PURE__ */ __name((payload, attempt) => runFetchOrder(ctx, payload, attempt), "runFetchOrder"),
|
|
981
|
+
// Null too when the host bound no catalogue port: nothing could be published.
|
|
982
|
+
catalogueMode: /* @__PURE__ */ __name((provider) => ports.catalogue ? registry.get(provider)?.catalogue?.mode ?? null : null, "catalogueMode"),
|
|
983
|
+
publish: /* @__PURE__ */ __name((input) => publishToCatalogue(ctx, input), "publish"),
|
|
984
|
+
withdraw: /* @__PURE__ */ __name((input) => withdrawFromCatalogue(ctx, input), "withdraw"),
|
|
985
|
+
catalogueState: /* @__PURE__ */ __name((input) => readCatalogueState(ctx, input), "catalogueState"),
|
|
986
|
+
catalogueDocument: /* @__PURE__ */ __name((input) => readCatalogueDocument(ctx, input), "catalogueDocument"),
|
|
987
|
+
runCatalogue: /* @__PURE__ */ __name((payload, attempt) => runCatalogue(ctx, payload, attempt), "runCatalogue"),
|
|
988
|
+
pauseStore: /* @__PURE__ */ __name((input) => pauseStore(ctx, input), "pauseStore"),
|
|
989
|
+
resumeStore: /* @__PURE__ */ __name((input) => resumeStore(ctx, input), "resumeStore"),
|
|
990
|
+
availabilityState: /* @__PURE__ */ __name((clientId) => availabilityState(ctx, clientId), "availabilityState"),
|
|
991
|
+
runAvailability: /* @__PURE__ */ __name((payload, attempt) => runAvailability(ctx, payload, attempt), "runAvailability"),
|
|
992
|
+
sweepAvailability: /* @__PURE__ */ __name((now) => sweepAvailability(ctx, now ?? ports.clock.now()), "sweepAvailability")
|
|
567
993
|
};
|
|
568
994
|
}
|
|
569
995
|
__name(createMarketplaceEngine, "createMarketplaceEngine");
|
|
570
996
|
export {
|
|
997
|
+
AvailabilityRetryError,
|
|
998
|
+
CATALOGUE_GIVE_UP_MS,
|
|
999
|
+
CATALOGUE_STALE_MS,
|
|
1000
|
+
CatalogueRetryError,
|
|
571
1001
|
FetchOrderRetryError,
|
|
1002
|
+
MARKETPLACE_AVAILABILITY_JOB,
|
|
1003
|
+
MARKETPLACE_CATALOGUE_JOB,
|
|
572
1004
|
MARKETPLACE_FETCH_ORDER_JOB,
|
|
573
1005
|
MARKETPLACE_OUTBOUND_JOB,
|
|
574
1006
|
MarketplaceAdapterRegistrationError,
|
|
@@ -576,12 +1008,16 @@ export {
|
|
|
576
1008
|
OUTBOUND_STALE_MS,
|
|
577
1009
|
OutboundRetryError,
|
|
578
1010
|
PROVIDER_ID_PATTERN,
|
|
1011
|
+
STORE_PAUSE_LEASE_MS,
|
|
579
1012
|
answered,
|
|
1013
|
+
availabilityDedupeKey,
|
|
1014
|
+
catalogueDedupeKey,
|
|
580
1015
|
createAdapterRegistry,
|
|
581
1016
|
createMarketplaceEngine,
|
|
582
1017
|
outboundDedupeKey,
|
|
583
1018
|
outboundResultOf,
|
|
584
1019
|
resolveReason,
|
|
1020
|
+
stableUuid,
|
|
585
1021
|
suggestReason
|
|
586
1022
|
};
|
|
587
1023
|
//# sourceMappingURL=index.js.map
|