Files
SupplierSystem/server/src/services/selfPickupCustomerService.ts
T
Ubuntu 56fe7de7a2
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
feat(客户列表): 主系统绑定客户隐藏「上门取货」开关;后端改该开关返回 409(待取货只由主系统免入库决定)
2026-10-19 20:42:45 +08:00

76 lines
3.3 KiB
TypeScript

import { pool } from '../db';
import { writeAuditLog } from './auditService';
import { runWithTenantDbContext } from './tenantDbContext';
/**
* 客户「是否取货」(自提)开关 —— 2026-10-15 开发者需求。
*
* · 写 `customers.is_self_pickup`;只影响**之后新建**的订单(录单时快照到 `orders.self_pickup`);
* · **仅管理员**(路由层 `requireAdminRole` 把关);
* · ⚠️ **必须走本领域接口**,不得让前端经通用网关(`/api/data`)直写 ——
* `src/lib/gatewayWriteBoundary.test.ts` 明确禁止新增网关写入点(新代码要走 `utils/workflowApi.ts`);
* · 写审计(复用既有 `data_update` 形态:target_table=customers,带改前/改后),操作日志里可追溯。
*/
function businessError(message: string, status: number, code: string): Error & { status: number; code: string } {
return Object.assign(new Error(message), { status, code });
}
export async function setCustomerSelfPickup(input: {
customerCode: string;
enabled: boolean;
branchCode?: string | null;
operatorId?: string | null;
operatorRole?: string | null;
}): Promise<{ customer_code: string; is_self_pickup: boolean; changed: boolean }> {
const customerCode = String(input.customerCode || '').trim();
if (!customerCode) throw businessError('缺少客户编码', 400, 'invalid_request');
const branchCode = String(input.branchCode || '').trim();
if (!branchCode) throw businessError('缺少分公司上下文', 400, 'branch_required');
const enabled = input.enabled === true;
return runWithTenantDbContext(branchCode, async () => {
const before = await pool.query<{ is_self_pickup: boolean | null; source: string | null }>(
`SELECT COALESCE(is_self_pickup, false) AS is_self_pickup, source
FROM customers WHERE customer_code = $1 LIMIT 1`,
[customerCode]
);
if (!before.rowCount) throw businessError('客户不存在', 404, 'not_found');
/**
* 主系统按绑定编码分配过来的客户:其「待取货」只由主系统「免入库」决定,
* 不允许在我方客户列表改这个开关(2026-10-19 开发者口径)。
*/
if (String(before.rows[0]?.source || '') === 'main_system') {
throw businessError('主系统客户的「上门取货」由主系统免入库决定,不能在此修改', 409, 'main_system_customer_locked');
}
const previous = before.rows[0]?.is_self_pickup === true;
if (previous === enabled) {
return { customer_code: customerCode, is_self_pickup: enabled, changed: false };
}
await pool.query(`UPDATE customers SET is_self_pickup = $2 WHERE customer_code = $1`, [
customerCode,
enabled,
]);
try {
await writeAuditLog({
operatorId: input.operatorId || null,
operatorRole: input.operatorRole || null,
operationType: 'data_update',
targetTable: 'customers',
targetId: customerCode,
oldValue: { is_self_pickup: previous },
newValue: { is_self_pickup: enabled },
branchCode,
actionCategory: 'data',
});
} catch (auditError) {
console.error('[self-pickup] 客户取货开关审计写入失败', auditError);
}
return { customer_code: customerCode, is_self_pickup: enabled, changed: true };
});
}