test(回归): 按新裁决更新 SEC-4 边界用例与散客入口契约;路由裸 SQL 移入 service
CI / server typecheck + tests (push) Canceled after 0s
CI / web typecheck + tests + miniprogram syntax (push) Canceled after 0s
CI / eslint (观察项,C7 骨架) (push) Canceled after 0s
CI / npm audit (critical 硬拦) (push) Canceled after 0s

This commit is contained in:
Ubuntu
2026-10-19 17:44:46 +08:00
parent 35213fd2a0
commit 8a08686e1a
5 changed files with 65 additions and 30 deletions
@@ -126,27 +126,19 @@ router.put('/quote/sheets/:code', authMiddleware, async (req: AuthRequest, res:
/** 本单当前挂的客户(编辑时必须原样回传,保证"本单客户不变") */
let ownCustomerCodes: string[] | undefined;
if (rawCustomerCodes && rawCustomerCodes.length) {
const { pool } = await import('../../../db');
const current = await pool.query<{ customer_code: string }>(
`SELECT customer_code FROM customer_quote_sheet_customers WHERE quote_sheet_code = $1`,
[req.params.code]
);
const currentCodes = new Set(current.rows.map((r) => String(r.customer_code)));
ownCustomerCodes = Array.from(currentCodes);
const extra = rawCustomerCodes.filter((c) => !currentCodes.has(c));
if (extra.length) {
const taken = await pool.query<{ customer_code: string }>(
`SELECT DISTINCT customer_code FROM customer_quote_sheet_customers WHERE customer_code = ANY($1::text[])`,
[extra]
);
if ((taken.rowCount || 0) > 0) {
return res.status(400).json({
error: 'customer_already_quoted',
error_description: `以下客户名下已有报价单,不能追加:${taken.rows.map((r) => r.customer_code).join('、')}`,
});
}
customersToCreate = extra;
const { resolveEditCustomerSplit } = await import('../../../services/quoteSheetService');
const split = await resolveEditCustomerSplit({
quoteSheetCode: String(req.params.code || ''),
requestedCustomerCodes: rawCustomerCodes,
});
ownCustomerCodes = split.ownCustomerCodes;
if (split.takenCustomerCodes.length) {
return res.status(400).json({
error: 'customer_already_quoted',
error_description: `以下客户名下已有报价单,不能追加:${split.takenCustomerCodes.join('、')}`,
});
}
customersToCreate = split.customersToCreate;
}
/**
* ⚠️ 只有"本单自己的客户"能传给编辑服务:
+1 -1
View File
@@ -48,7 +48,7 @@ async function assertCanEditRolePermissions(userId: string, roleCode: string): P
async function assertSuperAdmin(userId: string): Promise<void> {
const { isSuperAdmin } = await getRoleManagerContext(userId);
if (!isSuperAdmin) {
throw Object.assign(new Error('仅超级管理员可管理角色定义'), {
throw Object.assign(new Error('仅超级管理员可管理角色与角色权限'), {
status: 403,
code: 'forbidden',
});
+31
View File
@@ -164,6 +164,37 @@ async function loadSheetDetail(quoteSheetCode: string) {
}
/** 创建报价单(多商品 + 多/单客户) */
/**
* 编辑阶段「本单客户不变 + 追加客户另建单」的判定(2026-10-19)。
* 把查询放在 service(路由层禁止裸 SQL,见 routeLayerBoundary 契约测试)。
*/
export async function resolveEditCustomerSplit(input: {
quoteSheetCode: string;
requestedCustomerCodes: string[];
}): Promise<{ ownCustomerCodes: string[]; customersToCreate: string[]; takenCustomerCodes: string[] }> {
const code = String(input.quoteSheetCode || '').trim();
const requested = Array.from(new Set(input.requestedCustomerCodes.map((c) => String(c || '').trim()).filter(Boolean)));
const current = await pool.query<{ customer_code: string }>(
`SELECT customer_code FROM customer_quote_sheet_customers WHERE quote_sheet_code = $1`,
[code]
);
const ownCustomerCodes = current.rows.map((r) => String(r.customer_code));
const ownSet = new Set(ownCustomerCodes);
const extra = requested.filter((c) => !ownSet.has(c));
if (!extra.length) return { ownCustomerCodes, customersToCreate: [], takenCustomerCodes: [] };
const taken = await pool.query<{ customer_code: string }>(
`SELECT DISTINCT customer_code FROM customer_quote_sheet_customers WHERE customer_code = ANY($1::text[])`,
[extra]
);
const takenCustomerCodes = taken.rows.map((r) => String(r.customer_code));
const takenSet = new Set(takenCustomerCodes);
return {
ownCustomerCodes,
customersToCreate: extra.filter((c) => !takenSet.has(c)),
takenCustomerCodes,
};
}
export async function createQuoteSheet(input: {
title?: string;
customerCodes: string[];
+9 -2
View File
@@ -177,20 +177,27 @@ describe('T3/SEC-4:非 super_admin 写全局角色 ⇒ 403 且零写/零连接
expect(post.status).toBe(403);
expect(post.body.error).toBe('forbidden');
// 2026-10-19 裁决:角色权限由**管理员**维护,但管理员不能改 admin / super_admin 两个角色
const put = await request(rolesApp)
.put('/api/roles/admin/permissions')
.send({ permission_ids: ['perm-1'] });
expect(put.status).toBe(403);
expect(String(put.body.error_description)).toContain('仅超级管理员');
expect(String(put.body.error_description)).toContain('不能修改');
expect(H.connect, '403 必须在取连接之前').not.toHaveBeenCalled();
expect(writeSqls(), 'admin 403 时不得有任何 role_permissions 写 SQL').toEqual([]);
// 普通角色(operator)管理员可以改
const putOk = await request(rolesApp)
.put('/api/roles/operator/permissions')
.send({ permission_ids: ['perm-1'] });
expect(putOk.status).toBe(200);
const del = await request(rolesApp).delete('/api/roles/operator');
expect(del.status).toBe(403);
expect(H.createRole).not.toHaveBeenCalled();
expect(H.deleteRole).not.toHaveBeenCalled();
expect(H.clientSql).toEqual([]);
// 意图是"403 时零写 SQL";不要求 clientSql 全空(读角色上下文的 SELECT 不算写)
expect(writeSqls()).toEqual([]);
});
it('无管理员角色的普通账号(operator)同样 403', async () => {
@@ -13,15 +13,20 @@ import { describe, expect, it } from 'vitest';
const page = readFileSync(new URL('./CustomerOrder.tsx', import.meta.url), 'utf8');
describe('散客录单 · 录单页入口(防再丢)', () => {
it('入口开关存在:有一个控件调用 setRetailMode 打开散客模式', () => {
const decl = page.match(/const \[retailMode, setRetailMode\] = useState\(false\);/g) || [];
expect(decl).toHaveLength(1);
// 至少一处 onChange 里调用 setRetailMode(入口控件)
expect(page).toMatch(/onChange=\{\(e\) => setRetailMode\(e\.target\.checked\)\}/);
/**
* 2026-10-19 开发者裁决:**散客不再是独立勾选框**,它是客户下拉里的一个客户(编码 __RETAIL__)。
* 入口仍必须存在且能进散客模式,否则会重演"全仓无入口"的事故。
*/
it('散客入口存在:客户下拉内置散客客户(__RETAIL__),且不再用勾选框状态', () => {
expect(page).toContain("customer_code: '__RETAIL__'");
expect(page).toContain('RETAIL_CUSTOMER_OPTION');
// 勾选框状态必须已移除
expect(page).not.toMatch(/const \[retailMode, setRetailMode\] = useState/);
// retailMode 由所选客户派生
expect(page).toMatch(/const retailMode = selectedCustomer\?\.customer_code === RETAIL_CUSTOMER_OPTION\.customer_code/);
});
it('入口渲染了「散客录单」文案与提示(三语 key 已在 locale 中就位)', () => {
expect(page).toContain("t('orders.retailMode')");
it('选中散客时给出「只扣库存」提示(三语 key 已在 locale 中就位)', () => {
expect(page).toContain("t('orders.retailModeHint')");
});