Files
SupplierSystem/server/src/services/purchaseAssignDefaultConfigService.ts
T
Ubuntu deb3e72750
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
chore: 以 AI 生鲜配送系统 当前版本作为 SupplierSystem 起点
来源仓库:jxc/ai-fresh-delivery
来源提交:f0b1a180e2deb7e4673b9d1c8542619f7bc0f73d
快照树:  09f679334ff38f0ff25e907961c94a8223bcaedd(与来源 HEAD 工作树逐字节一致,3801 个文件)

按约定本仓库不带任何历史提交:来源仓库的历史中含真实凭据
(生产 SSH 私钥 deploy/id_rsa_tencent、server/.env、.env.tencent/.env.production/.env.gitea、
deploy/小程序AppSecret.txt,其中 MP_APPSECRET / S3 AccessKey 经哈希比对与线上同值),
整包推送会把这些凭据永久带入本仓库,故只取「当前版本」的单提交快照。
供应商系统后续开发自本提交开始。
2026-10-08 17:35:43 +08:00

93 lines
3.3 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import { pool } from '../db';
import type { Pool, PoolClient } from 'pg';
import type { PurchaserAssignMode } from './purchasePurchaserAssignService';
export type PurchaseAssignDefaultConfig = {
/** null = 未配置,建单回退主采购员 */
mode: PurchaserAssignMode | null;
purchaser_code: string;
};
type Db = Pool | PoolClient;
const CONFIG_KEY = 'purchase_assign_default';
const CONFIG_GROUP = 'purchase';
const CONFIG_DESCRIPTION =
'自采默认分配:建单时挂采购员。mode=by_sku|by_category|to_purchaser|free_pool;to_purchaser 需 purchaser_code';
const MODES = new Set<PurchaserAssignMode>(['by_sku', 'by_category', 'to_purchaser', 'free_pool']);
export function parsePurchaseAssignDefaultConfig(raw: unknown): PurchaseAssignDefaultConfig {
if (raw == null || typeof raw !== 'object') {
return { mode: null, purchaser_code: '' };
}
const obj = raw as Record<string, unknown>;
const modeRaw = String(obj.mode || '').trim() as PurchaserAssignMode;
if (!MODES.has(modeRaw)) {
return { mode: null, purchaser_code: '' };
}
return {
mode: modeRaw,
purchaser_code: String(obj.purchaser_code || '').trim(),
};
}
export async function getPurchaseAssignDefaultConfig(
branchCode?: string | null,
db: Db = pool
): Promise<PurchaseAssignDefaultConfig> {
const code = String(branchCode || '').trim();
if (!code) return { mode: null, purchaser_code: '' };
const branchRow = await db.query(
`SELECT config_value FROM system_configs
WHERE config_key = $1 AND branch_code = $2
LIMIT 1`,
[CONFIG_KEY, code]
);
if (branchRow.rows[0]?.config_value != null) {
return parsePurchaseAssignDefaultConfig(branchRow.rows[0].config_value);
}
return { mode: null, purchaser_code: '' };
}
export async function setPurchaseAssignDefaultConfig(
branchCode: string,
input: { mode: unknown; purchaser_code?: unknown }
): Promise<PurchaseAssignDefaultConfig> {
const branch = String(branchCode || '').trim();
if (!branch) {
throw Object.assign(new Error('缺少分公司编码'), { status: 400, code: 'invalid_request' });
}
const modeRaw = String(input.mode || '').trim() as PurchaserAssignMode;
if (!MODES.has(modeRaw)) {
throw Object.assign(new Error('无效的分配方式'), { status: 400, code: 'invalid_request' });
}
let purchaser_code = String(input.purchaser_code || '').trim();
if (modeRaw === 'to_purchaser') {
if (!purchaser_code) {
throw Object.assign(new Error('指定采购员时请选择采购员'), {
status: 400,
code: 'invalid_request',
});
}
} else {
purchaser_code = modeRaw === 'free_pool' ? '' : purchaser_code;
}
const next: PurchaseAssignDefaultConfig = { mode: modeRaw, purchaser_code };
const payload = JSON.stringify(next);
const updated = await pool.query(
`UPDATE system_configs
SET config_value = $1::jsonb, updated_at = NOW(), description = $4
WHERE config_key = $2 AND branch_code = $3`,
[payload, CONFIG_KEY, branch, CONFIG_DESCRIPTION]
);
if (!updated.rowCount) {
await pool.query(
`INSERT INTO system_configs (config_key, config_value, config_group, description, branch_code)
VALUES ($1, $2::jsonb, $4, $5, $3)`,
[CONFIG_KEY, payload, branch, CONFIG_GROUP, CONFIG_DESCRIPTION]
);
}
return next;
}