oms/backend/test/app/lib/externalSendTemplate.test.js
liufumingren da468f6cb2 fix(send): 取号记录必须与本单运单号匹配,不再取最近一条
上一版按「最近一次成功取号记录」取快递编码与面单标识,但同一单可能取过多次号
(每次分配新号,本地库里 dgj-10000267890142_1 就有 4 条不同运单号的记录)。
随便取一条会让发货模板(快递)和 WaybillCodes 面单标识与真正发出的运单号错位,
甚至把别的运单号发到平台。

- externalSendSync.findLatestFetchRecord:按配送单的运单号
  (express_codes / express_code)逐条匹配取号记录的 waybill_code / waybill_codes,
  主运单优先;运单号对不上返回 null 并 warn,报文宁可不带面单标识也不拿别人的;
  订单号 LIKE 前缀匹配转义下划线,避免 `..._1` 误匹配 `..._10`
- dgjSendRow:新增 parseOrderWaybills / parseFetchWaybills;
  pickWaybillFromFetchRecord(raw, waybills) 只取与订单运单号一致的那张;
  buildSendRow 的 WaybillCodes 只保留本单运单号,LastWaybillCode 以本单运单号为准
- 单测:dgjSendRow 补 3 例(按号选面单 / 对不上不回填 / 运单号清单解析),
  externalSendTemplate 补 3 例(按号挑记录 / 无匹配返回 null / 多包裹主运单优先)
- 文档补匹配口径与实测表

真实库实测:配送单运单 772085332374413 → 记录 #14;改成 772085215446719 → 记录 #13
(不再取最新那条);改成不存在的号 → null + warn。

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
2026-11-07 09:22:16 +08:00

181 lines
6.5 KiB
JavaScript
Raw Permalink 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.

'use strict';
const { app, assert } = require('egg-mock/bootstrap');
const mm = require('egg-mock');
/**
* 快递接口记录(纯对象形态,与 resolveByCode 返回一致)
* @param {Object} [overrides] 覆盖字段
* @return {Object} 快递接口记录
*/
function buildInterface(overrides = {}) {
return {
id: 25,
platform_type: 'TouTiao',
platform_code: 'DYXD',
template_id: '10776786',
express_company_id: 8,
express_company_code: 'STO',
express_company_name: '申通',
custom_code: null,
dbname: 'y/2wUDWCZOk=',
...overrides,
};
}
/**
* 包一层 Sequelize 实例的最小替身(服务里用了 .get({ plain: true }))
* @param {Object} row 纯对象记录
* @return {Object|null} 带 get() 的替身
*/
function asModel(row) {
return row ? { get: () => row, ...row } : null;
}
describe('service/externalSendSync 发货模板按运单快递定位', () => {
let ctx;
let service;
beforeEach(() => {
ctx = app.mockContext();
service = ctx.service.externalSendSync;
});
it('有绑定关系时用绑定模板(与取号同口径)', async () => {
const bound = buildInterface({ id: 29, template_id: '10824270', express_company_code: 'STO', custom_code: 'STO' });
ctx.service.externalPrintExpressInterface.resolveByCode = async () => bound;
ctx.model.ExternalPrintExpressInterface.findOne = async () => { throw new Error('不应走到兜底查询'); };
const template = await service.resolveSendTemplate({
account: { id: 1 },
platformCode: 'SPH',
expressCode: 'STO',
principalId: 5,
});
console.log('绑定命中:', template.id, template.template_id, template.express_company_code);
assert.strictEqual(template.id, 29);
});
it('没绑定 custom_code 时按模板快递公司编码兜底匹配', async () => {
ctx.service.externalPrintExpressInterface.resolveByCode = async () => null;
const queries = [];
ctx.model.ExternalPrintExpressInterface.findOne = async options => {
queries.push(options);
return asModel(buildInterface({ id: 25, express_company_code: 'STO' }));
};
const template = await service.resolveSendTemplate({
account: { id: 1 },
platformCode: 'DYXD',
expressCode: 'STO',
principalId: 5,
});
console.log('兜底命中:', template.id, template.express_company_code, '| 查询条件:', JSON.stringify(queries[0].where));
assert.strictEqual(template.id, 25);
assert.strictEqual(queries[0].where.express_company_code, 'STO');
});
it('都对不上时退回该平台第一条模板(保持历史行为)', async () => {
ctx.service.externalPrintExpressInterface.resolveByCode = async () => null;
let call = 0;
ctx.model.ExternalPrintExpressInterface.findOne = async () => {
call += 1;
// 第一次按快递公司编码查(无命中),第二次退回第一条
return call === 1 ? null : asModel(buildInterface({ id: 4, express_company_code: 'ZTO', template_id: '13987231' }));
};
const warnings = [];
mm(ctx, 'logger', { warn: (...args) => warnings.push(args), info() {}, error() {} });
const template = await service.resolveSendTemplate({
account: { id: 1 },
platformCode: 'DYXD',
expressCode: 'STO',
principalId: 5,
});
console.log('退回第一条:', template.id, template.express_company_code, '| 告警条数:', warnings.length);
assert.strictEqual(template.id, 4);
assert.ok(warnings.length > 0, '未绑定时应记一条告警便于排查');
mm.restore();
});
it('没有快递编码(历史单)时直接退回第一条,不告警', async () => {
ctx.service.externalPrintExpressInterface.resolveByCode = async () => null;
ctx.model.ExternalPrintExpressInterface.findOne = async () => asModel(buildInterface({ id: 4, express_company_code: 'ZTO' }));
const warnings = [];
mm(ctx, 'logger', { warn: (...args) => warnings.push(args), info() {}, error() {} });
const template = await service.resolveSendTemplate({
account: { id: 1 },
platformCode: 'DYXD',
expressCode: '',
principalId: 5,
});
assert.strictEqual(template.id, 4);
assert.strictEqual(warnings.length, 0);
});
});
describe('service/externalSendSync 取号记录必须与本单运单号匹配', () => {
let ctx;
let service;
beforeEach(() => {
ctx = app.mockContext();
service = ctx.service.externalSendSync;
});
it('按运单号挑记录(同一单多次取号时取对的那条),订单号下划线做转义', async () => {
const calls = [];
ctx.model.WaybillFetchRecord.findAll = async options => {
calls.push(options);
return [
{ id: 31, waybill_code: 'B', waybill_codes: '["B"]', express_code: 'ZTO' },
{ id: 30, waybill_code: 'A', waybill_codes: '["A"]', express_code: 'STO' },
];
};
const record = await service.findLatestFetchRecord({
order_no: 'FH_2_1',
principal_id: 5,
express_codes: '["A"]',
express_code: 'A',
});
const Op = ctx.app.Sequelize.Op;
const where = calls[0].where;
const likeValue = where[Op.or][1].delivery_order_code[Op.like];
console.log('命中记录:', record.id, record.express_code, '| LIKE:', likeValue);
assert.strictEqual(record.id, 30);
assert.strictEqual(likeValue, 'FH\\_2\\_1\\_%', '订单号里的下划线必须转义,避免前缀误匹配别的单');
});
it('运单号没有对应记录时返回 null(不带别人的面单)并告警', async () => {
ctx.model.WaybillFetchRecord.findAll = async () => [
{ id: 31, waybill_code: 'B', waybill_codes: '["B"]', express_code: 'ZTO' },
];
const warnings = [];
mm(ctx, 'logger', { warn: (...args) => warnings.push(args), info() {}, error() {} });
const record = await service.findLatestFetchRecord({
order_no: 'dgj-1',
principal_id: 5,
express_codes: '["C"]',
});
console.log('无匹配:', record, '| 告警条数:', warnings.length);
assert.strictEqual(record, null);
assert.ok(warnings.length > 0);
mm.restore();
});
it('多包裹时按运单号顺序优先匹配主运单', async () => {
ctx.model.WaybillFetchRecord.findAll = async () => [
{ id: 9, waybill_code: 'B', waybill_codes: '["B"]', express_code: 'ZTO' },
{ id: 8, waybill_code: 'A', waybill_codes: '["A"]', express_code: 'STO' },
];
const record = await service.findLatestFetchRecord({
order_no: 'dgj-2',
principal_id: 5,
express_codes: '["A","B"]',
});
assert.strictEqual(record.id, 8, '先按主运单 A 匹配');
});
});