@12-apps/marketplace 1.16.0 → 1.17.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 (71) hide show
  1. package/ADOPTING.md +115 -0
  2. package/dist/99food/index.d.ts +2 -2
  3. package/dist/99food/index.js +2 -2
  4. package/dist/{adapter-CjVdMhZ2.d.ts → adapter-CxzF-qGx.d.ts} +185 -2
  5. package/dist/{chunk-TRUK5ZBE.js → chunk-7IYUO2KC.js} +2 -2
  6. package/dist/{chunk-BTKVZAJI.js → chunk-CF5BSXE5.js} +1 -1
  7. package/dist/chunk-CF5BSXE5.js.map +1 -0
  8. package/dist/{chunk-DJIMUEHT.js → chunk-EABVLQKK.js} +79 -7
  9. package/dist/chunk-EABVLQKK.js.map +1 -0
  10. package/dist/{chunk-H2VQRD3B.js → chunk-KKFMDV5K.js} +1 -1
  11. package/dist/chunk-KKFMDV5K.js.map +1 -0
  12. package/dist/{chunk-SGOTN5PX.js → chunk-LO7OKO6V.js} +45 -4
  13. package/dist/chunk-LO7OKO6V.js.map +1 -0
  14. package/dist/{chunk-TICWLBSA.js → chunk-UZO2NILN.js} +365 -6
  15. package/dist/chunk-UZO2NILN.js.map +1 -0
  16. package/dist/{client-DgzCkl8u.d.ts → client-C-4QvVq3.d.ts} +1 -1
  17. package/dist/{engine-DSRj7qnC.d.ts → engine-Y6Rpf5p7.d.ts} +430 -185
  18. package/dist/hono/index.d.ts +2 -2
  19. package/dist/hono/index.js +4 -4
  20. package/dist/ifood/index.d.ts +9 -2
  21. package/dist/ifood/index.js +271 -23
  22. package/dist/ifood/index.js.map +1 -1
  23. package/dist/index.d.ts +21 -4
  24. package/dist/index.js +381 -42
  25. package/dist/index.js.map +1 -1
  26. package/dist/keeta/index.d.ts +2 -2
  27. package/dist/keeta/index.js +2 -2
  28. package/dist/manifest/index.d.ts +78 -0
  29. package/dist/manifest/index.js +1 -1
  30. package/dist/manifest/server.d.ts +6 -2
  31. package/dist/manifest/server.js +4 -4
  32. package/dist/rappi/index.d.ts +1 -1
  33. package/dist/rappi/index.js +1 -1
  34. package/dist/server/index.d.ts +93 -7
  35. package/dist/server/index.js +4 -4
  36. package/package.json +1 -1
  37. package/prisma/marketplace.prisma +69 -1
  38. package/prisma/migrations/20261203180000_marketplace_app_link_check/migration.sql +14 -0
  39. package/prisma/migrations/20261203190000_marketplace_disputes/migration.sql +96 -0
  40. package/src/core/adapter.ts +12 -1
  41. package/src/core/app-linking.ts +396 -0
  42. package/src/core/dispute-engine.ts +32 -0
  43. package/src/core/dispute-job.ts +142 -0
  44. package/src/core/dispute-port.ts +133 -0
  45. package/src/core/dispute-store.ts +203 -0
  46. package/src/core/disputes.ts +263 -0
  47. package/src/core/engine.ts +4 -55
  48. package/src/core/integration.ts +3 -1
  49. package/src/core/link-engine.ts +176 -0
  50. package/src/core/link-store.ts +15 -0
  51. package/src/core/linking-port.ts +35 -1
  52. package/src/core/linking.ts +8 -0
  53. package/src/core/outbound-sweep.ts +9 -1
  54. package/src/core/pipeline.ts +8 -3
  55. package/src/core/ports.ts +15 -0
  56. package/src/core/types.ts +8 -0
  57. package/src/ifood/app-linking.ts +119 -0
  58. package/src/ifood/disputes.ts +209 -0
  59. package/src/ifood/index.ts +12 -1
  60. package/src/ifood/normalize.ts +3 -0
  61. package/src/index.ts +34 -0
  62. package/src/server/jobs.ts +51 -3
  63. package/src/server/link-routes.ts +56 -4
  64. package/src/server/mcp-link.ts +44 -2
  65. package/src/server/mcp.ts +2 -1
  66. package/dist/chunk-BTKVZAJI.js.map +0 -1
  67. package/dist/chunk-DJIMUEHT.js.map +0 -1
  68. package/dist/chunk-H2VQRD3B.js.map +0 -1
  69. package/dist/chunk-SGOTN5PX.js.map +0 -1
  70. package/dist/chunk-TICWLBSA.js.map +0 -1
  71. /package/dist/{chunk-TRUK5ZBE.js.map → chunk-7IYUO2KC.js.map} +0 -0
@@ -1,4 +1,4 @@
1
- import { a9 as MarketplaceDeliveredBy, ak as NormalizedLineOption, af as MarketplaceLinkAttemptDelegate, u as CatalogueItem, t as CatalogueImage, a as NormalizedMarketplaceOrder, N as NormalizedEvent, ay as StatusStep, i as NormalizedCourierEvent, a4 as MarketplaceCourierFacts, a2 as MarketplaceCourierAnswer, av as PickupCodeValidation, ab as MarketplaceHttp, _ as MarketplaceAppCredentialsPort, c as MarketplaceAdapter, l as MarketplaceProvider, a8 as MarketplaceCredentials, Q as CatalogueWarningCode, aw as PresenceProbe, m as CancellationReason, Y as LinkedMerchant, ae as MarketplaceLifecycle, a5 as MarketplaceCourierStep, a3 as MarketplaceCourierEngine, as as OutboundOrderRef, b as CatalogueMenuOptions, B as CatalogueReadResult } from './adapter-CjVdMhZ2.js';
1
+ import { aj as MarketplaceDeliveredBy, aA as NormalizedLineOption, as as MarketplaceLinkAttemptDelegate, an as MarketplaceDisputeDelegate, w as CatalogueItem, v as CatalogueImage, a as NormalizedMarketplaceOrder, N as NormalizedEvent, aO as StatusStep, j as NormalizedCourierEvent, ae as MarketplaceCourierFacts, ac as MarketplaceCourierAnswer, aL as PickupCodeValidation, am as MarketplaceDisputeChange, ao as MarketplaceHttp, a8 as MarketplaceAppCredentialsPort, c as MarketplaceAdapter, m as MarketplaceProvider, ai as MarketplaceCredentials, T as CatalogueWarningCode, aM as PresenceProbe, n as CancellationReason, a6 as LinkedMerchant, _ as DisputeFailureCode, $ as DisputeItem, W as DisputeAlternative, a1 as DisputeReason, a0 as DisputeOutcome, ar as MarketplaceLifecycle, af as MarketplaceCourierStep, ad as MarketplaceCourierEngine, aI as OutboundOrderRef, b as CatalogueMenuOptions, E as CatalogueReadResult } from './adapter-CxzF-qGx.js';
2
2
 
3
3
  /** What the engine tells the host about the order a lifecycle event names. */
4
4
  interface MarketplaceLifecycleFacts {
@@ -199,6 +199,8 @@ interface MarketplaceDb {
199
199
  }>;
200
200
  };
201
201
  marketplaceLinkAttempt?: MarketplaceLinkAttemptDelegate;
202
+ /** OPTIONAL (FUT-6099): the buyer's disputes, migration `20261203190000_marketplace_disputes`. */
203
+ marketplaceDispute?: MarketplaceDisputeDelegate;
202
204
  }
203
205
  /** One decision as the host's admission sees it, BEFORE the claim commits. */
204
206
  interface MarketplaceAdmissionRequest {
@@ -313,6 +315,12 @@ interface MarketplaceOrderPort {
313
315
  * logged. Absent: the verdict is only logged.
314
316
  */
315
317
  onPickupCodeValidated?(clientId: string, orderId: string, validation: PickupCodeValidation): Promise<void>;
318
+ /**
319
+ * OPTIONAL (FUT-6099). A dispute opened or closed on this order, after its
320
+ * commit: the host refreshes its board and tells a person. A throw is
321
+ * logged. Absent: only `publishChanged` is called.
322
+ */
323
+ onDispute?(clientId: string, orderId: string, change: MarketplaceDisputeChange): Promise<void>;
316
324
  /** Queue facts for a batch of orders, keyed by id. Missing ids are dropped. */
317
325
  summarize(clientId: string, orderIds: readonly string[]): Promise<Map<string, MarketplaceOrderSummary>>;
318
326
  }
@@ -404,6 +412,12 @@ interface MarketplacePorts {
404
412
  * awaits it, and a dead bus costs latency, never a transition.
405
413
  */
406
414
  publishChanged: (clientId: string) => void;
415
+ /**
416
+ * OPTIONAL (FUT-5798). The periodic check saw the authorization of a store's
417
+ * centralized link arrive: the host hints that store's open screen to read
418
+ * its link again. Payload-free; absent, the screen sees it on its next read.
419
+ */
420
+ publishLinkSeen?: (clientId: string, provider: string) => void;
407
421
  /**
408
422
  * OPTIONAL. How long a store's cancellation request may wait for the
409
423
  * marketplace's answer before the outbound sweep fails it as TIMEOUT, in
@@ -1005,6 +1019,419 @@ interface MarketplaceConformityEngine {
1005
1019
  runCancellationReasons(payload: CancellationReasonsJobPayload, attempt: Attempt): Promise<CancellationReasonsOutcome>;
1006
1020
  }
1007
1021
 
1022
+ /** Why a linking call did nothing. Vocabulary, not copy: the host words each. */
1023
+ type LinkRefusalCode =
1024
+ /** No app configured for this marketplace, the adapter links nothing, or the host has no link table. */
1025
+ "UNAVAILABLE"
1026
+ /** `finish` with no `start` before it (or after another finish used it). */
1027
+ | "NO_LINK_IN_PROGRESS"
1028
+ /** The portal code's ten minutes are over: start again. */
1029
+ | "LINK_EXPIRED"
1030
+ /** The marketplace refused the authorization code (wrong, used, or for another code). */
1031
+ | "CODE_REFUSED"
1032
+ /** The marketplace refused another call (the code request, the merchant list). */
1033
+ | "MARKETPLACE_REFUSED"
1034
+ /** `selectMerchant` on a connection that is not linked, or is archived. */
1035
+ | "NOT_LINKED"
1036
+ /** The merchant is not one the grant covers. */
1037
+ | "UNKNOWN_MERCHANT"
1038
+ /** Another store's live connection already holds that merchant. */
1039
+ | "MERCHANT_TAKEN";
1040
+ /** A linking call that did nothing: refused for good, or worth trying again. */
1041
+ type LinkFailure = {
1042
+ kind: "refused";
1043
+ code: LinkRefusalCode;
1044
+ status: number | null;
1045
+ message: string | null;
1046
+ } | {
1047
+ kind: "retry";
1048
+ reason: string;
1049
+ };
1050
+ /** A linking in progress, as a person sees it: never the verifier. */
1051
+ interface LinkPendingView {
1052
+ userCode: string;
1053
+ verificationUrl: string;
1054
+ verificationUrlComplete: string;
1055
+ expiresAt: Date;
1056
+ /**
1057
+ * `app` for a centralized link (FUT-5798): no code (`userCode` is empty), the
1058
+ * portal is `verificationUrl`, and `seenAt` / `checkedAt` say when the app's
1059
+ * list first showed a store to pick and when it was last asked. Absent: a code.
1060
+ */
1061
+ mode?: "code" | "app";
1062
+ seenAt?: Date | null;
1063
+ checkedAt?: Date | null;
1064
+ }
1065
+ /** What `finishLink` answers: the connection, and the merchants the grant covers (null when they could not be listed). */
1066
+ interface LinkFinished {
1067
+ kind: "linked";
1068
+ integration: MarketplaceIntegrationView;
1069
+ merchants: LinkedMerchant[] | null;
1070
+ }
1071
+
1072
+ /**
1073
+ * The engine's linking surface (FUT-5613, centralized since FUT-5798): one
1074
+ * entry per call, dispatched to the centralized app's half (`./app-linking`)
1075
+ * when the adapter declares `appLinking`, else to the code flow (`./linking`).
1076
+ */
1077
+ interface MarketplaceLinkEngine {
1078
+ /**
1079
+ * Whether this store can link this marketplace through the software's own
1080
+ * app (FUT-5613), and the linking it left in progress. `available` is false
1081
+ * when the adapter links nothing, the host configured no app for it, or the
1082
+ * link table is not composed: the store's own credentials still work.
1083
+ */
1084
+ linkState(input: {
1085
+ clientId: string;
1086
+ provider: MarketplaceProvider;
1087
+ environment?: "SANDBOX" | "PRODUCTION";
1088
+ }): Promise<{
1089
+ available: boolean;
1090
+ pending: LinkPendingView | null;
1091
+ }>;
1092
+ /** Ask for the portal code; the verifier stays sealed server-side. Replaces an earlier attempt. */
1093
+ startLink(input: {
1094
+ clientId: string;
1095
+ provider: MarketplaceProvider;
1096
+ environment?: "SANDBOX" | "PRODUCTION";
1097
+ }): Promise<{
1098
+ kind: "started";
1099
+ pending: LinkPendingView;
1100
+ } | LinkFailure>;
1101
+ /** Exchange the pasted authorization code for the store's tokens; select the merchant when only one. */
1102
+ finishLink(input: {
1103
+ clientId: string;
1104
+ provider: MarketplaceProvider;
1105
+ authorizationCode: string;
1106
+ actorId: string | null;
1107
+ enabled?: boolean;
1108
+ }): Promise<LinkFinished | LinkFailure>;
1109
+ /** The merchants a linked connection's grant covers. */
1110
+ linkedMerchants(input: {
1111
+ clientId: string;
1112
+ provider: MarketplaceProvider;
1113
+ }): Promise<{
1114
+ kind: "listed";
1115
+ merchants: LinkedMerchant[];
1116
+ } | LinkFailure>;
1117
+ /** Choose the marketplace store of a linked connection whose grant covers several. */
1118
+ selectLinkedMerchant(input: {
1119
+ clientId: string;
1120
+ provider: MarketplaceProvider;
1121
+ merchantId: string;
1122
+ /** Who picked it, for the audit line of a connection unarchived by the pick (centralized linking). */
1123
+ actorId?: string | null;
1124
+ }): Promise<{
1125
+ kind: "selected";
1126
+ integration: MarketplaceIntegrationView;
1127
+ } | LinkFailure>;
1128
+ /**
1129
+ * Centralized linking (FUT-5798): ask the app's list NOW ("Já autorizei ·
1130
+ * verificar agora", the screen's first read and every read after a hint),
1131
+ * renewing the attempt. Refused UNAVAILABLE for a marketplace that links
1132
+ * through codes.
1133
+ */
1134
+ checkLink(input: {
1135
+ clientId: string;
1136
+ provider: MarketplaceProvider;
1137
+ }): Promise<{
1138
+ kind: "checked";
1139
+ pending: LinkPendingView | null;
1140
+ merchants: LinkedMerchant[];
1141
+ } | LinkFailure>;
1142
+ /** End this store's linking in progress (Cancelar). Nothing else changes. */
1143
+ cancelLink(input: {
1144
+ clientId: string;
1145
+ provider: MarketplaceProvider;
1146
+ }): Promise<void>;
1147
+ /**
1148
+ * The host's periodic job (every 30 s): every centralized attempt still
1149
+ * waiting asks the app's list; answers the stores whose authorization this
1150
+ * pass saw, for the host to hint their screens. Asks nothing when none waits.
1151
+ */
1152
+ sweepLinks(input?: {
1153
+ half?: boolean;
1154
+ }): Promise<{
1155
+ seen: {
1156
+ clientId: string;
1157
+ provider: string;
1158
+ }[];
1159
+ waiting: number;
1160
+ }>;
1161
+ }
1162
+
1163
+ /**
1164
+ * The `marketplace.dispute-answer` job (FUT-6099): sends one claimed answer
1165
+ * and stamps what the marketplace said. Idempotent by the row: only a SENDING
1166
+ * row is sent, so a duplicate run calls nobody.
1167
+ */
1168
+ /** The job's payload: identifiers only. */
1169
+ interface DisputeAnswerJobPayload {
1170
+ clientId: string;
1171
+ /** The marketplace's id for the dispute. */
1172
+ disputeId: string;
1173
+ }
1174
+ type DisputeAnswerOutcome = {
1175
+ kind: "skipped";
1176
+ reason: string;
1177
+ } | {
1178
+ kind: "answered";
1179
+ } | {
1180
+ kind: "failed";
1181
+ code: DisputeFailureCode;
1182
+ };
1183
+ /** Thrown so the queue retries. */
1184
+ declare class DisputeAnswerRetryError extends Error {
1185
+ constructor(message: string);
1186
+ }
1187
+
1188
+ /**
1189
+ * The dispute table (`marketplace_disputes`, FUT-6099): one row per dispute,
1190
+ * read into the view a host draws. OPEN → SENDING (an answer claimed and
1191
+ * queued) → ANSWERED (the marketplace took it) → SETTLED (the marketplace
1192
+ * closed it). A failed send goes back to OPEN with its `failure` code.
1193
+ */
1194
+ /** The wire name, `<namespace>.<blueprint>`. */
1195
+ declare const MARKETPLACE_DISPUTE_ANSWER_JOB = "marketplace.dispute-answer";
1196
+ type DisputeStatus = "OPEN" | "SENDING" | "ANSWERED" | "SETTLED";
1197
+ /** The store's answer as kept, with when it was given. */
1198
+ interface DisputeAnswerView {
1199
+ kind: "ACCEPT" | "REJECT" | "ALTERNATIVE";
1200
+ reasonCode: string | null;
1201
+ alternativeId: string | null;
1202
+ amountCents: number | null;
1203
+ minutes: number | null;
1204
+ at: Date;
1205
+ }
1206
+ /** One dispute, as a host draws it. The engine writes no sentence: every word is the host's or the marketplace's. */
1207
+ interface MarketplaceDisputeView {
1208
+ /** The marketplace's id for the dispute: the key `answerDispute` takes. */
1209
+ disputeId: string;
1210
+ orderId: string;
1211
+ provider: string;
1212
+ externalOrderId: string;
1213
+ status: DisputeStatus;
1214
+ action: string;
1215
+ handshakeType: string | null;
1216
+ reason: string | null;
1217
+ message: string | null;
1218
+ evidenceUrls: string[];
1219
+ items: DisputeItem[];
1220
+ requestedAmountCents: number | null;
1221
+ expiresAt: Date | null;
1222
+ openedAt: Date;
1223
+ timeoutAction: string | null;
1224
+ alternatives: DisputeAlternative[];
1225
+ rejectReasons: DisputeReason[];
1226
+ /** The answer sent or being sent; null while none stands (a failed one is cleared). */
1227
+ answer: DisputeAnswerView | null;
1228
+ /** Why the last answer did not land; with ALREADY_ANSWERED the status is ANSWERED (someone answered first). */
1229
+ failure: DisputeFailureCode | null;
1230
+ settlement: {
1231
+ outcome: DisputeOutcome;
1232
+ at: Date;
1233
+ amountCents: number | null;
1234
+ } | null;
1235
+ }
1236
+ /** A row as the delegate answers it. */
1237
+ interface DisputeRow {
1238
+ id: string;
1239
+ clientId: string;
1240
+ orderId: string;
1241
+ provider: string;
1242
+ externalOrderId: string;
1243
+ disputeId: string;
1244
+ status: string;
1245
+ action: string;
1246
+ handshakeType: string | null;
1247
+ reason: string | null;
1248
+ message: string | null;
1249
+ evidences: unknown;
1250
+ items: unknown;
1251
+ requestedAmountCents: number | null;
1252
+ expiresAt: Date | null;
1253
+ openedAt: Date;
1254
+ timeoutAction: string | null;
1255
+ alternatives: unknown;
1256
+ rejectReasons: unknown;
1257
+ acceptReasons: unknown;
1258
+ answer: string | null;
1259
+ answerReasonCode: string | null;
1260
+ answerAlternativeId: string | null;
1261
+ answerAmountCents: number | null;
1262
+ answerMinutes: number | null;
1263
+ answeredAt: Date | null;
1264
+ answerFailure: string | null;
1265
+ settlement: string | null;
1266
+ settledAt: Date | null;
1267
+ settledAmountCents: number | null;
1268
+ }
1269
+ /** A row as a host draws it. */
1270
+ declare function disputeView(row: DisputeRow): MarketplaceDisputeView;
1271
+
1272
+ /**
1273
+ * Normalized marketplace order → the host's own order.
1274
+ *
1275
+ * The rule this module is written around, stated once so every decision below
1276
+ * reads against it: **losing a paid marketplace order is the one unforgivable
1277
+ * failure.** An unmapped SKU, a buyer with no name, a total that does not equal
1278
+ * the line sum — none of them reject the order. They land, flagged, on a screen
1279
+ * a human can act on. The only thing that stops an ingestion is a condition
1280
+ * under which persisting would be WRONG rather than incomplete.
1281
+ *
1282
+ * Idempotent on `(provider, externalOrderId)`, which the acceptance table's
1283
+ * unique enforces. That is what makes a redelivered webhook, a reconciliation
1284
+ * poll racing it, and a retry after a crash all safe: each either creates the
1285
+ * one order or finds it already there. The pre-read below is a fast path, not
1286
+ * the guarantee — two concurrent deliveries both miss it and only one of them
1287
+ * reaches a row.
1288
+ */
1289
+ /** What one ingestion did. */
1290
+ interface IngestResult {
1291
+ /** The HOST's order id. */
1292
+ orderId: string;
1293
+ /** False when the order already existed — a redelivery, not a second sale. */
1294
+ created: boolean;
1295
+ /** SKUs that arrived with nothing behind them, in this order. */
1296
+ unmappedSkus: string[];
1297
+ /**
1298
+ * Whether this order was born accepted: the connection auto-accepts AND the
1299
+ * host's `mayAutoAccept`, when bound, allowed it.
1300
+ */
1301
+ autoAccepted: boolean;
1302
+ }
1303
+ /** The store's operating decisions, read from the connection at ingest time. */
1304
+ interface IngestSettings {
1305
+ autoAccept: boolean;
1306
+ ackSlaSeconds: number;
1307
+ /**
1308
+ * OPTIONAL. The connection's switch for following the marketplace's status
1309
+ * (FUT-4653). Absent is OFF: a host that builds settings by hand keeps the
1310
+ * behaviour it had before the switch existed.
1311
+ */
1312
+ followStatus?: boolean;
1313
+ }
1314
+
1315
+ /** How a delivery ended. `duplicate` and `ignored` are both ordinary outcomes. */
1316
+ type DeliveryOutcome = {
1317
+ kind: "ingested";
1318
+ orderId: string;
1319
+ unmappedSkus: string[];
1320
+ }
1321
+ /** A lifecycle event moved the order forward, through the host's `onLifecycle` (FUT-4653). */
1322
+ | {
1323
+ kind: "moved";
1324
+ orderId: string;
1325
+ lifecycle: MarketplaceLifecycle;
1326
+ }
1327
+ /** A courier event's facts were stored by the host's `onCourier` (FUT-5665). */
1328
+ | {
1329
+ kind: "courier";
1330
+ orderId: string;
1331
+ step: MarketplaceCourierStep;
1332
+ }
1333
+ /**
1334
+ * The store accepted it in the marketplace's own app (FUT-5653): `accepted`
1335
+ * when the host admitted it here too, else it waits AWAITING for a person
1336
+ * (with the host's `refusal`, when it refused), never expired.
1337
+ */
1338
+ | {
1339
+ kind: "confirmedByMarketplace";
1340
+ orderId: string;
1341
+ accepted: boolean;
1342
+ refusal?: {
1343
+ code: string;
1344
+ message: string;
1345
+ };
1346
+ }
1347
+ /** A dispute opened or closed on the order (FUT-6099); `unchanged` for a second event of one already kept. */
1348
+ | {
1349
+ kind: "dispute";
1350
+ orderId: string;
1351
+ disputeId: string;
1352
+ change: "opened" | "settled" | "unchanged";
1353
+ } | {
1354
+ kind: "cancelled";
1355
+ } | {
1356
+ kind: "cancellationFailed";
1357
+ }
1358
+ /** Recorded; the order body is fetched by the `marketplace.fetch-order` job. */
1359
+ | {
1360
+ kind: "awaitingOrder";
1361
+ } | {
1362
+ kind: "duplicate";
1363
+ } | {
1364
+ kind: "ignored";
1365
+ reason: string;
1366
+ } | {
1367
+ kind: "failed";
1368
+ reason: string;
1369
+ };
1370
+
1371
+ /**
1372
+ * A marketplace's disputes (FUT-6099), the engine's half: the event that opens
1373
+ * or closes one is kept per order, and the store's answer is claimed here and
1374
+ * sent by the `marketplace.dispute-answer` job, never inside the request.
1375
+ *
1376
+ * A dispute never touches the order or its acceptance: it may arrive for an
1377
+ * order in any state (delivered, in preparation, late), and what the store
1378
+ * does about it is an answer to the marketplace, not a move of the order.
1379
+ */
1380
+ /** The answer a person gives, before the engine judges it. */
1381
+ type DisputeAnswerInput = {
1382
+ kind: "ACCEPT";
1383
+ } | {
1384
+ kind: "REJECT";
1385
+ reasonCode: string;
1386
+ } | {
1387
+ kind: "ALTERNATIVE";
1388
+ alternativeId: string;
1389
+ amountCents?: number | null;
1390
+ minutes?: number | null;
1391
+ };
1392
+ /** Why an answer is refused locally, before anything is queued. */
1393
+ type DisputeRefusalCode = "NOT_FOUND" | "ALREADY_ANSWERED" | "EXPIRED" | "UNSUPPORTED" | "INVALID_REASON" | "INVALID_ALTERNATIVE" | "INVALID_AMOUNT" | "INVALID_MINUTES";
1394
+ type AnswerDisputeResult = {
1395
+ kind: "queued";
1396
+ dispute: MarketplaceDisputeView;
1397
+ } | {
1398
+ kind: "refused";
1399
+ code: DisputeRefusalCode;
1400
+ };
1401
+
1402
+ /**
1403
+ * The engine's dispute calls (FUT-6099), bound to its context. ADOPTING §15
1404
+ * is the host's contract.
1405
+ */
1406
+ interface MarketplaceDisputeEngine {
1407
+ /**
1408
+ * The disputes to draw, oldest first: of these orders; or, with
1409
+ * `settledSince`, every one still open, being answered or waiting on the
1410
+ * marketplace, plus those settled since then. One statement.
1411
+ */
1412
+ disputes(input: {
1413
+ clientId: string;
1414
+ orderIds?: readonly string[];
1415
+ settledSince?: Date;
1416
+ }): Promise<MarketplaceDisputeView[]>;
1417
+ /**
1418
+ * The store's answer: judged, claimed and queued; refused locally with a code. No call to the marketplace.
1419
+ * With `orderId`, a dispute of another order is NOT_FOUND, before anything is claimed.
1420
+ */
1421
+ answerDispute(input: {
1422
+ clientId: string;
1423
+ disputeId: string;
1424
+ orderId?: string;
1425
+ answer: DisputeAnswerInput;
1426
+ now?: Date;
1427
+ }): Promise<AnswerDisputeResult>;
1428
+ /** Send one claimed answer (the `marketplace.dispute-answer` job's body). */
1429
+ runDisputeAnswer(payload: DisputeAnswerJobPayload, attempt: {
1430
+ attempt: number;
1431
+ maxAttempts: number;
1432
+ }): Promise<DisputeAnswerOutcome>;
1433
+ }
1434
+
1008
1435
  /**
1009
1436
  * Fetching an order the event only announced — the engine's half of a
1010
1437
  * marketplace whose events carry no order body (Open Delivery's `CREATED`).
@@ -1114,91 +1541,6 @@ interface PendingInboxEvent {
1114
1541
  receivedAt?: Date | null;
1115
1542
  }
1116
1543
 
1117
- /**
1118
- * Normalized marketplace order → the host's own order.
1119
- *
1120
- * The rule this module is written around, stated once so every decision below
1121
- * reads against it: **losing a paid marketplace order is the one unforgivable
1122
- * failure.** An unmapped SKU, a buyer with no name, a total that does not equal
1123
- * the line sum — none of them reject the order. They land, flagged, on a screen
1124
- * a human can act on. The only thing that stops an ingestion is a condition
1125
- * under which persisting would be WRONG rather than incomplete.
1126
- *
1127
- * Idempotent on `(provider, externalOrderId)`, which the acceptance table's
1128
- * unique enforces. That is what makes a redelivered webhook, a reconciliation
1129
- * poll racing it, and a retry after a crash all safe: each either creates the
1130
- * one order or finds it already there. The pre-read below is a fast path, not
1131
- * the guarantee — two concurrent deliveries both miss it and only one of them
1132
- * reaches a row.
1133
- */
1134
- /** What one ingestion did. */
1135
- interface IngestResult {
1136
- /** The HOST's order id. */
1137
- orderId: string;
1138
- /** False when the order already existed — a redelivery, not a second sale. */
1139
- created: boolean;
1140
- /** SKUs that arrived with nothing behind them, in this order. */
1141
- unmappedSkus: string[];
1142
- /**
1143
- * Whether this order was born accepted: the connection auto-accepts AND the
1144
- * host's `mayAutoAccept`, when bound, allowed it.
1145
- */
1146
- autoAccepted: boolean;
1147
- }
1148
- /** The store's operating decisions, read from the connection at ingest time. */
1149
- interface IngestSettings {
1150
- autoAccept: boolean;
1151
- ackSlaSeconds: number;
1152
- /**
1153
- * OPTIONAL. The connection's switch for following the marketplace's status
1154
- * (FUT-4653). Absent is OFF: a host that builds settings by hand keeps the
1155
- * behaviour it had before the switch existed.
1156
- */
1157
- followStatus?: boolean;
1158
- }
1159
-
1160
- /** Why a linking call did nothing. Vocabulary, not copy: the host words each. */
1161
- type LinkRefusalCode =
1162
- /** No app configured for this marketplace, the adapter links nothing, or the host has no link table. */
1163
- "UNAVAILABLE"
1164
- /** `finish` with no `start` before it (or after another finish used it). */
1165
- | "NO_LINK_IN_PROGRESS"
1166
- /** The portal code's ten minutes are over: start again. */
1167
- | "LINK_EXPIRED"
1168
- /** The marketplace refused the authorization code (wrong, used, or for another code). */
1169
- | "CODE_REFUSED"
1170
- /** The marketplace refused another call (the code request, the merchant list). */
1171
- | "MARKETPLACE_REFUSED"
1172
- /** `selectMerchant` on a connection that is not linked, or is archived. */
1173
- | "NOT_LINKED"
1174
- /** The merchant is not one the grant covers. */
1175
- | "UNKNOWN_MERCHANT"
1176
- /** Another store's live connection already holds that merchant. */
1177
- | "MERCHANT_TAKEN";
1178
- /** A linking call that did nothing: refused for good, or worth trying again. */
1179
- type LinkFailure = {
1180
- kind: "refused";
1181
- code: LinkRefusalCode;
1182
- status: number | null;
1183
- message: string | null;
1184
- } | {
1185
- kind: "retry";
1186
- reason: string;
1187
- };
1188
- /** A linking in progress, as a person sees it: never the verifier. */
1189
- interface LinkPendingView {
1190
- userCode: string;
1191
- verificationUrl: string;
1192
- verificationUrlComplete: string;
1193
- expiresAt: Date;
1194
- }
1195
- /** What `finishLink` answers: the connection, and the merchants the grant covers (null when they could not be listed). */
1196
- interface LinkFinished {
1197
- kind: "linked";
1198
- integration: MarketplaceIntegrationView;
1199
- merchants: LinkedMerchant[] | null;
1200
- }
1201
-
1202
1544
  /**
1203
1545
  * The outbound job's name and payload — what a decision QUEUES.
1204
1546
  *
@@ -1500,55 +1842,6 @@ interface AvailabilitySweepResult {
1500
1842
  givenUp: number;
1501
1843
  }
1502
1844
 
1503
- /** How a delivery ended. `duplicate` and `ignored` are both ordinary outcomes. */
1504
- type DeliveryOutcome = {
1505
- kind: "ingested";
1506
- orderId: string;
1507
- unmappedSkus: string[];
1508
- }
1509
- /** A lifecycle event moved the order forward, through the host's `onLifecycle` (FUT-4653). */
1510
- | {
1511
- kind: "moved";
1512
- orderId: string;
1513
- lifecycle: MarketplaceLifecycle;
1514
- }
1515
- /** A courier event's facts were stored by the host's `onCourier` (FUT-5665). */
1516
- | {
1517
- kind: "courier";
1518
- orderId: string;
1519
- step: MarketplaceCourierStep;
1520
- }
1521
- /**
1522
- * The store accepted it in the marketplace's own app (FUT-5653): `accepted`
1523
- * when the host admitted it here too, else it waits AWAITING for a person
1524
- * (with the host's `refusal`, when it refused), never expired.
1525
- */
1526
- | {
1527
- kind: "confirmedByMarketplace";
1528
- orderId: string;
1529
- accepted: boolean;
1530
- refusal?: {
1531
- code: string;
1532
- message: string;
1533
- };
1534
- } | {
1535
- kind: "cancelled";
1536
- } | {
1537
- kind: "cancellationFailed";
1538
- }
1539
- /** Recorded; the order body is fetched by the `marketplace.fetch-order` job. */
1540
- | {
1541
- kind: "awaitingOrder";
1542
- } | {
1543
- kind: "duplicate";
1544
- } | {
1545
- kind: "ignored";
1546
- reason: string;
1547
- } | {
1548
- kind: "failed";
1549
- reason: string;
1550
- };
1551
-
1552
1845
  /**
1553
1846
  * What the host's order reads need to draw a marketplace order: where its
1554
1847
  * aceite and its cancellation request stand, and where a person goes when the
@@ -1576,7 +1869,7 @@ interface MarketplaceOrderState {
1576
1869
  }
1577
1870
 
1578
1871
  /** Everything the mounted engine can do, with the ports and adapters bound in. */
1579
- interface MarketplaceEngine extends MarketplaceCourierEngine, MarketplaceConformityEngine {
1872
+ interface MarketplaceEngine extends MarketplaceCourierEngine, MarketplaceConformityEngine, MarketplaceLinkEngine, MarketplaceDisputeEngine {
1580
1873
  /** Every registered marketplace, in registration order. */
1581
1874
  providers(): readonly MarketplaceProvider[];
1582
1875
  /** Whether a provider id (a URL segment, a body field) names a registered adapter. */
@@ -1630,54 +1923,6 @@ interface MarketplaceEngine extends MarketplaceCourierEngine, MarketplaceConform
1630
1923
  rows: readonly IntegrationRowInput[];
1631
1924
  validateOnly?: boolean;
1632
1925
  }): Promise<BulkResult<MarketplaceIntegrationView>>;
1633
- /**
1634
- * Whether this store can link this marketplace through the software's own
1635
- * app (FUT-5613), and the linking it left in progress. `available` is false
1636
- * when the adapter links nothing, the host configured no app for it, or the
1637
- * link table is not composed: the store's own credentials still work.
1638
- */
1639
- linkState(input: {
1640
- clientId: string;
1641
- provider: MarketplaceProvider;
1642
- environment?: "SANDBOX" | "PRODUCTION";
1643
- }): Promise<{
1644
- available: boolean;
1645
- pending: LinkPendingView | null;
1646
- }>;
1647
- /** Ask for the portal code; the verifier stays sealed server-side. Replaces an earlier attempt. */
1648
- startLink(input: {
1649
- clientId: string;
1650
- provider: MarketplaceProvider;
1651
- environment?: "SANDBOX" | "PRODUCTION";
1652
- }): Promise<{
1653
- kind: "started";
1654
- pending: LinkPendingView;
1655
- } | LinkFailure>;
1656
- /** Exchange the pasted authorization code for the store's tokens; select the merchant when only one. */
1657
- finishLink(input: {
1658
- clientId: string;
1659
- provider: MarketplaceProvider;
1660
- authorizationCode: string;
1661
- actorId: string | null;
1662
- enabled?: boolean;
1663
- }): Promise<LinkFinished | LinkFailure>;
1664
- /** The merchants a linked connection's grant covers. */
1665
- linkedMerchants(input: {
1666
- clientId: string;
1667
- provider: MarketplaceProvider;
1668
- }): Promise<{
1669
- kind: "listed";
1670
- merchants: LinkedMerchant[];
1671
- } | LinkFailure>;
1672
- /** Choose the marketplace store of a linked connection whose grant covers several. */
1673
- selectLinkedMerchant(input: {
1674
- clientId: string;
1675
- provider: MarketplaceProvider;
1676
- merchantId: string;
1677
- }): Promise<{
1678
- kind: "selected";
1679
- integration: MarketplaceIntegrationView;
1680
- } | LinkFailure>;
1681
1926
  /** The SKU map. `unmappedOnly` is the store's attention list; `ids` narrows. */
1682
1927
  menuLinks(input: {
1683
1928
  clientId: string;
@@ -1813,4 +2058,4 @@ interface MarketplaceEngine extends MarketplaceCourierEngine, MarketplaceConform
1813
2058
  */
1814
2059
  declare function createMarketplaceEngine(config: MarketplaceEngineConfig): MarketplaceEngine;
1815
2060
 
1816
- export { MARKETPLACE_CATALOGUE_JOB as $, type AcceptanceDecision as A, type BulkProblem as B, type CancellationFailureReason as C, type CatalogueTarget as D, type DecideManyResult as E, type DecidedRow as F, type DecisionInput as G, type DecisionProblem as H, type DecisionProblemCode as I, type DecisionResult as J, type DeliveryOutcome as K, type ExpirySweepResult as L, type FetchOrderJobPayload as M, type FetchOrderOutcome as N, FetchOrderRetryError as O, type InformStatusInput as P, type IngestResult as Q, type IngestSettings as R, type IngestionTarget as S, type IntegrationRowInput as T, type LinkFailure as U, type LinkFinished as V, type LinkPendingView as W, type LinkRefusalCode as X, MARKETPLACE_ACKNOWLEDGE_JOB as Y, MARKETPLACE_AVAILABILITY_JOB as Z, MARKETPLACE_CANCELLATION_REASONS_JOB as _, type AcceptanceStatus as a, MARKETPLACE_FETCH_ORDER_JOB as a0, MARKETPLACE_OUTBOUND_JOB as a1, type MarketplaceAdmission as a2, type MarketplaceAdmissionRequest as a3, type MarketplaceAdmissionResult as a4, type MarketplaceAuditEntry as a5, type MarketplaceAuditPort as a6, type MarketplaceAuthMode as a7, type MarketplaceCataloguePort as a8, type MarketplaceClockPort as a9, type PauseStoreInput as aA, type PauseStoreResult as aB, type PendingAcceptance as aC, type PendingInboxEvent as aD, type ResolvedOrderLine as aE, type ResumeOutcome as aF, STORE_PAUSE_LEASE_MS as aG, type SaveMarketplaceIntegrationInput as aH, type StoreAvailabilityView as aI, type StorePauseFailureCode as aJ, type StorePauseState as aK, type StorePauseWanted as aL, availabilityDedupeKey as aM, catalogueDedupeKey as aN, createMarketplaceEngine as aO, outboundDedupeKey as aP, type MarketplaceConformityEngine as aa, type MarketplaceDb as ab, type MarketplaceEngine as ac, type MarketplaceEngineConfig as ad, type MarketplaceEnqueuePort as ae, type MarketplaceIntegrationView as af, type MarketplaceLifecycleAnswer as ag, type MarketplaceLifecycleFacts as ah, type MarketplaceLogger as ai, type MarketplaceMenuLinkView as aj, type MarketplaceOrderPort as ak, type MarketplaceOrderState as al, type MarketplaceOrderSummary as am, type MarketplacePorts as an, type MarketplaceRefusal as ao, type MarketplaceSecretsPort as ap, type MenuLinkRowInput as aq, OUTBOUND_GIVE_UP_MS as ar, OUTBOUND_STALE_MS as as, type OutboundAction as at, type OutboundAttempt as au, type OutboundJobPayload as av, type OutboundOutcome as aw, OutboundRetryError as ax, type OutboundSweepResult as ay, type PauseOutcome as az, type AcknowledgeJobPayload as b, type AcknowledgeOutcome as c, AcknowledgeRetryError as d, type AvailabilityJobPayload as e, type AvailabilityOutcome as f, AvailabilityRetryError as g, type AvailabilitySweepResult as h, type BulkProblemCode as i, type BulkResult as j, type CancellationReasonsJobPayload as k, type CancellationReasonsOutcome as l, CancellationReasonsRetryError as m, type CancellationStatus as n, type CatalogueAction as o, type CatalogueActionResult as p, type CatalogueDocumentResult as q, type CatalogueFailedAction as r, type CatalogueFailureCode as s, type CatalogueJobPayload as t, type CatalogueOutcome as u, type CataloguePublicationView as v, type CataloguePublishInput as w, type CatalogueRefusalCode as x, CatalogueRetryError as y, type CatalogueStatus as z };
2061
+ export { type IngestionTarget as $, type AcceptanceDecision as A, type BulkProblem as B, type CancellationFailureReason as C, type CatalogueStatus as D, type CatalogueTarget as E, type DecideManyResult as F, type DecidedRow as G, type DecisionInput as H, type DecisionProblem as I, type DecisionProblemCode as J, type DecisionResult as K, type DeliveryOutcome as L, type DisputeAnswerInput as M, type DisputeAnswerJobPayload as N, type DisputeAnswerOutcome as O, DisputeAnswerRetryError as P, type DisputeAnswerView as Q, type DisputeRefusalCode as R, type DisputeRow as S, type DisputeStatus as T, type ExpirySweepResult as U, type FetchOrderJobPayload as V, type FetchOrderOutcome as W, FetchOrderRetryError as X, type InformStatusInput as Y, type IngestResult as Z, type IngestSettings as _, type AcceptanceStatus as a, disputeView as a$, type IntegrationRowInput as a0, type LinkFailure as a1, type LinkFinished as a2, type LinkPendingView as a3, type LinkRefusalCode as a4, MARKETPLACE_ACKNOWLEDGE_JOB as a5, MARKETPLACE_AVAILABILITY_JOB as a6, MARKETPLACE_CANCELLATION_REASONS_JOB as a7, MARKETPLACE_CATALOGUE_JOB as a8, MARKETPLACE_DISPUTE_ANSWER_JOB as a9, type MarketplaceRefusal as aA, type MarketplaceSecretsPort as aB, type MenuLinkRowInput as aC, OUTBOUND_GIVE_UP_MS as aD, OUTBOUND_STALE_MS as aE, type OutboundAction as aF, type OutboundAttempt as aG, type OutboundJobPayload as aH, type OutboundOutcome as aI, OutboundRetryError as aJ, type OutboundSweepResult as aK, type PauseOutcome as aL, type PauseStoreInput as aM, type PauseStoreResult as aN, type PendingAcceptance as aO, type PendingInboxEvent as aP, type ResolvedOrderLine as aQ, type ResumeOutcome as aR, STORE_PAUSE_LEASE_MS as aS, type SaveMarketplaceIntegrationInput as aT, type StoreAvailabilityView as aU, type StorePauseFailureCode as aV, type StorePauseState as aW, type StorePauseWanted as aX, availabilityDedupeKey as aY, catalogueDedupeKey as aZ, createMarketplaceEngine as a_, MARKETPLACE_FETCH_ORDER_JOB as aa, MARKETPLACE_OUTBOUND_JOB as ab, type MarketplaceAdmission as ac, type MarketplaceAdmissionRequest as ad, type MarketplaceAdmissionResult as ae, type MarketplaceAuditEntry as af, type MarketplaceAuditPort as ag, type MarketplaceAuthMode as ah, type MarketplaceCataloguePort as ai, type MarketplaceClockPort as aj, type MarketplaceConformityEngine as ak, type MarketplaceDb as al, type MarketplaceDisputeEngine as am, type MarketplaceDisputeView as an, type MarketplaceEngine as ao, type MarketplaceEngineConfig as ap, type MarketplaceEnqueuePort as aq, type MarketplaceIntegrationView as ar, type MarketplaceLifecycleAnswer as as, type MarketplaceLifecycleFacts as at, type MarketplaceLogger as au, type MarketplaceMenuLinkView as av, type MarketplaceOrderPort as aw, type MarketplaceOrderState as ax, type MarketplaceOrderSummary as ay, type MarketplacePorts as az, type AcknowledgeJobPayload as b, outboundDedupeKey as b0, type AcknowledgeOutcome as c, AcknowledgeRetryError as d, type AnswerDisputeResult as e, type AvailabilityJobPayload as f, type AvailabilityOutcome as g, AvailabilityRetryError as h, type AvailabilitySweepResult as i, type BulkProblemCode as j, type BulkResult as k, type CancellationReasonsJobPayload as l, type CancellationReasonsOutcome as m, CancellationReasonsRetryError as n, type CancellationStatus as o, type CatalogueAction as p, type CatalogueActionResult as q, type CatalogueDocumentResult as r, type CatalogueFailedAction as s, type CatalogueFailureCode as t, type CatalogueJobPayload as u, type CatalogueOutcome as v, type CataloguePublicationView as w, type CataloguePublishInput as x, type CatalogueRefusalCode as y, CatalogueRetryError as z };