开发者 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)。
100 lines
4.1 KiB
TypeScript
100 lines
4.1 KiB
TypeScript
import { pool } from '../db';
|
|
import { loadTenantContext } from './tenantScope';
|
|
import { isIndependentInboundCustomer } from './independentInbound';
|
|
|
|
export type OrderEntryScope = {
|
|
branchCode: string | null;
|
|
isSuperAdmin: boolean;
|
|
/** 归属判定(2026-10-19 录单员仅见自己录的单):这里直接复用 TenantContext 的原值 */
|
|
userId: string;
|
|
roles: string[];
|
|
};
|
|
|
|
export async function loadOrderEntryScope(userId: string): Promise<OrderEntryScope> {
|
|
const ctx = await loadTenantContext(userId);
|
|
if (!ctx) {
|
|
throw Object.assign(new Error('请先登录'), { status: 401, code: 'invalid_token' });
|
|
}
|
|
return {
|
|
branchCode: ctx.branchCode,
|
|
isSuperAdmin: ctx.isSuperAdmin,
|
|
userId: ctx.userId,
|
|
roles: ctx.roles,
|
|
};
|
|
}
|
|
|
|
export async function assertOrderEntryCustomerAccess(customerCode: string, scope: OrderEntryScope) {
|
|
/**
|
|
* 「独立进货」哨兵客户(2026-10-12 开发者立项):它不是 `customers` 里的真实客户,
|
|
* 而是录单处一个内建选项(用于从厂商进货、入库即完成)。这里显式放行,
|
|
* 但**仍要求录单员已绑定分公司**(订单必须落到某个分公司)。
|
|
*/
|
|
if (isIndependentInboundCustomer(customerCode)) {
|
|
if (!scope.branchCode && !scope.isSuperAdmin) {
|
|
throw Object.assign(new Error('录单员未绑定分公司,请联系管理员'), { status: 403, code: 'invalid_branch' });
|
|
}
|
|
return;
|
|
}
|
|
|
|
const result = await pool.query(`SELECT branch_code FROM customers WHERE customer_code = $1`, [customerCode]);
|
|
if (!result.rowCount) {
|
|
throw Object.assign(new Error('客户不存在'), { status: 404, code: 'not_found' });
|
|
}
|
|
const customerBranch = result.rows[0]?.branch_code as string | null;
|
|
|
|
if (scope.isSuperAdmin && !scope.branchCode) {
|
|
return;
|
|
}
|
|
|
|
if (!scope.branchCode) {
|
|
throw Object.assign(new Error('录单员未绑定分公司,请联系管理员'), { status: 403, code: 'invalid_branch' });
|
|
}
|
|
|
|
if (customerBranch && customerBranch !== scope.branchCode) {
|
|
throw Object.assign(new Error('无权为该分公司外的客户录单'), { status: 403, code: 'forbidden' });
|
|
}
|
|
}
|
|
|
|
export async function resolveOrderBranchForEntry(scope: OrderEntryScope, customerCode: string): Promise<string> {
|
|
// 独立进货:哨兵不在 customers 里 ⇒ 直接落到录单员自己的分公司
|
|
if (isIndependentInboundCustomer(customerCode)) {
|
|
if (scope.branchCode) return scope.branchCode;
|
|
throw Object.assign(new Error('无法确定订单所属分公司'), { status: 400, code: 'invalid_branch' });
|
|
}
|
|
const result = await pool.query(`SELECT branch_code FROM customers WHERE customer_code = $1`, [customerCode]);
|
|
const customerBranch = result.rows[0]?.branch_code as string | null;
|
|
if (customerBranch) return customerBranch;
|
|
if (scope.branchCode) return scope.branchCode;
|
|
throw Object.assign(new Error('无法确定订单所属分公司'), { status: 400, code: 'invalid_branch' });
|
|
}
|
|
|
|
/** 语音/图片解析时使用客户所属分公司,避免管理员账号 profile 无 branch_code 时解析失败 */
|
|
export async function resolveParseBranchCode(scope: OrderEntryScope, customerCode: string): Promise<string | null> {
|
|
const result = await pool.query(`SELECT branch_code FROM customers WHERE customer_code = $1`, [customerCode]);
|
|
const customerBranch = result.rows[0]?.branch_code as string | null;
|
|
return customerBranch || scope.branchCode;
|
|
}
|
|
|
|
export async function listOrderEntryCustomers(scope: OrderEntryScope) {
|
|
const params: string[] = [];
|
|
let branchSql = '';
|
|
|
|
if (scope.branchCode) {
|
|
params.push(scope.branchCode);
|
|
branchSql = ` AND branch_code = $${params.length}`;
|
|
} else if (!scope.isSuperAdmin) {
|
|
throw Object.assign(new Error('录单员未绑定分公司,请联系管理员'), { status: 403, code: 'invalid_branch' });
|
|
}
|
|
|
|
const result = await pool.query(
|
|
`SELECT customer_code, company_name, contact_phone, address, branch_code, delivery_time
|
|
FROM customers
|
|
WHERE status IN (10, 20)
|
|
${branchSql}
|
|
ORDER BY company_name ASC, created_at DESC
|
|
LIMIT 200`,
|
|
params
|
|
);
|
|
return result.rows;
|
|
}
|