fix(consignment): 寄售提报强制固化分类,修复空分类库存后台不可见

This commit is contained in:
2026-11-16 15:16:54 +08:00
parent 866f0d37fd
commit 748d45dace
4 changed files with 339 additions and 6 deletions
@@ -848,6 +848,14 @@ export async function approveAdminConsignmentSubmission(
errorCode: 'submission_already_reviewed',
})
}
// 分类/配置缺失的提报单不允许入库:库存一旦落 NULL,后台按分类永远查不到。
// 发布端已强制要求 profileId,这里是旧数据/旧客户端的最后一道防线。
if (!Number(submission.category_id) || !Number(submission.profile_id)) {
throw createHttpError('该提报单缺少寄卖分类,无法入库;请让打手在最新页面重新提报', {
statusCode: 409,
errorCode: 'consignment_category_required',
})
}
const now = nowIso()
const inventoryNo = generateConsignmentInventoryNo()
@@ -0,0 +1,43 @@
import assert from 'node:assert/strict'
import { readFile } from 'node:fs/promises'
import test from 'node:test'
async function readSource(fileName: string) {
return readFile(new URL(fileName, import.meta.url), 'utf8')
}
test('打手发布寄卖必须带已发布且挂有效分类的配置', async () => {
const source = await readSource('./consignment-worker-service.ts')
// 旧版页面不带 profileId:必须直接拒绝,不能落 category_id = NULL 的库存
assert.ok(
source.includes("errorCode: 'consignment_profile_required'"),
'缺少 profileId 时必须拒绝旧版页面提交',
)
assert.ok(source.includes('if (!profile) {'), '必须校验配置存在且已发布')
assert.ok(source.includes('if (!Number(profile.categoryId)) {'), '必须校验配置挂在有效分类下')
assert.ok(
source.includes("errorCode: 'consignment_category_required'"),
'配置缺少分类时必须有明确错误码',
)
})
test('审核入库必须拒绝缺少分类或配置的提报单', async () => {
const source = await readSource('./consignment-admin-service.ts')
assert.ok(
source.includes('!Number(submission.category_id) || !Number(submission.profile_id)'),
'审核入库前必须同时校验分类与配置',
)
assert.ok(
source.includes("errorCode: 'consignment_category_required'"),
'缺少分类时必须返回明确错误码',
)
// 校验必须早于入库写入,不能等 approveConsignmentSubmissionWithInbound 插完再报错
const guardIndex = source.indexOf(
'!Number(submission.category_id) || !Number(submission.profile_id)',
)
const inboundIndex = source.indexOf('approveConsignmentSubmissionWithInbound({')
assert.ok(guardIndex > 0 && inboundIndex > guardIndex, '分类校验必须早于入库写入')
})
@@ -343,18 +343,28 @@ export async function publishWorkerConsignment(workerId: number, payload: JsonOb
})
}
// 分类级寄卖配置:新链路必须带 profileId;存量调用不带则沿用旧逻辑。
// 分类级寄卖配置:发布必须带 profileId,且配置必须挂在有效分类下。
// 旧版页面不带 profileId 时不再落空分类(否则库存按分类永远查不到),直接要求刷新页面。
const profileIdInput = Number(payload.profileId ?? payload.profile_id ?? 0)
const profile =
Number.isInteger(profileIdInput) && profileIdInput > 0
? await getPublishedConsignmentProfileById(profileIdInput)
: null
if (Number.isInteger(profileIdInput) && profileIdInput > 0 && !profile) {
if (!Number.isInteger(profileIdInput) || profileIdInput <= 0) {
throw createHttpError('寄卖页面版本过旧,请刷新页面后重试', {
statusCode: 409,
errorCode: 'consignment_profile_required',
})
}
const profile = await getPublishedConsignmentProfileById(profileIdInput)
if (!profile) {
throw createHttpError('寄卖配置不存在或未发布', {
statusCode: 400,
errorCode: 'consignment_profile_unavailable',
})
}
if (!Number(profile.categoryId)) {
throw createHttpError('寄卖配置缺少分类,请联系管理员检查配置', {
statusCode: 409,
errorCode: 'consignment_category_required',
})
}
// 「允许修改寄售数量」关闭时固定 1 件:前端会禁用输入框,这里再兜一层防绕过。
const quantity = resolveConsignmentPublishQuantity(
profile?.config.fields.quantity !== false,