@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.
- package/ADOPTING.md +115 -0
- package/dist/99food/index.d.ts +2 -2
- package/dist/99food/index.js +2 -2
- package/dist/{adapter-CjVdMhZ2.d.ts → adapter-CxzF-qGx.d.ts} +185 -2
- package/dist/{chunk-TRUK5ZBE.js → chunk-7IYUO2KC.js} +2 -2
- package/dist/{chunk-BTKVZAJI.js → chunk-CF5BSXE5.js} +1 -1
- package/dist/chunk-CF5BSXE5.js.map +1 -0
- package/dist/{chunk-DJIMUEHT.js → chunk-EABVLQKK.js} +79 -7
- package/dist/chunk-EABVLQKK.js.map +1 -0
- package/dist/{chunk-H2VQRD3B.js → chunk-KKFMDV5K.js} +1 -1
- package/dist/chunk-KKFMDV5K.js.map +1 -0
- package/dist/{chunk-SGOTN5PX.js → chunk-LO7OKO6V.js} +45 -4
- package/dist/chunk-LO7OKO6V.js.map +1 -0
- package/dist/{chunk-TICWLBSA.js → chunk-UZO2NILN.js} +365 -6
- package/dist/chunk-UZO2NILN.js.map +1 -0
- package/dist/{client-DgzCkl8u.d.ts → client-C-4QvVq3.d.ts} +1 -1
- package/dist/{engine-DSRj7qnC.d.ts → engine-Y6Rpf5p7.d.ts} +430 -185
- package/dist/hono/index.d.ts +2 -2
- package/dist/hono/index.js +4 -4
- package/dist/ifood/index.d.ts +9 -2
- package/dist/ifood/index.js +271 -23
- package/dist/ifood/index.js.map +1 -1
- package/dist/index.d.ts +21 -4
- package/dist/index.js +381 -42
- package/dist/index.js.map +1 -1
- package/dist/keeta/index.d.ts +2 -2
- package/dist/keeta/index.js +2 -2
- package/dist/manifest/index.d.ts +78 -0
- package/dist/manifest/index.js +1 -1
- package/dist/manifest/server.d.ts +6 -2
- package/dist/manifest/server.js +4 -4
- package/dist/rappi/index.d.ts +1 -1
- package/dist/rappi/index.js +1 -1
- package/dist/server/index.d.ts +93 -7
- package/dist/server/index.js +4 -4
- package/package.json +1 -1
- package/prisma/marketplace.prisma +69 -1
- package/prisma/migrations/20261203180000_marketplace_app_link_check/migration.sql +14 -0
- package/prisma/migrations/20261203190000_marketplace_disputes/migration.sql +96 -0
- package/src/core/adapter.ts +12 -1
- package/src/core/app-linking.ts +396 -0
- package/src/core/dispute-engine.ts +32 -0
- package/src/core/dispute-job.ts +142 -0
- package/src/core/dispute-port.ts +133 -0
- package/src/core/dispute-store.ts +203 -0
- package/src/core/disputes.ts +263 -0
- package/src/core/engine.ts +4 -55
- package/src/core/integration.ts +3 -1
- package/src/core/link-engine.ts +176 -0
- package/src/core/link-store.ts +15 -0
- package/src/core/linking-port.ts +35 -1
- package/src/core/linking.ts +8 -0
- package/src/core/outbound-sweep.ts +9 -1
- package/src/core/pipeline.ts +8 -3
- package/src/core/ports.ts +15 -0
- package/src/core/types.ts +8 -0
- package/src/ifood/app-linking.ts +119 -0
- package/src/ifood/disputes.ts +209 -0
- package/src/ifood/index.ts +12 -1
- package/src/ifood/normalize.ts +3 -0
- package/src/index.ts +34 -0
- package/src/server/jobs.ts +51 -3
- package/src/server/link-routes.ts +56 -4
- package/src/server/mcp-link.ts +44 -2
- package/src/server/mcp.ts +2 -1
- package/dist/chunk-BTKVZAJI.js.map +0 -1
- package/dist/chunk-DJIMUEHT.js.map +0 -1
- package/dist/chunk-H2VQRD3B.js.map +0 -1
- package/dist/chunk-SGOTN5PX.js.map +0 -1
- package/dist/chunk-TICWLBSA.js.map +0 -1
- /package/dist/{chunk-TRUK5ZBE.js.map → chunk-7IYUO2KC.js.map} +0 -0
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import {
|
|
2
2
|
PROVIDER_ID_PATTERN
|
|
3
|
-
} from "./chunk-
|
|
3
|
+
} from "./chunk-KKFMDV5K.js";
|
|
4
4
|
import {
|
|
5
5
|
__name
|
|
6
6
|
} from "./chunk-7QVYU63E.js";
|
|
@@ -641,6 +641,19 @@ function openLinkRefreshToken(ctx, sealed) {
|
|
|
641
641
|
return token;
|
|
642
642
|
}
|
|
643
643
|
__name(openLinkRefreshToken, "openLinkRefreshToken");
|
|
644
|
+
function sealAppLink(ctx) {
|
|
645
|
+
return ctx.ports.secrets.seal(JSON.stringify({ clientId: "", clientSecret: "", webhookSecret: null, app: true }));
|
|
646
|
+
}
|
|
647
|
+
__name(sealAppLink, "sealAppLink");
|
|
648
|
+
function isAppLinkBlob(ctx, sealed) {
|
|
649
|
+
try {
|
|
650
|
+
const parsed = JSON.parse(ctx.ports.secrets.open(sealed));
|
|
651
|
+
return typeof parsed === "object" && parsed !== null && parsed.app === true;
|
|
652
|
+
} catch {
|
|
653
|
+
return false;
|
|
654
|
+
}
|
|
655
|
+
}
|
|
656
|
+
__name(isAppLinkBlob, "isAppLinkBlob");
|
|
644
657
|
async function appCredentialsOf(ctx, provider, environment) {
|
|
645
658
|
const port = ctx.ports.appCredentials;
|
|
646
659
|
if (!port) return null;
|
|
@@ -898,6 +911,7 @@ async function readSealedOutboundConnection(ctx, clientId, provider) {
|
|
|
898
911
|
if (row.authMode !== "LINKED") return { connection: { environment, credentials: openCredentials(ctx, sealed) }, sealed };
|
|
899
912
|
const app = await appCredentialsOf(ctx, provider, environment);
|
|
900
913
|
if (!app) throw new Error(`No app credentials are configured for ${provider}.`);
|
|
914
|
+
if (isAppLinkBlob(ctx, sealed)) return { connection: { environment, credentials: app }, sealed };
|
|
901
915
|
const refreshToken = openLinkRefreshToken(ctx, sealed);
|
|
902
916
|
const link = { clientId, provider, environment, refreshToken, app, sealed };
|
|
903
917
|
return { connection: { environment, credentials: app, grant: linkGrant(ctx, link) }, sealed };
|
|
@@ -1297,12 +1311,12 @@ var CancellationReasonsRetryError = class extends Error {
|
|
|
1297
1311
|
};
|
|
1298
1312
|
async function enqueueOrderReasons(ctx, input) {
|
|
1299
1313
|
const adapter = ctx.registry.get(input.provider);
|
|
1300
|
-
const
|
|
1301
|
-
if (!adapter?.orderCancellationReasons && !
|
|
1314
|
+
const deferred2 = input.deferredConfirmations === true && adapter?.capabilities.marketplaceConfirms === true;
|
|
1315
|
+
if (!adapter?.orderCancellationReasons && !deferred2) return;
|
|
1302
1316
|
const payload = {
|
|
1303
1317
|
clientId: input.clientId,
|
|
1304
1318
|
orderId: input.orderId,
|
|
1305
|
-
...
|
|
1319
|
+
...deferred2 ? { deferredConfirmations: true } : {}
|
|
1306
1320
|
};
|
|
1307
1321
|
const outcome = await ctx.ports.enqueue.enqueue(MARKETPLACE_CANCELLATION_REASONS_JOB, payload, {
|
|
1308
1322
|
dedupeKey: `${MARKETPLACE_CANCELLATION_REASONS_JOB}:${input.orderId}`
|
|
@@ -1371,6 +1385,337 @@ async function readOrderReasons(ctx, clientId, orderId) {
|
|
|
1371
1385
|
}
|
|
1372
1386
|
__name(readOrderReasons, "readOrderReasons");
|
|
1373
1387
|
|
|
1388
|
+
// src/core/dispute-store.ts
|
|
1389
|
+
var MARKETPLACE_DISPUTE_ANSWER_JOB = "marketplace.dispute-answer";
|
|
1390
|
+
async function disputeTable(ctx) {
|
|
1391
|
+
return (await ctx.ports.db()).marketplaceDispute ?? null;
|
|
1392
|
+
}
|
|
1393
|
+
__name(disputeTable, "disputeTable");
|
|
1394
|
+
function arrayOf(value, keep2) {
|
|
1395
|
+
return Array.isArray(value) ? value.filter(keep2) : [];
|
|
1396
|
+
}
|
|
1397
|
+
__name(arrayOf, "arrayOf");
|
|
1398
|
+
var isString = /* @__PURE__ */ __name((entry) => typeof entry === "string", "isString");
|
|
1399
|
+
var isReason = /* @__PURE__ */ __name((entry) => typeof entry?.code === "string" && typeof entry.description === "string", "isReason");
|
|
1400
|
+
var isItem = /* @__PURE__ */ __name((entry) => typeof entry?.name === "string", "isItem");
|
|
1401
|
+
var ALTERNATIVE_TYPES = /* @__PURE__ */ new Set(["REFUND", "BENEFIT", "ADDITIONAL_TIME"]);
|
|
1402
|
+
var isAlternative = /* @__PURE__ */ __name((entry) => typeof entry?.id === "string" && ALTERNATIVE_TYPES.has(entry.type), "isAlternative");
|
|
1403
|
+
function reasonsOf(value) {
|
|
1404
|
+
return arrayOf(value, isReason);
|
|
1405
|
+
}
|
|
1406
|
+
__name(reasonsOf, "reasonsOf");
|
|
1407
|
+
function alternativesOf(value) {
|
|
1408
|
+
return arrayOf(value, isAlternative);
|
|
1409
|
+
}
|
|
1410
|
+
__name(alternativesOf, "alternativesOf");
|
|
1411
|
+
function answerOf(row) {
|
|
1412
|
+
if (row.answer === null || row.answeredAt === null) return null;
|
|
1413
|
+
return {
|
|
1414
|
+
kind: row.answer,
|
|
1415
|
+
reasonCode: row.answerReasonCode,
|
|
1416
|
+
alternativeId: row.answerAlternativeId,
|
|
1417
|
+
amountCents: row.answerAmountCents,
|
|
1418
|
+
minutes: row.answerMinutes,
|
|
1419
|
+
at: row.answeredAt
|
|
1420
|
+
};
|
|
1421
|
+
}
|
|
1422
|
+
__name(answerOf, "answerOf");
|
|
1423
|
+
function settlementOf(row) {
|
|
1424
|
+
if (row.settlement === null || row.settledAt === null) return null;
|
|
1425
|
+
return { outcome: row.settlement, at: row.settledAt, amountCents: row.settledAmountCents };
|
|
1426
|
+
}
|
|
1427
|
+
__name(settlementOf, "settlementOf");
|
|
1428
|
+
function disputeView(row) {
|
|
1429
|
+
return {
|
|
1430
|
+
disputeId: row.disputeId,
|
|
1431
|
+
orderId: row.orderId,
|
|
1432
|
+
provider: row.provider,
|
|
1433
|
+
externalOrderId: row.externalOrderId,
|
|
1434
|
+
status: row.status,
|
|
1435
|
+
action: row.action,
|
|
1436
|
+
handshakeType: row.handshakeType,
|
|
1437
|
+
reason: row.reason,
|
|
1438
|
+
message: row.message,
|
|
1439
|
+
evidenceUrls: arrayOf(row.evidences, isString),
|
|
1440
|
+
items: arrayOf(row.items, isItem),
|
|
1441
|
+
requestedAmountCents: row.requestedAmountCents,
|
|
1442
|
+
expiresAt: row.expiresAt,
|
|
1443
|
+
openedAt: row.openedAt,
|
|
1444
|
+
timeoutAction: row.timeoutAction,
|
|
1445
|
+
alternatives: alternativesOf(row.alternatives),
|
|
1446
|
+
rejectReasons: reasonsOf(row.rejectReasons),
|
|
1447
|
+
answer: answerOf(row),
|
|
1448
|
+
failure: row.answerFailure,
|
|
1449
|
+
settlement: settlementOf(row)
|
|
1450
|
+
};
|
|
1451
|
+
}
|
|
1452
|
+
__name(disputeView, "disputeView");
|
|
1453
|
+
var LIVE = ["OPEN", "SENDING", "ANSWERED"];
|
|
1454
|
+
async function readDisputes(ctx, input) {
|
|
1455
|
+
const table = await disputeTable(ctx);
|
|
1456
|
+
if (!table) return [];
|
|
1457
|
+
const live = input.settledSince ? { OR: [{ status: { in: [...LIVE] } }, { settledAt: { gte: input.settledSince } }] } : {};
|
|
1458
|
+
const rows = await table.findMany({
|
|
1459
|
+
where: { clientId: input.clientId, ...input.orderIds ? { orderId: { in: [...input.orderIds] } } : {}, ...live },
|
|
1460
|
+
orderBy: { openedAt: "asc" }
|
|
1461
|
+
});
|
|
1462
|
+
return rows.map(disputeView);
|
|
1463
|
+
}
|
|
1464
|
+
__name(readDisputes, "readDisputes");
|
|
1465
|
+
async function findDispute(table, clientId, disputeId, orderId) {
|
|
1466
|
+
return await table.findFirst({ where: { clientId, disputeId, ...orderId === void 0 ? {} : { orderId } } });
|
|
1467
|
+
}
|
|
1468
|
+
__name(findDispute, "findDispute");
|
|
1469
|
+
|
|
1470
|
+
// src/core/disputes.ts
|
|
1471
|
+
async function heldOrder(ctx, clientId, event) {
|
|
1472
|
+
const db = await ctx.ports.db();
|
|
1473
|
+
return await db.marketplaceAcceptance.findFirst({
|
|
1474
|
+
where: { clientId, provider: event.provider, externalOrderId: event.externalOrderId },
|
|
1475
|
+
select: { orderId: true }
|
|
1476
|
+
});
|
|
1477
|
+
}
|
|
1478
|
+
__name(heldOrder, "heldOrder");
|
|
1479
|
+
async function tellHost(ctx, clientId, orderId, change) {
|
|
1480
|
+
ctx.ports.publishChanged(clientId);
|
|
1481
|
+
const hook = ctx.ports.order.onDispute;
|
|
1482
|
+
if (!hook) return;
|
|
1483
|
+
try {
|
|
1484
|
+
await hook.call(ctx.ports.order, clientId, orderId, change);
|
|
1485
|
+
} catch (error) {
|
|
1486
|
+
ctx.ports.logger.error(`marketplace.dispute: the host's onDispute failed for order ${orderId}`, error);
|
|
1487
|
+
}
|
|
1488
|
+
}
|
|
1489
|
+
__name(tellHost, "tellHost");
|
|
1490
|
+
function openedData(clientId, orderId, event, dispute, now) {
|
|
1491
|
+
return {
|
|
1492
|
+
clientId,
|
|
1493
|
+
orderId,
|
|
1494
|
+
provider: event.provider,
|
|
1495
|
+
externalOrderId: event.externalOrderId,
|
|
1496
|
+
disputeId: dispute.disputeId,
|
|
1497
|
+
status: "OPEN",
|
|
1498
|
+
action: dispute.action,
|
|
1499
|
+
handshakeType: dispute.handshakeType,
|
|
1500
|
+
reason: dispute.reason,
|
|
1501
|
+
message: dispute.message,
|
|
1502
|
+
evidences: dispute.evidenceUrls,
|
|
1503
|
+
items: dispute.items,
|
|
1504
|
+
requestedAmountCents: dispute.requestedAmountCents,
|
|
1505
|
+
expiresAt: dispute.expiresAt,
|
|
1506
|
+
openedAt: dispute.openedAt ?? now,
|
|
1507
|
+
timeoutAction: dispute.timeoutAction,
|
|
1508
|
+
alternatives: dispute.alternatives,
|
|
1509
|
+
rejectReasons: dispute.rejectReasons,
|
|
1510
|
+
acceptReasons: dispute.acceptReasons
|
|
1511
|
+
};
|
|
1512
|
+
}
|
|
1513
|
+
__name(openedData, "openedData");
|
|
1514
|
+
async function keepOpened(ctx, clientId, order, event, dispute) {
|
|
1515
|
+
const table = await disputeTable(ctx);
|
|
1516
|
+
if (!table) throw new Error("marketplace disputes need migration 20261203190000_marketplace_disputes");
|
|
1517
|
+
const existing = await table.findFirst({ where: { provider: event.provider, disputeId: dispute.disputeId } });
|
|
1518
|
+
if (existing) return "unchanged";
|
|
1519
|
+
await table.create({ data: openedData(clientId, order.orderId, event, dispute, ctx.ports.clock.now()) });
|
|
1520
|
+
return "opened";
|
|
1521
|
+
}
|
|
1522
|
+
__name(keepOpened, "keepOpened");
|
|
1523
|
+
async function keepSettled(ctx, clientId, event, settlement) {
|
|
1524
|
+
const table = await disputeTable(ctx);
|
|
1525
|
+
const row = table ? await findDispute(table, clientId, settlement.disputeId) : null;
|
|
1526
|
+
if (!table || !row || row.provider !== event.provider) return null;
|
|
1527
|
+
if (row.status === "SETTLED") return { orderId: row.orderId, changed: false };
|
|
1528
|
+
await table.updateMany({
|
|
1529
|
+
where: { id: row.id },
|
|
1530
|
+
data: {
|
|
1531
|
+
status: "SETTLED",
|
|
1532
|
+
settlement: settlement.outcome,
|
|
1533
|
+
settledAt: settlement.at ?? ctx.ports.clock.now(),
|
|
1534
|
+
settledAmountCents: settlement.amountCents
|
|
1535
|
+
}
|
|
1536
|
+
});
|
|
1537
|
+
return { orderId: row.orderId, changed: true };
|
|
1538
|
+
}
|
|
1539
|
+
__name(keepSettled, "keepSettled");
|
|
1540
|
+
async function deferred(ctx, inboxId, reason) {
|
|
1541
|
+
await deferInboxEvent(ctx, inboxId, reason);
|
|
1542
|
+
return { kind: "ignored", reason };
|
|
1543
|
+
}
|
|
1544
|
+
__name(deferred, "deferred");
|
|
1545
|
+
async function applyDisputeEvent(ctx, inboxId, clientId, event) {
|
|
1546
|
+
const dispute = event.dispute;
|
|
1547
|
+
if (!dispute || !event.externalOrderId) return null;
|
|
1548
|
+
if (dispute.kind === "settled") {
|
|
1549
|
+
const settled = await keepSettled(ctx, clientId, event, dispute.settlement);
|
|
1550
|
+
if (!settled) return deferred(ctx, inboxId, "settlement before its dispute");
|
|
1551
|
+
await settleInboxEvent(ctx, inboxId, "PROCESSED");
|
|
1552
|
+
const disputeId2 = dispute.settlement.disputeId;
|
|
1553
|
+
if (settled.changed) await tellHost(ctx, clientId, settled.orderId, { kind: "settled", disputeId: disputeId2 });
|
|
1554
|
+
return { kind: "dispute", orderId: settled.orderId, disputeId: disputeId2, change: settled.changed ? "settled" : "unchanged" };
|
|
1555
|
+
}
|
|
1556
|
+
const order = await heldOrder(ctx, clientId, event);
|
|
1557
|
+
if (!order) return deferred(ctx, inboxId, "dispute before its order");
|
|
1558
|
+
const change = await keepOpened(ctx, clientId, order, event, dispute.dispute);
|
|
1559
|
+
await settleInboxEvent(ctx, inboxId, "PROCESSED");
|
|
1560
|
+
const disputeId = dispute.dispute.disputeId;
|
|
1561
|
+
if (change === "opened") await tellHost(ctx, clientId, order.orderId, { kind: "opened", disputeId });
|
|
1562
|
+
return { kind: "dispute", orderId: order.orderId, disputeId, change };
|
|
1563
|
+
}
|
|
1564
|
+
__name(applyDisputeEvent, "applyDisputeEvent");
|
|
1565
|
+
var NONE = { answerReasonCode: null, answerAlternativeId: null, answerAmountCents: null, answerMinutes: null };
|
|
1566
|
+
function isPositiveInteger(value) {
|
|
1567
|
+
return typeof value === "number" && Number.isInteger(value) && value > 0;
|
|
1568
|
+
}
|
|
1569
|
+
__name(isPositiveInteger, "isPositiveInteger");
|
|
1570
|
+
function judgeAlternative(alternative, input) {
|
|
1571
|
+
const base = { ...NONE, answer: "ALTERNATIVE", answerAlternativeId: alternative.id };
|
|
1572
|
+
if (alternative.type === "ADDITIONAL_TIME") {
|
|
1573
|
+
const minutes = input.minutes;
|
|
1574
|
+
if (!isPositiveInteger(minutes)) return "INVALID_MINUTES";
|
|
1575
|
+
if (alternative.allowedMinutes.length > 0 && !alternative.allowedMinutes.includes(minutes)) return "INVALID_MINUTES";
|
|
1576
|
+
return { ...base, answerMinutes: minutes };
|
|
1577
|
+
}
|
|
1578
|
+
const amount = input.amountCents;
|
|
1579
|
+
if (!isPositiveInteger(amount)) return "INVALID_AMOUNT";
|
|
1580
|
+
if (alternative.maxAmountCents !== null && amount > alternative.maxAmountCents) return "INVALID_AMOUNT";
|
|
1581
|
+
return { ...base, answerAmountCents: amount };
|
|
1582
|
+
}
|
|
1583
|
+
__name(judgeAlternative, "judgeAlternative");
|
|
1584
|
+
function judgeAnswer(row, input) {
|
|
1585
|
+
if (input.kind === "ACCEPT") {
|
|
1586
|
+
return { ...NONE, answer: "ACCEPT", answerReasonCode: reasonsOf(row.acceptReasons)[0]?.code ?? null };
|
|
1587
|
+
}
|
|
1588
|
+
if (input.kind === "REJECT") {
|
|
1589
|
+
const known = reasonsOf(row.rejectReasons).some((reason) => reason.code === input.reasonCode);
|
|
1590
|
+
return known ? { ...NONE, answer: "REJECT", answerReasonCode: input.reasonCode } : "INVALID_REASON";
|
|
1591
|
+
}
|
|
1592
|
+
const alternative = alternativesOf(row.alternatives).find((entry) => entry.id === input.alternativeId);
|
|
1593
|
+
return alternative ? judgeAlternative(alternative, input) : "INVALID_ALTERNATIVE";
|
|
1594
|
+
}
|
|
1595
|
+
__name(judgeAnswer, "judgeAnswer");
|
|
1596
|
+
function closedFor(ctx, row, now) {
|
|
1597
|
+
if (row.status !== "OPEN") return "ALREADY_ANSWERED";
|
|
1598
|
+
if (row.expiresAt !== null && now.getTime() >= row.expiresAt.getTime()) return "EXPIRED";
|
|
1599
|
+
return ctx.registry.get(row.provider)?.disputes ? null : "UNSUPPORTED";
|
|
1600
|
+
}
|
|
1601
|
+
__name(closedFor, "closedFor");
|
|
1602
|
+
async function enqueueDisputeAnswer(ctx, row, key) {
|
|
1603
|
+
const outcome = await ctx.ports.enqueue.enqueue(
|
|
1604
|
+
MARKETPLACE_DISPUTE_ANSWER_JOB,
|
|
1605
|
+
{ clientId: row.clientId, disputeId: row.disputeId },
|
|
1606
|
+
{ dedupeKey: `${MARKETPLACE_DISPUTE_ANSWER_JOB}:${row.disputeId}:${key}` }
|
|
1607
|
+
);
|
|
1608
|
+
if (!outcome.enqueued) {
|
|
1609
|
+
ctx.ports.logger.warn(`${MARKETPLACE_DISPUTE_ANSWER_JOB}: dispute ${row.disputeId} not queued; the sweep re-queues it` + (outcome.reason ? ` (${outcome.reason})` : ""));
|
|
1610
|
+
}
|
|
1611
|
+
return outcome.enqueued;
|
|
1612
|
+
}
|
|
1613
|
+
__name(enqueueDisputeAnswer, "enqueueDisputeAnswer");
|
|
1614
|
+
async function answerDispute(ctx, input) {
|
|
1615
|
+
const now = input.now ?? ctx.ports.clock.now();
|
|
1616
|
+
const table = await disputeTable(ctx);
|
|
1617
|
+
const row = table ? await findDispute(table, input.clientId, input.disputeId, input.orderId) : null;
|
|
1618
|
+
if (!table || !row) return { kind: "refused", code: "NOT_FOUND" };
|
|
1619
|
+
const closed = closedFor(ctx, row, now);
|
|
1620
|
+
if (closed) return { kind: "refused", code: closed };
|
|
1621
|
+
const judged = judgeAnswer(row, input.answer);
|
|
1622
|
+
if (typeof judged === "string") return { kind: "refused", code: judged };
|
|
1623
|
+
const data = { ...judged, status: "SENDING", answeredAt: now, answerFailure: null };
|
|
1624
|
+
const claimed = await table.updateMany({ where: { id: row.id, status: "OPEN" }, data });
|
|
1625
|
+
if (claimed.count === 0) return { kind: "refused", code: "ALREADY_ANSWERED" };
|
|
1626
|
+
await enqueueDisputeAnswer(ctx, row, String(now.getTime()));
|
|
1627
|
+
ctx.ports.publishChanged(input.clientId);
|
|
1628
|
+
return { kind: "queued", dispute: disputeView({ ...row, ...data }) };
|
|
1629
|
+
}
|
|
1630
|
+
__name(answerDispute, "answerDispute");
|
|
1631
|
+
|
|
1632
|
+
// src/core/dispute-job.ts
|
|
1633
|
+
var DisputeAnswerRetryError = class extends Error {
|
|
1634
|
+
static {
|
|
1635
|
+
__name(this, "DisputeAnswerRetryError");
|
|
1636
|
+
}
|
|
1637
|
+
constructor(message) {
|
|
1638
|
+
super(message);
|
|
1639
|
+
this.name = "DisputeAnswerRetryError";
|
|
1640
|
+
}
|
|
1641
|
+
};
|
|
1642
|
+
function answerToSend(row) {
|
|
1643
|
+
if (row.answer === "ACCEPT") return { kind: "ACCEPT", reasonCode: row.answerReasonCode };
|
|
1644
|
+
if (row.answer === "REJECT") return row.answerReasonCode ? { kind: "REJECT", reasonCode: row.answerReasonCode } : null;
|
|
1645
|
+
const alternative = alternativesOf(row.alternatives).find((entry) => entry.id === row.answerAlternativeId);
|
|
1646
|
+
if (row.answer !== "ALTERNATIVE" || !alternative) return null;
|
|
1647
|
+
return { kind: "ALTERNATIVE", alternative, amountCents: row.answerAmountCents, minutes: row.answerMinutes };
|
|
1648
|
+
}
|
|
1649
|
+
__name(answerToSend, "answerToSend");
|
|
1650
|
+
async function giveBack(ctx, table, row, code) {
|
|
1651
|
+
await table.updateMany({
|
|
1652
|
+
where: { id: row.id, status: "SENDING" },
|
|
1653
|
+
data: {
|
|
1654
|
+
status: "OPEN",
|
|
1655
|
+
answerFailure: code,
|
|
1656
|
+
answer: null,
|
|
1657
|
+
answerReasonCode: null,
|
|
1658
|
+
answerAlternativeId: null,
|
|
1659
|
+
answerAmountCents: null,
|
|
1660
|
+
answerMinutes: null,
|
|
1661
|
+
answeredAt: null
|
|
1662
|
+
}
|
|
1663
|
+
});
|
|
1664
|
+
ctx.ports.logger.warn(`${MARKETPLACE_DISPUTE_ANSWER_JOB}: dispute ${row.disputeId} not answered: ${code}`);
|
|
1665
|
+
ctx.ports.publishChanged(row.clientId);
|
|
1666
|
+
return { kind: "failed", code };
|
|
1667
|
+
}
|
|
1668
|
+
__name(giveBack, "giveBack");
|
|
1669
|
+
async function stampAnswered(ctx, table, row, failure) {
|
|
1670
|
+
await table.updateMany({ where: { id: row.id, status: "SENDING" }, data: { status: "ANSWERED", answerFailure: failure } });
|
|
1671
|
+
ctx.ports.publishChanged(row.clientId);
|
|
1672
|
+
return { kind: "answered" };
|
|
1673
|
+
}
|
|
1674
|
+
__name(stampAnswered, "stampAnswered");
|
|
1675
|
+
async function settleSend(ctx, table, row, result, attempt) {
|
|
1676
|
+
if (result.kind === "accepted") return stampAnswered(ctx, table, row, null);
|
|
1677
|
+
if (result.kind === "refused") {
|
|
1678
|
+
return result.code === "ALREADY_ANSWERED" ? stampAnswered(ctx, table, row, "ALREADY_ANSWERED") : giveBack(ctx, table, row, result.code);
|
|
1679
|
+
}
|
|
1680
|
+
if (attempt.attempt < attempt.maxAttempts) throw new DisputeAnswerRetryError(`dispute ${row.disputeId}: ${result.reason}`);
|
|
1681
|
+
return giveBack(ctx, table, row, "UNREACHABLE");
|
|
1682
|
+
}
|
|
1683
|
+
__name(settleSend, "settleSend");
|
|
1684
|
+
async function runDisputeAnswer(ctx, payload, attempt) {
|
|
1685
|
+
const table = await disputeTable(ctx);
|
|
1686
|
+
const row = table ? await findDispute(table, payload.clientId, payload.disputeId) : null;
|
|
1687
|
+
if (!table || !row || row.status !== "SENDING") return { kind: "skipped", reason: "no answer to send" };
|
|
1688
|
+
const api = ctx.registry.get(row.provider)?.disputes;
|
|
1689
|
+
const answer = answerToSend(row);
|
|
1690
|
+
if (!api || !answer) return giveBack(ctx, table, row, "UNSUPPORTED");
|
|
1691
|
+
const connection = await readOutboundConnection(ctx, row.clientId, row.provider).catch(() => null);
|
|
1692
|
+
if (!connection) return giveBack(ctx, table, row, "NOT_CONNECTED");
|
|
1693
|
+
const call2 = { http: ctx.ports.http, tokens: ctx.tokens, now: ctx.ports.clock.now(), ...connection };
|
|
1694
|
+
const result = await api.answer(call2, { externalOrderId: row.externalOrderId, disputeId: row.disputeId }, answer);
|
|
1695
|
+
return settleSend(ctx, table, row, result, attempt);
|
|
1696
|
+
}
|
|
1697
|
+
__name(runDisputeAnswer, "runDisputeAnswer");
|
|
1698
|
+
var SWEEP_BATCH = 100;
|
|
1699
|
+
var DISPUTE_STALE_MS = 10 * 6e4;
|
|
1700
|
+
var DISPUTE_GIVE_UP_MS = 24 * 60 * 6e4;
|
|
1701
|
+
async function sweepDisputes(ctx, now) {
|
|
1702
|
+
const table = await disputeTable(ctx);
|
|
1703
|
+
if (!table) return { requeued: 0, givenUp: 0 };
|
|
1704
|
+
const stale = new Date(now.getTime() - DISPUTE_STALE_MS);
|
|
1705
|
+
const giveUp = new Date(now.getTime() - DISPUTE_GIVE_UP_MS);
|
|
1706
|
+
const rows = await table.findMany({ where: { status: "SENDING", answeredAt: { lt: stale } }, orderBy: { answeredAt: "asc" }, take: SWEEP_BATCH });
|
|
1707
|
+
let requeued = 0;
|
|
1708
|
+
let givenUp = 0;
|
|
1709
|
+
for (const row of rows) {
|
|
1710
|
+
if (row.answeredAt !== null && row.answeredAt.getTime() < giveUp.getTime()) {
|
|
1711
|
+
await giveBack(ctx, table, row, "UNREACHABLE");
|
|
1712
|
+
givenUp += 1;
|
|
1713
|
+
} else if (await enqueueDisputeAnswer(ctx, row, `sweep:${now.getTime()}`)) requeued += 1;
|
|
1714
|
+
}
|
|
1715
|
+
return { requeued, givenUp };
|
|
1716
|
+
}
|
|
1717
|
+
__name(sweepDisputes, "sweepDisputes");
|
|
1718
|
+
|
|
1374
1719
|
// src/core/menu-links.ts
|
|
1375
1720
|
var LINK_SELECT = {
|
|
1376
1721
|
id: true,
|
|
@@ -2021,7 +2366,13 @@ async function sweepOutbound(ctx, now) {
|
|
|
2021
2366
|
if (givenUp > 0) ctx.ports.logger.error(`marketplace.outbound-sweep: gave up on ${givenUp} cancellation request(s).`);
|
|
2022
2367
|
const catalogueRequeued = await sweepCatalogue(ctx, now);
|
|
2023
2368
|
const status = await sweepStatus(ctx, now);
|
|
2024
|
-
|
|
2369
|
+
const disputes = await sweepDisputes(ctx, now);
|
|
2370
|
+
return {
|
|
2371
|
+
requeued: requeued + status.requeued + disputes.requeued,
|
|
2372
|
+
givenUp: givenUp + status.givenUp + disputes.givenUp,
|
|
2373
|
+
timedOut,
|
|
2374
|
+
catalogueRequeued
|
|
2375
|
+
};
|
|
2025
2376
|
}
|
|
2026
2377
|
__name(sweepOutbound, "sweepOutbound");
|
|
2027
2378
|
|
|
@@ -2881,6 +3232,7 @@ export {
|
|
|
2881
3232
|
expireLapsedAcceptances,
|
|
2882
3233
|
cancelAcceptance,
|
|
2883
3234
|
sealLink,
|
|
3235
|
+
sealAppLink,
|
|
2884
3236
|
appCredentialsOf,
|
|
2885
3237
|
linkTokenKey,
|
|
2886
3238
|
cacheLinkToken,
|
|
@@ -2923,6 +3275,13 @@ export {
|
|
|
2923
3275
|
CancellationReasonsRetryError,
|
|
2924
3276
|
runCancellationReasons,
|
|
2925
3277
|
readOrderReasons,
|
|
3278
|
+
MARKETPLACE_DISPUTE_ANSWER_JOB,
|
|
3279
|
+
disputeView,
|
|
3280
|
+
readDisputes,
|
|
3281
|
+
applyDisputeEvent,
|
|
3282
|
+
answerDispute,
|
|
3283
|
+
DisputeAnswerRetryError,
|
|
3284
|
+
runDisputeAnswer,
|
|
2926
3285
|
ingestMarketplaceOrder,
|
|
2927
3286
|
CANCELLED_BEFORE_INGEST_REASON,
|
|
2928
3287
|
markCancelledBeforeIngest,
|
|
@@ -2951,4 +3310,4 @@ export {
|
|
|
2951
3310
|
AvailabilityRetryError,
|
|
2952
3311
|
runAvailability
|
|
2953
3312
|
};
|
|
2954
|
-
//# sourceMappingURL=chunk-
|
|
3313
|
+
//# sourceMappingURL=chunk-UZO2NILN.js.map
|