feat(订单范围): 录单员(operator)只能看到/操作自己录的订单
开发者 2026-10-19:录单员只能看自己录的单,看不到其他人订单。 · 唯一口径 orderOwnerScope.ts:isOperatorScoped(非超管、非 admin、含 operator) + assertOperatorOwnOrder(403 order_scope_forbidden)+ assertOrderAccess(分公司边界 + operator 归属, 合并原先 orderWorkflowService / orderDeleteService 两份重复实现); · 三层收口:① 订单列表 SQL(stats/count/list 共用同一 where)② 详情/写入单点 assertOrderAccess(覆盖 Web 25 处 + 小程序 + MCP + 状态推进)③ /api/data 通用网关 (Web 详情/明细/导出走 supabase:orders/order_items 的 select/update/delete 注入可信表达式); · 补上原先没有 assert 的入口:散客确认、分享链接、主系统核货事实、批量动作、批量重新分配(SQL 层收窄)、 删单/恢复/彻底删除、回收站列表、小程序录单/管理端、MCP 订单详情与改单; · 历史 created_by 为空的单:operator 看不到;不做参与过就放行(参与制另立需求); · 未动状态链/分配/财务口径/表结构;未加索引与迁移。 验证:根与 server tsc 0 错;orderOperatorScope 31 条 + 相关 6 文件 67 条全过; 全量后端 556 文件通过、2 条失败为 mainSystemWriteback 既有失败(HEAD 同样失败); 本地真库 CTE + PREPARE/EXPLAIN 验证三视角可见数(operator 1/4、admin 4/4、finance 4/4)。
This commit is contained in:
@@ -15,6 +15,8 @@ export type OrderRow = {
|
||||
branch_code: string | null;
|
||||
total_amount: number | string | null;
|
||||
delivery_date: string | Date | null;
|
||||
/** 录单员归属(2026-10-19:operator 仅见自己录的单) */
|
||||
created_by?: string | null;
|
||||
};
|
||||
|
||||
export interface WorkflowTransitionContext {
|
||||
|
||||
@@ -7,6 +7,7 @@ import {
|
||||
assertOrderCodeAccess,
|
||||
getOrderWorkflowStatus,
|
||||
} from '../../../services/orderWorkflowService';
|
||||
import { isOperatorScoped } from '../../../services/orderOwnerScope';
|
||||
import { pool } from '../../../db';
|
||||
import {
|
||||
deleteOrderWithDependencies,
|
||||
@@ -212,6 +213,8 @@ router.post('/orders/:orderCode/retail-confirm', authMiddleware, async (req: Aut
|
||||
if (!branchCode) {
|
||||
return res.status(400).json({ error: 'operating_branch_required', error_description: '请先选择操作分公司' });
|
||||
}
|
||||
// 录单员仅可确认自己的单(2026-10-19)
|
||||
await assertOrderCodeAccess(String(req.params.orderCode || ''), ctx!);
|
||||
const data = await confirmRetailOrderComplete(String(req.params.orderCode || ''), branchCode);
|
||||
return res.json({ success: true, data });
|
||||
} catch (error: any) {
|
||||
@@ -237,6 +240,8 @@ router.post('/orders/:orderCode/share-link', authMiddleware, async (req: AuthReq
|
||||
if (!branchCode) {
|
||||
return res.status(400).json({ error: 'operating_branch_required', error_description: '请先选择操作分公司' });
|
||||
}
|
||||
// 录单员仅可为自己的单生成分享链接(2026-10-19)
|
||||
await assertOrderCodeAccess(String(req.params.orderCode || ''), ctx!);
|
||||
const data = await createOrReuseOrderShareLink({
|
||||
orderCode: String(req.params.orderCode || ''),
|
||||
branchCode,
|
||||
@@ -260,6 +265,8 @@ router.get('/orders/:orderCode/share-link', authMiddleware, async (req: AuthRequ
|
||||
if (!branchCode) {
|
||||
return res.status(400).json({ error: 'operating_branch_required', error_description: '请先选择操作分公司' });
|
||||
}
|
||||
// 录单员仅可读自己的单的分享状态(2026-10-19)
|
||||
await assertOrderCodeAccess(String(req.params.orderCode || ''), ctx!);
|
||||
const data = await getOrderShareLinkStatus({
|
||||
orderCode: String(req.params.orderCode || ''),
|
||||
branchCode,
|
||||
@@ -282,6 +289,8 @@ router.delete('/orders/:orderCode/share-link', authMiddleware, async (req: AuthR
|
||||
if (!branchCode) {
|
||||
return res.status(400).json({ error: 'operating_branch_required', error_description: '请先选择操作分公司' });
|
||||
}
|
||||
// 录单员仅可停止自己单的分享(2026-10-19)
|
||||
await assertOrderCodeAccess(String(req.params.orderCode || ''), ctx!);
|
||||
const data = await revokeOrderShareLink({
|
||||
orderCode: String(req.params.orderCode || ''),
|
||||
branchCode,
|
||||
@@ -596,6 +605,8 @@ router.get('/orders/:orderCode/main-system-prep-facts', authMiddleware, async (r
|
||||
if (!orderCode) {
|
||||
return res.status(400).json({ error: 'invalid_request', error_description: '缺少订单号' });
|
||||
}
|
||||
// 录单员仅可读自己单的核货事实(2026-10-19)
|
||||
await assertOrderCodeAccess(orderCode, ctx!);
|
||||
const items = await listOrderActualFacts({ branchCode, ourOrderCode: orderCode });
|
||||
return res.json({ data: { items }, error: null });
|
||||
} catch (error: any) {
|
||||
@@ -657,6 +668,8 @@ router.post('/orders/:orderCode/main-system-prep-facts/refresh', authMiddleware,
|
||||
return res.status(400).json({ error: 'invalid_request', error_description: '缺少订单号' });
|
||||
}
|
||||
// 失败不抛:刷新是"锦上添花",详情展示必须照常(用库内已存事实)
|
||||
// 录单员仅可刷新自己单的核货事实(2026-10-19)
|
||||
await assertOrderCodeAccess(orderCode, ctx!);
|
||||
const outcome = await refreshOrderActualFacts({ branchCode, ourOrderCode: orderCode });
|
||||
return res.json({
|
||||
data: { items: outcome.items, refreshed: outcome.refreshed, reason: outcome.reason ?? null },
|
||||
@@ -1025,12 +1038,19 @@ router.post('/orders/reprocess-allocation', authMiddleware, async (req: AuthRequ
|
||||
const requested = Array.isArray(req.body?.statuses)
|
||||
? req.body.statuses.map(Number).filter((status: number) => allowedStatuses.has(status))
|
||||
: [...allowedStatuses];
|
||||
/**
|
||||
* 录单员(operator)批量重新分配只能覆盖**自己录的单**(2026-10-19):
|
||||
* 在 SQL 层就收窄,而不是循环里逐单 assert —— 否则前面的单已产生分配副作用才抛错。
|
||||
*/
|
||||
const reprocessParams: unknown[] = [branchCode, requested];
|
||||
const operatorOwnerSql = isOperatorScoped(ctx!) ? ` AND created_by = $3` : '';
|
||||
if (operatorOwnerSql) reprocessParams.push(ctx!.userId);
|
||||
const orders = await pool.query(
|
||||
`SELECT order_code, customer_code, branch_code
|
||||
FROM orders
|
||||
WHERE branch_code = $1 AND order_status = ANY($2::int[])
|
||||
WHERE branch_code = $1 AND order_status = ANY($2::int[])${operatorOwnerSql}
|
||||
ORDER BY created_at ASC`,
|
||||
[branchCode, requested]
|
||||
reprocessParams
|
||||
);
|
||||
const summary = {
|
||||
ordersProcessed: 0,
|
||||
@@ -1720,9 +1740,14 @@ router.post('/orders/batch-action', authMiddleware, async (req: AuthRequest, res
|
||||
try {
|
||||
const ctx = await getWorkflowCtx(req);
|
||||
requireWorkflowPermission(ctx, ['order_manage']);
|
||||
const orderCodes = Array.isArray(req.body?.order_codes) ? req.body.order_codes.map(String) : [];
|
||||
// 录单员仅可对涉及自己的单做批量动作(2026-10-19):逐单 assert,任一越界即整体拒绝
|
||||
for (const code of orderCodes) {
|
||||
await assertOrderCodeAccess(String(code || '').trim(), ctx!);
|
||||
}
|
||||
const data = await logOrderBatchAction({
|
||||
actionType: req.body?.action_type === 'merge' ? 'merge' : 'split',
|
||||
orderCodes: Array.isArray(req.body?.order_codes) ? req.body.order_codes.map(String) : [],
|
||||
orderCodes,
|
||||
reason: req.body?.reason,
|
||||
branchCode: ctx?.branchCode,
|
||||
createdBy: req.userId,
|
||||
|
||||
Reference in New Issue
Block a user