@harrylabsj/kiwi 0.12.14 → 0.12.16
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/dist/a2a/node.d.ts +5 -0
- package/dist/a2a/node.js +69 -2
- package/dist/a2a/node.js.map +1 -1
- package/dist/a2a/server/merchant-handler.d.ts +8 -0
- package/dist/a2a/server/merchant-handler.js +105 -14
- package/dist/a2a/server/merchant-handler.js.map +1 -1
- package/dist/a2a/server/negotiation-advisor.d.ts +54 -0
- package/dist/a2a/server/negotiation-advisor.js +107 -0
- package/dist/a2a/server/negotiation-advisor.js.map +1 -0
- package/dist/a2a/server/negotiation-approvals.d.ts +114 -0
- package/dist/a2a/server/negotiation-approvals.js +240 -0
- package/dist/a2a/server/negotiation-approvals.js.map +1 -0
- package/dist/buyer-core/a2a-knp.d.ts +14 -0
- package/dist/buyer-core/a2a-knp.js +24 -0
- package/dist/buyer-core/a2a-knp.js.map +1 -1
- package/dist/buyer-core/a2a-negotiator.js +66 -11
- package/dist/buyer-core/a2a-negotiator.js.map +1 -1
- package/dist/buyer-core/protocol-recovery.d.ts +1 -1
- package/dist/buyer-core/receive-gate.js +7 -0
- package/dist/buyer-core/receive-gate.js.map +1 -1
- package/dist/buyer-core/service.d.ts +13 -0
- package/dist/buyer-core/service.js +73 -10
- package/dist/buyer-core/service.js.map +1 -1
- package/dist/cli.js +247 -0
- package/dist/cli.js.map +1 -1
- package/dist/cloud/bootstrap.js +12 -0
- package/dist/cloud/bootstrap.js.map +1 -1
- package/dist/http/merchant-management/api.d.ts +3 -0
- package/dist/http/merchant-management/api.js +419 -0
- package/dist/http/merchant-management/api.js.map +1 -1
- package/dist/http/merchant-management/page.js +95 -1
- package/dist/http/merchant-management/page.js.map +1 -1
- package/dist/mcp/merchant-ops-server.d.ts +38 -0
- package/dist/mcp/merchant-ops-server.js +283 -0
- package/dist/mcp/merchant-ops-server.js.map +1 -0
- package/dist/mcp/merchant-runtime-assembly.js +28 -0
- package/dist/mcp/merchant-runtime-assembly.js.map +1 -1
- package/dist/mcp/merchant-server.d.ts +16 -0
- package/dist/mcp/merchant-server.js +24 -0
- package/dist/mcp/merchant-server.js.map +1 -1
- package/dist/merchant/application/actor.d.ts +1 -1
- package/dist/merchant/application/actor.js +43 -1
- package/dist/merchant/application/actor.js.map +1 -1
- package/dist/merchant/negotiation-observer.d.ts +35 -0
- package/dist/merchant/negotiation-observer.js +159 -51
- package/dist/merchant/negotiation-observer.js.map +1 -1
- package/dist/merchant/negotiation-ops.d.ts +77 -0
- package/dist/merchant/negotiation-ops.js +203 -0
- package/dist/merchant/negotiation-ops.js.map +1 -0
- package/dist/merchant/quote-calculator.d.ts +7 -0
- package/dist/merchant/quote-calculator.js +35 -24
- package/dist/merchant/quote-calculator.js.map +1 -1
- package/dist/merchant/watch/detect.d.ts +37 -0
- package/dist/merchant/watch/detect.js +147 -0
- package/dist/merchant/watch/detect.js.map +1 -0
- package/dist/merchant/watch/notify.d.ts +48 -0
- package/dist/merchant/watch/notify.js +108 -0
- package/dist/merchant/watch/notify.js.map +1 -0
- package/dist/merchant/watch/service.d.ts +43 -0
- package/dist/merchant/watch/service.js +167 -0
- package/dist/merchant/watch/service.js.map +1 -0
- package/dist/merchant/watch/state.d.ts +49 -0
- package/dist/merchant/watch/state.js +68 -0
- package/dist/merchant/watch/state.js.map +1 -0
- package/dist/merchant/watch/weixin.d.ts +54 -0
- package/dist/merchant/watch/weixin.js +271 -0
- package/dist/merchant/watch/weixin.js.map +1 -0
- package/dist/merchant-core/commercial/calculate.d.ts +18 -0
- package/dist/merchant-core/commercial/calculate.js +196 -0
- package/dist/merchant-core/commercial/calculate.js.map +1 -0
- package/dist/merchant-core/commercial/types.d.ts +91 -0
- package/dist/merchant-core/commercial/types.js +17 -0
- package/dist/merchant-core/commercial/types.js.map +1 -0
- package/dist/merchant-core/contribution/calculate.d.ts +24 -0
- package/dist/merchant-core/contribution/calculate.js +172 -0
- package/dist/merchant-core/contribution/calculate.js.map +1 -0
- package/dist/merchant-core/contribution/repository.d.ts +60 -0
- package/dist/merchant-core/contribution/repository.js +121 -0
- package/dist/merchant-core/contribution/repository.js.map +1 -0
- package/dist/merchant-core/contribution/types.d.ts +97 -0
- package/dist/merchant-core/contribution/types.js +24 -0
- package/dist/merchant-core/contribution/types.js.map +1 -0
- package/dist/merchant-core/followup/lease.d.ts +27 -0
- package/dist/merchant-core/followup/lease.js +45 -0
- package/dist/merchant-core/followup/lease.js.map +1 -0
- package/dist/merchant-core/followup/repository.d.ts +141 -0
- package/dist/merchant-core/followup/repository.js +280 -0
- package/dist/merchant-core/followup/repository.js.map +1 -0
- package/dist/merchant-core/followup/retry.d.ts +27 -0
- package/dist/merchant-core/followup/retry.js +61 -0
- package/dist/merchant-core/followup/retry.js.map +1 -0
- package/dist/merchant-core/followup/service.d.ts +114 -0
- package/dist/merchant-core/followup/service.js +191 -0
- package/dist/merchant-core/followup/service.js.map +1 -0
- package/dist/merchant-core/followup/state.d.ts +25 -0
- package/dist/merchant-core/followup/state.js +126 -0
- package/dist/merchant-core/followup/state.js.map +1 -0
- package/dist/merchant-core/followup/types.d.ts +75 -0
- package/dist/merchant-core/followup/types.js +41 -0
- package/dist/merchant-core/followup/types.js.map +1 -0
- package/dist/merchant-core/metrics/timing.d.ts +63 -0
- package/dist/merchant-core/metrics/timing.js +125 -0
- package/dist/merchant-core/metrics/timing.js.map +1 -0
- package/dist/merchant-core/operating/repository.d.ts +97 -0
- package/dist/merchant-core/operating/repository.js +266 -0
- package/dist/merchant-core/operating/repository.js.map +1 -0
- package/dist/merchant-core/operating/service.d.ts +63 -0
- package/dist/merchant-core/operating/service.js +336 -0
- package/dist/merchant-core/operating/service.js.map +1 -0
- package/dist/merchant-core/operating/types.d.ts +102 -0
- package/dist/merchant-core/operating/types.js +24 -0
- package/dist/merchant-core/operating/types.js.map +1 -0
- package/dist/merchant-core/opportunity/repository.d.ts +128 -0
- package/dist/merchant-core/opportunity/repository.js +251 -0
- package/dist/merchant-core/opportunity/repository.js.map +1 -0
- package/dist/merchant-core/opportunity/service.d.ts +117 -0
- package/dist/merchant-core/opportunity/service.js +388 -0
- package/dist/merchant-core/opportunity/service.js.map +1 -0
- package/dist/merchant-core/opportunity/state.d.ts +33 -0
- package/dist/merchant-core/opportunity/state.js +166 -0
- package/dist/merchant-core/opportunity/state.js.map +1 -0
- package/dist/merchant-core/opportunity/types.d.ts +141 -0
- package/dist/merchant-core/opportunity/types.js +42 -0
- package/dist/merchant-core/opportunity/types.js.map +1 -0
- package/dist/merchant-core/ops/facade.d.ts +159 -0
- package/dist/merchant-core/ops/facade.js +213 -0
- package/dist/merchant-core/ops/facade.js.map +1 -0
- package/dist/merchant-core/ops/schema.d.ts +25 -0
- package/dist/merchant-core/ops/schema.js +448 -0
- package/dist/merchant-core/ops/schema.js.map +1 -0
- package/dist/merchant-core/ops/surface.d.ts +49 -0
- package/dist/merchant-core/ops/surface.js +304 -0
- package/dist/merchant-core/ops/surface.js.map +1 -0
- package/dist/merchant-core/orders/csv.d.ts +36 -0
- package/dist/merchant-core/orders/csv.js +173 -0
- package/dist/merchant-core/orders/csv.js.map +1 -0
- package/dist/merchant-core/orders/model.d.ts +155 -0
- package/dist/merchant-core/orders/model.js +237 -0
- package/dist/merchant-core/orders/model.js.map +1 -0
- package/dist/merchant-core/orders/repository.d.ts +99 -0
- package/dist/merchant-core/orders/repository.js +221 -0
- package/dist/merchant-core/orders/repository.js.map +1 -0
- package/dist/merchant-core/orders/service.d.ts +84 -0
- package/dist/merchant-core/orders/service.js +121 -0
- package/dist/merchant-core/orders/service.js.map +1 -0
- package/dist/merchant-core/orders/shopping-cli-source.d.ts +58 -0
- package/dist/merchant-core/orders/shopping-cli-source.js +164 -0
- package/dist/merchant-core/orders/shopping-cli-source.js.map +1 -0
- package/dist/merchant-core/proposal/engine.d.ts +31 -0
- package/dist/merchant-core/proposal/engine.js +348 -0
- package/dist/merchant-core/proposal/engine.js.map +1 -0
- package/dist/merchant-core/proposal/publish.d.ts +46 -0
- package/dist/merchant-core/proposal/publish.js +79 -0
- package/dist/merchant-core/proposal/publish.js.map +1 -0
- package/dist/merchant-core/proposal/repository.d.ts +60 -0
- package/dist/merchant-core/proposal/repository.js +186 -0
- package/dist/merchant-core/proposal/repository.js.map +1 -0
- package/dist/merchant-core/proposal/service.d.ts +73 -0
- package/dist/merchant-core/proposal/service.js +149 -0
- package/dist/merchant-core/proposal/service.js.map +1 -0
- package/dist/merchant-core/proposal/types.d.ts +163 -0
- package/dist/merchant-core/proposal/types.js +25 -0
- package/dist/merchant-core/proposal/types.js.map +1 -0
- package/dist/merchant-runtime/services.d.ts +0 -8
- package/dist/merchant-runtime/services.js +38 -0
- package/dist/merchant-runtime/services.js.map +1 -1
- package/dist/product-cli.js +5 -0
- package/dist/product-cli.js.map +1 -1
- package/package.json +1 -1
|
@@ -0,0 +1,155 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Copyright 2026 harrylabsj
|
|
3
|
+
*
|
|
4
|
+
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
5
|
+
* you may not use this file except in compliance with the License.
|
|
6
|
+
* You may obtain a copy of the License at
|
|
7
|
+
*
|
|
8
|
+
* http://www.apache.org/licenses/LICENSE-2.0
|
|
9
|
+
*
|
|
10
|
+
* Unless required by applicable law or agreed to in writing, software
|
|
11
|
+
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
12
|
+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
13
|
+
* See the License for the specific language governing permissions and
|
|
14
|
+
* limitations under the License.
|
|
15
|
+
*/
|
|
16
|
+
/**
|
|
17
|
+
* 外部订单事实模型(v0.2 §11–§12 / FR19–FR25)。
|
|
18
|
+
*
|
|
19
|
+
* 纯函数,不读外部源。三类结论分开:订单存在、订单归因、订单贡献——
|
|
20
|
+
* 任一结论证据不足都不能拖累其余事实展示。事件去重、乱序、字段级权威
|
|
21
|
+
* 合并与退款更正都在这里定义;真实来源契约仍由 M0 冻结。
|
|
22
|
+
*/
|
|
23
|
+
export interface ExternalOrderIdentity {
|
|
24
|
+
merchant_id: string;
|
|
25
|
+
source_namespace: string;
|
|
26
|
+
source_account_id: string;
|
|
27
|
+
external_order_id: string;
|
|
28
|
+
}
|
|
29
|
+
export interface ExternalRefundIdentity extends ExternalOrderIdentity {
|
|
30
|
+
external_refund_id: string;
|
|
31
|
+
}
|
|
32
|
+
export type OrderDimension = "payment" | "fulfillment" | "refund" | "cancellation";
|
|
33
|
+
export interface OrderEvent {
|
|
34
|
+
/** 来源事件 ID(同一来源账户内唯一)。 */
|
|
35
|
+
event_id: string;
|
|
36
|
+
identity: ExternalOrderIdentity;
|
|
37
|
+
dimension: OrderDimension;
|
|
38
|
+
/** 该维度权威流内的版本;真实来源必须保证可比较。 */
|
|
39
|
+
source_version: string;
|
|
40
|
+
occurred_at: string;
|
|
41
|
+
observed_at: string;
|
|
42
|
+
/** 原始状态值(不解释,只保存)。 */
|
|
43
|
+
value: string;
|
|
44
|
+
amount_minor?: number;
|
|
45
|
+
refund_id?: string;
|
|
46
|
+
/** 原始事件摘要(用于同版本异内容冲突检测)。 */
|
|
47
|
+
raw_digest: string;
|
|
48
|
+
}
|
|
49
|
+
export interface OrderStreamContract {
|
|
50
|
+
dimension: OrderDimension;
|
|
51
|
+
/** monotonic:来源提供可比版本;unorderable:只能重读权威快照。 */
|
|
52
|
+
ordering: "monotonic" | "unorderable";
|
|
53
|
+
/** 版本作用域(例如 payment / fulfillment / refund),不跨流比较。 */
|
|
54
|
+
version_scope: string;
|
|
55
|
+
}
|
|
56
|
+
export type MergeEventResult = {
|
|
57
|
+
status: "accepted";
|
|
58
|
+
current: OrderEvent;
|
|
59
|
+
} | {
|
|
60
|
+
status: "duplicate";
|
|
61
|
+
current: OrderEvent;
|
|
62
|
+
} | {
|
|
63
|
+
status: "stale";
|
|
64
|
+
current: OrderEvent;
|
|
65
|
+
} | {
|
|
66
|
+
status: "conflict";
|
|
67
|
+
current: OrderEvent;
|
|
68
|
+
reason: "same_version_different_content";
|
|
69
|
+
} | {
|
|
70
|
+
status: "unverified";
|
|
71
|
+
current?: OrderEvent;
|
|
72
|
+
reason: "unorderable_source" | "different_version_scope";
|
|
73
|
+
};
|
|
74
|
+
export interface OrderLineFact {
|
|
75
|
+
line_id: string;
|
|
76
|
+
sku: string;
|
|
77
|
+
quantity: number;
|
|
78
|
+
amount_minor: number;
|
|
79
|
+
}
|
|
80
|
+
export interface AttributionInput {
|
|
81
|
+
order_line_id: string;
|
|
82
|
+
opportunity_id: string;
|
|
83
|
+
quantity: number;
|
|
84
|
+
amount_minor: number;
|
|
85
|
+
method: "handoff_reference" | "verified_source_reference" | "manual_confirmation";
|
|
86
|
+
confirmed_by?: string;
|
|
87
|
+
}
|
|
88
|
+
export interface AttributionResult {
|
|
89
|
+
allocations: Array<AttributionInput & {
|
|
90
|
+
attribution_id: string;
|
|
91
|
+
}>;
|
|
92
|
+
totals_by_opportunity: Record<string, {
|
|
93
|
+
quantity: number;
|
|
94
|
+
amount_minor: number;
|
|
95
|
+
}>;
|
|
96
|
+
unassigned: Array<{
|
|
97
|
+
line_id: string;
|
|
98
|
+
quantity: number;
|
|
99
|
+
amount_minor: number;
|
|
100
|
+
}>;
|
|
101
|
+
}
|
|
102
|
+
export interface RefundFact {
|
|
103
|
+
identity: ExternalRefundIdentity;
|
|
104
|
+
amount_minor: number;
|
|
105
|
+
/** 同一退款的状态/金额更正版本。 */
|
|
106
|
+
version: number;
|
|
107
|
+
status: "active" | "reversed";
|
|
108
|
+
}
|
|
109
|
+
export type RefundMergeResult = {
|
|
110
|
+
status: "inserted";
|
|
111
|
+
refunds: RefundFact[];
|
|
112
|
+
} | {
|
|
113
|
+
status: "corrected";
|
|
114
|
+
refunds: RefundFact[];
|
|
115
|
+
} | {
|
|
116
|
+
status: "duplicate";
|
|
117
|
+
refunds: RefundFact[];
|
|
118
|
+
} | {
|
|
119
|
+
status: "conflict";
|
|
120
|
+
refunds: RefundFact[];
|
|
121
|
+
reason: "same_version_different_amount";
|
|
122
|
+
};
|
|
123
|
+
export declare class OrderFactError extends Error {
|
|
124
|
+
readonly code: "validation" | "identity_conflict" | "not_found";
|
|
125
|
+
constructor(code: "validation" | "identity_conflict" | "not_found", message: string);
|
|
126
|
+
}
|
|
127
|
+
/** 校验订单行事实;人工/CSV/连接器入口共用同一不变量。 */
|
|
128
|
+
export declare function validateOrderLineFacts(value: unknown): OrderLineFact[];
|
|
129
|
+
/** 复合外部订单身份(商家、来源命名空间、来源账户、外部订单 ID)。 */
|
|
130
|
+
export declare function orderIdentityKey(identity: ExternalOrderIdentity): string;
|
|
131
|
+
/** 退款业务唯一键:订单复合身份 + 外部退款 ID。 */
|
|
132
|
+
export declare function refundIdentityKey(identity: ExternalRefundIdentity): string;
|
|
133
|
+
/** 版本比较:纯数字按 BigInt,其余按字典序;真实契约应保证同作用域可比较。 */
|
|
134
|
+
export declare function compareSourceVersion(left: string, right: string): number;
|
|
135
|
+
/**
|
|
136
|
+
* 字段级权威合并:
|
|
137
|
+
* - 同事件 ID → duplicate;
|
|
138
|
+
* - 来源不可排序 → unverified(保持当前,重读权威快照);
|
|
139
|
+
* - 版本作用域不同 → unverified(不跨流比较);
|
|
140
|
+
* - 新版本 → accepted;旧版本 → stale;同版本异内容 → conflict。
|
|
141
|
+
*/
|
|
142
|
+
export declare function mergeOrderEvent(input: {
|
|
143
|
+
current?: OrderEvent;
|
|
144
|
+
incoming: OrderEvent;
|
|
145
|
+
contract: OrderStreamContract;
|
|
146
|
+
}): MergeEventResult;
|
|
147
|
+
/** 行级归因:每个订单行的数量/金额有上限,不能重复归因;剩余显式未分配。 */
|
|
148
|
+
export declare function allocateAttributions(lines: readonly OrderLineFact[], allocations: readonly AttributionInput[]): AttributionResult;
|
|
149
|
+
/**
|
|
150
|
+
* 退款合并:同一外部退款后续状态/金额更正更新投影,不作为新退款再次扣减。
|
|
151
|
+
* 同版本异金额 → conflict(保持原值,进入待核实)。
|
|
152
|
+
*/
|
|
153
|
+
export declare function mergeRefund(existing: readonly RefundFact[], incoming: RefundFact): RefundMergeResult;
|
|
154
|
+
/** 当前退款总额:同一退款 ID 只取最新版本,重放不重复扣减。 */
|
|
155
|
+
export declare function refundTotalMinor(refunds: readonly RefundFact[]): number;
|
|
@@ -0,0 +1,237 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Copyright 2026 harrylabsj
|
|
3
|
+
*
|
|
4
|
+
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
5
|
+
* you may not use this file except in compliance with the License.
|
|
6
|
+
* You may obtain a copy of the License at
|
|
7
|
+
*
|
|
8
|
+
* http://www.apache.org/licenses/LICENSE-2.0
|
|
9
|
+
*
|
|
10
|
+
* Unless required by applicable law or agreed to in writing, software
|
|
11
|
+
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
12
|
+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
13
|
+
* See the License for the specific language governing permissions and
|
|
14
|
+
* limitations under the License.
|
|
15
|
+
*/
|
|
16
|
+
export class OrderFactError extends Error {
|
|
17
|
+
code;
|
|
18
|
+
constructor(code, message) {
|
|
19
|
+
super(message);
|
|
20
|
+
this.name = "OrderFactError";
|
|
21
|
+
this.code = code;
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
function requireText(value, name, max = 256) {
|
|
25
|
+
if (typeof value !== "string" || value.trim() === "" || value.length > max) {
|
|
26
|
+
throw new OrderFactError("validation", `${name} 必须是非空字符串(≤${max})`);
|
|
27
|
+
}
|
|
28
|
+
return value;
|
|
29
|
+
}
|
|
30
|
+
function requireMinor(value, name) {
|
|
31
|
+
if (typeof value !== "number" || !Number.isSafeInteger(value) || value < 0) {
|
|
32
|
+
throw new OrderFactError("validation", `${name} 必须是非负安全整数`);
|
|
33
|
+
}
|
|
34
|
+
return value;
|
|
35
|
+
}
|
|
36
|
+
/** 校验订单行事实;人工/CSV/连接器入口共用同一不变量。 */
|
|
37
|
+
export function validateOrderLineFacts(value) {
|
|
38
|
+
if (!Array.isArray(value) || value.length === 0) {
|
|
39
|
+
throw new OrderFactError("validation", "订单行必须是非空数组");
|
|
40
|
+
}
|
|
41
|
+
const seen = new Set();
|
|
42
|
+
return value.map((raw, index) => {
|
|
43
|
+
if (raw === null || typeof raw !== "object" || Array.isArray(raw)) {
|
|
44
|
+
throw new OrderFactError("validation", `lines[${index}] 必须是对象`);
|
|
45
|
+
}
|
|
46
|
+
const line = raw;
|
|
47
|
+
const lineId = requireText(line["line_id"], `lines[${index}].line_id`, 128);
|
|
48
|
+
if (seen.has(lineId))
|
|
49
|
+
throw new OrderFactError("validation", `重复 line_id:${lineId}`);
|
|
50
|
+
seen.add(lineId);
|
|
51
|
+
const quantity = requireMinor(line["quantity"], `lines[${index}].quantity`);
|
|
52
|
+
if (quantity < 1)
|
|
53
|
+
throw new OrderFactError("validation", `lines[${index}].quantity 必须 ≥ 1`);
|
|
54
|
+
return {
|
|
55
|
+
line_id: lineId,
|
|
56
|
+
sku: requireText(line["sku"], `lines[${index}].sku`, 128),
|
|
57
|
+
quantity,
|
|
58
|
+
amount_minor: requireMinor(line["amount_minor"], `lines[${index}].amount_minor`),
|
|
59
|
+
...(line["unit_price_minor"] !== undefined
|
|
60
|
+
? { unit_price_minor: requireMinor(line["unit_price_minor"], `lines[${index}].unit_price_minor`) }
|
|
61
|
+
: {}),
|
|
62
|
+
};
|
|
63
|
+
});
|
|
64
|
+
}
|
|
65
|
+
/** 复合外部订单身份(商家、来源命名空间、来源账户、外部订单 ID)。 */
|
|
66
|
+
export function orderIdentityKey(identity) {
|
|
67
|
+
return [
|
|
68
|
+
requireText(identity.merchant_id, "merchant_id", 128),
|
|
69
|
+
requireText(identity.source_namespace, "source_namespace", 64),
|
|
70
|
+
requireText(identity.source_account_id, "source_account_id", 128),
|
|
71
|
+
requireText(identity.external_order_id, "external_order_id", 256),
|
|
72
|
+
].join("\u0000");
|
|
73
|
+
}
|
|
74
|
+
/** 退款业务唯一键:订单复合身份 + 外部退款 ID。 */
|
|
75
|
+
export function refundIdentityKey(identity) {
|
|
76
|
+
return `${orderIdentityKey(identity)}\u0000${requireText(identity.external_refund_id, "external_refund_id", 256)}`;
|
|
77
|
+
}
|
|
78
|
+
/** 版本比较:纯数字按 BigInt,其余按字典序;真实契约应保证同作用域可比较。 */
|
|
79
|
+
export function compareSourceVersion(left, right) {
|
|
80
|
+
requireText(left, "left_version");
|
|
81
|
+
requireText(right, "right_version");
|
|
82
|
+
if (/^[0-9]+$/.test(left) && /^[0-9]+$/.test(right)) {
|
|
83
|
+
const a = BigInt(left);
|
|
84
|
+
const b = BigInt(right);
|
|
85
|
+
return a < b ? -1 : a > b ? 1 : 0;
|
|
86
|
+
}
|
|
87
|
+
return left < right ? -1 : left > right ? 1 : 0;
|
|
88
|
+
}
|
|
89
|
+
/**
|
|
90
|
+
* 字段级权威合并:
|
|
91
|
+
* - 同事件 ID → duplicate;
|
|
92
|
+
* - 来源不可排序 → unverified(保持当前,重读权威快照);
|
|
93
|
+
* - 版本作用域不同 → unverified(不跨流比较);
|
|
94
|
+
* - 新版本 → accepted;旧版本 → stale;同版本异内容 → conflict。
|
|
95
|
+
*/
|
|
96
|
+
export function mergeOrderEvent(input) {
|
|
97
|
+
const { current, incoming, contract } = input;
|
|
98
|
+
if (incoming.dimension !== contract.dimension) {
|
|
99
|
+
throw new OrderFactError("validation", "事件维度与来源契约不一致");
|
|
100
|
+
}
|
|
101
|
+
if (current === undefined) {
|
|
102
|
+
return contract.ordering === "unorderable"
|
|
103
|
+
? { status: "unverified", reason: "unorderable_source" }
|
|
104
|
+
: { status: "accepted", current: incoming };
|
|
105
|
+
}
|
|
106
|
+
if (orderIdentityKey(current.identity) !== orderIdentityKey(incoming.identity)) {
|
|
107
|
+
throw new OrderFactError("identity_conflict", "事件不属于同一外部订单");
|
|
108
|
+
}
|
|
109
|
+
if (current.event_id === incoming.event_id) {
|
|
110
|
+
return { status: "duplicate", current };
|
|
111
|
+
}
|
|
112
|
+
if (contract.ordering === "unorderable") {
|
|
113
|
+
return { status: "unverified", current, reason: "unorderable_source" };
|
|
114
|
+
}
|
|
115
|
+
if (!current.raw_digest) {
|
|
116
|
+
throw new OrderFactError("validation", "current.raw_digest 不能为空");
|
|
117
|
+
}
|
|
118
|
+
const comparison = compareSourceVersion(incoming.source_version, current.source_version);
|
|
119
|
+
if (comparison > 0)
|
|
120
|
+
return { status: "accepted", current: incoming };
|
|
121
|
+
if (comparison < 0)
|
|
122
|
+
return { status: "stale", current };
|
|
123
|
+
if (incoming.raw_digest !== current.raw_digest) {
|
|
124
|
+
return { status: "conflict", current, reason: "same_version_different_content" };
|
|
125
|
+
}
|
|
126
|
+
return { status: "duplicate", current };
|
|
127
|
+
}
|
|
128
|
+
/** 行级归因:每个订单行的数量/金额有上限,不能重复归因;剩余显式未分配。 */
|
|
129
|
+
export function allocateAttributions(lines, allocations) {
|
|
130
|
+
const byLine = new Map();
|
|
131
|
+
for (const line of lines) {
|
|
132
|
+
requireText(line.line_id, "lines[].line_id", 128);
|
|
133
|
+
if (byLine.has(line.line_id)) {
|
|
134
|
+
throw new OrderFactError("validation", `重复订单行:${line.line_id}`);
|
|
135
|
+
}
|
|
136
|
+
requireMinor(line.quantity, "lines[].quantity");
|
|
137
|
+
requireMinor(line.amount_minor, "lines[].amount_minor");
|
|
138
|
+
byLine.set(line.line_id, line);
|
|
139
|
+
}
|
|
140
|
+
const usedQuantity = new Map();
|
|
141
|
+
const usedAmount = new Map();
|
|
142
|
+
const seenPair = new Set();
|
|
143
|
+
const totals = {};
|
|
144
|
+
const accepted = [];
|
|
145
|
+
for (const allocation of allocations) {
|
|
146
|
+
const line = byLine.get(allocation.order_line_id);
|
|
147
|
+
if (line === undefined) {
|
|
148
|
+
throw new OrderFactError("validation", `归因引用了未知订单行:${allocation.order_line_id}`);
|
|
149
|
+
}
|
|
150
|
+
requireText(allocation.opportunity_id, "allocations[].opportunity_id", 128);
|
|
151
|
+
const quantity = requireMinor(allocation.quantity, "allocations[].quantity");
|
|
152
|
+
const amount = requireMinor(allocation.amount_minor, "allocations[].amount_minor");
|
|
153
|
+
if (quantity === 0 && amount === 0) {
|
|
154
|
+
throw new OrderFactError("validation", "归因数量与金额不能同时为 0");
|
|
155
|
+
}
|
|
156
|
+
const pair = `${allocation.order_line_id}\u0000${allocation.opportunity_id}`;
|
|
157
|
+
if (seenPair.has(pair)) {
|
|
158
|
+
throw new OrderFactError("validation", "同一订单行不能重复归因到同一机会");
|
|
159
|
+
}
|
|
160
|
+
seenPair.add(pair);
|
|
161
|
+
const nextQuantity = (usedQuantity.get(line.line_id) ?? 0) + quantity;
|
|
162
|
+
const nextAmount = (usedAmount.get(line.line_id) ?? 0) + amount;
|
|
163
|
+
if (nextQuantity > line.quantity) {
|
|
164
|
+
throw new OrderFactError("validation", `订单行 ${line.line_id} 数量归因超过上限`);
|
|
165
|
+
}
|
|
166
|
+
if (nextAmount > line.amount_minor) {
|
|
167
|
+
throw new OrderFactError("validation", `订单行 ${line.line_id} 金额归因超过上限`);
|
|
168
|
+
}
|
|
169
|
+
usedQuantity.set(line.line_id, nextQuantity);
|
|
170
|
+
usedAmount.set(line.line_id, nextAmount);
|
|
171
|
+
accepted.push({
|
|
172
|
+
...allocation,
|
|
173
|
+
attribution_id: `attr_${line.line_id}_${allocation.opportunity_id}`,
|
|
174
|
+
...(allocation.confirmed_by !== undefined
|
|
175
|
+
? { confirmed_by: requireText(allocation.confirmed_by, "allocations[].confirmed_by", 128) }
|
|
176
|
+
: {}),
|
|
177
|
+
});
|
|
178
|
+
const current = totals[allocation.opportunity_id] ?? { quantity: 0, amount_minor: 0 };
|
|
179
|
+
totals[allocation.opportunity_id] = {
|
|
180
|
+
quantity: current.quantity + quantity,
|
|
181
|
+
amount_minor: current.amount_minor + amount,
|
|
182
|
+
};
|
|
183
|
+
}
|
|
184
|
+
const unassigned = lines.map((line) => ({
|
|
185
|
+
line_id: line.line_id,
|
|
186
|
+
quantity: line.quantity - (usedQuantity.get(line.line_id) ?? 0),
|
|
187
|
+
amount_minor: line.amount_minor - (usedAmount.get(line.line_id) ?? 0),
|
|
188
|
+
}));
|
|
189
|
+
return { allocations: accepted, totals_by_opportunity: totals, unassigned };
|
|
190
|
+
}
|
|
191
|
+
/**
|
|
192
|
+
* 退款合并:同一外部退款后续状态/金额更正更新投影,不作为新退款再次扣减。
|
|
193
|
+
* 同版本异金额 → conflict(保持原值,进入待核实)。
|
|
194
|
+
*/
|
|
195
|
+
export function mergeRefund(existing, incoming) {
|
|
196
|
+
const key = refundIdentityKey(incoming.identity);
|
|
197
|
+
requireMinor(incoming.amount_minor, "refund.amount_minor");
|
|
198
|
+
if (!Number.isSafeInteger(incoming.version) || incoming.version < 1) {
|
|
199
|
+
throw new OrderFactError("validation", "refund.version 必须是正整数");
|
|
200
|
+
}
|
|
201
|
+
const index = existing.findIndex((refund) => refundIdentityKey(refund.identity) === key);
|
|
202
|
+
if (index === -1) {
|
|
203
|
+
return {
|
|
204
|
+
status: "inserted",
|
|
205
|
+
refunds: [...existing, { ...incoming }],
|
|
206
|
+
};
|
|
207
|
+
}
|
|
208
|
+
const current = existing[index];
|
|
209
|
+
if (incoming.version < current.version) {
|
|
210
|
+
return { status: "duplicate", refunds: [...existing] };
|
|
211
|
+
}
|
|
212
|
+
if (incoming.version === current.version) {
|
|
213
|
+
if (incoming.amount_minor !== current.amount_minor || incoming.status !== current.status) {
|
|
214
|
+
return {
|
|
215
|
+
status: "conflict",
|
|
216
|
+
refunds: [...existing],
|
|
217
|
+
reason: "same_version_different_amount",
|
|
218
|
+
};
|
|
219
|
+
}
|
|
220
|
+
return { status: "duplicate", refunds: [...existing] };
|
|
221
|
+
}
|
|
222
|
+
const refunds = [...existing];
|
|
223
|
+
refunds[index] = { ...incoming };
|
|
224
|
+
return { status: "corrected", refunds };
|
|
225
|
+
}
|
|
226
|
+
/** 当前退款总额:同一退款 ID 只取最新版本,重放不重复扣减。 */
|
|
227
|
+
export function refundTotalMinor(refunds) {
|
|
228
|
+
let total = 0;
|
|
229
|
+
for (const refund of refunds) {
|
|
230
|
+
if (refund.status === "active")
|
|
231
|
+
total += requireMinor(refund.amount_minor, "refund.amount_minor");
|
|
232
|
+
}
|
|
233
|
+
if (!Number.isSafeInteger(total))
|
|
234
|
+
throw new OrderFactError("validation", "退款总额超出安全整数");
|
|
235
|
+
return total;
|
|
236
|
+
}
|
|
237
|
+
//# sourceMappingURL=model.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"model.js","sourceRoot":"","sources":["../../../src/merchant-core/orders/model.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;GAcG;AA2FH,MAAM,OAAO,cAAe,SAAQ,KAAK;IAC9B,IAAI,CAAmD;IAChE,YAAY,IAAsD,EAAE,OAAe;QACjF,KAAK,CAAC,OAAO,CAAC,CAAC;QACf,IAAI,CAAC,IAAI,GAAG,gBAAgB,CAAC;QAC7B,IAAI,CAAC,IAAI,GAAG,IAAI,CAAC;IACnB,CAAC;CACF;AAED,SAAS,WAAW,CAAC,KAAc,EAAE,IAAY,EAAE,GAAG,GAAG,GAAG;IAC1D,IAAI,OAAO,KAAK,KAAK,QAAQ,IAAI,KAAK,CAAC,IAAI,EAAE,KAAK,EAAE,IAAI,KAAK,CAAC,MAAM,GAAG,GAAG,EAAE,CAAC;QAC3E,MAAM,IAAI,cAAc,CAAC,YAAY,EAAE,GAAG,IAAI,cAAc,GAAG,GAAG,CAAC,CAAC;IACtE,CAAC;IACD,OAAO,KAAK,CAAC;AACf,CAAC;AAED,SAAS,YAAY,CAAC,KAAc,EAAE,IAAY;IAChD,IAAI,OAAO,KAAK,KAAK,QAAQ,IAAI,CAAC,MAAM,CAAC,aAAa,CAAC,KAAK,CAAC,IAAI,KAAK,GAAG,CAAC,EAAE,CAAC;QAC3E,MAAM,IAAI,cAAc,CAAC,YAAY,EAAE,GAAG,IAAI,YAAY,CAAC,CAAC;IAC9D,CAAC;IACD,OAAO,KAAK,CAAC;AACf,CAAC;AAED,mCA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,IAAI,CAAC,MAAM,CAAC,aAAa,CAAC,KAAK,CAAC;QAAE,MAAM,IAAI,cAAc,CAAC,YAAY,EAAE,YAAY,CAAC,CAAC;IACvF,OAAO,KAAK,CAAC;AACf,CAAC"}
|
|
@@ -0,0 +1,99 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Copyright 2026 harrylabsj
|
|
3
|
+
*
|
|
4
|
+
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
5
|
+
* you may not use this file except in compliance with the License.
|
|
6
|
+
* You may obtain a copy of the License at
|
|
7
|
+
*
|
|
8
|
+
* http://www.apache.org/licenses/LICENSE-2.0
|
|
9
|
+
*
|
|
10
|
+
* Unless required by applicable law or agreed to in writing, software
|
|
11
|
+
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
12
|
+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
13
|
+
* See the License for the specific language governing permissions and
|
|
14
|
+
* limitations under the License.
|
|
15
|
+
*/
|
|
16
|
+
import type { DatabaseSync } from "node:sqlite";
|
|
17
|
+
import { orderIdentityKey, type AttributionInput, type ExternalOrderIdentity, type OrderEvent, type OrderLineFact, type OrderStreamContract, type RefundFact } from "./model.js";
|
|
18
|
+
export interface OrderRecordV2 {
|
|
19
|
+
record_id: string;
|
|
20
|
+
merchant_id: string;
|
|
21
|
+
source_namespace: string;
|
|
22
|
+
source_account_id: string;
|
|
23
|
+
external_order_id: string;
|
|
24
|
+
evidence_source: "manual_report" | "csv_import" | "external_verified";
|
|
25
|
+
payment_status: string | null;
|
|
26
|
+
fulfillment_status: string | null;
|
|
27
|
+
refund_status: string | null;
|
|
28
|
+
cancellation_status: string | null;
|
|
29
|
+
currency: string | null;
|
|
30
|
+
total_minor: number | null;
|
|
31
|
+
source_version: string | null;
|
|
32
|
+
observed_at: string | null;
|
|
33
|
+
verified_at: string | null;
|
|
34
|
+
created_at: string;
|
|
35
|
+
updated_at: string;
|
|
36
|
+
}
|
|
37
|
+
export interface OrderRepositoryOptions {
|
|
38
|
+
db: DatabaseSync;
|
|
39
|
+
now: () => string;
|
|
40
|
+
}
|
|
41
|
+
export declare class OrderRepository {
|
|
42
|
+
private readonly db;
|
|
43
|
+
private readonly now;
|
|
44
|
+
private inTx;
|
|
45
|
+
constructor(options: OrderRepositoryOptions);
|
|
46
|
+
transaction<T>(work: () => T): T;
|
|
47
|
+
createOrder(input: {
|
|
48
|
+
identity: ExternalOrderIdentity;
|
|
49
|
+
evidenceSource: OrderRecordV2["evidence_source"];
|
|
50
|
+
paymentStatus?: string;
|
|
51
|
+
fulfillmentStatus?: string;
|
|
52
|
+
refundStatus?: string;
|
|
53
|
+
cancellationStatus?: string;
|
|
54
|
+
currency?: string;
|
|
55
|
+
totalMinor?: number;
|
|
56
|
+
sourceVersion?: string;
|
|
57
|
+
observedAt?: string;
|
|
58
|
+
verifiedAt?: string;
|
|
59
|
+
lines: OrderLineFact[];
|
|
60
|
+
}): {
|
|
61
|
+
order: OrderRecordV2;
|
|
62
|
+
created: boolean;
|
|
63
|
+
};
|
|
64
|
+
findByIdentity(identity: ExternalOrderIdentity): OrderRecordV2 | undefined;
|
|
65
|
+
getOrder(merchantId: string, recordId: string): OrderRecordV2 | undefined;
|
|
66
|
+
listOrders(merchantId: string): OrderRecordV2[];
|
|
67
|
+
updateEvidence(merchantId: string, recordId: string, input: {
|
|
68
|
+
evidenceSource?: OrderRecordV2["evidence_source"];
|
|
69
|
+
verifiedAt?: string;
|
|
70
|
+
sourceVersion?: string;
|
|
71
|
+
}): OrderRecordV2;
|
|
72
|
+
listLines(recordId: string): OrderLineFact[];
|
|
73
|
+
/** 事件合并:同维度按契约比较;accepted 才写入。 */
|
|
74
|
+
appendEvent(input: {
|
|
75
|
+
merchantId: string;
|
|
76
|
+
recordId: string;
|
|
77
|
+
incoming: OrderEvent;
|
|
78
|
+
contract: OrderStreamContract;
|
|
79
|
+
}): {
|
|
80
|
+
inserted: boolean;
|
|
81
|
+
status: string;
|
|
82
|
+
};
|
|
83
|
+
listEvents(recordId: string, dimension?: OrderEvent["dimension"], identity?: ExternalOrderIdentity): OrderEvent[];
|
|
84
|
+
replaceAttributions(merchantId: string, recordId: string, allocations: AttributionInput[]): {
|
|
85
|
+
allocations: AttributionInput[];
|
|
86
|
+
unassigned: Array<{
|
|
87
|
+
line_id: string;
|
|
88
|
+
quantity: number;
|
|
89
|
+
amount_minor: number;
|
|
90
|
+
}>;
|
|
91
|
+
};
|
|
92
|
+
listAttributions(recordId: string): AttributionInput[];
|
|
93
|
+
mergeRefund(merchantId: string, recordId: string, incoming: RefundFact): {
|
|
94
|
+
status: string;
|
|
95
|
+
totalMinor: number;
|
|
96
|
+
};
|
|
97
|
+
listRefunds(merchantId: string, recordId: string): RefundFact[];
|
|
98
|
+
}
|
|
99
|
+
export { orderIdentityKey };
|