修复:保护平台订单原始事实不被清理和覆盖
This commit is contained in:
@@ -38,7 +38,7 @@ test('resolveOrderItemSyncPlan preserves matching order item ids across repeated
|
||||
},
|
||||
])
|
||||
assert.deepEqual(plan.creates, [])
|
||||
assert.deepEqual(plan.deletes, [])
|
||||
assert.deepEqual(plan.surplusExistingIds, [])
|
||||
})
|
||||
|
||||
test('resolveOrderItemSyncPlan matches by identity before falling back to position', () => {
|
||||
@@ -74,10 +74,10 @@ test('resolveOrderItemSyncPlan matches by identity before falling back to positi
|
||||
[22, 21],
|
||||
)
|
||||
assert.deepEqual(plan.creates, [])
|
||||
assert.deepEqual(plan.deletes, [])
|
||||
assert.deepEqual(plan.surplusExistingIds, [])
|
||||
})
|
||||
|
||||
test('resolveOrderItemSyncPlan keeps surplus existing ids for conditional cleanup instead of recreating everything', () => {
|
||||
test('resolveOrderItemSyncPlan reports surplus existing items without deleting them', () => {
|
||||
const existingItems = [
|
||||
{ id: 31, sku_code: 'sku-a', sku_name: '礼包 A', quantity: 1 },
|
||||
{ id: 32, sku_code: 'sku-b', sku_name: '礼包 B', quantity: 1 },
|
||||
@@ -101,7 +101,7 @@ test('resolveOrderItemSyncPlan keeps surplus existing ids for conditional cleanu
|
||||
[31],
|
||||
)
|
||||
assert.deepEqual(plan.creates, [])
|
||||
assert.deepEqual(plan.deletes, [32])
|
||||
assert.deepEqual(plan.surplusExistingIds, [32])
|
||||
})
|
||||
|
||||
test('已分配订单重推时冻结原渠道决策与中文内部 SKU', () => {
|
||||
@@ -155,7 +155,7 @@ test('已分配订单重推时冻结原渠道决策与中文内部 SKU', () => {
|
||||
})
|
||||
})
|
||||
|
||||
test('已分配订单重推写库时不会用平台新 SKU 覆盖冻结值', async () => {
|
||||
test('平台订单重推只追加新订单项,不更新或删除已有原始订单项', async () => {
|
||||
const existing = {
|
||||
id: '41',
|
||||
order_id: 7,
|
||||
@@ -184,24 +184,37 @@ test('已分配订单重推写库时不会用平台新 SKU 覆盖冻结值', asy
|
||||
}
|
||||
const updateParams: unknown[][] = []
|
||||
let selectCount = 0
|
||||
let writeSql = ''
|
||||
|
||||
const executor = async (sql: string, params: unknown[] = []) => {
|
||||
if (sql.includes('SELECT *') && sql.includes('FROM v_order_item_current')) {
|
||||
selectCount += 1
|
||||
return { rows: selectCount === 1 ? [existing] : [] }
|
||||
return { rows: selectCount === 1 ? [existing] : [{ ...existing, id: 42 }] }
|
||||
}
|
||||
if (sql.includes('UPDATE order_items')) {
|
||||
if (sql.includes('INSERT INTO order_items')) {
|
||||
updateParams.push(params)
|
||||
writeSql += sql
|
||||
return { rows: [], rowCount: 1 }
|
||||
}
|
||||
if (/UPDATE\s+order_items|DELETE\s+FROM\s+order_items/i.test(sql)) {
|
||||
throw new Error(`permanent source item must not be mutated: ${sql}`)
|
||||
}
|
||||
throw new Error(`unexpected SQL: ${sql}`)
|
||||
}
|
||||
|
||||
await replaceOrderItemsWithExecutor(executor as never, 7, [incoming])
|
||||
await replaceOrderItemsWithExecutor(executor as never, 7, [
|
||||
incoming,
|
||||
{
|
||||
...incoming,
|
||||
skuCode: 'platform-extra',
|
||||
skuName: '平台新增商品',
|
||||
},
|
||||
])
|
||||
|
||||
assert.equal(updateParams.length, 1)
|
||||
assert.equal(updateParams[0]?.[0], 'lucky_coin_x2')
|
||||
assert.equal(updateParams[0]?.[1], '幸运币2个')
|
||||
assert.ok(writeSql.includes('INSERT INTO order_items'))
|
||||
assert.equal(updateParams[0]?.[1], 'platform-extra')
|
||||
assert.match(String(updateParams[0]?.[2]), /平台新增商品/)
|
||||
})
|
||||
|
||||
test('所有替换订单项入口都在同一事务内分摊订单收入', async () => {
|
||||
|
||||
@@ -13,7 +13,7 @@ type OrderItemSyncPlan = {
|
||||
item: OrderItemReplaceInput
|
||||
}>
|
||||
creates: OrderItemReplaceInput[]
|
||||
deletes: number[]
|
||||
surplusExistingIds: number[]
|
||||
}
|
||||
|
||||
type OrderItemIdentityInput = Partial<OrderItemReplaceInput> & Partial<OrderItemRow>
|
||||
@@ -100,36 +100,6 @@ export async function replaceOrderItemsWithExecutor(
|
||||
): Promise<OrderItemRow[]> {
|
||||
const existingItems = await listOrderItemsByOrderIdWithExecutor(executor, orderId)
|
||||
const plan = resolveOrderItemSyncPlan(existingItems, items)
|
||||
const existingById = new Map(existingItems.map((item) => [Number(item.id), item]))
|
||||
|
||||
for (const update of plan.updates) {
|
||||
const effectiveItem = preserveAssignedOrderItemState(
|
||||
existingById.get(Number(update.orderItemId)),
|
||||
update.item,
|
||||
)
|
||||
await executor(
|
||||
`
|
||||
UPDATE order_items
|
||||
SET
|
||||
sku_code = $1,
|
||||
sku_name = $2,
|
||||
quantity = $3,
|
||||
spec_json = $4::jsonb,
|
||||
item_snapshot_json = $5::jsonb,
|
||||
updated_at = $6
|
||||
WHERE id = $7
|
||||
`,
|
||||
[
|
||||
effectiveItem.skuCode,
|
||||
effectiveItem.skuName,
|
||||
update.item.quantity,
|
||||
safeJsonStringify(effectiveItem.specJson || '{}'),
|
||||
safeJsonStringify(effectiveItem.itemSnapshotJson || effectiveItem.specJson || '{}'),
|
||||
effectiveItem.updatedAt,
|
||||
update.orderItemId,
|
||||
],
|
||||
)
|
||||
}
|
||||
|
||||
for (const create of plan.creates) {
|
||||
await executor(
|
||||
@@ -158,14 +128,6 @@ export async function replaceOrderItemsWithExecutor(
|
||||
)
|
||||
}
|
||||
|
||||
if (plan.deletes.length > 0) {
|
||||
const deletableIds = await listDeletableOrderItemIdsWithExecutor(executor, plan.deletes)
|
||||
|
||||
if (deletableIds.length > 0) {
|
||||
await executor('DELETE FROM order_items WHERE id = ANY($1::bigint[])', [deletableIds])
|
||||
}
|
||||
}
|
||||
|
||||
return listOrderItemsByOrderIdWithExecutor(executor, orderId)
|
||||
}
|
||||
|
||||
@@ -280,7 +242,9 @@ export function resolveOrderItemSyncPlan(
|
||||
return {
|
||||
updates,
|
||||
creates,
|
||||
deletes: unmatchedExisting.map((item) => Number(item.id)).filter((value) => value > 0),
|
||||
surplusExistingIds: unmatchedExisting
|
||||
.map((item) => Number(item.id))
|
||||
.filter((value) => value > 0),
|
||||
}
|
||||
}
|
||||
|
||||
@@ -301,34 +265,6 @@ function buildOrderItemIdentity(item: OrderItemIdentityInput): string {
|
||||
].join('::')
|
||||
}
|
||||
|
||||
async function listDeletableOrderItemIdsWithExecutor(
|
||||
executor: QueryExecutor,
|
||||
orderItemIds: number[],
|
||||
): Promise<number[]> {
|
||||
const normalizedIds = Array.isArray(orderItemIds)
|
||||
? orderItemIds.map((value) => Number(value)).filter((value) => value > 0)
|
||||
: []
|
||||
|
||||
if (normalizedIds.length === 0) {
|
||||
return []
|
||||
}
|
||||
|
||||
const result = await executor(
|
||||
`
|
||||
SELECT oi.id
|
||||
FROM v_order_item_current oi
|
||||
LEFT JOIN fulfillment_tasks ft ON ft.order_item_id = oi.id
|
||||
WHERE oi.id = ANY($1::bigint[])
|
||||
GROUP BY oi.id
|
||||
HAVING COUNT(ft.id) = 0
|
||||
ORDER BY oi.id ASC
|
||||
`,
|
||||
[normalizedIds],
|
||||
)
|
||||
|
||||
return result.rows.map((row) => Number(row.id)).filter((value) => value > 0)
|
||||
}
|
||||
|
||||
async function listOrderItemsByOrderIdWithExecutor(
|
||||
executor: QueryExecutor,
|
||||
orderId: number | string,
|
||||
|
||||
@@ -1,8 +1,18 @@
|
||||
import assert from 'node:assert/strict'
|
||||
import { readFile } from 'node:fs/promises'
|
||||
import test from 'node:test'
|
||||
|
||||
import { listOrdersWithExecutor, type OrderQueryExecutor } from './order-repo.js'
|
||||
|
||||
test('订单更新永不覆盖首次写入的平台原始报文', async () => {
|
||||
const source = await readFile(new URL('./order-repo.ts', import.meta.url), 'utf8')
|
||||
const updateSource = source.slice(
|
||||
source.indexOf('export async function updateOrderWithExecutor'),
|
||||
source.indexOf('export async function getOrderById'),
|
||||
)
|
||||
assert.doesNotMatch(updateSource, /raw_payload_json\s*=/)
|
||||
})
|
||||
|
||||
test('订单列表综合筛选下推到总数和分页查询', async () => {
|
||||
const calls: Array<{ sql: string; params: unknown[] }> = []
|
||||
const executor = createExecutor(calls, 3)
|
||||
|
||||
@@ -191,10 +191,9 @@ export async function updateOrderWithExecutor(
|
||||
receiver_contact = $9,
|
||||
total_amount = $10,
|
||||
currency = $11,
|
||||
raw_payload_json = $12::jsonb,
|
||||
paid_at = $13,
|
||||
updated_at = $14
|
||||
WHERE id = $15
|
||||
paid_at = $12,
|
||||
updated_at = $13
|
||||
WHERE id = $14
|
||||
RETURNING *
|
||||
`,
|
||||
[
|
||||
@@ -209,7 +208,6 @@ export async function updateOrderWithExecutor(
|
||||
sanitizeWellFormedUnicode(input.receiverContact),
|
||||
input.totalAmount,
|
||||
input.currency,
|
||||
safeJsonStringify(input.rawPayloadJson || '{}'),
|
||||
input.paidAt || null,
|
||||
input.updatedAt,
|
||||
Number(orderId),
|
||||
|
||||
@@ -0,0 +1,137 @@
|
||||
/**
|
||||
* 平台订单原始事实不可变集成测试(需要真实 PostgreSQL)。
|
||||
*
|
||||
* ORDER_SOURCE_FACTS_TEST_DATABASE_URL=postgres://... \
|
||||
* npx tsx --test src/repositories/order-source-facts.integration.test.ts
|
||||
*/
|
||||
import assert from 'node:assert/strict'
|
||||
import test from 'node:test'
|
||||
|
||||
const TEST_DATABASE_URL = String(process.env.ORDER_SOURCE_FACTS_TEST_DATABASE_URL || '').trim()
|
||||
|
||||
test(
|
||||
'平台重推只追加原始事实,不覆盖订单报文、不删除已有订单项',
|
||||
{ skip: !TEST_DATABASE_URL ? '未设置 ORDER_SOURCE_FACTS_TEST_DATABASE_URL' : false },
|
||||
async () => {
|
||||
process.env.DATABASE_URL = TEST_DATABASE_URL
|
||||
const { closeDb, query, withTransaction } = await import('../db/client.js')
|
||||
const { appendOrderSourceSnapshotWithExecutor } =
|
||||
await import('./order-source-snapshot-repo.js')
|
||||
const { replaceOrderItemsWithExecutor } = await import('./order-item-repo.js')
|
||||
const suffix = `${Date.now()}_${Math.floor(Math.random() * 1_000_000)}`
|
||||
let orderId = 0
|
||||
|
||||
try {
|
||||
const order = await query<{ id: number }>(
|
||||
`
|
||||
INSERT INTO orders (
|
||||
provider, platform, shop_id, shop_name, platform_order_id,
|
||||
order_status, pay_status, total_amount, raw_payload_json, created_at, updated_at
|
||||
) VALUES (
|
||||
'91kaquan', 'kuaishou-91', $1, '原始事实测试店', $2,
|
||||
'paid', 'paid', 1000, $3::jsonb, NOW(), NOW()
|
||||
) RETURNING id
|
||||
`,
|
||||
[
|
||||
`source-facts-shop-${suffix}`,
|
||||
`SOURCE-FACTS-${suffix}`,
|
||||
JSON.stringify({ orderNo: `SOURCE-FACTS-${suffix}`, version: 1 }),
|
||||
],
|
||||
)
|
||||
orderId = Number(order.rows[0]?.id || 0)
|
||||
assert.ok(orderId > 0)
|
||||
|
||||
const item = await query<{ id: number }>(
|
||||
`
|
||||
INSERT INTO order_items (
|
||||
order_id, sku_code, sku_name, quantity, spec_json, item_snapshot_json,
|
||||
revenue_amount_fen, revenue_basis, created_at, updated_at
|
||||
) VALUES (
|
||||
$1, 'sku-old', '原始商品', 1, '{}'::jsonb, '{}'::jsonb,
|
||||
1000, 'order_total', NOW(), NOW()
|
||||
) RETURNING id
|
||||
`,
|
||||
[orderId],
|
||||
)
|
||||
const originalItemId = Number(item.rows[0]?.id || 0)
|
||||
assert.ok(originalItemId > 0)
|
||||
|
||||
const snapshotInput = {
|
||||
orderId,
|
||||
provider: '91kaquan',
|
||||
platform: 'kuaishou-91',
|
||||
shopId: `source-facts-shop-${suffix}`,
|
||||
platformOrderId: `SOURCE-FACTS-${suffix}`,
|
||||
sourceLabel: 'integration-test',
|
||||
payload: { orderNo: `SOURCE-FACTS-${suffix}`, version: 1, items: ['sku-old'] },
|
||||
recordedAt: new Date().toISOString(),
|
||||
}
|
||||
assert.equal(await appendOrderSourceSnapshotWithExecutor(query, snapshotInput), true)
|
||||
assert.equal(await appendOrderSourceSnapshotWithExecutor(query, snapshotInput), false)
|
||||
|
||||
const snapshots = await query<{ count: number }>(
|
||||
'SELECT COUNT(*)::int AS count FROM order_source_snapshots WHERE order_id = $1',
|
||||
[orderId],
|
||||
)
|
||||
assert.equal(Number(snapshots.rows[0]?.count), 1)
|
||||
|
||||
await replaceOrderItemsWithExecutor(query, orderId, [
|
||||
{
|
||||
skuCode: 'sku-new',
|
||||
skuName: '平台新商品',
|
||||
quantity: 1,
|
||||
specJson: '{}',
|
||||
itemSnapshotJson: '{}',
|
||||
createdAt: new Date().toISOString(),
|
||||
updatedAt: new Date().toISOString(),
|
||||
},
|
||||
{
|
||||
skuCode: 'sku-extra',
|
||||
skuName: '平台新增商品',
|
||||
quantity: 1,
|
||||
specJson: '{}',
|
||||
itemSnapshotJson: '{}',
|
||||
createdAt: new Date().toISOString(),
|
||||
updatedAt: new Date().toISOString(),
|
||||
},
|
||||
])
|
||||
|
||||
const items = await query<{ original_exists: boolean; total: number }>(
|
||||
`
|
||||
SELECT
|
||||
EXISTS (SELECT 1 FROM order_items WHERE id = $1) AS original_exists,
|
||||
(SELECT COUNT(*)::int FROM order_items WHERE order_id = $2) AS total
|
||||
`,
|
||||
[originalItemId, orderId],
|
||||
)
|
||||
assert.equal(items.rows[0]?.original_exists, true)
|
||||
assert.equal(Number(items.rows[0]?.total), 2)
|
||||
|
||||
await replaceOrderItemsWithExecutor(query, orderId, [
|
||||
{
|
||||
skuCode: 'sku-new',
|
||||
skuName: '平台新商品',
|
||||
quantity: 1,
|
||||
specJson: '{}',
|
||||
itemSnapshotJson: '{}',
|
||||
createdAt: new Date().toISOString(),
|
||||
updatedAt: new Date().toISOString(),
|
||||
},
|
||||
])
|
||||
const afterShrink = await query<{ total: number }>(
|
||||
'SELECT COUNT(*)::int AS total FROM order_items WHERE order_id = $1',
|
||||
[orderId],
|
||||
)
|
||||
assert.equal(Number(afterShrink.rows[0]?.total), 2)
|
||||
} finally {
|
||||
if (orderId > 0) {
|
||||
await withTransaction(async (client) => {
|
||||
await client.query(`SET LOCAL app.allow_order_fact_repair = 'on'`)
|
||||
await client.query('DELETE FROM order_source_snapshots WHERE order_id = $1', [orderId])
|
||||
await client.query('DELETE FROM orders WHERE id = $1', [orderId])
|
||||
})
|
||||
}
|
||||
await closeDb()
|
||||
}
|
||||
},
|
||||
)
|
||||
@@ -0,0 +1,40 @@
|
||||
import assert from 'node:assert/strict'
|
||||
import test from 'node:test'
|
||||
|
||||
import {
|
||||
appendOrderSourceSnapshotWithExecutor,
|
||||
orderSourceSnapshotHash,
|
||||
} from './order-source-snapshot-repo.js'
|
||||
|
||||
test('订单来源快照哈希稳定且能区分不同报文', () => {
|
||||
const first = orderSourceSnapshotHash({ orderNo: 'A-1', status: 'paid' })
|
||||
assert.equal(first, orderSourceSnapshotHash({ orderNo: 'A-1', status: 'paid' }))
|
||||
assert.notEqual(first, orderSourceSnapshotHash({ orderNo: 'A-1', status: 'refunded' }))
|
||||
assert.match(first, /^[0-9a-f]{64}$/)
|
||||
})
|
||||
|
||||
test('同订单相同来源快照只追加一次', async () => {
|
||||
const calls: unknown[][] = []
|
||||
const executor = async (sql: string, params: unknown[] = []) => {
|
||||
calls.push(params)
|
||||
assert.ok(sql.includes('INSERT INTO order_source_snapshots'))
|
||||
assert.ok(sql.includes('ON CONFLICT (order_id, payload_sha256) DO NOTHING'))
|
||||
return { rows: [{ id: 1 }] }
|
||||
}
|
||||
|
||||
const inserted = await appendOrderSourceSnapshotWithExecutor(executor as never, {
|
||||
orderId: 7,
|
||||
provider: 'kuaishou-91',
|
||||
platform: 'kuaishou-91',
|
||||
shopId: 'shop-1',
|
||||
platformOrderId: 'ORDER-1',
|
||||
sourceLabel: 'source-order-upsert',
|
||||
payload: { orderNo: 'ORDER-1', status: 'paid' },
|
||||
recordedAt: '2026-11-09T00:00:00.000Z',
|
||||
})
|
||||
|
||||
assert.equal(inserted, true)
|
||||
assert.equal(calls.length, 1)
|
||||
assert.equal(calls[0]?.[0], 7)
|
||||
assert.equal(calls[0]?.[7], orderSourceSnapshotHash({ orderNo: 'ORDER-1', status: 'paid' }))
|
||||
})
|
||||
@@ -0,0 +1,67 @@
|
||||
import { createHash } from 'node:crypto'
|
||||
import type { QueryResult, QueryResultRow } from 'pg'
|
||||
|
||||
import { safeJsonStringify } from '../utils/json.js'
|
||||
|
||||
type QueryExecutor = <T extends QueryResultRow = QueryResultRow>(
|
||||
text: string,
|
||||
params?: unknown[],
|
||||
) => Promise<QueryResult<T>>
|
||||
|
||||
export type OrderSourceSnapshotInput = {
|
||||
orderId: number
|
||||
provider: string
|
||||
platform: string
|
||||
shopId: string
|
||||
platformOrderId: string
|
||||
sourceLabel: string
|
||||
payload: unknown
|
||||
recordedAt: string
|
||||
}
|
||||
|
||||
export function orderSourceSnapshotHash(payload: unknown): string {
|
||||
return createHash('sha256')
|
||||
.update(safeJsonStringify(payload || {}))
|
||||
.digest('hex')
|
||||
}
|
||||
|
||||
/**
|
||||
* 追加平台订单原始快照。
|
||||
* 同一订单的相同内容只写一次,不同内容永远追加,不覆盖订单主表原始报文。
|
||||
*/
|
||||
export async function appendOrderSourceSnapshotWithExecutor(
|
||||
executor: QueryExecutor,
|
||||
input: OrderSourceSnapshotInput,
|
||||
): Promise<boolean> {
|
||||
const payloadJson = safeJsonStringify(input.payload || {})
|
||||
const payloadHash = orderSourceSnapshotHash(input.payload)
|
||||
const result = await executor<{ id: number }>(
|
||||
`
|
||||
INSERT INTO order_source_snapshots (
|
||||
order_id,
|
||||
provider,
|
||||
platform,
|
||||
shop_id,
|
||||
platform_order_id,
|
||||
source_label,
|
||||
payload_json,
|
||||
payload_sha256,
|
||||
recorded_at
|
||||
) VALUES ($1, $2, $3, $4, $5, $6, $7::jsonb, $8, $9)
|
||||
ON CONFLICT (order_id, payload_sha256) DO NOTHING
|
||||
RETURNING id
|
||||
`,
|
||||
[
|
||||
input.orderId,
|
||||
input.provider,
|
||||
input.platform,
|
||||
input.shopId,
|
||||
input.platformOrderId,
|
||||
input.sourceLabel,
|
||||
payloadJson,
|
||||
payloadHash,
|
||||
input.recordedAt,
|
||||
],
|
||||
)
|
||||
return result.rows.length > 0
|
||||
}
|
||||
Reference in New Issue
Block a user