fix(consignment): 撤销寄售履约释放占用,修复现货回库后仍被占用

撤销寄售履约旧实现只更新 consignment_inventories(status/bound_*),
不释放 consignment_inventory_allocations,也不回补 remaining_quantity。
结果:现货状态回到在库、余量停在 0、占用行停在 reserved ——
库存永远匹配不到,卖家端还一直显示进行中(工单 217368 / 库存 1584)。

- 新增 releaseConsignmentAllocationsForRevokedFulfillmentWithClient:
  回库分支复用 releaseAllocationRows(回补余量、consumed→in_stock、解绑/改绑);
  报废分支只关闭占用行、不回补余量,避免作废货重新进池。
- 撤销事务在库存更新后调用释放,并重读库存让接口返回真实余量。
- 补 5 个用例:回库回补、其它占用改绑、报废不回补、空占用幂等、
  以及源码断言锁定撤销必须释放占用。
This commit is contained in:
2026-11-15 22:39:18 +08:00
parent 8181ef50a5
commit 24d25aa77a
4 changed files with 211 additions and 1 deletions
@@ -40,6 +40,7 @@ import type {
} from '../../domain/consignment-deposit-policy.js'
import { settleWorkOrderDepositWithClient } from '../../repositories/worker-platform/work-order-deposit-repo.js'
import { buildConsignmentRevokeInventoryUpdate } from '../../repositories/worker-platform/consignment-revoke-inventory-sql.js'
import { releaseConsignmentAllocationsForRevokedFulfillmentWithClient } from '../../repositories/worker-platform/consignment-allocation-repo.js'
import { nowIso } from '../../utils/time.js'
import { safeParseJson } from '../admin/admin-query-utils.js'
import { publishWorkOrderRealtimeChange } from '../realtime/realtime-event-service.js'
@@ -1083,7 +1084,7 @@ export async function revokeWorkOrderConsignmentFulfillment(input: {
const lockedMaterial = safeParseJson(lockedOrder.material_json)
resolveRevocableConsignmentInventoryId(safeParseJson(lockedMaterial.consignment))
let inv: ConsignmentInventoryRow | null = null
let inv: ConsignmentInventoryRow | null
// 1. 处理关联库存解绑与状态流转。
// 必须带「仍被本工单占用」条件:否则会把别的工单正在使用的现货抢过来解绑,
@@ -1112,6 +1113,22 @@ export async function revokeWorkOrderConsignmentFulfillment(input: {
})
}
// 1.1 释放占用记录:remaining_quantity 与卖家端「进行中」都以 allocation 为准。
// 旧实现只改 consignment_inventories(status/bound_*),占用行停在 reserved、余量停在 0,
// 现货回库却永远匹配不到,卖家端还一直显示占用(工单 217368 / 库存 1584)。
await releaseConsignmentAllocationsForRevokedFulfillmentWithClient(client, {
workOrderId: Number(lockedOrder.id),
inventoryId: Number(inv.id),
archive: archiveOnly,
now,
})
// 释放后余量/状态已变,重读一次让接口返回真实值,前端不用再等下一次刷新。
const refreshedInv = await client.query<ConsignmentInventoryRow>(
'SELECT * FROM consignment_inventories WHERE id = $1',
[inv.id],
)
inv = refreshedInv.rows[0] || inv
// 卖家发布押金按选择走:auto 时回库保持冻结、作废退还;只有显式「扣除没收」才扣。
// 买家退款不因已确认加好友而没收卖家押金。
const consignmentDepositAction = resolveConsignmentDepositAction(input.consignmentDeposit, {
@@ -32,6 +32,21 @@ test('撤销寄售履约的现货去向和押金都按运营选择,已确认
assert.ok(source.includes('inventoryArchived: archiveOnly'))
})
test('撤销寄售履约必须释放占用记录,不能只把库存状态改回在库', async () => {
const source = await readServiceSource()
const start = source.indexOf('export async function revokeWorkOrderConsignmentFulfillment(')
assert.ok(start > 0, '未找到撤销寄售履约函数')
const body = source.slice(start, source.indexOf('\n}\n', start))
const inventoryUpdateIndex = body.indexOf('buildConsignmentRevokeInventoryUpdate(')
const releaseIndex = body.indexOf('releaseConsignmentAllocationsForRevokedFulfillmentWithClient(')
assert.ok(inventoryUpdateIndex > 0, '撤销必须先解绑库存')
assert.ok(
releaseIndex > inventoryUpdateIndex,
'解绑库存后必须释放 allocation:remaining_quantity 与卖家端「进行中」都以它为准,只改库存会让现货永远匹配不到',
)
})
test('已确认加好友后作废现货:必须关掉匹配开关并留痕', async () => {
const source = await readServiceSource()
// 解绑 SQL 已抽到 consignment-revoke-inventory-sql.ts(参数个数由该模块自己的单测锁定)