Files
SupplierSystem/server/tests/mainSystemCustomerBinding.test.ts
T
Ubuntu 9e58332cc6 feat(客户): 绑定即建客户、解绑即删(有订单则停用),修复报价单选客户有时无
用户要求:供应商编码被某配送商分公司绑定后,该分公司客户应立刻出现在客户列表,不等派单;
解绑后消失。
- 根因:MAIN_<主系统分公司> 客户此前只在订单导入时创建;且 /api/data 客户缓存未失效 → 时有时无
- 新增 mainSystemCustomerBindingService:绑定建客户(status 10、缓存失效)、解绑删除;
  有历史订单删不掉 ⇒ 停用 status=40(保住历史归属);重新绑定恢复启用
- 接入 upsertBinding / unbindBinding(含改绑时清理旧分公司客户);通配 * 跳过
2026-10-18 20:59:23 +08:00

113 lines
4.0 KiB
TypeScript

import { beforeEach, describe, expect, it, vi } from 'vitest';
/**
* 绑定即建客户 / 解绑即删客户(2026-10-18 用户要求)
*
* 用户原话:「供应商的编码被哪一个配送商分公司绑定了,那个配送商分公司应该自动出现在供应商的
* 客户列表里面,而不是等着派单过来才出现;解绑了就消失」。
*/
const h = vi.hoisted(() => ({
ensureCalls: [] as unknown[],
deleteCalls: [] as string[],
deleteImpl: null as null | (() => Promise<unknown>),
updateCalls: [] as unknown[],
}));
vi.mock('../src/services/tenantDbContext', () => ({
runWithTenantDbContext: async (_branch: string, fn: () => unknown) => fn(),
}));
vi.mock('../src/services/mainSystemOrderImport', () => ({
ensureMainBranchCustomerForBinding: async (input: unknown) => {
h.ensureCalls.push(input);
return 'MAIN_BR002';
},
}));
vi.mock('../src/services/customerDeleteService', () => ({
deleteCustomer: async (code: string) => {
h.deleteCalls.push(code);
if (h.deleteImpl) return h.deleteImpl();
return { customer_code: code };
},
}));
vi.mock('../src/db', async (importOriginal) => {
const actual = await importOriginal<typeof import('../src/db')>();
return {
...actual,
pool: {
query: async (text: string, params: unknown[]) => {
h.updateCalls.push({ text, params });
return { rows: [], rowCount: 1 };
},
},
};
});
import {
ensureCustomerForBinding,
mainBranchCustomerCode,
removeCustomerForBinding,
} from '../src/services/mainSystemCustomerBindingService';
beforeEach(() => {
h.ensureCalls = [];
h.deleteCalls = [];
h.deleteImpl = null;
h.updateCalls = [];
});
describe('绑定即建客户', () => {
it('主系统分公司 BR002 ⇒ 建/取 MAIN_BR002(不再等订单)', async () => {
const res = await ensureCustomerForBinding({ branchCode: 'BR001', mainBranchCode: 'br002' });
expect(res).toMatchObject({ ok: true, action: 'created_or_kept', customer_code: 'MAIN_BR002' });
expect(h.ensureCalls).toHaveLength(1);
expect((h.ensureCalls[0] as any).mainBranchCode).toBe('BR002');
expect((h.ensureCalls[0] as any).branchCode).toBe('BR001');
});
it('通配绑定 * 或空 ⇒ 跳过(无法对应具体分公司)', async () => {
expect(await ensureCustomerForBinding({ branchCode: 'BR001', mainBranchCode: '*' })).toMatchObject({
action: 'skipped',
});
expect(await ensureCustomerForBinding({ branchCode: 'BR001', mainBranchCode: '' })).toMatchObject({
action: 'skipped',
});
expect(h.ensureCalls).toHaveLength(0);
});
it('客户编码口径:MAIN_<大写分公司码>', () => {
expect(mainBranchCustomerCode('br002')).toBe('MAIN_BR002');
expect(mainBranchCustomerCode('*')).toBeNull();
});
});
describe('解绑即删客户', () => {
it('无引用 ⇒ 硬删', async () => {
const res = await removeCustomerForBinding({ branchCode: 'BR001', mainBranchCode: 'BR002' });
expect(res).toMatchObject({ ok: true, action: 'deleted', customer_code: 'MAIN_BR002' });
expect(h.deleteCalls).toEqual(['MAIN_BR002']);
expect(h.updateCalls).toHaveLength(0);
});
it('有历史订单(删除守卫 400)⇒ 退化为停用 status=40,不丢历史归属', async () => {
h.deleteImpl = async () => {
throw Object.assign(new Error('该客户存在历史订单,无法删除'), { status: 400 });
};
const res = await removeCustomerForBinding({ branchCode: 'BR001', mainBranchCode: 'BR002' });
expect(res).toMatchObject({ ok: true, action: 'disabled', customer_code: 'MAIN_BR002' });
expect(h.updateCalls).toHaveLength(1);
expect(String((h.updateCalls[0] as any).text)).toContain('status = 40');
});
it('客户本来就不存在(404)⇒ 幂等成功', async () => {
h.deleteImpl = async () => {
throw Object.assign(new Error('客户不存在'), { status: 404 });
};
const res = await removeCustomerForBinding({ branchCode: 'BR001', mainBranchCode: 'BR002' });
expect(res).toMatchObject({ ok: true, action: 'deleted' });
});
});