fix(orders): 订单项替换统一分摊收入
This commit is contained in:
@@ -1,5 +1,6 @@
|
|||||||
import test from 'node:test'
|
import test from 'node:test'
|
||||||
import assert from 'node:assert/strict'
|
import assert from 'node:assert/strict'
|
||||||
|
import { readFile } from 'node:fs/promises'
|
||||||
|
|
||||||
import {
|
import {
|
||||||
preserveAssignedOrderItemState,
|
preserveAssignedOrderItemState,
|
||||||
@@ -202,3 +203,19 @@ test('已分配订单重推写库时不会用平台新 SKU 覆盖冻结值', asy
|
|||||||
assert.equal(updateParams[0]?.[0], 'lucky_coin_x2')
|
assert.equal(updateParams[0]?.[0], 'lucky_coin_x2')
|
||||||
assert.equal(updateParams[0]?.[1], '幸运币2个')
|
assert.equal(updateParams[0]?.[1], '幸运币2个')
|
||||||
})
|
})
|
||||||
|
|
||||||
|
test('所有替换订单项入口都在同一事务内分摊订单收入', async () => {
|
||||||
|
const source = await readFile(new URL('./order-item-repo.ts', import.meta.url), 'utf8')
|
||||||
|
const replaceSource = source.slice(
|
||||||
|
source.indexOf('export async function replaceOrderItems('),
|
||||||
|
source.indexOf('export async function applyOrderItemRevenueAllocationWithExecutor('),
|
||||||
|
)
|
||||||
|
|
||||||
|
assert.ok(replaceSource.includes('withTransaction'))
|
||||||
|
assert.ok(replaceSource.includes('applyOrderItemRevenueAllocationWithExecutor'))
|
||||||
|
assert.ok(replaceSource.includes('SELECT total_amount FROM orders'))
|
||||||
|
assert.ok(
|
||||||
|
replaceSource.indexOf('replaceOrderItemsWithExecutor') <
|
||||||
|
replaceSource.indexOf('applyOrderItemRevenueAllocationWithExecutor'),
|
||||||
|
)
|
||||||
|
})
|
||||||
|
|||||||
@@ -42,9 +42,22 @@ export async function replaceOrderItems(
|
|||||||
orderId: number | string,
|
orderId: number | string,
|
||||||
items: OrderItemReplaceInput[],
|
items: OrderItemReplaceInput[],
|
||||||
): Promise<OrderItemRow[]> {
|
): Promise<OrderItemRow[]> {
|
||||||
return withTransaction((client: PoolClient) =>
|
return withTransaction(async (client: PoolClient) => {
|
||||||
replaceOrderItemsWithExecutor(client.query.bind(client), orderId, items),
|
const executor = client.query.bind(client)
|
||||||
)
|
await replaceOrderItemsWithExecutor(executor, orderId, items)
|
||||||
|
const order = await client.query<{ total_amount: string | number }>(
|
||||||
|
`SELECT total_amount FROM orders WHERE id = $1`,
|
||||||
|
[orderId],
|
||||||
|
)
|
||||||
|
if (order.rows[0]) {
|
||||||
|
await applyOrderItemRevenueAllocationWithExecutor(
|
||||||
|
executor,
|
||||||
|
orderId,
|
||||||
|
Number(order.rows[0].total_amount) || 0,
|
||||||
|
)
|
||||||
|
}
|
||||||
|
return listOrderItemsByOrderIdWithExecutor(executor, orderId)
|
||||||
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function applyOrderItemRevenueAllocationWithExecutor(
|
export async function applyOrderItemRevenueAllocationWithExecutor(
|
||||||
|
|||||||
Reference in New Issue
Block a user