@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.
Files changed (69) hide show
  1. package/ADOPTING.md +76 -4
  2. package/README.md +163 -3
  3. package/dist/99food/index.d.ts +79 -2
  4. package/dist/99food/index.js +117 -2
  5. package/dist/99food/index.js.map +1 -1
  6. package/dist/adapter-B8QtQ2RA.d.ts +612 -0
  7. package/dist/{chunk-CRYFYDQ2.js → chunk-DPCZVN2C.js} +69 -5
  8. package/dist/chunk-DPCZVN2C.js.map +1 -0
  9. package/dist/{chunk-D2X7TOR6.js → chunk-EBAIZQPB.js} +841 -86
  10. package/dist/chunk-EBAIZQPB.js.map +1 -0
  11. package/dist/chunk-M2V4RFMA.js +23 -0
  12. package/dist/chunk-M2V4RFMA.js.map +1 -0
  13. package/dist/{chunk-24RQZPSV.js → chunk-SZRZTLVU.js} +1 -1
  14. package/dist/chunk-SZRZTLVU.js.map +1 -0
  15. package/dist/{engine-D-a4mttc.d.ts → engine-o5cm54vC.d.ts} +462 -2
  16. package/dist/hono/index.d.ts +2 -2
  17. package/dist/hono/index.js +2 -2
  18. package/dist/ifood/index.d.ts +22 -2
  19. package/dist/ifood/index.js +396 -56
  20. package/dist/ifood/index.js.map +1 -1
  21. package/dist/index.d.ts +30 -4
  22. package/dist/index.js +445 -9
  23. package/dist/index.js.map +1 -1
  24. package/dist/manifest/server.d.ts +5 -2
  25. package/dist/manifest/server.js +2 -2
  26. package/dist/rappi/index.d.ts +15 -2
  27. package/dist/rappi/index.js +62 -2
  28. package/dist/rappi/index.js.map +1 -1
  29. package/dist/server/index.d.ts +25 -10
  30. package/dist/server/index.js +4 -2
  31. package/package.json +1 -1
  32. package/prisma/marketplace.prisma +158 -1
  33. package/prisma/migrations/20261113120000_marketplace_catalogue/migration.sql +97 -0
  34. package/prisma/migrations/20261113230000_marketplace_store_pause/migration.sql +70 -0
  35. package/src/99food/catalogue.ts +117 -0
  36. package/src/99food/index.ts +14 -0
  37. package/src/99food/outbound.ts +3 -3
  38. package/src/99food/pause.ts +56 -0
  39. package/src/core/adapter.ts +20 -1
  40. package/src/core/catalogue-category.ts +119 -0
  41. package/src/core/catalogue-ids.ts +23 -0
  42. package/src/core/catalogue-job.ts +332 -0
  43. package/src/core/catalogue-port.ts +173 -0
  44. package/src/core/catalogue-store.ts +172 -0
  45. package/src/core/catalogue-sweep.ts +43 -0
  46. package/src/core/catalogue.ts +226 -0
  47. package/src/core/engine.ts +83 -0
  48. package/src/core/outbound-sweep.ts +7 -2
  49. package/src/core/pause-job.ts +381 -0
  50. package/src/core/pause-port.ts +110 -0
  51. package/src/core/pause-retired.ts +139 -0
  52. package/src/core/pause-store.ts +151 -0
  53. package/src/core/pause-sweep.ts +148 -0
  54. package/src/core/pause.ts +316 -0
  55. package/src/core/ports.ts +47 -0
  56. package/src/ifood/catalogue.ts +273 -0
  57. package/src/ifood/index.ts +8 -0
  58. package/src/ifood/outbound.ts +9 -6
  59. package/src/ifood/pause.ts +257 -0
  60. package/src/index.ts +64 -0
  61. package/src/rappi/index.ts +17 -0
  62. package/src/rappi/outbound.ts +2 -2
  63. package/src/rappi/pause.ts +98 -0
  64. package/src/server/index.ts +1 -0
  65. package/src/server/jobs.ts +100 -10
  66. package/dist/adapter-_iuSxvVD.d.ts +0 -319
  67. package/dist/chunk-24RQZPSV.js.map +0 -1
  68. package/dist/chunk-CRYFYDQ2.js.map +0 -1
  69. package/dist/chunk-D2X7TOR6.js.map +0 -1
@@ -0,0 +1,43 @@
1
+ import { catalogueDedupeKey, enqueueCatalogue, type CatalogueJobPayload } from "./catalogue-job";
2
+ import { CATALOGUE_ROW_SELECT, catalogueTable, type CatalogueRow } from "./catalogue-store";
3
+ import type { EngineContext } from "./ports";
4
+
5
+ /**
6
+ * The outbound sweep's catalogue half (FUT-4768): nothing the store switched
7
+ * on or off is lost, and nothing waits forever on "sending".
8
+ *
9
+ * A row still owed (PUBLISHING or WITHDRAWING) older than the catalogue job's
10
+ * own retry span is re-queued under a key of its own per pass; one older than
11
+ * the give-up is FAILED, with the action it failed at, so the host shows the
12
+ * reason and "try again" instead of a switch stuck on its way.
13
+ */
14
+
15
+ /** Longer than the catalogue job's three attempts with their backoff. */
16
+ export const CATALOGUE_STALE_MS = 10 * 60_000;
17
+
18
+ /** Past this, the marketplace is not going to answer this ask. */
19
+ export const CATALOGUE_GIVE_UP_MS = 24 * 60 * 60_000;
20
+
21
+ /** One pass. Returns how many runs it re-queued. */
22
+ export async function sweepCatalogue(ctx: EngineContext, now: Date): Promise<number> {
23
+ const table = await catalogueTable(ctx);
24
+ if (!table) return 0;
25
+ const stale = new Date(now.getTime() - CATALOGUE_STALE_MS);
26
+ const giveUp = new Date(now.getTime() - CATALOGUE_GIVE_UP_MS);
27
+ for (const [status, failedAction] of [["PUBLISHING", "PUBLISH"], ["WITHDRAWING", "WITHDRAW"]] as const) {
28
+ await table.updateMany({
29
+ where: { status, requestedAt: { lt: giveUp } },
30
+ data: { status: "FAILED", failedAction, failureCode: "TIMEOUT", failureReason: null, failedAt: now },
31
+ });
32
+ }
33
+ const rows = (await table.findMany({
34
+ where: { status: { in: ["PUBLISHING", "WITHDRAWING"] }, requestedAt: { lt: stale, gte: giveUp } },
35
+ select: CATALOGUE_ROW_SELECT,
36
+ })) as CatalogueRow[];
37
+ for (const row of rows) {
38
+ const action = row.status === "PUBLISHING" ? "publish" : "withdraw";
39
+ const payload: CatalogueJobPayload = { clientId: row.clientId, publicationId: row.id, action };
40
+ await enqueueCatalogue(ctx, payload, `${catalogueDedupeKey(payload, row.requestedAt)}:sweep:${now.getTime()}`);
41
+ }
42
+ return rows.length;
43
+ }
@@ -0,0 +1,226 @@
1
+ import { catalogueDedupeKey, enqueueCatalogue, type CatalogueJobPayload } from "./catalogue-job";
2
+ import type { CatalogueMenuEntry, CatalogueMenuOptions } from "./catalogue-port";
3
+ import {
4
+ CATALOGUE_ROW_SELECT,
5
+ NO_FAILURE,
6
+ catalogueConnection,
7
+ catalogueTable,
8
+ viewOf,
9
+ type CataloguePublicationView,
10
+ type CatalogueRow,
11
+ type CatalogueTable,
12
+ } from "./catalogue-store";
13
+ import { connectionMerchantId } from "./integration";
14
+ import type { EngineContext } from "./ports";
15
+ import type { MarketplaceProvider } from "./types";
16
+
17
+ /**
18
+ * Catalogue publishing, as a host calls it (FUT-4768): from a product's page,
19
+ * the store turns the product ON (`publish`) or OFF (`withdraw`) per connected
20
+ * marketplace, with an optional price for that marketplace, and reads where
21
+ * each stands (`catalogueState`, one read for a whole page).
22
+ *
23
+ * Nothing here calls a marketplace: a publish or withdraw stamps the row and
24
+ * queues `marketplace.catalogue`, whose job makes the call. The host's product
25
+ * is read through its `catalogue` port, never from a host table, and only the
26
+ * job reads it for sending, so what goes out is the product as it is then.
27
+ */
28
+
29
+ /** Why a catalogue call was refused before anything was stamped. Vocabulary, not copy. */
30
+ export type CatalogueRefusalCode =
31
+ /** The marketplace has no catalogue capability, or the host bound no catalogue port or table. */
32
+ | "UNSUPPORTED"
33
+ /** No live connection with credentials and a merchant id. */
34
+ | "NOT_CONNECTED"
35
+ /** The host's catalogue does not have this product. */
36
+ | "UNKNOWN_PRODUCT"
37
+ /** The price for the marketplace is not a positive integer of the smallest unit. */
38
+ | "INVALID_PRICE";
39
+
40
+ /** What `publish` and `withdraw` answer. */
41
+ export type CatalogueActionResult =
42
+ /** Stamped and queued (or already on its way: a double tap is this, with nothing new queued). */
43
+ | { kind: "queued"; publication: CataloguePublicationView }
44
+ /** Nothing to do: the product is not in that marketplace's catalogue, or was never there. */
45
+ | { kind: "unchanged"; publication: CataloguePublicationView | null }
46
+ | { kind: "refused"; code: CatalogueRefusalCode };
47
+
48
+ /** What a pull marketplace's menu document read answers. */
49
+ export type CatalogueDocumentResult =
50
+ | { kind: "document"; document: unknown }
51
+ | { kind: "refused"; code: Exclude<CatalogueRefusalCode, "UNKNOWN_PRODUCT" | "INVALID_PRICE"> };
52
+
53
+ /** One product on one marketplace. */
54
+ export interface CatalogueTarget {
55
+ clientId: string;
56
+ provider: MarketplaceProvider;
57
+ productId: string;
58
+ }
59
+
60
+ /** A publish: the target, and the price for this marketplace — absent keeps the stored one, null is the store's. */
61
+ export interface CataloguePublishInput extends CatalogueTarget {
62
+ priceCents?: number | null;
63
+ }
64
+
65
+ const refused = (code: CatalogueRefusalCode): CatalogueActionResult => ({ kind: "refused", code });
66
+
67
+ /** The table, or why the catalogue cannot be used at all for this marketplace. */
68
+ async function usableTable(ctx: EngineContext, provider: string): Promise<CatalogueTable | null> {
69
+ if (!ctx.ports.catalogue || !ctx.registry.get(provider)?.catalogue) return null;
70
+ return catalogueTable(ctx);
71
+ }
72
+
73
+ async function findRow(table: CatalogueTable, target: CatalogueTarget): Promise<CatalogueRow | null> {
74
+ return (await table.findFirst({
75
+ where: { clientId: target.clientId, provider: target.provider, productId: target.productId },
76
+ select: CATALOGUE_ROW_SELECT,
77
+ })) as CatalogueRow | null;
78
+ }
79
+
80
+ async function queue(ctx: EngineContext, row: CatalogueRow, action: CatalogueJobPayload["action"]): Promise<CatalogueActionResult> {
81
+ const payload: CatalogueJobPayload = { clientId: row.clientId, publicationId: row.id, action };
82
+ await enqueueCatalogue(ctx, payload, catalogueDedupeKey(payload, row.requestedAt));
83
+ ctx.ports.publishChanged(row.clientId);
84
+ return { kind: "queued", publication: viewOf(row) };
85
+ }
86
+
87
+ const validPrice = (price: number | null | undefined): boolean =>
88
+ price === undefined || price === null || (Number.isInteger(price) && price > 0);
89
+
90
+ /** Why a publish cannot be asked, or null. */
91
+ async function publishRefusal(ctx: EngineContext, input: CataloguePublishInput): Promise<CatalogueRefusalCode | null> {
92
+ if (!validPrice(input.priceCents)) return "INVALID_PRICE";
93
+ if ("problem" in (await catalogueConnection(ctx, input.clientId, input.provider))) return "NOT_CONNECTED";
94
+ const items = await ctx.ports.catalogue?.items(input.clientId, [input.productId]);
95
+ return items?.has(input.productId) ? null : "UNKNOWN_PRODUCT";
96
+ }
97
+
98
+ /**
99
+ * Put the product in the marketplace's catalogue, or send it again (a new
100
+ * price, a retry after FAILED). A double tap while it is on its way keeps the
101
+ * same ask and queues nothing new.
102
+ */
103
+ export async function publishToCatalogue(ctx: EngineContext, input: CataloguePublishInput): Promise<CatalogueActionResult> {
104
+ const table = await usableTable(ctx, input.provider);
105
+ if (!table) return refused("UNSUPPORTED");
106
+ const refusal = await publishRefusal(ctx, input);
107
+ if (refusal) return refused(refusal);
108
+ const existing = await findRow(table, input);
109
+ const now = ctx.ports.clock.now();
110
+ const price = input.priceCents === undefined ? {} : { priceCents: input.priceCents };
111
+ // A double tap keeps the ask on its way; a NEW price is a new ask, so the
112
+ // run already queued (which reads the row's price at its own instant) misses
113
+ // its stamp and the new run sends the new price.
114
+ const samePrice = input.priceCents === undefined || input.priceCents === existing?.priceCents;
115
+ const asked = existing?.status === "PUBLISHING" && samePrice ? {} : { requestedAt: now };
116
+ const row = (await table.upsert({
117
+ where: { clientId_provider_productId: { clientId: input.clientId, provider: input.provider, productId: input.productId } },
118
+ create: { clientId: input.clientId, provider: input.provider, productId: input.productId, status: "PUBLISHING", requestedAt: now, ...price },
119
+ update: { status: "PUBLISHING", ...NO_FAILURE, ...asked, ...price },
120
+ select: CATALOGUE_ROW_SELECT,
121
+ })) as CatalogueRow;
122
+ return queue(ctx, row, "publish");
123
+ }
124
+
125
+ /**
126
+ * Take the product out of the marketplace's catalogue. A product never
127
+ * switched on (no row, or WITHDRAWN) is `unchanged`; a double tap while it is
128
+ * on its way out queues nothing new; a withdraw while a publish is on its way
129
+ * wins (the job re-withdraws a publish that lands after it).
130
+ *
131
+ * A withdraw is SENT even after a publish that FAILED: a call whose answer was
132
+ * lost may have put the item there, and a marketplace answers a withdraw of
133
+ * an item it does not have as done. Only a store with no live connection any
134
+ * more (archived, removed) ends WITHDRAWN at once: there is nobody to tell.
135
+ */
136
+ export async function withdrawFromCatalogue(ctx: EngineContext, target: CatalogueTarget): Promise<CatalogueActionResult> {
137
+ const table = await usableTable(ctx, target.provider);
138
+ if (!table) return refused("UNSUPPORTED");
139
+ const existing = await findRow(table, target);
140
+ if (!existing || existing.status === "WITHDRAWN") return { kind: "unchanged", publication: existing && viewOf(existing) };
141
+ if (existing.status === "WITHDRAWING") return queue(ctx, existing, "withdraw");
142
+ const now = ctx.ports.clock.now();
143
+ // No live connection (archived, removed): nothing to tell, so it is out here and now.
144
+ const local = (await connectionMerchantId(ctx, target.clientId, target.provider)) === null;
145
+ const data = local
146
+ ? { status: "WITHDRAWN", withdrawnAt: now, requestedAt: now, ...NO_FAILURE }
147
+ : { status: "WITHDRAWING", requestedAt: now, ...NO_FAILURE };
148
+ const moved = await table.updateMany({ where: { id: existing.id, status: existing.status }, data });
149
+ // Lost a race with another write: answer what the row now says.
150
+ const row = (await findRow(table, target)) ?? existing;
151
+ if (moved.count === 0 || local) {
152
+ ctx.ports.publishChanged(target.clientId);
153
+ return { kind: "unchanged", publication: viewOf(row) };
154
+ }
155
+ return queue(ctx, row, "withdraw");
156
+ }
157
+
158
+ /**
159
+ * Where these products stand on these marketplaces, in ONE read: a product's
160
+ * page asks for one product on every connected marketplace; a list asks for
161
+ * many. Rows only for what was ever switched on.
162
+ */
163
+ export async function readCatalogueState(
164
+ ctx: EngineContext,
165
+ input: { clientId: string; productIds: readonly string[]; providers?: readonly string[] },
166
+ ): Promise<CataloguePublicationView[]> {
167
+ const table = await catalogueTable(ctx);
168
+ if (!table || input.productIds.length === 0) return [];
169
+ const rows = (await table.findMany({
170
+ where: {
171
+ clientId: input.clientId,
172
+ productId: { in: [...input.productIds] },
173
+ ...(input.providers ? { provider: { in: [...input.providers] } } : {}),
174
+ },
175
+ select: CATALOGUE_ROW_SELECT,
176
+ })) as CatalogueRow[];
177
+ return rows.map(viewOf);
178
+ }
179
+
180
+ /**
181
+ * Every product a pull marketplace's menu lists: switched on, on its way, or
182
+ * already published once and since FAILED to be published again (it stays,
183
+ * at the price last sent, until it is withdrawn).
184
+ */
185
+ const IN_MENU = ["PUBLISHING", "PUBLISHED", "FAILED"];
186
+
187
+ /** Whether a row belongs in the served menu, and at which price. */
188
+ function menuPrice(row: CatalogueRow, storePrice: number): number | null {
189
+ if (row.status !== "FAILED") return row.priceCents ?? storePrice;
190
+ // Published, and not withdrawn since: a withdraw → re-publish that FAILED is
191
+ // not on the marketplace's menu, so it is not listed at an old price.
192
+ const live = row.publishedAt !== null && (row.withdrawnAt === null || row.withdrawnAt < row.publishedAt);
193
+ const republishFailed = row.failedAction === "PUBLISH" && live;
194
+ return republishFailed ? (row.sentPriceCents ?? row.priceCents ?? storePrice) : null;
195
+ }
196
+
197
+ /**
198
+ * The menu part of the document a pull marketplace reads, for the host to
199
+ * serve (merged into the rest of that document, which is the host's). Built
200
+ * from every product switched on for the connection, read through the host's
201
+ * catalogue port; a product that no longer exists is left out.
202
+ */
203
+ export async function readCatalogueDocument(
204
+ ctx: EngineContext,
205
+ input: { clientId: string; provider: MarketplaceProvider; options: CatalogueMenuOptions },
206
+ ): Promise<CatalogueDocumentResult> {
207
+ const catalogue = ctx.registry.get(input.provider)?.catalogue;
208
+ const table = await usableTable(ctx, input.provider);
209
+ if (!table || catalogue?.mode !== "pull") return { kind: "refused", code: "UNSUPPORTED" };
210
+ const connection = await catalogueConnection(ctx, input.clientId, input.provider);
211
+ if ("problem" in connection) return { kind: "refused", code: "NOT_CONNECTED" };
212
+ const rows = (await table.findMany({
213
+ where: { clientId: input.clientId, provider: input.provider, status: { in: IN_MENU } },
214
+ select: CATALOGUE_ROW_SELECT,
215
+ orderBy: { createdAt: "asc" },
216
+ })) as CatalogueRow[];
217
+ const items = (await ctx.ports.catalogue?.items(input.clientId, rows.map((row) => row.productId))) ?? new Map();
218
+ const entries: CatalogueMenuEntry[] = rows.flatMap((row) => {
219
+ const item = items.get(row.productId);
220
+ const priceCents = item ? menuPrice(row, item.priceCents) : null;
221
+ if (!item || priceCents === null) return [];
222
+ return [{ ...item, priceCents, ref: row.id, productId: row.productId, externalId: row.externalId, storedImage: null }];
223
+ });
224
+ const document = catalogue.menuDocument({ externalMerchantId: connection.externalMerchantId, entries, options: input.options });
225
+ return { kind: "document", document };
226
+ }
@@ -20,6 +20,19 @@ import {
20
20
  type DecisionInput,
21
21
  type DecisionResult,
22
22
  } from "./decisions";
23
+ import {
24
+ publishToCatalogue,
25
+ readCatalogueDocument,
26
+ readCatalogueState,
27
+ withdrawFromCatalogue,
28
+ type CatalogueActionResult,
29
+ type CatalogueDocumentResult,
30
+ type CataloguePublishInput,
31
+ type CatalogueTarget,
32
+ } from "./catalogue";
33
+ import { runCatalogue, type CatalogueJobPayload, type CatalogueOutcome } from "./catalogue-job";
34
+ import type { CatalogueMenuOptions } from "./catalogue-port";
35
+ import type { CataloguePublicationView } from "./catalogue-store";
23
36
  import {
24
37
  runFetchOrder,
25
38
  type FetchOrderJobPayload,
@@ -45,6 +58,18 @@ import {
45
58
  } from "./integration";
46
59
  import { listMarketplaceMenuLinks, type MarketplaceMenuLinkView } from "./menu-links";
47
60
  import { runOutbound, type OutboundAttempt, type OutboundOutcome } from "./outbound";
61
+ import {
62
+ availabilityState,
63
+ pauseStore,
64
+ resumeStore,
65
+ type PauseStoreInput,
66
+ type PauseStoreResult,
67
+ type ResumeOutcome,
68
+ type StoreAvailabilityView,
69
+ } from "./pause";
70
+ import { runAvailability, type AvailabilityOutcome } from "./pause-job";
71
+ import type { AvailabilityJobPayload } from "./pause-store";
72
+ import { sweepAvailability, type AvailabilitySweepResult } from "./pause-sweep";
48
73
  import type { OutboundJobPayload } from "./outbound-queue";
49
74
  import {
50
75
  normalizeEvent,
@@ -170,6 +195,50 @@ export interface MarketplaceEngine {
170
195
  runOutbound(payload: OutboundJobPayload, attempt: OutboundAttempt): Promise<OutboundOutcome>;
171
196
  /** Fetch and ingest one announced order (the `marketplace.fetch-order` job's body). */
172
197
  runFetchOrder(payload: FetchOrderJobPayload, attempt: OutboundAttempt): Promise<FetchOrderOutcome>;
198
+
199
+ /**
200
+ * How this marketplace's catalogue is published (`push`, `pull`), or null
201
+ * when it cannot be: the adapter states no catalogue, or the host bound no
202
+ * catalogue port (FUT-4768).
203
+ */
204
+ catalogueMode(provider: string): "push" | "pull" | null;
205
+ /** Put a product in a marketplace's catalogue (or send it again), with an optional price for it. Queued. */
206
+ publish(input: CataloguePublishInput): Promise<CatalogueActionResult>;
207
+ /** Take a product out of a marketplace's catalogue. Queued. */
208
+ withdraw(input: CatalogueTarget): Promise<CatalogueActionResult>;
209
+ /** Where these products stand on these marketplaces, in one read. */
210
+ catalogueState(input: {
211
+ clientId: string;
212
+ productIds: readonly string[];
213
+ providers?: readonly string[];
214
+ }): Promise<CataloguePublicationView[]>;
215
+ /** The menu part of the document a PULL marketplace reads, for the host to serve. */
216
+ catalogueDocument(input: {
217
+ clientId: string;
218
+ provider: MarketplaceProvider;
219
+ options: CatalogueMenuOptions;
220
+ }): Promise<CatalogueDocumentResult>;
221
+ /** Run one queued publish or withdraw (the `marketplace.catalogue` job's body). */
222
+ runCatalogue(payload: CatalogueJobPayload, attempt: OutboundAttempt): Promise<CatalogueOutcome>;
223
+
224
+ /**
225
+ * The kitchen paused (or renewed its pause): close the store until `until`
226
+ * on every live connection whose marketplace can be paused (FUT-4851).
227
+ * Idempotent: the `until` already standing does nothing; any other, an
228
+ * earlier one included, renews the closure.
229
+ */
230
+ pauseStore(input: PauseStoreInput): Promise<PauseStoreResult>;
231
+ /** The kitchen's pause is over: delete every closure the engine created, and nothing else. */
232
+ resumeStore(input: {
233
+ clientId: string;
234
+ now?: Date;
235
+ }): Promise<{ marketplaces: { provider: MarketplaceProvider; outcome: ResumeOutcome }[] }>;
236
+ /** Where the store's pause stands on each marketplace, for the screen. */
237
+ availabilityState(clientId: string): Promise<StoreAvailabilityView[]>;
238
+ /** Bring one marketplace in line with the last ask (the `marketplace.availability` job's body). */
239
+ runAvailability(payload: AvailabilityJobPayload, attempt: OutboundAttempt): Promise<AvailabilityOutcome>;
240
+ /** Expire, re-queue and give up on store pauses (the `marketplace.availability-sweep` job's body). */
241
+ sweepAvailability(now?: Date): Promise<AvailabilitySweepResult>;
173
242
  }
174
243
 
175
244
  /**
@@ -233,5 +302,19 @@ export function createMarketplaceEngine(config: MarketplaceEngineConfig): Market
233
302
  sweepOutbound: (now) => sweepOutbound(ctx, now ?? ports.clock.now()),
234
303
  runOutbound: (payload, attempt) => runOutbound(ctx, payload, attempt),
235
304
  runFetchOrder: (payload, attempt) => runFetchOrder(ctx, payload, attempt),
305
+
306
+ // Null too when the host bound no catalogue port: nothing could be published.
307
+ catalogueMode: (provider) => (ports.catalogue ? (registry.get(provider)?.catalogue?.mode ?? null) : null),
308
+ publish: (input) => publishToCatalogue(ctx, input),
309
+ withdraw: (input) => withdrawFromCatalogue(ctx, input),
310
+ catalogueState: (input) => readCatalogueState(ctx, input),
311
+ catalogueDocument: (input) => readCatalogueDocument(ctx, input),
312
+ runCatalogue: (payload, attempt) => runCatalogue(ctx, payload, attempt),
313
+
314
+ pauseStore: (input) => pauseStore(ctx, input),
315
+ resumeStore: (input) => resumeStore(ctx, input),
316
+ availabilityState: (clientId) => availabilityState(ctx, clientId),
317
+ runAvailability: (payload, attempt) => runAvailability(ctx, payload, attempt),
318
+ sweepAvailability: (now) => sweepAvailability(ctx, now ?? ports.clock.now()),
236
319
  };
237
320
  }
@@ -1,4 +1,5 @@
1
1
  import { NO_STANDING_REQUEST } from "./cancellation";
2
+ import { sweepCatalogue } from "./catalogue-sweep";
2
3
  import { cancellationWindowMs, failedRequestData, requestsFirst, tellCancellationFailed } from "./cancellation-outcome";
3
4
  import { MARKETPLACE_OUTBOUND_JOB, enqueueOutbound, type OutboundAction } from "./outbound-queue";
4
5
  import type { EngineContext } from "./ports";
@@ -55,6 +56,8 @@ export interface OutboundSweepResult {
55
56
  givenUp: number;
56
57
  /** Requests failed as TIMEOUT past their marketplace's answer window. */
57
58
  timedOut: number;
59
+ /** Catalogue publishes and withdraws still owed, re-queued (FUT-4768). */
60
+ catalogueRequeued: number;
58
61
  }
59
62
 
60
63
  /** A store's cancellation request still waiting on the marketplace. */
@@ -136,7 +139,8 @@ async function expireUnanswered(ctx: EngineContext, now: Date): Promise<number>
136
139
 
137
140
  /**
138
141
  * One sweep pass: expire the requests nobody answered, give up on the very old,
139
- * and re-queue what is still owed.
142
+ * and re-queue what is still owed, the catalogue's publishes and withdraws
143
+ * included (`./catalogue-sweep.ts`).
140
144
  */
141
145
  export async function sweepOutbound(ctx: EngineContext, now: Date): Promise<OutboundSweepResult> {
142
146
  const db = await ctx.ports.db();
@@ -165,5 +169,6 @@ export async function sweepOutbound(ctx: EngineContext, now: Date): Promise<Outb
165
169
  const requeued =
166
170
  (await requeue(ctx, cancellations, "requestCancellation", now)) + (await requeue(ctx, confirms, "confirm", now));
167
171
  if (givenUp > 0) ctx.ports.logger.error(`marketplace.outbound-sweep: gave up on ${givenUp} cancellation request(s).`);
168
- return { requeued, givenUp, timedOut };
172
+ const catalogueRequeued = await sweepCatalogue(ctx, now);
173
+ return { requeued, givenUp, timedOut, catalogueRequeued };
169
174
  }