fix(consignment): 修复内部匹配绑定与留痕并顺延迁移
This commit is contained in:
@@ -25,6 +25,10 @@ export async function reserveConsignmentInventoryWithClient(
|
||||
quantity?: number
|
||||
unitPriceFen: number
|
||||
now: string
|
||||
/** 占用后用于卖家端关联工单的绑定信息(内部匹配/自动匹配都要写) */
|
||||
boundOrderId?: number | null
|
||||
boundPlatformOrderId?: string
|
||||
shopName?: string
|
||||
},
|
||||
): Promise<ConsignmentAllocationRow | null> {
|
||||
const quantity = Math.max(1, Math.round(Number(input.quantity || 1)))
|
||||
@@ -40,6 +44,10 @@ export async function reserveConsignmentInventoryWithClient(
|
||||
remaining_quantity = remaining_quantity - $1,
|
||||
status = CASE WHEN remaining_quantity - $1 <= 0 THEN 'consumed' ELSE status END,
|
||||
allocation_status = '已安排',
|
||||
bound_order_id = $4,
|
||||
bound_work_order_id = $5,
|
||||
bound_platform_order_id = $6,
|
||||
shop_name = $7,
|
||||
consumed_at = CASE WHEN remaining_quantity - $1 <= 0 THEN $2 ELSE consumed_at END,
|
||||
updated_at = $2
|
||||
WHERE id = $3
|
||||
@@ -48,7 +56,15 @@ export async function reserveConsignmentInventoryWithClient(
|
||||
AND remaining_quantity >= $1
|
||||
RETURNING id, remaining_quantity, status, allow_matching
|
||||
`,
|
||||
[quantity, input.now, input.inventoryId],
|
||||
[
|
||||
quantity,
|
||||
input.now,
|
||||
input.inventoryId,
|
||||
input.boundOrderId ?? null,
|
||||
input.workOrderId,
|
||||
String(input.boundPlatformOrderId || '').trim(),
|
||||
String(input.shopName || '').trim(),
|
||||
],
|
||||
)
|
||||
if (!inventory.rows[0]) return null
|
||||
|
||||
@@ -96,6 +112,29 @@ async function releaseAllocationRows(
|
||||
`UPDATE consignment_inventory_allocations SET status = 'released', updated_at = $1 WHERE id = $2`,
|
||||
[now, row.id],
|
||||
)
|
||||
// 没有其它在途占用了才清空卖家端绑定,避免部分释放时把仍在履约的工单抹掉。
|
||||
const active = await client.query<{ count: number }>(
|
||||
`SELECT COUNT(*)::integer AS count
|
||||
FROM consignment_inventory_allocations
|
||||
WHERE inventory_id = $1 AND status IN ('reserved', 'consumed')`,
|
||||
[row.inventory_id],
|
||||
)
|
||||
if (Number(active.rows[0]?.count || 0) === 0) {
|
||||
await client.query(
|
||||
`
|
||||
UPDATE consignment_inventories
|
||||
SET
|
||||
bound_order_id = NULL,
|
||||
bound_work_order_id = NULL,
|
||||
bound_platform_order_id = '',
|
||||
shop_name = '',
|
||||
allocation_status = CASE WHEN status = 'in_stock' THEN '待安排' ELSE allocation_status END,
|
||||
updated_at = $1
|
||||
WHERE id = $2
|
||||
`,
|
||||
[now, row.inventory_id],
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -4,8 +4,18 @@ import test from 'node:test'
|
||||
|
||||
import { deriveConsignmentItemStage } from '../../domain/consignment-item-stage.js'
|
||||
|
||||
test('寄卖仓库按关键字搜索必须能搜到"寄卖单号"(SUB…),且计数查询带上同一个 join', async () => {
|
||||
/**
|
||||
* 读取仓库源码并统一换行为 LF。
|
||||
* 仓库在 Windows 上检出为 CRLF,若直接用 `\n}\n` 定位函数结尾会匹配失败,
|
||||
* slice(start, -1) 会把整份文件都包进来,断言就失去意义(曾因此漏掉真实的 SELECT *)。
|
||||
*/
|
||||
async function readRepoSource(): Promise<string> {
|
||||
const source = await readFile(new URL('./consignment-repo.ts', import.meta.url), 'utf8')
|
||||
return source.replace(/\r\n/g, '\n')
|
||||
}
|
||||
|
||||
test('寄卖仓库按关键字搜索必须能搜到"寄卖单号"(SUB…),且计数查询带上同一个 join', async () => {
|
||||
const source = await readRepoSource()
|
||||
// 卖家端与客服端现在只把提报单号(SUB…)当主标识展示,搜不到它客服等于瞎了
|
||||
assert.ok(source.includes('OR cs.submission_no ILIKE $${values.length}'))
|
||||
// 条件里引用了 cs.submission_no,计数 SQL 就必须同样 join,否则 COUNT 直接 500
|
||||
@@ -22,7 +32,7 @@ test('寄卖仓库按关键字搜索必须能搜到"寄卖单号"(SUB…),
|
||||
})
|
||||
|
||||
test('寄卖仓库搜索必须覆盖供货打手姓名、打手电话与页面展示的卖家电话', async () => {
|
||||
const source = await readFile(new URL('./consignment-repo.ts', import.meta.url), 'utf8')
|
||||
const source = await readRepoSource()
|
||||
const inventoryListStart = source.indexOf('export async function listConsignmentInventories')
|
||||
const inventoryListBody = source.slice(
|
||||
inventoryListStart,
|
||||
@@ -55,7 +65,7 @@ test('寄卖仓库搜索必须覆盖供货打手姓名、打手电话与页面
|
||||
})
|
||||
|
||||
test('寄卖仓库按通过审核入库时间筛选,结束日期必须包含当天全天', async () => {
|
||||
const source = await readFile(new URL('./consignment-repo.ts', import.meta.url), 'utf8')
|
||||
const source = await readRepoSource()
|
||||
const inventoryListStart = source.indexOf('export async function listConsignmentInventories')
|
||||
const inventoryListBody = source.slice(
|
||||
inventoryListStart,
|
||||
@@ -77,7 +87,7 @@ test('寄卖仓库按通过审核入库时间筛选,结束日期必须包含
|
||||
})
|
||||
|
||||
test('匹配候选要带出寄卖单号,并且 join 之后所有列名都必须加 ci. 前缀', async () => {
|
||||
const source = await readFile(new URL('./consignment-repo.ts', import.meta.url), 'utf8')
|
||||
const source = await readRepoSource()
|
||||
const start = source.indexOf('export async function listActiveConsignmentInventoriesForMatching')
|
||||
assert.ok(start > 0, '未找到匹配候选查询')
|
||||
const body = source.slice(start, source.indexOf('\n}\n', start))
|
||||
@@ -87,16 +97,19 @@ test('匹配候选要带出寄卖单号,并且 join 之后所有列名都必
|
||||
assert.ok(body.includes('LEFT JOIN consignment_submissions cs'))
|
||||
// consignment_submissions 也有 status/game_name/category_id:不加前缀会 ambiguous column 直接 500
|
||||
assert.ok(
|
||||
!/WHERE[^`]*[^.]\bstatus = /.test(body.replace(/ci\.status/g, 'X')),
|
||||
'存在未加前缀的 status',
|
||||
body.includes("ci.status = 'in_stock'"),
|
||||
'status 条件必须带 ci. 前缀,避免 join 后 ambiguous column',
|
||||
)
|
||||
assert.ok(
|
||||
!/(?<!ci\.)\bstatus = 'in_stock'/.test(body),
|
||||
'出现未加 ci. 前缀的 status 条件,join 后必然 ambiguous column',
|
||||
)
|
||||
assert.ok(body.includes("ci.status = 'in_stock'"))
|
||||
assert.ok(body.includes('ci.allow_matching = TRUE'))
|
||||
assert.ok(!body.includes('SELECT *'), 'SELECT * 会把两表同名列搞乱,必须显式列出')
|
||||
assert.ok(!/SELECT\s+\*/.test(body), 'SELECT * 会把两表同名列搞乱,必须显式列出(允许 ci.*)')
|
||||
})
|
||||
|
||||
test('占库写入的 allocation_status 默认值必须在 080 CHECK 枚举(待安排/已安排)内', async () => {
|
||||
const source = await readFile(new URL('./consignment-repo.ts', import.meta.url), 'utf8')
|
||||
const source = await readRepoSource()
|
||||
const start = source.indexOf('export async function claimConsignmentInventoryForWorkerWithClient')
|
||||
assert.ok(start > 0, '未找到打手指派占库函数')
|
||||
const body = source.slice(start, source.indexOf('\n}\n', start))
|
||||
@@ -120,7 +133,7 @@ function readWorkerPageQuery(source: string) {
|
||||
}
|
||||
|
||||
test('我的寄卖:关键字必须覆盖卖家找单的口径(单号/规格/区服/钻级/角色/买家)', async () => {
|
||||
const source = await readFile(new URL('./consignment-repo.ts', import.meta.url), 'utf8')
|
||||
const source = await readRepoSource()
|
||||
const body = readWorkerPageQuery(source)
|
||||
|
||||
for (const field of [
|
||||
@@ -140,13 +153,14 @@ test('我的寄卖:关键字必须覆盖卖家找单的口径(单号/规格/
|
||||
}
|
||||
|
||||
// 兜底行(没有归属提报单的库存)里不能引用提报单别名,否则真库直接 42703 报错
|
||||
const orphanStart = body.indexOf('NULL AS submission_id')
|
||||
const orphanBody = body.slice(orphanStart, body.indexOf(')\n `', orphanStart))
|
||||
const orphanStart = body.indexOf('orphanKeywordFilter = `')
|
||||
const orphanEnd = body.indexOf('`', orphanStart + 'orphanKeywordFilter = `'.length)
|
||||
const orphanBody = body.slice(orphanStart, orphanEnd)
|
||||
assert.ok(!orphanBody.includes('cs.'), '兜底库存行的关键字条件引用了提报单别名 cs.')
|
||||
})
|
||||
|
||||
test('我的寄卖:分页与分组过滤必须走 SQL,不能再把全量记录捞回前端', async () => {
|
||||
const source = await readFile(new URL('./consignment-repo.ts', import.meta.url), 'utf8')
|
||||
const source = await readRepoSource()
|
||||
const body = readWorkerPageQuery(source)
|
||||
|
||||
assert.ok(body.includes('ORDER BY sort_at DESC'), '排序必须在 SQL 完成')
|
||||
@@ -158,7 +172,7 @@ test('我的寄卖:分页与分组过滤必须走 SQL,不能再把全量记
|
||||
})
|
||||
|
||||
test('我的寄卖:SQL 里的分组口径与状态机的 group 映射保持一致', async () => {
|
||||
const source = await readFile(new URL('./consignment-repo.ts', import.meta.url), 'utf8')
|
||||
const source = await readRepoSource()
|
||||
const body = readWorkerPageQuery(source)
|
||||
|
||||
// 真源是 domain/consignment-item-stage.ts 的 STAGE_META.group:
|
||||
|
||||
Reference in New Issue
Block a user