@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.
- 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 +101 -0
- package/dist/a2a/server/negotiation-approvals.js +208 -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 +59 -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/cloud/merchant-connect.d.ts +8 -0
- package/dist/cloud/merchant-connect.js +3 -0
- package/dist/cloud/merchant-connect.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/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/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-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/product-cli.js +5 -0
- package/dist/product-cli.js.map +1 -1
- package/package.json +1 -1
|
@@ -0,0 +1,221 @@
|
|
|
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 / §16)。
|
|
18
|
+
*
|
|
19
|
+
* 订单、行、事件、归因、退款分开存储;事件与退款按复合身份幂等。归因上限
|
|
20
|
+
* 在写入前由 `allocateAttributions` 校验,数据库只保存已确认分配。
|
|
21
|
+
*/
|
|
22
|
+
import { randomUUID } from "node:crypto";
|
|
23
|
+
import { allocateAttributions, mergeOrderEvent, mergeRefund, orderIdentityKey, refundTotalMinor, OrderFactError, validateOrderLineFacts, } from "./model.js";
|
|
24
|
+
export class OrderRepository {
|
|
25
|
+
db;
|
|
26
|
+
now;
|
|
27
|
+
inTx = false;
|
|
28
|
+
constructor(options) {
|
|
29
|
+
this.db = options.db;
|
|
30
|
+
this.now = options.now;
|
|
31
|
+
}
|
|
32
|
+
transaction(work) {
|
|
33
|
+
if (this.inTx)
|
|
34
|
+
return work();
|
|
35
|
+
this.inTx = true;
|
|
36
|
+
this.db.exec("BEGIN IMMEDIATE");
|
|
37
|
+
try {
|
|
38
|
+
const result = work();
|
|
39
|
+
this.db.exec("COMMIT");
|
|
40
|
+
return result;
|
|
41
|
+
}
|
|
42
|
+
catch (err) {
|
|
43
|
+
this.db.exec("ROLLBACK");
|
|
44
|
+
throw err;
|
|
45
|
+
}
|
|
46
|
+
finally {
|
|
47
|
+
this.inTx = false;
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
createOrder(input) {
|
|
51
|
+
const lines = validateOrderLineFacts(input.lines);
|
|
52
|
+
return this.transaction(() => {
|
|
53
|
+
const existing = this.findByIdentity(input.identity);
|
|
54
|
+
if (existing !== undefined)
|
|
55
|
+
return { order: existing, created: false };
|
|
56
|
+
const recordId = `ord_${randomUUID()}`;
|
|
57
|
+
const now = this.now();
|
|
58
|
+
{
|
|
59
|
+
this.db
|
|
60
|
+
.prepare(`INSERT INTO order_records_v2
|
|
61
|
+
(record_id, merchant_id, source_namespace, source_account_id, external_order_id,
|
|
62
|
+
evidence_source, payment_status, fulfillment_status, refund_status, cancellation_status,
|
|
63
|
+
currency, total_minor, source_version, observed_at, verified_at, created_at, updated_at)
|
|
64
|
+
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)`)
|
|
65
|
+
.run(recordId, input.identity.merchant_id, input.identity.source_namespace, input.identity.source_account_id, input.identity.external_order_id, input.evidenceSource, input.paymentStatus ?? null, input.fulfillmentStatus ?? null, input.refundStatus ?? null, input.cancellationStatus ?? null, input.currency ?? null, input.totalMinor ?? null, input.sourceVersion ?? null, input.observedAt ?? null, input.verifiedAt ?? null, now, now);
|
|
66
|
+
for (const line of lines) {
|
|
67
|
+
this.db
|
|
68
|
+
.prepare(`INSERT INTO order_lines_v2
|
|
69
|
+
(record_id, line_id, sku, quantity, amount_minor, unit_price_minor)
|
|
70
|
+
VALUES (?, ?, ?, ?, ?, ?)`)
|
|
71
|
+
.run(recordId, line.line_id, line.sku, line.quantity, line.amount_minor, null);
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
return { order: this.getOrder(input.identity.merchant_id, recordId), created: true };
|
|
75
|
+
});
|
|
76
|
+
}
|
|
77
|
+
findByIdentity(identity) {
|
|
78
|
+
const row = this.db
|
|
79
|
+
.prepare(`SELECT * FROM order_records_v2
|
|
80
|
+
WHERE merchant_id = ? AND source_namespace = ? AND source_account_id = ? AND external_order_id = ?`)
|
|
81
|
+
.get(identity.merchant_id, identity.source_namespace, identity.source_account_id, identity.external_order_id);
|
|
82
|
+
return row === undefined ? undefined : row;
|
|
83
|
+
}
|
|
84
|
+
getOrder(merchantId, recordId) {
|
|
85
|
+
const row = this.db
|
|
86
|
+
.prepare("SELECT * FROM order_records_v2 WHERE merchant_id = ? AND record_id = ?")
|
|
87
|
+
.get(merchantId, recordId);
|
|
88
|
+
return row === undefined ? undefined : row;
|
|
89
|
+
}
|
|
90
|
+
listOrders(merchantId) {
|
|
91
|
+
return this.db
|
|
92
|
+
.prepare("SELECT * FROM order_records_v2 WHERE merchant_id = ? ORDER BY observed_at DESC")
|
|
93
|
+
.all(merchantId);
|
|
94
|
+
}
|
|
95
|
+
updateEvidence(merchantId, recordId, input) {
|
|
96
|
+
const order = this.getOrder(merchantId, recordId);
|
|
97
|
+
if (order === undefined)
|
|
98
|
+
throw new OrderFactError("not_found", "order not found");
|
|
99
|
+
this.db
|
|
100
|
+
.prepare(`UPDATE order_records_v2
|
|
101
|
+
SET evidence_source = ?, verified_at = ?, source_version = ?, updated_at = ?
|
|
102
|
+
WHERE merchant_id = ? AND record_id = ?`)
|
|
103
|
+
.run(input.evidenceSource ?? order.evidence_source, input.verifiedAt ?? order.verified_at, input.sourceVersion ?? order.source_version, this.now(), merchantId, recordId);
|
|
104
|
+
return this.getOrder(merchantId, recordId);
|
|
105
|
+
}
|
|
106
|
+
listLines(recordId) {
|
|
107
|
+
return this.db
|
|
108
|
+
.prepare("SELECT line_id, sku, quantity, amount_minor FROM order_lines_v2 WHERE record_id = ? ORDER BY rowid")
|
|
109
|
+
.all(recordId);
|
|
110
|
+
}
|
|
111
|
+
/** 事件合并:同维度按契约比较;accepted 才写入。 */
|
|
112
|
+
appendEvent(input) {
|
|
113
|
+
const events = this.listEvents(input.recordId, input.incoming.dimension, input.incoming.identity);
|
|
114
|
+
const current = events.length === 0 ? undefined : events[events.length - 1];
|
|
115
|
+
const merge = mergeOrderEvent({
|
|
116
|
+
...(current !== undefined ? { current } : {}),
|
|
117
|
+
incoming: input.incoming,
|
|
118
|
+
contract: input.contract,
|
|
119
|
+
});
|
|
120
|
+
if (merge.status !== "accepted")
|
|
121
|
+
return { inserted: false, status: merge.status };
|
|
122
|
+
this.db
|
|
123
|
+
.prepare(`INSERT INTO order_events_v2
|
|
124
|
+
(event_id, record_id, external_event_id, dimension, source_version, occurred_at,
|
|
125
|
+
observed_at, value, amount_minor, refund_id, raw_digest, created_at)
|
|
126
|
+
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)`)
|
|
127
|
+
.run(randomUUID(), input.recordId, input.incoming.event_id, input.incoming.dimension, input.incoming.source_version, input.incoming.occurred_at, input.incoming.observed_at, input.incoming.value, input.incoming.amount_minor ?? null, input.incoming.refund_id ?? null, input.incoming.raw_digest, this.now());
|
|
128
|
+
return { inserted: true, status: "accepted" };
|
|
129
|
+
}
|
|
130
|
+
listEvents(recordId, dimension, identity) {
|
|
131
|
+
const where = ["record_id = ?"];
|
|
132
|
+
const params = [recordId];
|
|
133
|
+
if (dimension !== undefined) {
|
|
134
|
+
where.push("dimension = ?");
|
|
135
|
+
params.push(dimension);
|
|
136
|
+
}
|
|
137
|
+
const rows = this.db
|
|
138
|
+
.prepare(`SELECT * FROM order_events_v2 WHERE ${where.join(" AND ")} ORDER BY rowid`)
|
|
139
|
+
.all(...params);
|
|
140
|
+
return rows.map((row) => ({
|
|
141
|
+
event_id: row.event_id,
|
|
142
|
+
identity: identity ?? {
|
|
143
|
+
merchant_id: "",
|
|
144
|
+
source_namespace: "",
|
|
145
|
+
source_account_id: "",
|
|
146
|
+
external_order_id: "",
|
|
147
|
+
},
|
|
148
|
+
dimension: row.dimension,
|
|
149
|
+
source_version: row.source_version,
|
|
150
|
+
occurred_at: row.occurred_at,
|
|
151
|
+
observed_at: row.observed_at,
|
|
152
|
+
value: row.value,
|
|
153
|
+
...(row.amount_minor !== null ? { amount_minor: row.amount_minor } : {}),
|
|
154
|
+
...(row.refund_id !== null ? { refund_id: row.refund_id } : {}),
|
|
155
|
+
raw_digest: row.raw_digest,
|
|
156
|
+
}));
|
|
157
|
+
}
|
|
158
|
+
replaceAttributions(merchantId, recordId, allocations) {
|
|
159
|
+
if (this.getOrder(merchantId, recordId) === undefined) {
|
|
160
|
+
throw new OrderFactError("not_found", `order ${recordId} not found`);
|
|
161
|
+
}
|
|
162
|
+
const lines = this.listLines(recordId);
|
|
163
|
+
const result = allocateAttributions(lines, allocations);
|
|
164
|
+
return this.transaction(() => {
|
|
165
|
+
this.db.prepare("DELETE FROM order_attributions_v2 WHERE record_id = ?").run(recordId);
|
|
166
|
+
for (const allocation of result.allocations) {
|
|
167
|
+
this.db
|
|
168
|
+
.prepare(`INSERT INTO order_attributions_v2
|
|
169
|
+
(attribution_id, record_id, order_line_id, opportunity_id, quantity, amount_minor,
|
|
170
|
+
method, confirmed_by, created_at)
|
|
171
|
+
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)`)
|
|
172
|
+
.run(allocation.attribution_id, recordId, allocation.order_line_id, allocation.opportunity_id, allocation.quantity, allocation.amount_minor, allocation.method, allocation.confirmed_by ?? null, this.now());
|
|
173
|
+
}
|
|
174
|
+
return { allocations: result.allocations, unassigned: result.unassigned };
|
|
175
|
+
});
|
|
176
|
+
}
|
|
177
|
+
listAttributions(recordId) {
|
|
178
|
+
return this.db
|
|
179
|
+
.prepare("SELECT order_line_id, opportunity_id, quantity, amount_minor, method, confirmed_by FROM order_attributions_v2 WHERE record_id = ? ORDER BY rowid")
|
|
180
|
+
.all(recordId);
|
|
181
|
+
}
|
|
182
|
+
mergeRefund(merchantId, recordId, incoming) {
|
|
183
|
+
const current = this.listRefunds(merchantId, recordId);
|
|
184
|
+
const merged = mergeRefund(current, incoming);
|
|
185
|
+
if (merged.status === "inserted" || merged.status === "corrected") {
|
|
186
|
+
const next = merged.refunds.find((refund) => refund.identity.external_refund_id === incoming.identity.external_refund_id);
|
|
187
|
+
this.db
|
|
188
|
+
.prepare(`INSERT INTO refunds_v2
|
|
189
|
+
(refund_row_id, merchant_id, record_id, source_namespace, source_account_id,
|
|
190
|
+
external_order_id, external_refund_id, amount_minor, version, status, updated_at)
|
|
191
|
+
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
|
|
192
|
+
ON CONFLICT (merchant_id, source_namespace, source_account_id, external_order_id, external_refund_id)
|
|
193
|
+
DO UPDATE SET amount_minor = excluded.amount_minor, version = excluded.version,
|
|
194
|
+
status = excluded.status, updated_at = excluded.updated_at`)
|
|
195
|
+
.run(randomUUID(), merchantId, recordId, next.identity.source_namespace, next.identity.source_account_id, next.identity.external_order_id, next.identity.external_refund_id, next.amount_minor, next.version, next.status, this.now());
|
|
196
|
+
}
|
|
197
|
+
return {
|
|
198
|
+
status: merged.status,
|
|
199
|
+
totalMinor: refundTotalMinor(merged.refunds),
|
|
200
|
+
};
|
|
201
|
+
}
|
|
202
|
+
listRefunds(merchantId, recordId) {
|
|
203
|
+
const rows = this.db
|
|
204
|
+
.prepare("SELECT source_namespace, source_account_id, external_order_id, external_refund_id, amount_minor, version, status FROM refunds_v2 WHERE merchant_id = ? AND record_id = ? ORDER BY rowid")
|
|
205
|
+
.all(merchantId, recordId);
|
|
206
|
+
return rows.map((row) => ({
|
|
207
|
+
identity: {
|
|
208
|
+
merchant_id: merchantId,
|
|
209
|
+
source_namespace: row.source_namespace,
|
|
210
|
+
source_account_id: row.source_account_id,
|
|
211
|
+
external_order_id: row.external_order_id,
|
|
212
|
+
external_refund_id: row.external_refund_id,
|
|
213
|
+
},
|
|
214
|
+
amount_minor: row.amount_minor,
|
|
215
|
+
version: row.version,
|
|
216
|
+
status: row.status,
|
|
217
|
+
}));
|
|
218
|
+
}
|
|
219
|
+
}
|
|
220
|
+
export { orderIdentityKey };
|
|
221
|
+
//# sourceMappingURL=repository.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"repository.js","sourceRoot":"","sources":["../../../src/merchant-core/orders/repository.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;GAcG;AAEH;;;;;GAKG;AAEH,OAAO,EAAE,UAAU,EAAE,MAAM,aAAa,CAAC;AAGzC,OAAO,EACL,oBAAoB,EACpB,eAAe,EACf,WAAW,EACX,gBAAgB,EAChB,gBAAgB,EAChB,cAAc,EAOd,sBAAsB,GACvB,MAAM,YAAY,CAAC;AA8DpB,MAAM,OAAO,eAAe;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AAC,GAAG,CAAC,CAAC,GAAG,EAAE,EAAE,CAAC,CAAC;YACxB,QAAQ,EAAE;gBACR,WAAW,EAAE,UAAU;gBACvB,gBAAgB,EAAE,GAAG,CAAC,gBAAgB;gBACtC,iBAAiB,EAAE,GAAG,CAAC,iBAAiB;gBACxC,iBAAiB,EAAE,GAAG,CAAC,iBAAiB;gBACxC,kBAAkB,EAAE,GAAG,CAAC,kBAAkB;aAC3C;YACD,YAAY,EAAE,GAAG,CAAC,YAAY;YAC9B,OAAO,EAAE,GAAG,CAAC,OAAO;YACpB,MAAM,EAAE,GAAG,CAAC,MAAM;SACnB,CAAC,CAAC,CAAC;IACN,CAAC;CACF;AAED,OAAO,EAAE,gBAAgB,EAAE,CAAC"}
|
|
@@ -0,0 +1,84 @@
|
|
|
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 { ContributionRepository } from "../contribution/repository.js";
|
|
17
|
+
import type { ContributionInput } from "../contribution/types.js";
|
|
18
|
+
import { type AttributionInput, type ExternalOrderIdentity, type OrderStreamContract, type RefundFact } from "./model.js";
|
|
19
|
+
import type { OrderRecordV2, OrderRepository } from "./repository.js";
|
|
20
|
+
export interface OrderSourceContract {
|
|
21
|
+
source_namespace: string;
|
|
22
|
+
source_account_id: string;
|
|
23
|
+
ordering: "monotonic" | "unorderable";
|
|
24
|
+
version_scope: string;
|
|
25
|
+
event_id_scope: string;
|
|
26
|
+
supports_watermark: boolean;
|
|
27
|
+
}
|
|
28
|
+
export declare function validateOrderSourceContract(contract: OrderSourceContract): void;
|
|
29
|
+
export interface ManualOrderReportInput {
|
|
30
|
+
identity: ExternalOrderIdentity;
|
|
31
|
+
lines: unknown;
|
|
32
|
+
currency?: string;
|
|
33
|
+
total_minor?: number;
|
|
34
|
+
payment_status?: string;
|
|
35
|
+
fulfillment_status?: string;
|
|
36
|
+
observed_at: string;
|
|
37
|
+
reported_by: string;
|
|
38
|
+
evidence_ref?: string;
|
|
39
|
+
}
|
|
40
|
+
export interface MerchantOrderServiceOptions {
|
|
41
|
+
orders: OrderRepository;
|
|
42
|
+
contributions: ContributionRepository;
|
|
43
|
+
}
|
|
44
|
+
export declare class MerchantOrderService {
|
|
45
|
+
private readonly orders;
|
|
46
|
+
private readonly contributions;
|
|
47
|
+
constructor(options: MerchantOrderServiceOptions);
|
|
48
|
+
/** 人工/CSV 申报:只记申报事实,绝不升级为外部已核实。 */
|
|
49
|
+
recordManualReport(input: ManualOrderReportInput): OrderRecordV2;
|
|
50
|
+
listOrders(merchantId: string): OrderRecordV2[];
|
|
51
|
+
getOrder(merchantId: string, recordId: string): OrderRecordV2 | undefined;
|
|
52
|
+
/** CSV 申报:整批解析后逐单写入 csv_import 证据;坏行整批拒绝。 */
|
|
53
|
+
recordCsvReport(input: {
|
|
54
|
+
merchantId: string;
|
|
55
|
+
csv: string;
|
|
56
|
+
reportedBy: string;
|
|
57
|
+
observedAtFallback: string;
|
|
58
|
+
}): OrderRecordV2[];
|
|
59
|
+
appendSourceEvent(input: {
|
|
60
|
+
merchantId: string;
|
|
61
|
+
recordId: string;
|
|
62
|
+
event: Parameters<OrderRepository["appendEvent"]>[0]["incoming"];
|
|
63
|
+
contract: OrderStreamContract;
|
|
64
|
+
}): {
|
|
65
|
+
inserted: boolean;
|
|
66
|
+
status: string;
|
|
67
|
+
};
|
|
68
|
+
attribute(merchantId: string, recordId: string, allocations: AttributionInput[]): ReturnType<OrderRepository["replaceAttributions"]>;
|
|
69
|
+
recordRefund(input: {
|
|
70
|
+
merchantId: string;
|
|
71
|
+
recordId: string;
|
|
72
|
+
refund: RefundFact;
|
|
73
|
+
}): {
|
|
74
|
+
status: string;
|
|
75
|
+
totalMinor: number;
|
|
76
|
+
};
|
|
77
|
+
/** 读取当前退款总额并计算贡献;成本快照可由调用方先行保存。 */
|
|
78
|
+
assessContribution(input: {
|
|
79
|
+
merchantId: string;
|
|
80
|
+
recordId: string;
|
|
81
|
+
contribution: ContributionInput;
|
|
82
|
+
costSnapshotId?: string;
|
|
83
|
+
}): ReturnType<ContributionRepository["saveAssessment"]>;
|
|
84
|
+
}
|
|
@@ -0,0 +1,121 @@
|
|
|
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–§13 / FR19–FR27)。
|
|
18
|
+
*
|
|
19
|
+
* 人工/CSV 申报只生成 `manual_report`/`csv_import`;只有受信来源返回有效
|
|
20
|
+
* 事实后才升级为 `external_verified`。归因、退款与贡献分别独立记录。
|
|
21
|
+
*/
|
|
22
|
+
import { calculateContribution } from "../contribution/calculate.js";
|
|
23
|
+
import { OrderFactError, validateOrderLineFacts, } from "./model.js";
|
|
24
|
+
import { parseOrderCsv } from "./csv.js";
|
|
25
|
+
export function validateOrderSourceContract(contract) {
|
|
26
|
+
for (const [name, value] of Object.entries(contract)) {
|
|
27
|
+
if (typeof value === "string" && value.trim() === "") {
|
|
28
|
+
throw new OrderFactError("validation", `source contract ${name} 不能为空`);
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
if (contract.ordering !== "monotonic" && contract.ordering !== "unorderable") {
|
|
32
|
+
throw new OrderFactError("validation", "source contract ordering 非法");
|
|
33
|
+
}
|
|
34
|
+
if (typeof contract.supports_watermark !== "boolean") {
|
|
35
|
+
throw new OrderFactError("validation", "source contract supports_watermark 必须是布尔值");
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
export class MerchantOrderService {
|
|
39
|
+
orders;
|
|
40
|
+
contributions;
|
|
41
|
+
constructor(options) {
|
|
42
|
+
this.orders = options.orders;
|
|
43
|
+
this.contributions = options.contributions;
|
|
44
|
+
}
|
|
45
|
+
/** 人工/CSV 申报:只记申报事实,绝不升级为外部已核实。 */
|
|
46
|
+
recordManualReport(input) {
|
|
47
|
+
const lines = validateOrderLineFacts(input.lines);
|
|
48
|
+
if (input.reported_by.trim() === "") {
|
|
49
|
+
throw new OrderFactError("validation", "reported_by 不能为空");
|
|
50
|
+
}
|
|
51
|
+
if (!Number.isFinite(Date.parse(input.observed_at))) {
|
|
52
|
+
throw new OrderFactError("validation", "observed_at 必须是可解析时间");
|
|
53
|
+
}
|
|
54
|
+
return this.orders.createOrder({
|
|
55
|
+
identity: input.identity,
|
|
56
|
+
evidenceSource: "manual_report",
|
|
57
|
+
...(input.payment_status !== undefined ? { paymentStatus: input.payment_status } : {}),
|
|
58
|
+
...(input.fulfillment_status !== undefined
|
|
59
|
+
? { fulfillmentStatus: input.fulfillment_status }
|
|
60
|
+
: {}),
|
|
61
|
+
...(input.currency !== undefined ? { currency: input.currency } : {}),
|
|
62
|
+
...(input.total_minor !== undefined ? { totalMinor: input.total_minor } : {}),
|
|
63
|
+
observedAt: input.observed_at,
|
|
64
|
+
lines,
|
|
65
|
+
}).order;
|
|
66
|
+
}
|
|
67
|
+
listOrders(merchantId) {
|
|
68
|
+
return this.orders.listOrders(merchantId);
|
|
69
|
+
}
|
|
70
|
+
getOrder(merchantId, recordId) {
|
|
71
|
+
return this.orders.getOrder(merchantId, recordId);
|
|
72
|
+
}
|
|
73
|
+
/** CSV 申报:整批解析后逐单写入 csv_import 证据;坏行整批拒绝。 */
|
|
74
|
+
recordCsvReport(input) {
|
|
75
|
+
if (input.reportedBy.trim() === "") {
|
|
76
|
+
throw new OrderFactError("validation", "reportedBy 不能为空");
|
|
77
|
+
}
|
|
78
|
+
const reports = parseOrderCsv(input.csv);
|
|
79
|
+
if (!Number.isFinite(Date.parse(input.observedAtFallback))) {
|
|
80
|
+
throw new OrderFactError("validation", "observedAtFallback 必须是可解析时间");
|
|
81
|
+
}
|
|
82
|
+
return this.orders.transaction(() => reports.map((report) => this.orders.createOrder({
|
|
83
|
+
identity: { ...report.identity, merchant_id: input.merchantId },
|
|
84
|
+
evidenceSource: "csv_import",
|
|
85
|
+
...(report.payment_status !== undefined ? { paymentStatus: report.payment_status } : {}),
|
|
86
|
+
...(report.fulfillment_status !== undefined
|
|
87
|
+
? { fulfillmentStatus: report.fulfillment_status }
|
|
88
|
+
: {}),
|
|
89
|
+
...(report.currency !== undefined ? { currency: report.currency } : {}),
|
|
90
|
+
...(report.total_minor !== undefined ? { totalMinor: report.total_minor } : {}),
|
|
91
|
+
observedAt: report.observed_at || input.observedAtFallback,
|
|
92
|
+
lines: report.lines,
|
|
93
|
+
}).order));
|
|
94
|
+
}
|
|
95
|
+
appendSourceEvent(input) {
|
|
96
|
+
return this.orders.appendEvent({
|
|
97
|
+
merchantId: input.merchantId,
|
|
98
|
+
recordId: input.recordId,
|
|
99
|
+
incoming: input.event,
|
|
100
|
+
contract: input.contract,
|
|
101
|
+
});
|
|
102
|
+
}
|
|
103
|
+
attribute(merchantId, recordId, allocations) {
|
|
104
|
+
return this.orders.replaceAttributions(merchantId, recordId, allocations);
|
|
105
|
+
}
|
|
106
|
+
recordRefund(input) {
|
|
107
|
+
return this.orders.mergeRefund(input.merchantId, input.recordId, input.refund);
|
|
108
|
+
}
|
|
109
|
+
/** 读取当前退款总额并计算贡献;成本快照可由调用方先行保存。 */
|
|
110
|
+
assessContribution(input) {
|
|
111
|
+
const assessment = calculateContribution(input.contribution);
|
|
112
|
+
return this.contributions.saveAssessment({
|
|
113
|
+
merchantId: input.merchantId,
|
|
114
|
+
refKind: "order",
|
|
115
|
+
refId: input.recordId,
|
|
116
|
+
assessment,
|
|
117
|
+
...(input.costSnapshotId !== undefined ? { costSnapshotId: input.costSnapshotId } : {}),
|
|
118
|
+
});
|
|
119
|
+
}
|
|
120
|
+
}
|
|
121
|
+
//# sourceMappingURL=service.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"service.js","sourceRoot":"","sources":["../../../src/merchant-core/orders/service.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;GAcG;AAEH;;;;;GAKG;AAEH,OAAO,EAAE,qBAAqB,EAAE,MAAM,8BAA8B,CAAC;AAGrE,OAAO,EACL,cAAc,EAKd,sBAAsB,GACvB,MAAM,YAAY,CAAC;AAEpB,OAAO,EAAE,aAAa,EAAE,MAAM,UAAU,CAAC;AAWzC,MAAM,UAAU,2BAA2B,CAAC,QAA6B;IACvE,KAAK,MAAM,CAAC,IAAI,EAAE,KAAK,CAAC,IAAI,MAAM,CAAC,OAAO,CAAC,QAAQ,CAAC,EAAE,CAAC;QACrD,IAAI,OAAO,KAAK,KAAK,QAAQ,IAAI,KAAK,CAAC,IAAI,EAAE,KAAK,EAAE,EAAE,CAAC;YACrD,MAAM,IAAI,cAAc,CAAC,YAAY,EAAE,mBAAmB,IAAI,OAAO,CAAC,CAAC;QACzE,CAAC;IACH,CAAC;IACD,IAAI,QAAQ,CAAC,QAAQ,KAAK,WAAW,IAAI,QAAQ,CAAC,Q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AAU,EAAE,QAAQ,EAAE,WAAW,CAAC,CAAC;IAC5E,CAAC;IAED,YAAY,CAAC,KAIZ;QACC,OAAO,IAAI,CAAC,MAAM,CAAC,WAAW,CAAC,KAAK,CAAC,UAAU,EAAE,KAAK,CAAC,QAAQ,EAAE,KAAK,CAAC,MAAM,CAAC,CAAC;IACjF,CAAC;IAED,mCAAmC;IACnC,kBAAkB,CAAC,KAKlB;QACC,MAAM,UAAU,GAAG,qBAAqB,CAAC,KAAK,CAAC,YAAY,CAAC,CAAC;QAC7D,OAAO,IAAI,CAAC,aAAa,CAAC,cAAc,CAAC;YACvC,UAAU,EAAE,KAAK,CAAC,UAAU;YAC5B,OAAO,EAAE,OAAO;YAChB,KAAK,EAAE,KAAK,CAAC,QAAQ;YACrB,UAAU;YACV,GAAG,CAAC,KAAK,CAAC,cAAc,KAAK,SAAS,CAAC,CAAC,CAAC,EAAE,cAAc,EAAE,KAAK,CAAC,cAAc,EAAE,CAAC,CAAC,CAAC,EAAE,CAAC;SACxF,CAAC,CAAC;IACL,CAAC;CACF"}
|
|
@@ -0,0 +1,58 @@
|
|
|
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 interface ShoppingCliOrderSourceOptions {
|
|
17
|
+
baseUrl: string;
|
|
18
|
+
merchantId: string;
|
|
19
|
+
token: string;
|
|
20
|
+
/** 受信订单连接器凭据;仅用于 `external_verified` 写入。 */
|
|
21
|
+
connectorToken?: string;
|
|
22
|
+
fetchImpl?: typeof fetch;
|
|
23
|
+
timeoutMs?: number;
|
|
24
|
+
}
|
|
25
|
+
export type ShoppingCliSourceErrorCode = "auth" | "forbidden" | "not_found" | "conflict" | "invalid" | "unavailable" | "timeout";
|
|
26
|
+
export declare class ShoppingCliSourceError extends Error {
|
|
27
|
+
readonly code: ShoppingCliSourceErrorCode;
|
|
28
|
+
readonly status?: number;
|
|
29
|
+
constructor(code: ShoppingCliSourceErrorCode, message: string, status?: number);
|
|
30
|
+
}
|
|
31
|
+
export declare class ShoppingCliOrderSource {
|
|
32
|
+
private readonly baseUrl;
|
|
33
|
+
private readonly merchantId;
|
|
34
|
+
private readonly token;
|
|
35
|
+
private readonly connectorToken;
|
|
36
|
+
private readonly fetchImpl;
|
|
37
|
+
private readonly timeoutMs;
|
|
38
|
+
constructor(options: ShoppingCliOrderSourceOptions);
|
|
39
|
+
listOrders(input?: {
|
|
40
|
+
limit?: number;
|
|
41
|
+
offset?: number;
|
|
42
|
+
}): Promise<unknown>;
|
|
43
|
+
getOrder(externalOrderId: string, scope?: {
|
|
44
|
+
sourceNamespace?: string;
|
|
45
|
+
sourceAccountId?: string;
|
|
46
|
+
}): Promise<unknown>;
|
|
47
|
+
/** 受信连接器写入 external_verified;缺 connectorToken 时 fail-closed。 */
|
|
48
|
+
recordVerifiedOrder(input: Record<string, unknown>): Promise<unknown>;
|
|
49
|
+
recordRefund(externalOrderId: string, input: Record<string, unknown>, scope?: {
|
|
50
|
+
sourceNamespace?: string;
|
|
51
|
+
sourceAccountId?: string;
|
|
52
|
+
}): Promise<unknown>;
|
|
53
|
+
saveCostSnapshot(input: Record<string, unknown>): Promise<unknown>;
|
|
54
|
+
getCostSnapshot(snapshotId: string): Promise<unknown>;
|
|
55
|
+
registerSourceContract(input: Record<string, unknown>): Promise<unknown>;
|
|
56
|
+
getSourceContract(sourceNamespace: string, sourceAccountId: string): Promise<unknown>;
|
|
57
|
+
private request;
|
|
58
|
+
}
|