@12-apps/marketplace 1.3.0 → 1.5.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/ADOPTING.md +47 -0
- package/README.md +122 -1
- package/dist/99food/index.d.ts +33 -2
- package/dist/99food/index.js +34 -2
- package/dist/99food/index.js.map +1 -1
- package/dist/{adapter-0l_EmuJA.d.ts → adapter-CCWMqt2g.d.ts} +275 -3
- package/dist/{chunk-XUAFXNOH.js → chunk-FKUEFVVA.js} +396 -4
- package/dist/chunk-FKUEFVVA.js.map +1 -0
- package/dist/{chunk-RVBTARIY.js → chunk-RGMSD7S7.js} +1 -1
- package/dist/chunk-RGMSD7S7.js.map +1 -0
- package/dist/{chunk-AYYBRQJV.js → chunk-UONWZIBO.js} +43 -3
- package/dist/chunk-UONWZIBO.js.map +1 -0
- package/dist/chunk-WN2EK4KX.js +15 -0
- package/dist/chunk-WN2EK4KX.js.map +1 -0
- package/dist/{engine-CZsP8KWd.d.ts → engine-C3uq_BJd.d.ts} +252 -2
- package/dist/hono/index.d.ts +2 -2
- package/dist/hono/index.js +2 -2
- package/dist/ifood/index.d.ts +44 -2
- package/dist/ifood/index.js +441 -1
- package/dist/ifood/index.js.map +1 -1
- package/dist/index.d.ts +3 -3
- package/dist/index.js +342 -3
- package/dist/index.js.map +1 -1
- package/dist/manifest/server.d.ts +4 -2
- package/dist/manifest/server.js +2 -2
- package/dist/rappi/index.d.ts +15 -2
- package/dist/rappi/index.js +56 -2
- package/dist/rappi/index.js.map +1 -1
- package/dist/server/index.d.ts +18 -7
- package/dist/server/index.js +4 -2
- package/package.json +1 -1
- package/prisma/marketplace.prisma +67 -1
- package/prisma/migrations/20261113230000_marketplace_store_pause/migration.sql +70 -0
- package/src/99food/index.ts +10 -0
- package/src/99food/pause.ts +56 -0
- package/src/core/adapter.ts +19 -2
- package/src/core/catalogue-read-port.ts +151 -0
- package/src/core/catalogue-read.ts +51 -0
- package/src/core/engine.ts +46 -0
- package/src/core/pause-job.ts +381 -0
- package/src/core/pause-port.ts +110 -0
- package/src/core/pause-retired.ts +139 -0
- package/src/core/pause-store.ts +151 -0
- package/src/core/pause-sweep.ts +148 -0
- package/src/core/pause.ts +316 -0
- package/src/core/ports.ts +11 -0
- package/src/ifood/catalogue-read.ts +187 -0
- package/src/ifood/catalogue-snapshot.ts +246 -0
- package/src/ifood/index.ts +9 -0
- package/src/ifood/pause.ts +257 -0
- package/src/index.ts +39 -0
- package/src/rappi/index.ts +11 -0
- package/src/rappi/outbound.ts +2 -2
- package/src/rappi/pause.ts +98 -0
- package/src/server/index.ts +1 -0
- package/src/server/jobs.ts +64 -4
- package/dist/chunk-AYYBRQJV.js.map +0 -1
- package/dist/chunk-RVBTARIY.js.map +0 -1
- package/dist/chunk-XUAFXNOH.js.map +0 -1
package/dist/index.js
CHANGED
|
@@ -1,10 +1,12 @@
|
|
|
1
1
|
import {
|
|
2
|
+
AvailabilityRetryError,
|
|
2
3
|
CATALOGUE_GIVE_UP_MS,
|
|
3
4
|
CATALOGUE_ROW_SELECT,
|
|
4
5
|
CATALOGUE_STALE_MS,
|
|
5
6
|
CLAIM_SELECT,
|
|
6
7
|
CatalogueRetryError,
|
|
7
8
|
FetchOrderRetryError,
|
|
9
|
+
MARKETPLACE_AVAILABILITY_JOB,
|
|
8
10
|
MARKETPLACE_CATALOGUE_JOB,
|
|
9
11
|
MARKETPLACE_FETCH_ORDER_JOB,
|
|
10
12
|
MARKETPLACE_OUTBOUND_JOB,
|
|
@@ -15,6 +17,8 @@ import {
|
|
|
15
17
|
OUTBOUND_STALE_MS,
|
|
16
18
|
OutboundRetryError,
|
|
17
19
|
PROVIDER_ID_PATTERN,
|
|
20
|
+
STORE_PAUSE_LEASE_MS,
|
|
21
|
+
availabilityDedupeKey,
|
|
18
22
|
awaitOrderBody,
|
|
19
23
|
cancelAcceptance,
|
|
20
24
|
catalogueConnection,
|
|
@@ -23,10 +27,13 @@ import {
|
|
|
23
27
|
connectionMerchantId,
|
|
24
28
|
createAdapterRegistry,
|
|
25
29
|
deferInboxEvent,
|
|
30
|
+
enqueueAvailability,
|
|
26
31
|
enqueueCatalogue,
|
|
27
32
|
enqueueOutbound,
|
|
28
33
|
expireLapsedAcceptances,
|
|
29
34
|
failCancellationRequest,
|
|
35
|
+
hasPauseTable,
|
|
36
|
+
holdsSomething,
|
|
30
37
|
ingestMarketplaceOrder,
|
|
31
38
|
listMarketplaceIntegrations,
|
|
32
39
|
listMarketplaceMenuLinks,
|
|
@@ -36,7 +43,10 @@ import {
|
|
|
36
43
|
merchantMismatch,
|
|
37
44
|
notPendingProblem,
|
|
38
45
|
outboundDedupeKey,
|
|
46
|
+
pauseTable,
|
|
39
47
|
planCancellation,
|
|
48
|
+
readOutboundConnection,
|
|
49
|
+
readPause,
|
|
40
50
|
recordInboxEvent,
|
|
41
51
|
refusalData,
|
|
42
52
|
requestData,
|
|
@@ -45,6 +55,7 @@ import {
|
|
|
45
55
|
resolveIngestionTarget,
|
|
46
56
|
resolveReason,
|
|
47
57
|
resolveTenantByMerchant,
|
|
58
|
+
runAvailability,
|
|
48
59
|
runCatalogue,
|
|
49
60
|
runFetchOrder,
|
|
50
61
|
runOutbound,
|
|
@@ -55,14 +66,17 @@ import {
|
|
|
55
66
|
tellCancellationFailed,
|
|
56
67
|
upsertLink,
|
|
57
68
|
viewOf
|
|
58
|
-
} from "./chunk-
|
|
69
|
+
} from "./chunk-FKUEFVVA.js";
|
|
70
|
+
import {
|
|
71
|
+
centsFromMajor
|
|
72
|
+
} from "./chunk-WN2EK4KX.js";
|
|
59
73
|
import {
|
|
60
74
|
stableUuid
|
|
61
75
|
} from "./chunk-M2V4RFMA.js";
|
|
62
76
|
import {
|
|
63
77
|
answered,
|
|
64
78
|
outboundResultOf
|
|
65
|
-
} from "./chunk-
|
|
79
|
+
} from "./chunk-RGMSD7S7.js";
|
|
66
80
|
import {
|
|
67
81
|
__name
|
|
68
82
|
} from "./chunk-7QVYU63E.js";
|
|
@@ -498,6 +512,320 @@ async function readCatalogueDocument(ctx, input) {
|
|
|
498
512
|
}
|
|
499
513
|
__name(readCatalogueDocument, "readCatalogueDocument");
|
|
500
514
|
|
|
515
|
+
// src/core/catalogue-read.ts
|
|
516
|
+
async function readMarketplaceCatalogue(ctx, input) {
|
|
517
|
+
const reader = ctx.registry.get(input.provider)?.catalogueReader;
|
|
518
|
+
if (!reader) return engineRefusal("UNSUPPORTED");
|
|
519
|
+
const merchantId = await connectionMerchantId(ctx, input.clientId, input.provider);
|
|
520
|
+
if (merchantId === null) return engineRefusal("NOT_CONNECTED");
|
|
521
|
+
let connection;
|
|
522
|
+
try {
|
|
523
|
+
connection = await readOutboundConnection(ctx, input.clientId, input.provider);
|
|
524
|
+
} catch (error) {
|
|
525
|
+
ctx.ports.logger.error(
|
|
526
|
+
`marketplace.catalogue-read: ${input.provider} credentials of ${input.clientId} do not open: ` + (error instanceof Error ? error.message : String(error))
|
|
527
|
+
);
|
|
528
|
+
return engineRefusal("CREDENTIALS");
|
|
529
|
+
}
|
|
530
|
+
if (!connection) return engineRefusal("CREDENTIALS");
|
|
531
|
+
try {
|
|
532
|
+
return await reader.read({
|
|
533
|
+
...connection,
|
|
534
|
+
http: ctx.ports.http,
|
|
535
|
+
tokens: ctx.tokens,
|
|
536
|
+
now: input.now ?? ctx.ports.clock.now(),
|
|
537
|
+
externalMerchantId: merchantId
|
|
538
|
+
});
|
|
539
|
+
} catch (error) {
|
|
540
|
+
return { kind: "retry", reason: error instanceof Error ? error.message : String(error) };
|
|
541
|
+
}
|
|
542
|
+
}
|
|
543
|
+
__name(readMarketplaceCatalogue, "readMarketplaceCatalogue");
|
|
544
|
+
var engineRefusal = /* @__PURE__ */ __name((code) => ({
|
|
545
|
+
kind: "refused",
|
|
546
|
+
code,
|
|
547
|
+
status: null,
|
|
548
|
+
message: null
|
|
549
|
+
}), "engineRefusal");
|
|
550
|
+
|
|
551
|
+
// src/core/pause.ts
|
|
552
|
+
async function liveConnections(ctx, clientId) {
|
|
553
|
+
const db = await ctx.ports.db();
|
|
554
|
+
return await db.marketplaceIntegration.findMany({
|
|
555
|
+
where: { clientId, enabled: true, archivedAt: null },
|
|
556
|
+
select: { provider: true, externalMerchantId: true, credentials: true },
|
|
557
|
+
orderBy: { provider: "asc" }
|
|
558
|
+
});
|
|
559
|
+
}
|
|
560
|
+
__name(liveConnections, "liveConnections");
|
|
561
|
+
var isConnected = /* @__PURE__ */ __name((row) => row.credentials !== "" && Boolean(row.externalMerchantId), "isConnected");
|
|
562
|
+
function validTimeZone(timeZone) {
|
|
563
|
+
try {
|
|
564
|
+
new Intl.DateTimeFormat("en-US", { timeZone });
|
|
565
|
+
return timeZone !== "";
|
|
566
|
+
} catch {
|
|
567
|
+
return false;
|
|
568
|
+
}
|
|
569
|
+
}
|
|
570
|
+
__name(validTimeZone, "validTimeZone");
|
|
571
|
+
function alreadyAsked(row, until) {
|
|
572
|
+
if (!row || row.wanted !== "PAUSED") return false;
|
|
573
|
+
if (row.state !== "PAUSING" && row.state !== "PAUSED") return false;
|
|
574
|
+
return row.until !== null && row.until.getTime() === until.getTime();
|
|
575
|
+
}
|
|
576
|
+
__name(alreadyAsked, "alreadyAsked");
|
|
577
|
+
async function askPause(ctx, input, provider) {
|
|
578
|
+
const existing = await readPause(ctx, input.clientId, provider);
|
|
579
|
+
if (alreadyAsked(existing, input.until)) return "unchanged";
|
|
580
|
+
const table = pauseTable(await ctx.ports.db());
|
|
581
|
+
const data = {
|
|
582
|
+
wanted: "PAUSED",
|
|
583
|
+
state: "PAUSING",
|
|
584
|
+
until: input.until,
|
|
585
|
+
reason: input.reason,
|
|
586
|
+
timeZone: input.timeZone,
|
|
587
|
+
requestedAt: input.now,
|
|
588
|
+
// No key is minted here: the job writes one just before its call, so a
|
|
589
|
+
// resume that lands first knows nothing of ours can stand there. A key
|
|
590
|
+
// still in doubt stays on the row (its create may have landed).
|
|
591
|
+
failureCode: null,
|
|
592
|
+
failureDetail: null,
|
|
593
|
+
failedAt: null
|
|
594
|
+
};
|
|
595
|
+
await table.upsert({
|
|
596
|
+
where: { clientId_provider: { clientId: input.clientId, provider } },
|
|
597
|
+
create: { clientId: input.clientId, provider, ...data },
|
|
598
|
+
update: data
|
|
599
|
+
});
|
|
600
|
+
const payload = { clientId: input.clientId, provider };
|
|
601
|
+
await enqueueAvailability(ctx, payload, availabilityDedupeKey(payload, input.now));
|
|
602
|
+
return "queued";
|
|
603
|
+
}
|
|
604
|
+
__name(askPause, "askPause");
|
|
605
|
+
async function pauseStore(ctx, input) {
|
|
606
|
+
const now = input.now ?? ctx.ports.clock.now();
|
|
607
|
+
if (input.until.getTime() <= now.getTime()) return { kind: "invalid", code: "UNTIL_NOT_FUTURE" };
|
|
608
|
+
if (!validTimeZone(input.timeZone)) return { kind: "invalid", code: "TIME_ZONE_INVALID" };
|
|
609
|
+
pauseTable(await ctx.ports.db());
|
|
610
|
+
const full = { ...input, now };
|
|
611
|
+
const marketplaces = [];
|
|
612
|
+
for (const connection of await liveConnections(ctx, input.clientId)) {
|
|
613
|
+
const adapter = ctx.registry.get(connection.provider);
|
|
614
|
+
let outcome;
|
|
615
|
+
if (!adapter?.availability) outcome = "unsupported";
|
|
616
|
+
else if (!isConnected(connection)) outcome = "not-connected";
|
|
617
|
+
else outcome = await askPause(ctx, full, connection.provider);
|
|
618
|
+
marketplaces.push({ provider: connection.provider, outcome });
|
|
619
|
+
}
|
|
620
|
+
if (marketplaces.some((entry) => entry.outcome === "queued")) ctx.ports.publishChanged(input.clientId);
|
|
621
|
+
return { kind: "ok", marketplaces };
|
|
622
|
+
}
|
|
623
|
+
__name(pauseStore, "pauseStore");
|
|
624
|
+
async function askResume(ctx, read, now) {
|
|
625
|
+
let row = read;
|
|
626
|
+
for (let tries = 0; row && tries < 3; tries += 1) {
|
|
627
|
+
if (resumeAsked(row)) return "unchanged";
|
|
628
|
+
const outcome = await writeResume(ctx, row, now);
|
|
629
|
+
if (outcome !== null) return outcome;
|
|
630
|
+
row = await readPause(ctx, read.clientId, read.provider);
|
|
631
|
+
}
|
|
632
|
+
return row ? forceResume(ctx, row, now) : "unchanged";
|
|
633
|
+
}
|
|
634
|
+
__name(askResume, "askResume");
|
|
635
|
+
async function forceResume(ctx, row, now) {
|
|
636
|
+
const table = pauseTable(await ctx.ports.db());
|
|
637
|
+
await table.updateMany({
|
|
638
|
+
where: { id: row.id },
|
|
639
|
+
data: { wanted: "OPEN", state: "RESUMING", requestedAt: now, failureCode: null, failureDetail: null, failedAt: null }
|
|
640
|
+
});
|
|
641
|
+
const payload = { clientId: row.clientId, provider: row.provider };
|
|
642
|
+
await enqueueAvailability(ctx, payload, availabilityDedupeKey(payload, now));
|
|
643
|
+
return "queued";
|
|
644
|
+
}
|
|
645
|
+
__name(forceResume, "forceResume");
|
|
646
|
+
function resumeAsked(row) {
|
|
647
|
+
if (row.wanted !== "OPEN") return false;
|
|
648
|
+
return row.state === "RESUMING" || row.state === "OPEN" && !holdsSomething(row);
|
|
649
|
+
}
|
|
650
|
+
__name(resumeAsked, "resumeAsked");
|
|
651
|
+
async function writeResume(ctx, row, now) {
|
|
652
|
+
const held = holdsSomething(row) || row.state === "PAUSING";
|
|
653
|
+
const table = pauseTable(await ctx.ports.db());
|
|
654
|
+
const moved = await table.updateMany({
|
|
655
|
+
where: { id: row.id, attemptKey: row.attemptKey },
|
|
656
|
+
data: {
|
|
657
|
+
wanted: "OPEN",
|
|
658
|
+
state: held ? "RESUMING" : "OPEN",
|
|
659
|
+
requestedAt: now,
|
|
660
|
+
failureCode: null,
|
|
661
|
+
failureDetail: null,
|
|
662
|
+
failedAt: null,
|
|
663
|
+
...held ? {} : { resumedAt: now }
|
|
664
|
+
}
|
|
665
|
+
});
|
|
666
|
+
if (moved.count === 0) return null;
|
|
667
|
+
if (!held) return "open";
|
|
668
|
+
const payload = { clientId: row.clientId, provider: row.provider };
|
|
669
|
+
await enqueueAvailability(ctx, payload, availabilityDedupeKey(payload, now));
|
|
670
|
+
return "queued";
|
|
671
|
+
}
|
|
672
|
+
__name(writeResume, "writeResume");
|
|
673
|
+
async function resumeStore(ctx, input) {
|
|
674
|
+
const now = input.now ?? ctx.ports.clock.now();
|
|
675
|
+
const table = pauseTable(await ctx.ports.db());
|
|
676
|
+
const rows = await table.findMany({
|
|
677
|
+
where: { clientId: input.clientId },
|
|
678
|
+
orderBy: { provider: "asc" }
|
|
679
|
+
});
|
|
680
|
+
const marketplaces = [];
|
|
681
|
+
for (const row of rows) marketplaces.push({ provider: row.provider, outcome: await askResume(ctx, row, now) });
|
|
682
|
+
if (marketplaces.some((entry) => entry.outcome !== "unchanged")) ctx.ports.publishChanged(input.clientId);
|
|
683
|
+
return { marketplaces };
|
|
684
|
+
}
|
|
685
|
+
__name(resumeStore, "resumeStore");
|
|
686
|
+
var NEVER_ASKED = {
|
|
687
|
+
state: "OPEN",
|
|
688
|
+
wanted: "OPEN",
|
|
689
|
+
until: null,
|
|
690
|
+
sentUntil: null,
|
|
691
|
+
failureCode: null,
|
|
692
|
+
failureDetail: null,
|
|
693
|
+
pausedAt: null,
|
|
694
|
+
resumedAt: null,
|
|
695
|
+
updatedAt: null
|
|
696
|
+
};
|
|
697
|
+
function viewOf2(ctx, provider, connection, row) {
|
|
698
|
+
const availability = ctx.registry.get(provider)?.availability;
|
|
699
|
+
const recorded = row ? {
|
|
700
|
+
state: row.state,
|
|
701
|
+
wanted: row.wanted,
|
|
702
|
+
until: row.until,
|
|
703
|
+
sentUntil: row.sentUntil,
|
|
704
|
+
failureCode: row.failureCode,
|
|
705
|
+
failureDetail: row.failureDetail,
|
|
706
|
+
pausedAt: row.pausedAt,
|
|
707
|
+
resumedAt: row.resumedAt,
|
|
708
|
+
updatedAt: row.updatedAt
|
|
709
|
+
} : NEVER_ASKED;
|
|
710
|
+
return {
|
|
711
|
+
provider,
|
|
712
|
+
supported: availability !== void 0,
|
|
713
|
+
kind: availability?.kind ?? null,
|
|
714
|
+
connected: connection !== void 0 && isConnected(connection),
|
|
715
|
+
...recorded
|
|
716
|
+
};
|
|
717
|
+
}
|
|
718
|
+
__name(viewOf2, "viewOf");
|
|
719
|
+
async function availabilityState(ctx, clientId) {
|
|
720
|
+
const db = await ctx.ports.db();
|
|
721
|
+
const connections = await liveConnections(ctx, clientId);
|
|
722
|
+
const rows = db.marketplaceStorePause ? await db.marketplaceStorePause.findMany({ where: { clientId } }) : [];
|
|
723
|
+
const byProvider = new Map(rows.map((row) => [row.provider, row]));
|
|
724
|
+
const views = connections.map(
|
|
725
|
+
(connection) => viewOf2(ctx, connection.provider, connection, byProvider.get(connection.provider))
|
|
726
|
+
);
|
|
727
|
+
const live = new Set(connections.map((connection) => connection.provider));
|
|
728
|
+
for (const row of rows) {
|
|
729
|
+
if (!live.has(row.provider) && holdsSomething(row)) views.push(viewOf2(ctx, row.provider, void 0, row));
|
|
730
|
+
}
|
|
731
|
+
return views;
|
|
732
|
+
}
|
|
733
|
+
__name(availabilityState, "availabilityState");
|
|
734
|
+
|
|
735
|
+
// src/core/pause-sweep.ts
|
|
736
|
+
var sweepKey = /* @__PURE__ */ __name((row, now) => availabilityDedupeKey({ clientId: row.clientId, provider: row.provider }, row.requestedAt, `:sweep:${now.getTime()}`), "sweepKey");
|
|
737
|
+
async function requeue(ctx, rows, now) {
|
|
738
|
+
let queued = 0;
|
|
739
|
+
for (const row of rows) {
|
|
740
|
+
if (await enqueueAvailability(ctx, { clientId: row.clientId, provider: row.provider }, sweepKey(row, now))) queued += 1;
|
|
741
|
+
}
|
|
742
|
+
return queued;
|
|
743
|
+
}
|
|
744
|
+
__name(requeue, "requeue");
|
|
745
|
+
async function expire(ctx, now) {
|
|
746
|
+
const table = pauseTable(await ctx.ports.db());
|
|
747
|
+
const rows = await table.findMany({
|
|
748
|
+
where: { wanted: "PAUSED", state: { in: ["PAUSING", "PAUSED", "FAILED"] }, until: { lt: now } }
|
|
749
|
+
});
|
|
750
|
+
let expired = 0;
|
|
751
|
+
for (const row of rows) {
|
|
752
|
+
const held = holdsSomething(row);
|
|
753
|
+
const moved = await table.updateMany({
|
|
754
|
+
where: { id: row.id, wanted: "PAUSED", until: row.until },
|
|
755
|
+
data: { wanted: "OPEN", state: held ? "RESUMING" : "OPEN", requestedAt: now, failureCode: null, failureDetail: null, failedAt: null }
|
|
756
|
+
});
|
|
757
|
+
if (moved.count === 0) continue;
|
|
758
|
+
expired += 1;
|
|
759
|
+
ctx.ports.publishChanged(row.clientId);
|
|
760
|
+
if (held) await requeue(ctx, [{ ...row, requestedAt: now }], now);
|
|
761
|
+
}
|
|
762
|
+
return expired;
|
|
763
|
+
}
|
|
764
|
+
__name(expire, "expire");
|
|
765
|
+
async function giveUp(ctx, cutoff, now) {
|
|
766
|
+
const table = pauseTable(await ctx.ports.db());
|
|
767
|
+
let failed = 0;
|
|
768
|
+
for (const [state, code] of [["PAUSING", "PAUSE_UNANSWERED"], ["RESUMING", "RESUME_UNANSWERED"]]) {
|
|
769
|
+
const moved = await table.updateMany({
|
|
770
|
+
where: { state, requestedAt: { lt: cutoff } },
|
|
771
|
+
data: { state: "FAILED", failureCode: code, failureDetail: null, failedAt: now }
|
|
772
|
+
});
|
|
773
|
+
failed += moved.count;
|
|
774
|
+
}
|
|
775
|
+
return failed;
|
|
776
|
+
}
|
|
777
|
+
__name(giveUp, "giveUp");
|
|
778
|
+
async function retryFailedResumes(ctx, stale, cutoff) {
|
|
779
|
+
const table = pauseTable(await ctx.ports.db());
|
|
780
|
+
const rows = await table.findMany({
|
|
781
|
+
where: { state: "FAILED", wanted: "OPEN", failedAt: { lt: stale }, requestedAt: { gte: cutoff } }
|
|
782
|
+
});
|
|
783
|
+
const retried = [];
|
|
784
|
+
for (const row of rows.filter(holdsSomething)) {
|
|
785
|
+
const moved = await table.updateMany({ where: { id: row.id, state: "FAILED", wanted: "OPEN" }, data: { state: "RESUMING" } });
|
|
786
|
+
if (moved.count > 0) retried.push(row);
|
|
787
|
+
}
|
|
788
|
+
return retried;
|
|
789
|
+
}
|
|
790
|
+
__name(retryFailedResumes, "retryFailedResumes");
|
|
791
|
+
async function reopenOrphans(ctx, stale) {
|
|
792
|
+
const table = pauseTable(await ctx.ports.db());
|
|
793
|
+
const rows = await table.findMany({
|
|
794
|
+
where: {
|
|
795
|
+
state: "OPEN",
|
|
796
|
+
wanted: "OPEN",
|
|
797
|
+
requestedAt: { lt: stale },
|
|
798
|
+
OR: [{ attemptKey: { not: null } }, { externalId: { not: null } }, { sentUntil: { not: null } }, { retiredExternalIds: { not: null } }]
|
|
799
|
+
}
|
|
800
|
+
});
|
|
801
|
+
const reopened = [];
|
|
802
|
+
for (const row of rows) {
|
|
803
|
+
const moved = await table.updateMany({ where: { id: row.id, state: "OPEN", wanted: "OPEN" }, data: { state: "RESUMING" } });
|
|
804
|
+
if (moved.count > 0) reopened.push(row);
|
|
805
|
+
}
|
|
806
|
+
return reopened;
|
|
807
|
+
}
|
|
808
|
+
__name(reopenOrphans, "reopenOrphans");
|
|
809
|
+
async function sweepAvailability(ctx, now) {
|
|
810
|
+
const db = await ctx.ports.db();
|
|
811
|
+
if (!hasPauseTable(db)) return { expired: 0, requeued: 0, givenUp: 0 };
|
|
812
|
+
const table = pauseTable(db);
|
|
813
|
+
const stale = new Date(now.getTime() - OUTBOUND_STALE_MS);
|
|
814
|
+
const cutoff = new Date(now.getTime() - OUTBOUND_GIVE_UP_MS);
|
|
815
|
+
const expired = await expire(ctx, now);
|
|
816
|
+
const givenUp = await giveUp(ctx, cutoff, now);
|
|
817
|
+
const waiting = await table.findMany({
|
|
818
|
+
where: { state: { in: ["PAUSING", "RESUMING"] }, requestedAt: { lt: stale, gte: cutoff } }
|
|
819
|
+
});
|
|
820
|
+
const retired = await table.findMany({ where: { state: "PAUSED", retiredExternalIds: { not: null } } });
|
|
821
|
+
const failedResumes = await retryFailedResumes(ctx, stale, cutoff);
|
|
822
|
+
const orphans = await reopenOrphans(ctx, stale);
|
|
823
|
+
const requeued = await requeue(ctx, [...waiting, ...retired, ...failedResumes, ...orphans], now);
|
|
824
|
+
if (givenUp > 0) ctx.ports.logger.error(`marketplace.availability-sweep: gave up on ${givenUp} store pause ask(s).`);
|
|
825
|
+
return { expired, requeued, givenUp };
|
|
826
|
+
}
|
|
827
|
+
__name(sweepAvailability, "sweepAvailability");
|
|
828
|
+
|
|
501
829
|
// src/core/pipeline.ts
|
|
502
830
|
function normalizeEvent(ctx, provider, payload) {
|
|
503
831
|
return ctx.registry.get(provider)?.normalize(payload) ?? null;
|
|
@@ -696,15 +1024,23 @@ function createMarketplaceEngine(config) {
|
|
|
696
1024
|
withdraw: /* @__PURE__ */ __name((input) => withdrawFromCatalogue(ctx, input), "withdraw"),
|
|
697
1025
|
catalogueState: /* @__PURE__ */ __name((input) => readCatalogueState(ctx, input), "catalogueState"),
|
|
698
1026
|
catalogueDocument: /* @__PURE__ */ __name((input) => readCatalogueDocument(ctx, input), "catalogueDocument"),
|
|
699
|
-
runCatalogue: /* @__PURE__ */ __name((payload, attempt) => runCatalogue(ctx, payload, attempt), "runCatalogue")
|
|
1027
|
+
runCatalogue: /* @__PURE__ */ __name((payload, attempt) => runCatalogue(ctx, payload, attempt), "runCatalogue"),
|
|
1028
|
+
readCatalogue: /* @__PURE__ */ __name((input) => readMarketplaceCatalogue(ctx, input), "readCatalogue"),
|
|
1029
|
+
pauseStore: /* @__PURE__ */ __name((input) => pauseStore(ctx, input), "pauseStore"),
|
|
1030
|
+
resumeStore: /* @__PURE__ */ __name((input) => resumeStore(ctx, input), "resumeStore"),
|
|
1031
|
+
availabilityState: /* @__PURE__ */ __name((clientId) => availabilityState(ctx, clientId), "availabilityState"),
|
|
1032
|
+
runAvailability: /* @__PURE__ */ __name((payload, attempt) => runAvailability(ctx, payload, attempt), "runAvailability"),
|
|
1033
|
+
sweepAvailability: /* @__PURE__ */ __name((now) => sweepAvailability(ctx, now ?? ports.clock.now()), "sweepAvailability")
|
|
700
1034
|
};
|
|
701
1035
|
}
|
|
702
1036
|
__name(createMarketplaceEngine, "createMarketplaceEngine");
|
|
703
1037
|
export {
|
|
1038
|
+
AvailabilityRetryError,
|
|
704
1039
|
CATALOGUE_GIVE_UP_MS,
|
|
705
1040
|
CATALOGUE_STALE_MS,
|
|
706
1041
|
CatalogueRetryError,
|
|
707
1042
|
FetchOrderRetryError,
|
|
1043
|
+
MARKETPLACE_AVAILABILITY_JOB,
|
|
708
1044
|
MARKETPLACE_CATALOGUE_JOB,
|
|
709
1045
|
MARKETPLACE_FETCH_ORDER_JOB,
|
|
710
1046
|
MARKETPLACE_OUTBOUND_JOB,
|
|
@@ -713,8 +1049,11 @@ export {
|
|
|
713
1049
|
OUTBOUND_STALE_MS,
|
|
714
1050
|
OutboundRetryError,
|
|
715
1051
|
PROVIDER_ID_PATTERN,
|
|
1052
|
+
STORE_PAUSE_LEASE_MS,
|
|
716
1053
|
answered,
|
|
1054
|
+
availabilityDedupeKey,
|
|
717
1055
|
catalogueDedupeKey,
|
|
1056
|
+
centsFromMajor,
|
|
718
1057
|
createAdapterRegistry,
|
|
719
1058
|
createMarketplaceEngine,
|
|
720
1059
|
outboundDedupeKey,
|