@harrylabsj/kiwi 0.12.13 → 0.12.15

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 (140) hide show
  1. package/dist/a2a/node.d.ts +5 -0
  2. package/dist/a2a/node.js +69 -2
  3. package/dist/a2a/node.js.map +1 -1
  4. package/dist/a2a/server/merchant-handler.d.ts +8 -0
  5. package/dist/a2a/server/merchant-handler.js +105 -14
  6. package/dist/a2a/server/merchant-handler.js.map +1 -1
  7. package/dist/a2a/server/negotiation-advisor.d.ts +54 -0
  8. package/dist/a2a/server/negotiation-advisor.js +107 -0
  9. package/dist/a2a/server/negotiation-advisor.js.map +1 -0
  10. package/dist/a2a/server/negotiation-approvals.d.ts +101 -0
  11. package/dist/a2a/server/negotiation-approvals.js +208 -0
  12. package/dist/a2a/server/negotiation-approvals.js.map +1 -0
  13. package/dist/buyer-core/a2a-knp.d.ts +14 -0
  14. package/dist/buyer-core/a2a-knp.js +24 -0
  15. package/dist/buyer-core/a2a-knp.js.map +1 -1
  16. package/dist/buyer-core/a2a-negotiator.js +66 -11
  17. package/dist/buyer-core/a2a-negotiator.js.map +1 -1
  18. package/dist/buyer-core/protocol-recovery.d.ts +1 -1
  19. package/dist/buyer-core/receive-gate.js +7 -0
  20. package/dist/buyer-core/receive-gate.js.map +1 -1
  21. package/dist/buyer-core/service.d.ts +13 -0
  22. package/dist/buyer-core/service.js +73 -10
  23. package/dist/buyer-core/service.js.map +1 -1
  24. package/dist/cli.js +59 -0
  25. package/dist/cli.js.map +1 -1
  26. package/dist/cloud/bootstrap.js +12 -0
  27. package/dist/cloud/bootstrap.js.map +1 -1
  28. package/dist/cloud/merchant-connect.d.ts +8 -0
  29. package/dist/cloud/merchant-connect.js +3 -0
  30. package/dist/cloud/merchant-connect.js.map +1 -1
  31. package/dist/http/merchant-management/api.d.ts +3 -0
  32. package/dist/http/merchant-management/api.js +419 -0
  33. package/dist/http/merchant-management/api.js.map +1 -1
  34. package/dist/http/merchant-management/page.js +95 -1
  35. package/dist/http/merchant-management/page.js.map +1 -1
  36. package/dist/merchant/application/actor.d.ts +1 -1
  37. package/dist/merchant/application/actor.js +43 -1
  38. package/dist/merchant/application/actor.js.map +1 -1
  39. package/dist/merchant/quote-calculator.d.ts +7 -0
  40. package/dist/merchant/quote-calculator.js +35 -24
  41. package/dist/merchant/quote-calculator.js.map +1 -1
  42. package/dist/merchant-core/commercial/calculate.d.ts +18 -0
  43. package/dist/merchant-core/commercial/calculate.js +196 -0
  44. package/dist/merchant-core/commercial/calculate.js.map +1 -0
  45. package/dist/merchant-core/commercial/types.d.ts +91 -0
  46. package/dist/merchant-core/commercial/types.js +17 -0
  47. package/dist/merchant-core/commercial/types.js.map +1 -0
  48. package/dist/merchant-core/contribution/calculate.d.ts +24 -0
  49. package/dist/merchant-core/contribution/calculate.js +172 -0
  50. package/dist/merchant-core/contribution/calculate.js.map +1 -0
  51. package/dist/merchant-core/contribution/repository.d.ts +60 -0
  52. package/dist/merchant-core/contribution/repository.js +121 -0
  53. package/dist/merchant-core/contribution/repository.js.map +1 -0
  54. package/dist/merchant-core/contribution/types.d.ts +97 -0
  55. package/dist/merchant-core/contribution/types.js +24 -0
  56. package/dist/merchant-core/contribution/types.js.map +1 -0
  57. package/dist/merchant-core/followup/lease.d.ts +27 -0
  58. package/dist/merchant-core/followup/lease.js +45 -0
  59. package/dist/merchant-core/followup/lease.js.map +1 -0
  60. package/dist/merchant-core/followup/repository.d.ts +141 -0
  61. package/dist/merchant-core/followup/repository.js +280 -0
  62. package/dist/merchant-core/followup/repository.js.map +1 -0
  63. package/dist/merchant-core/followup/retry.d.ts +27 -0
  64. package/dist/merchant-core/followup/retry.js +61 -0
  65. package/dist/merchant-core/followup/retry.js.map +1 -0
  66. package/dist/merchant-core/followup/service.d.ts +114 -0
  67. package/dist/merchant-core/followup/service.js +191 -0
  68. package/dist/merchant-core/followup/service.js.map +1 -0
  69. package/dist/merchant-core/followup/state.d.ts +25 -0
  70. package/dist/merchant-core/followup/state.js +126 -0
  71. package/dist/merchant-core/followup/state.js.map +1 -0
  72. package/dist/merchant-core/followup/types.d.ts +75 -0
  73. package/dist/merchant-core/followup/types.js +41 -0
  74. package/dist/merchant-core/followup/types.js.map +1 -0
  75. package/dist/merchant-core/metrics/timing.d.ts +63 -0
  76. package/dist/merchant-core/metrics/timing.js +125 -0
  77. package/dist/merchant-core/metrics/timing.js.map +1 -0
  78. package/dist/merchant-core/operating/repository.d.ts +97 -0
  79. package/dist/merchant-core/operating/repository.js +266 -0
  80. package/dist/merchant-core/operating/repository.js.map +1 -0
  81. package/dist/merchant-core/operating/service.d.ts +63 -0
  82. package/dist/merchant-core/operating/service.js +336 -0
  83. package/dist/merchant-core/operating/service.js.map +1 -0
  84. package/dist/merchant-core/operating/types.d.ts +102 -0
  85. package/dist/merchant-core/operating/types.js +24 -0
  86. package/dist/merchant-core/operating/types.js.map +1 -0
  87. package/dist/merchant-core/opportunity/repository.d.ts +128 -0
  88. package/dist/merchant-core/opportunity/repository.js +251 -0
  89. package/dist/merchant-core/opportunity/repository.js.map +1 -0
  90. package/dist/merchant-core/opportunity/service.d.ts +117 -0
  91. package/dist/merchant-core/opportunity/service.js +388 -0
  92. package/dist/merchant-core/opportunity/service.js.map +1 -0
  93. package/dist/merchant-core/opportunity/state.d.ts +33 -0
  94. package/dist/merchant-core/opportunity/state.js +166 -0
  95. package/dist/merchant-core/opportunity/state.js.map +1 -0
  96. package/dist/merchant-core/opportunity/types.d.ts +141 -0
  97. package/dist/merchant-core/opportunity/types.js +42 -0
  98. package/dist/merchant-core/opportunity/types.js.map +1 -0
  99. package/dist/merchant-core/ops/facade.d.ts +159 -0
  100. package/dist/merchant-core/ops/facade.js +213 -0
  101. package/dist/merchant-core/ops/facade.js.map +1 -0
  102. package/dist/merchant-core/ops/schema.d.ts +25 -0
  103. package/dist/merchant-core/ops/schema.js +448 -0
  104. package/dist/merchant-core/ops/schema.js.map +1 -0
  105. package/dist/merchant-core/ops/surface.d.ts +49 -0
  106. package/dist/merchant-core/ops/surface.js +304 -0
  107. package/dist/merchant-core/ops/surface.js.map +1 -0
  108. package/dist/merchant-core/orders/csv.d.ts +36 -0
  109. package/dist/merchant-core/orders/csv.js +173 -0
  110. package/dist/merchant-core/orders/csv.js.map +1 -0
  111. package/dist/merchant-core/orders/model.d.ts +155 -0
  112. package/dist/merchant-core/orders/model.js +237 -0
  113. package/dist/merchant-core/orders/model.js.map +1 -0
  114. package/dist/merchant-core/orders/repository.d.ts +99 -0
  115. package/dist/merchant-core/orders/repository.js +221 -0
  116. package/dist/merchant-core/orders/repository.js.map +1 -0
  117. package/dist/merchant-core/orders/service.d.ts +84 -0
  118. package/dist/merchant-core/orders/service.js +121 -0
  119. package/dist/merchant-core/orders/service.js.map +1 -0
  120. package/dist/merchant-core/orders/shopping-cli-source.d.ts +58 -0
  121. package/dist/merchant-core/orders/shopping-cli-source.js +164 -0
  122. package/dist/merchant-core/orders/shopping-cli-source.js.map +1 -0
  123. package/dist/merchant-core/proposal/engine.d.ts +31 -0
  124. package/dist/merchant-core/proposal/engine.js +348 -0
  125. package/dist/merchant-core/proposal/engine.js.map +1 -0
  126. package/dist/merchant-core/proposal/publish.d.ts +46 -0
  127. package/dist/merchant-core/proposal/publish.js +79 -0
  128. package/dist/merchant-core/proposal/publish.js.map +1 -0
  129. package/dist/merchant-core/proposal/repository.d.ts +60 -0
  130. package/dist/merchant-core/proposal/repository.js +186 -0
  131. package/dist/merchant-core/proposal/repository.js.map +1 -0
  132. package/dist/merchant-core/proposal/service.d.ts +73 -0
  133. package/dist/merchant-core/proposal/service.js +149 -0
  134. package/dist/merchant-core/proposal/service.js.map +1 -0
  135. package/dist/merchant-core/proposal/types.d.ts +163 -0
  136. package/dist/merchant-core/proposal/types.js +25 -0
  137. package/dist/merchant-core/proposal/types.js.map +1 -0
  138. package/dist/product-cli.js +5 -0
  139. package/dist/product-cli.js.map +1 -1
  140. package/package.json +1 -1
@@ -56,6 +56,11 @@ import { WorkbenchConfirmationError, } from "./webauthn-confirmation.js";
56
56
  import { MerchantWorkbenchError, } from "../../merchant/workbench-service.js";
57
57
  import { NEGOTIATION_STATUS_FILTERS, } from "../../merchant/negotiation-observer.js";
58
58
  import { parseReportPeriod, } from "../../merchant/operations-report.js";
59
+ import { OrderFactError } from "../../merchant-core/orders/model.js";
60
+ import { OperatingPlanError } from "../../merchant-core/operating/types.js";
61
+ import { OpportunityError } from "../../merchant-core/opportunity/types.js";
62
+ import { ProposalError } from "../../merchant-core/proposal/types.js";
63
+ import { FollowUpError } from "../../merchant-core/followup/types.js";
59
64
  import { parseExactMoney, WorkbenchMoneyError, WORKBENCH_CURRENCY_TABLE_VERSION, } from "../../merchant/application/money.js";
60
65
  import { CONNECTION_PAIRING_SAFE_CODES } from "../../cloud/connect-service.js";
61
66
  import { EXACT_PRODUCT_TOOLS } from "../../merchant/exact-product-executors.js";
@@ -588,9 +593,203 @@ export function createMerchantManagementApiHandler(options) {
588
593
  }), { "x-request-id": requestId });
589
594
  return;
590
595
  }
596
+ if (options.merchantOperations !== undefined) {
597
+ const ops = options.merchantOperations;
598
+ if (rest === "/operating-plans") {
599
+ const auth = requireActor(req);
600
+ authorizeOrThrow(auth.ctx, "plans:read");
601
+ writeJson(res, 200, { plans: ops.listPlans() }, { "x-request-id": requestId });
602
+ return;
603
+ }
604
+ const planMatch = /^\/operating-plans\/([^/]+)$/.exec(rest);
605
+ if (planMatch !== null) {
606
+ const auth = requireActor(req);
607
+ authorizeOrThrow(auth.ctx, "plans:read");
608
+ writeJson(res, 200, ops.getPlan(pathSegment(planMatch[1] ?? "")), { "x-request-id": requestId });
609
+ return;
610
+ }
611
+ if (rest === "/opportunities") {
612
+ const auth = requireActor(req);
613
+ authorizeOrThrow(auth.ctx, "opportunities:read");
614
+ writeJson(res, 200, { opportunities: ops.listOpportunities() }, { "x-request-id": requestId });
615
+ return;
616
+ }
617
+ const opportunityMatch = /^\/opportunities\/([^/]+)$/.exec(rest);
618
+ if (opportunityMatch !== null) {
619
+ const auth = requireActor(req);
620
+ authorizeOrThrow(auth.ctx, "opportunities:read");
621
+ writeJson(res, 200, ops.getOpportunity(pathSegment(opportunityMatch[1] ?? "")), { "x-request-id": requestId });
622
+ return;
623
+ }
624
+ const proposalsMatch = /^\/opportunities\/([^/]+)\/proposals$/.exec(rest);
625
+ if (proposalsMatch !== null) {
626
+ const auth = requireActor(req);
627
+ authorizeOrThrow(auth.ctx, "opportunities:read");
628
+ writeJson(res, 200, { proposals: ops.listProposals(pathSegment(proposalsMatch[1] ?? "")) }, { "x-request-id": requestId });
629
+ return;
630
+ }
631
+ if (rest === "/followups") {
632
+ const auth = requireActor(req);
633
+ authorizeOrThrow(auth.ctx, "followups:read");
634
+ writeJson(res, 200, { followups: ops.listFollowUps() }, { "x-request-id": requestId });
635
+ return;
636
+ }
637
+ if (rest === "/orders") {
638
+ const auth = requireActor(req);
639
+ authorizeOrThrow(auth.ctx, "orders:read");
640
+ writeJson(res, 200, { orders: ops.listOrders() }, { "x-request-id": requestId });
641
+ return;
642
+ }
643
+ }
591
644
  writeWorkbenchProblem(res, "RESOURCE_NOT_FOUND", requestId, "资源不存在", "Workbench API 路由不存在或尚未实现。");
592
645
  }
593
646
  async function routeWorkbenchV1Post(req, res, rest, requestId, url) {
647
+ if (options.merchantOperations !== undefined) {
648
+ const ops = options.merchantOperations;
649
+ const readOpsBody = async () => {
650
+ const body = await readJsonBody(req);
651
+ if (body !== null && typeof body === "object" && !Array.isArray(body)) {
652
+ const key = body.idempotency_key;
653
+ if (typeof key === "string" && key !== "")
654
+ return { body, idempotencyKey: key };
655
+ }
656
+ return { body };
657
+ };
658
+ if (rest === "/operating-plans") {
659
+ const auth = requireActor(req);
660
+ assertWriteGuards(req, auth.sessionId);
661
+ authorizeOrThrow(auth.ctx, "plans:manage");
662
+ const { body, idempotencyKey } = await readOpsBody();
663
+ writeJson(res, 201, ops.createPlan(body, auth.ctx.actorId, idempotencyKey), { "x-request-id": requestId });
664
+ return;
665
+ }
666
+ const planActionMatch = /^\/operating-plans\/([^/]+)\/(pause|resume)$/.exec(rest);
667
+ if (planActionMatch !== null) {
668
+ const auth = requireActor(req);
669
+ assertWriteGuards(req, auth.sessionId);
670
+ authorizeOrThrow(auth.ctx, "plans:manage");
671
+ const planId = pathSegment(planActionMatch[1] ?? "");
672
+ const { body } = await readOpsBody();
673
+ writeJson(res, 200, planActionMatch[2] === "pause"
674
+ ? ops.pausePlan(planId, body, auth.ctx.actorId)
675
+ : ops.resumePlan(planId, body, auth.ctx.actorId), { "x-request-id": requestId });
676
+ return;
677
+ }
678
+ if (rest === "/opportunities") {
679
+ const auth = requireActor(req);
680
+ assertWriteGuards(req, auth.sessionId);
681
+ authorizeOrThrow(auth.ctx, "opportunities:manage");
682
+ const { body, idempotencyKey } = await readOpsBody();
683
+ writeJson(res, 201, ops.createOpportunity(body, auth.ctx.actorId, idempotencyKey), { "x-request-id": requestId });
684
+ return;
685
+ }
686
+ const requirementMatch = /^\/opportunities\/([^/]+)\/requirements$/.exec(rest);
687
+ if (requirementMatch !== null) {
688
+ const auth = requireActor(req);
689
+ assertWriteGuards(req, auth.sessionId);
690
+ authorizeOrThrow(auth.ctx, "opportunities:manage");
691
+ const { body, idempotencyKey } = await readOpsBody();
692
+ writeJson(res, 200, ops.recordRequirement(pathSegment(requirementMatch[1] ?? ""), body, auth.ctx.actorId, idempotencyKey), { "x-request-id": requestId });
693
+ return;
694
+ }
695
+ const advanceOpportunityMatch = /^\/opportunities\/([^/]+)\/advance$/.exec(rest);
696
+ if (advanceOpportunityMatch !== null) {
697
+ const auth = requireActor(req);
698
+ assertWriteGuards(req, auth.sessionId);
699
+ authorizeOrThrow(auth.ctx, "opportunities:manage");
700
+ const { body, idempotencyKey } = await readOpsBody();
701
+ writeJson(res, 200, ops.advanceOpportunity(pathSegment(advanceOpportunityMatch[1] ?? ""), body, auth.ctx.actorId, idempotencyKey), { "x-request-id": requestId });
702
+ return;
703
+ }
704
+ if (rest === "/proposal-sets") {
705
+ const auth = requireActor(req);
706
+ assertWriteGuards(req, auth.sessionId);
707
+ authorizeOrThrow(auth.ctx, "opportunities:manage");
708
+ const { body, idempotencyKey } = await readOpsBody();
709
+ writeJson(res, 201, ops.prepareProposal(body, idempotencyKey), { "x-request-id": requestId });
710
+ return;
711
+ }
712
+ if (rest === "/proposal-sets/select") {
713
+ const auth = requireActor(req);
714
+ assertWriteGuards(req, auth.sessionId);
715
+ authorizeOrThrow(auth.ctx, "opportunities:manage");
716
+ const { body, idempotencyKey } = await readOpsBody();
717
+ writeJson(res, 201, ops.selectProposal(body, auth.ctx.actorId, idempotencyKey), { "x-request-id": requestId });
718
+ return;
719
+ }
720
+ if (rest === "/proposal-sets/approve") {
721
+ const auth = requireActor(req);
722
+ assertWriteGuards(req, auth.sessionId);
723
+ authorizeOrThrow(auth.ctx, "approvals:decide");
724
+ const { body, idempotencyKey } = await readOpsBody();
725
+ writeJson(res, 201, ops.approveProposal(body, idempotencyKey), { "x-request-id": requestId });
726
+ return;
727
+ }
728
+ if (rest === "/followups") {
729
+ const auth = requireActor(req);
730
+ assertWriteGuards(req, auth.sessionId);
731
+ authorizeOrThrow(auth.ctx, "followups:manage");
732
+ const { body } = await readOpsBody();
733
+ writeJson(res, 201, ops.scheduleFollowUp(body), { "x-request-id": requestId });
734
+ return;
735
+ }
736
+ const followUpActionMatch = /^\/followups\/([^/]+)\/(claim|finish|stop)$/.exec(rest);
737
+ if (followUpActionMatch !== null) {
738
+ const auth = requireActor(req);
739
+ assertWriteGuards(req, auth.sessionId);
740
+ authorizeOrThrow(auth.ctx, "followups:manage");
741
+ const taskId = pathSegment(followUpActionMatch[1] ?? "");
742
+ const { body } = await readOpsBody();
743
+ const action = followUpActionMatch[2];
744
+ writeJson(res, 200, action === "claim" ? ops.claimFollowUp(taskId, body)
745
+ : action === "finish" ? ops.finishFollowUp(taskId, body)
746
+ : ops.stopFollowUp(taskId, body), { "x-request-id": requestId });
747
+ return;
748
+ }
749
+ if (rest === "/orders/reports") {
750
+ const auth = requireActor(req);
751
+ assertWriteGuards(req, auth.sessionId);
752
+ authorizeOrThrow(auth.ctx, "orders:report");
753
+ const { body } = await readOpsBody();
754
+ writeJson(res, 201, ops.recordOrderReport(body, auth.ctx.actorId), { "x-request-id": requestId });
755
+ return;
756
+ }
757
+ if (rest === "/orders/csv") {
758
+ const auth = requireActor(req);
759
+ assertWriteGuards(req, auth.sessionId);
760
+ authorizeOrThrow(auth.ctx, "orders:report");
761
+ const { body } = await readOpsBody();
762
+ writeJson(res, 201, { orders: ops.recordCsvOrderReport(body, auth.ctx.actorId) }, { "x-request-id": requestId });
763
+ return;
764
+ }
765
+ const orderAttributionMatch = /^\/orders\/([^/]+)\/attributions$/.exec(rest);
766
+ if (orderAttributionMatch !== null) {
767
+ const auth = requireActor(req);
768
+ assertWriteGuards(req, auth.sessionId);
769
+ authorizeOrThrow(auth.ctx, "orders:report");
770
+ const { body } = await readOpsBody();
771
+ writeJson(res, 200, ops.attributeOrder(pathSegment(orderAttributionMatch[1] ?? ""), body), { "x-request-id": requestId });
772
+ return;
773
+ }
774
+ const orderRefundMatch = /^\/orders\/([^/]+)\/refunds$/.exec(rest);
775
+ if (orderRefundMatch !== null) {
776
+ const auth = requireActor(req);
777
+ assertWriteGuards(req, auth.sessionId);
778
+ authorizeOrThrow(auth.ctx, "orders:report");
779
+ const { body } = await readOpsBody();
780
+ writeJson(res, 200, ops.recordOrderRefund(pathSegment(orderRefundMatch[1] ?? ""), body), { "x-request-id": requestId });
781
+ return;
782
+ }
783
+ const orderContributionMatch = /^\/orders\/([^/]+)\/contribution$/.exec(rest);
784
+ if (orderContributionMatch !== null) {
785
+ const auth = requireActor(req);
786
+ assertWriteGuards(req, auth.sessionId);
787
+ authorizeOrThrow(auth.ctx, "contribution:manage");
788
+ const { body } = await readOpsBody();
789
+ writeJson(res, 200, ops.assessOrderContribution(pathSegment(orderContributionMatch[1] ?? ""), body), { "x-request-id": requestId });
790
+ return;
791
+ }
792
+ }
594
793
  const publicationCommitMatch = /^\/products\/publication-drafts\/(flp_[a-z0-9]+_[0-9a-f]{12})\/commit$/.exec(rest);
595
794
  if (rest === "/products/publication-drafts" || publicationCommitMatch !== null) {
596
795
  const auth = requireActor(req);
@@ -1701,6 +1900,50 @@ export function createMerchantManagementApiHandler(options) {
1701
1900
  writeJson(res, 200, readService.getOperation(auth.ctx, pathSegment(operationMatch[1] ?? "")));
1702
1901
  return;
1703
1902
  }
1903
+ if (options.merchantOperations !== undefined) {
1904
+ const ops = options.merchantOperations;
1905
+ if (rest === "/operating-plans") {
1906
+ const auth = requireActor(req);
1907
+ authorizeOrThrow(auth.ctx, "plans:read");
1908
+ writeJson(res, 200, { plans: ops.listPlans() });
1909
+ return;
1910
+ }
1911
+ const planMatch = /^\/operating-plans\/([^/]+)$/.exec(rest);
1912
+ if (planMatch !== null) {
1913
+ const auth = requireActor(req);
1914
+ authorizeOrThrow(auth.ctx, "plans:read");
1915
+ writeJson(res, 200, ops.getPlan(pathSegment(planMatch[1] ?? "")));
1916
+ return;
1917
+ }
1918
+ if (rest === "/opportunities") {
1919
+ const auth = requireActor(req);
1920
+ authorizeOrThrow(auth.ctx, "opportunities:read");
1921
+ writeJson(res, 200, { opportunities: ops.listOpportunities() });
1922
+ return;
1923
+ }
1924
+ const opportunityMatch = /^\/opportunities\/([^/]+)$/.exec(rest);
1925
+ if (opportunityMatch !== null) {
1926
+ const auth = requireActor(req);
1927
+ authorizeOrThrow(auth.ctx, "opportunities:read");
1928
+ writeJson(res, 200, ops.getOpportunity(pathSegment(opportunityMatch[1] ?? "")));
1929
+ return;
1930
+ }
1931
+ const proposalsMatch = /^\/opportunities\/([^/]+)\/proposals$/.exec(rest);
1932
+ if (proposalsMatch !== null) {
1933
+ const auth = requireActor(req);
1934
+ authorizeOrThrow(auth.ctx, "opportunities:read");
1935
+ writeJson(res, 200, {
1936
+ proposals: ops.listProposals(pathSegment(proposalsMatch[1] ?? "")),
1937
+ });
1938
+ return;
1939
+ }
1940
+ if (rest === "/followups") {
1941
+ const auth = requireActor(req);
1942
+ authorizeOrThrow(auth.ctx, "followups:read");
1943
+ writeJson(res, 200, { followups: ops.listFollowUps() });
1944
+ return;
1945
+ }
1946
+ }
1704
1947
  if (isKnownPath(rest)) {
1705
1948
  writeJson(res, 405, errorBody("invalid_input", "method not allowed"), { allow: "POST" });
1706
1949
  return;
@@ -1709,6 +1952,111 @@ export function createMerchantManagementApiHandler(options) {
1709
1952
  }
1710
1953
  // ── 写路由(BD §10.1;每条都过 CSRF/Origin + 幂等 + 服务层复检)──────────
1711
1954
  async function routePost(req, res, rest) {
1955
+ if (options.merchantOperations !== undefined) {
1956
+ const ops = options.merchantOperations;
1957
+ const readOpsBody = async () => {
1958
+ const body = await readJsonBody(req);
1959
+ if (body !== null && typeof body === "object" && !Array.isArray(body)) {
1960
+ const key = body.idempotency_key;
1961
+ if (typeof key === "string" && key !== "")
1962
+ return { body, idempotencyKey: key };
1963
+ }
1964
+ return { body };
1965
+ };
1966
+ if (rest === "/operating-plans") {
1967
+ const auth = requireActor(req);
1968
+ assertWriteGuards(req, auth.sessionId);
1969
+ authorizeOrThrow(auth.ctx, "plans:manage");
1970
+ const { body, idempotencyKey } = await readOpsBody();
1971
+ writeJson(res, 201, ops.createPlan(body, auth.ctx.actorId, idempotencyKey));
1972
+ return;
1973
+ }
1974
+ const planActionMatch = /^\/operating-plans\/([^/]+)\/(pause|resume)$/.exec(rest);
1975
+ if (planActionMatch !== null) {
1976
+ const auth = requireActor(req);
1977
+ assertWriteGuards(req, auth.sessionId);
1978
+ authorizeOrThrow(auth.ctx, "plans:manage");
1979
+ const planId = pathSegment(planActionMatch[1] ?? "");
1980
+ const { body } = await readOpsBody();
1981
+ writeJson(res, 200, planActionMatch[2] === "pause"
1982
+ ? ops.pausePlan(planId, body, auth.ctx.actorId)
1983
+ : ops.resumePlan(planId, body, auth.ctx.actorId));
1984
+ return;
1985
+ }
1986
+ if (rest === "/opportunities") {
1987
+ const auth = requireActor(req);
1988
+ assertWriteGuards(req, auth.sessionId);
1989
+ authorizeOrThrow(auth.ctx, "opportunities:manage");
1990
+ const { body, idempotencyKey } = await readOpsBody();
1991
+ writeJson(res, 201, ops.createOpportunity(body, auth.ctx.actorId, idempotencyKey));
1992
+ return;
1993
+ }
1994
+ const requirementMatch = /^\/opportunities\/([^/]+)\/requirements$/.exec(rest);
1995
+ if (requirementMatch !== null) {
1996
+ const auth = requireActor(req);
1997
+ assertWriteGuards(req, auth.sessionId);
1998
+ authorizeOrThrow(auth.ctx, "opportunities:manage");
1999
+ const { body, idempotencyKey } = await readOpsBody();
2000
+ writeJson(res, 200, ops.recordRequirement(pathSegment(requirementMatch[1] ?? ""), body, auth.ctx.actorId, idempotencyKey));
2001
+ return;
2002
+ }
2003
+ const advanceOpportunityMatch = /^\/opportunities\/([^/]+)\/advance$/.exec(rest);
2004
+ if (advanceOpportunityMatch !== null) {
2005
+ const auth = requireActor(req);
2006
+ assertWriteGuards(req, auth.sessionId);
2007
+ authorizeOrThrow(auth.ctx, "opportunities:manage");
2008
+ const { body, idempotencyKey } = await readOpsBody();
2009
+ writeJson(res, 200, ops.advanceOpportunity(pathSegment(advanceOpportunityMatch[1] ?? ""), body, auth.ctx.actorId, idempotencyKey));
2010
+ return;
2011
+ }
2012
+ if (rest === "/proposal-sets") {
2013
+ const auth = requireActor(req);
2014
+ assertWriteGuards(req, auth.sessionId);
2015
+ authorizeOrThrow(auth.ctx, "opportunities:manage");
2016
+ const { body, idempotencyKey } = await readOpsBody();
2017
+ writeJson(res, 201, ops.prepareProposal(body, idempotencyKey));
2018
+ return;
2019
+ }
2020
+ if (rest === "/proposal-sets/select") {
2021
+ const auth = requireActor(req);
2022
+ assertWriteGuards(req, auth.sessionId);
2023
+ authorizeOrThrow(auth.ctx, "opportunities:manage");
2024
+ const { body, idempotencyKey } = await readOpsBody();
2025
+ writeJson(res, 201, ops.selectProposal(body, auth.ctx.actorId, idempotencyKey));
2026
+ return;
2027
+ }
2028
+ if (rest === "/proposal-sets/approve") {
2029
+ const auth = requireActor(req);
2030
+ assertWriteGuards(req, auth.sessionId);
2031
+ authorizeOrThrow(auth.ctx, "approvals:decide");
2032
+ const { body, idempotencyKey } = await readOpsBody();
2033
+ writeJson(res, 201, ops.approveProposal(body, idempotencyKey));
2034
+ return;
2035
+ }
2036
+ if (rest === "/followups") {
2037
+ const auth = requireActor(req);
2038
+ assertWriteGuards(req, auth.sessionId);
2039
+ authorizeOrThrow(auth.ctx, "followups:manage");
2040
+ const { body } = await readOpsBody();
2041
+ writeJson(res, 201, ops.scheduleFollowUp(body));
2042
+ return;
2043
+ }
2044
+ const followUpActionMatch = /^\/followups\/([^/]+)\/(claim|finish|stop)$/.exec(rest);
2045
+ if (followUpActionMatch !== null) {
2046
+ const auth = requireActor(req);
2047
+ assertWriteGuards(req, auth.sessionId);
2048
+ authorizeOrThrow(auth.ctx, "followups:manage");
2049
+ const taskId = pathSegment(followUpActionMatch[1] ?? "");
2050
+ const { body } = await readOpsBody();
2051
+ const action = followUpActionMatch[2];
2052
+ writeJson(res, 200, action === "claim"
2053
+ ? ops.claimFollowUp(taskId, body)
2054
+ : action === "finish"
2055
+ ? ops.finishFollowUp(taskId, body)
2056
+ : ops.stopFollowUp(taskId, body));
2057
+ return;
2058
+ }
2059
+ }
1712
2060
  if (rest === "/confirmations") {
1713
2061
  await postConfirmation(req, res);
1714
2062
  return;
@@ -2206,6 +2554,11 @@ export function createMerchantManagementApiHandler(options) {
2206
2554
  return;
2207
2555
  }
2208
2556
  catch (err) {
2557
+ const domain = domainErrorStatus(err);
2558
+ if (domain !== undefined) {
2559
+ writeJson(res, domain.status, errorBody(domain.code, err instanceof Error ? err.message : "domain error"));
2560
+ return;
2561
+ }
2209
2562
  if (err instanceof ManagementError) {
2210
2563
  // 校验/授权/摘要/状态/过期类失败:未触达执行 → 释放占位,原键可重试。
2211
2564
  operations.release(begun.operationId);
@@ -2270,6 +2623,11 @@ export function createMerchantManagementApiHandler(options) {
2270
2623
  return;
2271
2624
  }
2272
2625
  catch (err) {
2626
+ const domain = domainErrorStatus(err);
2627
+ if (domain !== undefined) {
2628
+ writeJson(res, domain.status, errorBody(domain.code, err instanceof Error ? err.message : "domain error"));
2629
+ return;
2630
+ }
2273
2631
  if (err instanceof ManagementError) {
2274
2632
  operations.release(begun.operationId);
2275
2633
  respondError(res, err);
@@ -2349,6 +2707,11 @@ export function createMerchantManagementApiHandler(options) {
2349
2707
  return;
2350
2708
  }
2351
2709
  catch (err) {
2710
+ const domain = domainErrorStatus(err);
2711
+ if (domain !== undefined) {
2712
+ writeJson(res, domain.status, errorBody(domain.code, err instanceof Error ? err.message : "domain error"));
2713
+ return;
2714
+ }
2352
2715
  if (err instanceof ManagementError) {
2353
2716
  operations.release(begun.operationId);
2354
2717
  respondError(res, err);
@@ -3146,6 +3509,18 @@ async function runAccessRoute(res, requestId, run) {
3146
3509
  }
3147
3510
  }
3148
3511
  function respondWorkbenchError(res, error, requestId) {
3512
+ const domain = domainErrorStatus(error);
3513
+ if (domain !== undefined) {
3514
+ const code = domain.status === 404
3515
+ ? "RESOURCE_NOT_FOUND"
3516
+ : domain.status === 409
3517
+ ? "VERSION_CONFLICT"
3518
+ : domain.status === 403
3519
+ ? "PERMISSION_REVOKED"
3520
+ : "VALIDATION_ERROR";
3521
+ writeWorkbenchProblem(res, code, requestId, "经营请求未完成", error instanceof Error ? error.message : "经营请求未完成");
3522
+ return;
3523
+ }
3149
3524
  if (error instanceof WorkbenchEventProjectionError) {
3150
3525
  writeWorkbenchProblem(res, "VALIDATION_ERROR", requestId, "事件游标无效", error.message);
3151
3526
  return;
@@ -3236,6 +3611,45 @@ function respondWorkbenchError(res, error, requestId) {
3236
3611
  process.stderr.write(`[merchant-management] Workbench 未预期异常:${error instanceof Error ? error.message : String(error)}\n`);
3237
3612
  writeWorkbenchProblem(res, "INTERNAL_ERROR", requestId, "内部错误", "请求未完成,请使用请求编号联系支持。");
3238
3613
  }
3614
+ function domainErrorStatus(err) {
3615
+ if (err instanceof OrderFactError) {
3616
+ if (err.code === "not_found")
3617
+ return { status: 404, code: "not_found" };
3618
+ if (err.code === "identity_conflict")
3619
+ return { status: 409, code: "conflict" };
3620
+ return { status: 400, code: "invalid_input" };
3621
+ }
3622
+ if (err instanceof OperatingPlanError) {
3623
+ if (err.code === "not_found")
3624
+ return { status: 404, code: "not_found" };
3625
+ if (err.code === "authorization_revoked")
3626
+ return { status: 403, code: "forbidden" };
3627
+ if (err.code === "validation" || err.code === "invalid_interval") {
3628
+ return { status: 400, code: "invalid_input" };
3629
+ }
3630
+ return { status: 409, code: "conflict" };
3631
+ }
3632
+ if (err instanceof OpportunityError) {
3633
+ if (err.code === "not_found")
3634
+ return { status: 404, code: "not_found" };
3635
+ if (err.code === "validation")
3636
+ return { status: 400, code: "invalid_input" };
3637
+ return { status: 409, code: "conflict" };
3638
+ }
3639
+ if (err instanceof ProposalError) {
3640
+ if (err.code === "not_found")
3641
+ return { status: 404, code: "not_found" };
3642
+ if (err.code === "validation")
3643
+ return { status: 400, code: "invalid_input" };
3644
+ return { status: 409, code: "conflict" };
3645
+ }
3646
+ if (err instanceof FollowUpError) {
3647
+ if (err.code === "validation")
3648
+ return { status: 400, code: "invalid_input" };
3649
+ return { status: 409, code: "conflict" };
3650
+ }
3651
+ return undefined;
3652
+ }
3239
3653
  function respondError(res, err) {
3240
3654
  if (res.headersSent) {
3241
3655
  res.end();
@@ -3250,6 +3664,11 @@ function respondError(res, err) {
3250
3664
  writeJson(res, 503, errorBody("unavailable", "actor context integrity failure"));
3251
3665
  return;
3252
3666
  }
3667
+ const domain = domainErrorStatus(err);
3668
+ if (domain !== undefined) {
3669
+ writeJson(res, domain.status, errorBody(domain.code, err instanceof Error ? err.message : "domain error"));
3670
+ return;
3671
+ }
3253
3672
  if (err instanceof ManagementError) {
3254
3673
  writeJson(res, MANAGEMENT_ERROR_STATUS[err.code], errorBody(err.code, err.message, { retryable: err.retryable, supportId: err.supportId }));
3255
3674
  return;