feat(consignment): 一个寄卖单号锚定全流程 + 统一状态机 + 卖家侧确认加好友/提交赠送截图

问题:一件货身上挂着三套互不相通的状态(提报单审核态、仓库库存态、工单赠送态),
my-items 同时返回两个数组、卖家端分两个列表渲染,同一件货显示两行两个号;
且卖家端只映射了前两套状态,"已出库"之后流程就断了,卖家不知道该干什么。

- 提报单号 SUB… 作为唯一「寄卖单号」从发布锚定到结算(零迁移);
  listWorkerConsignments 改成一件货一条记录,库存/工单信息挂上去,
  客服手工入库的现货(无提报单)才回退用库存号当锚点
- 新增 domain/consignment-item-stage.ts:把三套底层状态推导成卖家看得懂的一个阶段
  (待审核/已通过待匹配/已匹配-待加好友/冷却中/待赠送/已赠送待审核/已完成/驳回/作废/取消)
  + 一句"下一步做什么" + 能力位(谁能确认加好友、谁该传截图),列表筛选与 KPI 也改由它驱动
- 卖家侧新增动作(以前只有 publish + my-items,卖家只能干看):
  POST /worker/consignment/items/:submissionId/{confirm-friend,gift-evidence}
  POST /worker/consignment/inventories/:inventoryId/{confirm-friend,gift-evidence}(手工入库现货)
  复用既有工单链路 confirmWorkOrderFriendAdded / submitWorkerOrderAcceptance,
  只加归属校验(不是本人履约的订单 403),顺带白拿"冷却没结束不许提交凭证""必须带图"
- 客服端用同一个号:库存列表/详情补 sourceSubmissionNo(LEFT JOIN 提报单),
  履约/指派写进工单的寄售信息也带上;关键字搜索支持按寄卖单号搜(计数 SQL 同步补 join)

真实库端到端验证:确认加好友 → gift_phase=friend_countdown / friend_added_at 落库 /
gift_available_at 正好 +72h;冷却期内提交截图被拒(work_order_gift_cooldown_active);
越权操作被拒(consignment_item_not_found);按寄卖单号搜索命中正确。临时数据已清理。
This commit is contained in:
yml2213
2026-09-17 10:57:57 +08:00
parent 99752dce9e
commit 44de11a3a7
19 changed files with 1574 additions and 204 deletions
@@ -0,0 +1,20 @@
import assert from 'node:assert/strict'
import { readFile } from 'node:fs/promises'
import test from 'node:test'
test('寄卖仓库按关键字搜索必须能搜到"寄卖单号"(SUB…),且计数查询带上同一个 join', async () => {
const source = await readFile(new URL('./consignment-repo.ts', import.meta.url), 'utf8')
// 卖家端与客服端现在只把提报单号(SUB…)当主标识展示,搜不到它客服等于瞎了
assert.ok(source.includes('OR cs.submission_no ILIKE $${values.length}'))
// 条件里引用了 cs.submission_no,计数 SQL 就必须同样 join,否则 COUNT 直接 500
const countSqlLine = source
.split('\n')
.find((line) =>
line.includes('SELECT COUNT(*)::integer AS total FROM consignment_inventories ci'),
)
assert.ok(countSqlLine, '未找到计数 SQL')
assert.ok(
countSqlLine!.includes('LEFT JOIN consignment_submissions cs'),
'计数 SQL 缺少提报单 join,会导致按寄卖单号搜索时报 500',
)
})
@@ -33,6 +33,8 @@ export type ConsignmentInventoryRow = {
shop_name: string
source_type: 'manual' | 'worker_portal' | 'recycle'
source_submission_id: string | number | null
/** 唯一锚点:来源提报单号(客服手工入库的现货没有) */
source_submission_no?: string | null
worker_id?: string | number | null
worker_name?: string
worker_phone?: string
@@ -159,12 +161,13 @@ export async function listConsignmentInventories(filters: {
if (filters.keyword) {
values.push(`%${filters.keyword.trim()}%`)
conditions.push(
`(ci.item_name ILIKE $${values.length} OR ci.inventory_no ILIKE $${values.length} OR ci.bound_platform_order_id ILIKE $${values.length} OR ci.seller_contact ILIKE $${values.length} OR ci.seller_game_id ILIKE $${values.length} OR ci.seller_payment_info ILIKE $${values.length} OR ci.remark ILIKE $${values.length})`,
`(ci.item_name ILIKE $${values.length} OR ci.inventory_no ILIKE $${values.length} OR ci.bound_platform_order_id ILIKE $${values.length} OR ci.seller_contact ILIKE $${values.length} OR ci.seller_game_id ILIKE $${values.length} OR ci.seller_payment_info ILIKE $${values.length} OR ci.remark ILIKE $${values.length} OR cs.submission_no ILIKE $${values.length})`,
)
}
const whereClause = conditions.length > 0 ? `WHERE ${conditions.join(' AND ')}` : ''
const countSql = `SELECT COUNT(*)::integer AS total FROM consignment_inventories ci ${whereClause}`
// 计数也要带上提报单 join:关键字现在包含 cs.submission_no(卖家端主标识就是它)
const countSql = `SELECT COUNT(*)::integer AS total FROM consignment_inventories ci LEFT JOIN consignment_submissions cs ON cs.id = ci.source_submission_id ${whereClause}`
const countRes = await query<{ total: number }>(countSql, values)
const total = countRes.rows[0]?.total || 0
@@ -176,9 +179,11 @@ export async function listConsignmentInventories(filters: {
const listSql = `
SELECT
ci.*,
wc.name AS category_name
wc.name AS category_name,
cs.submission_no AS source_submission_no
FROM consignment_inventories ci
LEFT JOIN work_categories wc ON wc.id = ci.category_id
LEFT JOIN consignment_submissions cs ON cs.id = ci.source_submission_id
${whereClause}
ORDER BY ci.id DESC
LIMIT $${values.length - 1} OFFSET $${values.length}
@@ -284,9 +289,11 @@ export async function getConsignmentInventoryById(
const sql = `
SELECT
ci.*,
wc.name AS category_name
wc.name AS category_name,
cs.submission_no AS source_submission_no
FROM consignment_inventories ci
LEFT JOIN work_categories wc ON wc.id = ci.category_id
LEFT JOIN consignment_submissions cs ON cs.id = ci.source_submission_id
WHERE ci.id = $1
LIMIT 1
`
@@ -948,6 +955,33 @@ export async function listConsignmentSubmissions(filters: {
return { items: listRes.rows, total }
}
/**
* 按提报单精确取现货。
*
* 卖家「确认加好友 / 提交赠送截图」必须精确落到自己那件货上,
* 不能用"列表前 N 条里找"——提报件数一多就会漏,操作类接口不能有这个隐患。
*/
export async function getConsignmentInventoryBySubmissionId(
submissionId: number | string,
): Promise<ConsignmentInventoryRow | null> {
const res = await query<ConsignmentInventoryRow>(
`
SELECT
ci.*,
wc.name AS category_name,
cs.submission_no AS source_submission_no
FROM consignment_inventories ci
LEFT JOIN work_categories wc ON wc.id = ci.category_id
LEFT JOIN consignment_submissions cs ON cs.id = ci.source_submission_id
WHERE ci.source_submission_id = $1
ORDER BY ci.id DESC
LIMIT 1
`,
[Number(submissionId)],
)
return res.rows[0] || null
}
export async function getConsignmentSubmissionById(
id: number | string,
): Promise<ConsignmentSubmissionRow | null> {
@@ -678,6 +678,23 @@ export async function getWorkOrderByIdWithClient(
return result.rows[0] || null
}
/**
* 按 id 批量取工单。
*
* 「我的寄卖」列表要把库存绑定的工单状态一次性拼上(阶段推导要用到赠送阶段/冷却时间),
* 一件货查一次太浪费往返。
*/
export async function listWorkOrdersByIds(ids: number[]): Promise<WorkOrderRow[]> {
const normalized = Array.from(
new Set(ids.map((id) => Number(id)).filter((id) => Number.isInteger(id) && id > 0)),
)
if (normalized.length === 0) return []
const result = await query<WorkOrderRow>(`${WORK_ORDER_SELECT} WHERE wo.id = ANY($1::bigint[])`, [
normalized,
])
return result.rows
}
export function buildWorkOrderWhere({
statuses = [],
workOrderId = 0,