- dxm.fetchWaybillNo 改为返回 pdf_base64(纯 base64,不带 data: 前缀),不再塞进 printinfo - waybillGet 透传 pdf_base64 给 WMS(printinfo 仍留给店管家等 JSON/加密串口径的平台) - waybill_fetch_records 新增 pdf_base64 列(迁移,TEXT medium); result_data 里只留 pdf_base64_length 避免同一张 PDF 存三份,重放时从列补回 - 文档补:WMS 取号响应新增 pdf_base64 字段(含 WMS 落库/打印建议)、店小秘 6.1 存储口径 - 补 4 条单测(dxm 返回 pdf_base64、PDF 失败降级、取号透传、重放补回) - 实测真实账号:运单号 3CAB6111900043,pdf_base64 88,796 字符,解码为 %PDF-1.4 Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
347 lines
12 KiB
JavaScript
347 lines
12 KiB
JavaScript
'use strict';
|
||
|
||
const { app, assert } = require('egg-mock/bootstrap');
|
||
const mm = require('egg-mock');
|
||
const orderCallback = require('../../../app/lib/orderCallback');
|
||
const waybillGet = require('../../../app/lib/waybillGet');
|
||
|
||
const PRINCIPAL_ID = 5;
|
||
const WAREHOUSE_ID = 8;
|
||
const DELIVERY_ORDER_CODE = 'dgj-550598429043014280';
|
||
|
||
function makeEntry(data, list) {
|
||
const entry = {
|
||
...data,
|
||
update: async values => Object.assign(entry, values),
|
||
destroy: async () => {
|
||
const index = Array.isArray(list) ? list.indexOf(entry) : -1;
|
||
if (index >= 0) list.splice(index, 1);
|
||
},
|
||
};
|
||
return entry;
|
||
}
|
||
|
||
function setupContext({ entries = [], legacyRecord = null, waybillCode = 'WB-NEW', fetchError = null } = {}) {
|
||
const ctx = app.mockContext();
|
||
const fetchedKeys = [];
|
||
const platformRoutes = [];
|
||
let platformCalls = 0;
|
||
|
||
ctx.model.WaybillFetchIdempotency.findOne = async ({ where }) =>
|
||
entries.find(entry => Object.keys(where).every(key => entry[key] === where[key])) || null;
|
||
ctx.model.WaybillFetchIdempotency.create = async data => {
|
||
const duplicate = entries.find(entry =>
|
||
[ 'principal_id', 'warehouse_id', 'delivery_order_code', 'express_code', 'package_index' ]
|
||
.every(key => entry[key] === data[key]));
|
||
if (duplicate) throw new Error('unique key conflict');
|
||
const entry = makeEntry(data, entries);
|
||
entries.push(entry);
|
||
return entry;
|
||
};
|
||
ctx.model.WaybillFetchRecord.findOne = async options => {
|
||
fetchedKeys.push(options.where);
|
||
return legacyRecord;
|
||
};
|
||
ctx.model.WaybillFetchRecord.create = async () => {};
|
||
ctx.service.cache.getShopBasic = async () => ({
|
||
id: 10,
|
||
principal_id: PRINCIPAL_ID,
|
||
type: 2,
|
||
external_print_account_id: 12,
|
||
});
|
||
ctx.model.Shop.findByPk = async () => ({
|
||
toJSON: () => ({
|
||
id: 10,
|
||
principal_id: PRINCIPAL_ID,
|
||
type: 2,
|
||
external_print_account_id: 12,
|
||
}),
|
||
});
|
||
ctx.model.ExternalPrintAccount.findOne = async () => ({
|
||
id: 12,
|
||
platform: { code: 'TEST_PLATFORM', name: 'Test platform', fetch_waybill_no: 1 },
|
||
get: () => ({ id: 12, platform: { code: 'TEST_PLATFORM', name: 'Test platform' } }),
|
||
});
|
||
ctx.service.externalPrintExpressInterface.resolveByCode = async () => ({
|
||
template_id: 'template-1',
|
||
get: () => ({ template_id: 'template-1' }),
|
||
});
|
||
ctx.service.externalPrintOrder.createPlatformService = account => {
|
||
platformRoutes.push(account.platform.code);
|
||
return {
|
||
needFetchWaybillNo: true,
|
||
fetchWaybillNo: async () => {
|
||
platformCalls += 1;
|
||
if (fetchError) throw fetchError;
|
||
return { waybill_code: waybillCode, printinfo: 'print-data' };
|
||
},
|
||
};
|
||
};
|
||
mm(orderCallback, 'resolveDeliveryBill', async () => ({
|
||
isNewModel: false,
|
||
order: {
|
||
warehouse_id: WAREHOUSE_ID,
|
||
status: 1,
|
||
source_platform_code: 'KS',
|
||
shop_id: 10,
|
||
get: () => ({
|
||
warehouse_id: WAREHOUSE_ID,
|
||
status: 1,
|
||
source_platform_code: 'KS',
|
||
shop_id: 10,
|
||
}),
|
||
},
|
||
}));
|
||
return { ctx, entries, fetchedKeys, platformRoutes, get platformCalls() { return platformCalls; } };
|
||
}
|
||
|
||
function requestItem(overrides = {}) {
|
||
return {
|
||
deliveryOrderCode: DELIVERY_ORDER_CODE,
|
||
expressCode: 'STO',
|
||
packageIndex: 0,
|
||
...overrides,
|
||
};
|
||
}
|
||
|
||
function requestBody(item) {
|
||
return { request: { orders: [ item ], request_id: `request-${Date.now()}` } };
|
||
}
|
||
|
||
describe('lib/waybillGet 订单级幂等', () => {
|
||
afterEach(() => mm.restore());
|
||
|
||
it('同订单、快递和包裹序号直接重放数据库中的完整结果', async () => {
|
||
const savedResult = {
|
||
delivery_order_code: DELIVERY_ORDER_CODE,
|
||
success: true,
|
||
message: 'ok',
|
||
express_code: 'STO',
|
||
package_index: 0,
|
||
waybill_code: 'WB-SAVED',
|
||
waybill_codes: [ 'WB-SAVED' ],
|
||
printinfo: 'saved-print-data',
|
||
key: 'saved-key',
|
||
};
|
||
const entry = makeEntry({
|
||
principal_id: PRINCIPAL_ID,
|
||
warehouse_id: WAREHOUSE_ID,
|
||
delivery_order_code: DELIVERY_ORDER_CODE,
|
||
express_code: 'STO',
|
||
package_index: 0,
|
||
status: 1,
|
||
result_data: JSON.stringify(savedResult),
|
||
});
|
||
const state = setupContext({ entries: [ entry ] });
|
||
|
||
const result = await waybillGet.getWaybills(state.ctx, {
|
||
principalId: PRINCIPAL_ID,
|
||
warehouseId: WAREHOUSE_ID,
|
||
data: requestBody(requestItem()),
|
||
});
|
||
|
||
assert.deepStrictEqual(result.data.results[0], savedResult);
|
||
assert.strictEqual(state.platformRoutes.length, 0);
|
||
assert.strictEqual(state.fetchedKeys.length, 0);
|
||
});
|
||
|
||
it('同一批里的重复订单项只调用一次平台并复用结果', async () => {
|
||
const state = setupContext();
|
||
const item = requestItem();
|
||
|
||
const result = await waybillGet.getWaybills(state.ctx, {
|
||
principalId: PRINCIPAL_ID,
|
||
warehouseId: WAREHOUSE_ID,
|
||
data: { request: { orders: [ item, { ...item }] } },
|
||
});
|
||
|
||
assert.strictEqual(result.data.success, 2);
|
||
assert.strictEqual(result.data.results[0].waybill_code, 'WB-NEW');
|
||
assert.strictEqual(result.data.results[1].waybill_code, 'WB-NEW');
|
||
assert.strictEqual(state.platformRoutes.length, 1);
|
||
});
|
||
|
||
it('相同订单但快递编码不同会按订单当前平台路由重新取号', async () => {
|
||
const state = setupContext();
|
||
|
||
const result = await waybillGet.getWaybills(state.ctx, {
|
||
principalId: PRINCIPAL_ID,
|
||
warehouseId: WAREHOUSE_ID,
|
||
data: requestBody(requestItem({ expressCode: 'ZTO' })),
|
||
});
|
||
|
||
assert.strictEqual(result.data.results[0].waybill_code, 'WB-NEW');
|
||
assert.strictEqual(state.platformRoutes[0], 'TEST_PLATFORM');
|
||
assert.strictEqual(state.entries[0].express_code, 'ZTO');
|
||
});
|
||
|
||
it('相同订单和快递但包裹序号不同会独立取号', async () => {
|
||
const state = setupContext();
|
||
|
||
const result = await waybillGet.getWaybills(state.ctx, {
|
||
principalId: PRINCIPAL_ID,
|
||
warehouseId: WAREHOUSE_ID,
|
||
data: requestBody(requestItem({ packageIndex: 1 })),
|
||
});
|
||
|
||
assert.strictEqual(result.data.results[0].package_index, 1);
|
||
assert.strictEqual(state.entries[0].package_index, 1);
|
||
assert.strictEqual(state.platformRoutes[0], 'TEST_PLATFORM');
|
||
});
|
||
|
||
it('另一请求已有相同订单键正在处理时不重复调用平台', async () => {
|
||
const state = setupContext({
|
||
entries: [ makeEntry({
|
||
principal_id: PRINCIPAL_ID,
|
||
warehouse_id: WAREHOUSE_ID,
|
||
delivery_order_code: DELIVERY_ORDER_CODE,
|
||
express_code: 'STO',
|
||
package_index: 0,
|
||
status: 0,
|
||
}) ],
|
||
});
|
||
|
||
const result = await waybillGet.getWaybills(state.ctx, {
|
||
principalId: PRINCIPAL_ID,
|
||
warehouseId: WAREHOUSE_ID,
|
||
data: requestBody(requestItem()),
|
||
});
|
||
|
||
assert.strictEqual(result.data.results[0].success, false);
|
||
assert.match(result.data.results[0].message, /正在处理中/);
|
||
assert.strictEqual(state.platformRoutes.length, 0);
|
||
});
|
||
|
||
it('并发的不同 request_id 由数据库唯一键仲裁,只执行一次平台取号', async () => {
|
||
const state = setupContext();
|
||
const makePayload = requestId => ({
|
||
principalId: PRINCIPAL_ID,
|
||
warehouseId: WAREHOUSE_ID,
|
||
data: { request: { orders: [ requestItem() ], request_id: requestId } },
|
||
});
|
||
|
||
await Promise.all([
|
||
waybillGet.getWaybills(state.ctx, makePayload('request-a')),
|
||
waybillGet.getWaybills(state.ctx, makePayload('request-b')),
|
||
]);
|
||
|
||
assert.strictEqual(state.platformCalls, 1);
|
||
assert.strictEqual(state.entries.length, 1);
|
||
assert.strictEqual(state.entries[0].status, 1);
|
||
});
|
||
|
||
it('平台请求超时后标记结果待核实,后续重试不再次消耗面单', async () => {
|
||
const state = setupContext({ fetchError: new Error('platform request timeout') });
|
||
const payload = {
|
||
principalId: PRINCIPAL_ID,
|
||
warehouseId: WAREHOUSE_ID,
|
||
data: requestBody(requestItem()),
|
||
};
|
||
|
||
const first = await waybillGet.getWaybills(state.ctx, payload);
|
||
const second = await waybillGet.getWaybills(state.ctx, payload);
|
||
|
||
assert.strictEqual(first.data.results[0].success, false);
|
||
assert.match(first.data.results[0].message, /结果暂不确定/);
|
||
assert.strictEqual(second.data.results[0].success, false);
|
||
assert.match(second.data.results[0].message, /待核实/);
|
||
assert.strictEqual(state.platformCalls, 1);
|
||
assert.strictEqual(state.entries[0].status, 2);
|
||
});
|
||
|
||
it('未消耗面单的明确失败(如店管家查不到订单)允许重试,不写待核实', async () => {
|
||
const fetchError = new Error('店管家未找到订单:系统编号 40000834686950(域名 fxdd.dgjapp.com,分区 -)');
|
||
fetchError.waybillNotConsumed = true;
|
||
const state = setupContext({ fetchError });
|
||
const payload = {
|
||
principalId: PRINCIPAL_ID,
|
||
warehouseId: WAREHOUSE_ID,
|
||
data: requestBody(requestItem()),
|
||
};
|
||
|
||
const first = await waybillGet.getWaybills(state.ctx, payload);
|
||
const second = await waybillGet.getWaybills(state.ctx, payload);
|
||
|
||
assert.strictEqual(first.data.results[0].success, false);
|
||
assert.match(first.data.results[0].message, /未找到订单/);
|
||
assert.doesNotMatch(first.data.results[0].message, /待核实|暂不确定/);
|
||
assert.match(second.data.results[0].message, /未找到订单/);
|
||
assert.strictEqual(state.platformCalls, 2);
|
||
assert.strictEqual(state.entries.length, 0);
|
||
});
|
||
|
||
it('首次命中旧取号记录时回填幂等结果,不再调用平台', async () => {
|
||
const state = setupContext({
|
||
legacyRecord: {
|
||
waybill_code: 'WB-LEGACY',
|
||
waybill_codes: '["WB-LEGACY"]',
|
||
waybill_source: 'express_print',
|
||
printinfo: 'legacy-print-data',
|
||
signature: 'legacy-signature',
|
||
print_history_ids: '[]',
|
||
cai_niao_auth_info_id: 123,
|
||
platform_code: 'KS',
|
||
},
|
||
});
|
||
|
||
const result = await waybillGet.getWaybills(state.ctx, {
|
||
principalId: PRINCIPAL_ID,
|
||
warehouseId: WAREHOUSE_ID,
|
||
data: requestBody(requestItem()),
|
||
});
|
||
|
||
assert.strictEqual(result.data.results[0].waybill_code, 'WB-LEGACY');
|
||
assert.strictEqual(result.data.results[0].printinfo, 'legacy-print-data');
|
||
assert.strictEqual(state.platformRoutes.length, 0);
|
||
assert.strictEqual(state.entries[0].status, 1);
|
||
});
|
||
|
||
it('平台返回面单 PDF 时以 pdf_base64 透传(不进 printinfo)', async () => {
|
||
const state = setupContext();
|
||
const platformService = state.ctx.service.externalPrintOrder.createPlatformService({ platform: { code: 'dxm' } });
|
||
state.ctx.service.externalPrintOrder.createPlatformService = () => ({
|
||
...platformService,
|
||
fetchWaybillNo: async () => ({
|
||
waybill_code: '3CAB6112100007',
|
||
waybill_codes: [ '3CAB6112100007' ],
|
||
pdf_base64: 'JVBERi0xLjQK',
|
||
raw: { agentTrackNum: '3CAB6112100007' },
|
||
}),
|
||
});
|
||
|
||
const result = await waybillGet.getWaybills(state.ctx, {
|
||
principalId: PRINCIPAL_ID,
|
||
warehouseId: WAREHOUSE_ID,
|
||
data: requestBody(requestItem()),
|
||
});
|
||
|
||
const item = result.data.results[0];
|
||
assert.strictEqual(item.success, true);
|
||
assert.strictEqual(item.waybill_code, '3CAB6112100007');
|
||
assert.strictEqual(item.pdf_base64, 'JVBERi0xLjQK');
|
||
assert.strictEqual(item.printinfo, '');
|
||
});
|
||
|
||
it('重放已保存结果时补回 pdf_base64(result_data 里只存长度)', async () => {
|
||
const state = setupContext({
|
||
legacyRecord: {
|
||
waybill_code: 'WB-LEGACY',
|
||
waybill_codes: '["WB-LEGACY"]',
|
||
waybill_source: 'express_print',
|
||
result_data: JSON.stringify({ success: true, waybill_code: 'WB-LEGACY', pdf_base64_length: 12 }),
|
||
pdf_base64: 'JVBERi0xLjQK',
|
||
print_history_ids: '[]',
|
||
},
|
||
});
|
||
|
||
const result = await waybillGet.getWaybills(state.ctx, {
|
||
principalId: PRINCIPAL_ID,
|
||
warehouseId: WAREHOUSE_ID,
|
||
data: requestBody(requestItem()),
|
||
});
|
||
|
||
assert.strictEqual(result.data.results[0].pdf_base64, 'JVBERi0xLjQK');
|
||
assert.strictEqual(state.platformRoutes.length, 0);
|
||
});
|
||
});
|