WMS_MAIN/test/app/service/systems/order_print.getWavePlatformPrintData.test.js
liufumingren 0c3518e65d 1
2026-05-13 03:18:39 +08:00

452 lines
13 KiB
JavaScript

'use strict';
const assert = require('node:assert/strict');
const OrderPrintSystemService = require('../../../../app/service/systems/order_print');
function createOrderPrintServiceHarness() {
const ctx = {
app: {
baseDir: 'd:/wms',
},
user: {
store_id: 100000,
},
logger: {
info() {},
warn() {},
error() {},
},
model: {
QueryTypes: {
SELECT: 'SELECT',
},
},
service: {
systems: {
storeWarehouse: {
async getContractConfig() {
return {
policy_config: {},
};
},
},
},
},
};
return new OrderPrintSystemService(ctx);
}
describe('test/app/service/systems/order_print.getWavePlatformPrintData.test.js', () => {
it('should map kuaishou encryptedData/signature/key from order_express fields', () => {
const service = createOrderPrintServiceHarness();
const result = service.buildOrderPlatformPrintItem({
orderRow: {
id: 101,
wave_id: 10,
wave_no: 'W010',
delivery_order_code: 'DO101',
store_id: 1,
warehouse_id: 100000,
items: JSON.stringify([
{
item_name: '化妆品',
pick_location: 'E2区02-04-02',
barcode: '6973348850399',
quantity: 2,
},
]),
},
orderExpress: {
waybill_code: 'WB101',
printinfo: 'ENC_FROM_DB',
signature: 'SIG_FROM_DB',
encryptprintinfo: 'KEY_FROM_DB',
},
policyConfig: {
province_name: '浙江省',
city_name: '杭州市',
district_name: '滨江区',
address: '网商路 699 号',
sender_name: '张三',
sender_tel: '13800000000',
waybill_custom_display_type: '5',
},
platformCode: 'kuaishou',
templateSample: {
cmd: 'print',
requestID: 'sample-request',
version: '1.0',
taskID: 'sample-task',
task: {
documents: [
{
documentID: 'sample-doc',
contents: [
{
encryptedData: 'SAMPLE_ENCRYPTED_DATA',
signature: 'SAMPLE_SIGNATURE',
key: 'SAMPLE_KEY',
templateURL: 'https://example.com/kuaishou.xml',
data: {
productInfo: '#0 sample product info',
},
},
{
templateURL: 'http://www.ye4s.com/mb/kuaishou.xml',
customData: {
productInfo: '#1 sample custom product info',
},
},
],
},
],
},
},
});
assert.equal(result.print_data.documentID, 'WB101');
assert.equal(result.print_data.waybillCode, 'WB101');
assert.ok(Array.isArray(result.print_data.contents));
const mappedContent = result.print_data.contents[0];
assert.equal(mappedContent.encryptedData, 'ENC_FROM_DB');
assert.equal(mappedContent.signature, 'SIG_FROM_DB');
assert.equal(mappedContent.key, 'KEY_FROM_DB');
assert.equal(mappedContent.templateURL, 'https://example.com/kuaishou.xml');
assert.equal(mappedContent.data.productInfo, '__SAMPLE__');
assert.equal(mappedContent.addData.senderInfo.address.countryCode, 'CHN');
assert.equal(mappedContent.addData.senderInfo.address.detailAddress, '网商路 699 号');
assert.equal(mappedContent.addData.senderInfo.contact.name, '张三');
assert.equal(mappedContent.addData.senderInfo.contact.mobile, '13800000000');
const customDataContent = result.print_data.contents.find(content => content && content.customData);
assert.ok(customDataContent);
assert.equal(customDataContent.templateURL, 'http://www.ye4s.com/mb/kuaishou.xml');
assert.ok(customDataContent.customData.productInfo.includes('商品总数 【2】'));
assert.ok(customDataContent.customData.productInfo.includes('E2区02-04-02 化妆品 6973348850399 【2】'));
});
it('should map pdd encryptedData/signature and sender fields from order_express and policy_config', () => {
const service = createOrderPrintServiceHarness();
const result = service.buildOrderPlatformPrintItem({
orderRow: {
id: 201,
wave_id: 20,
wave_no: 'W020',
delivery_order_code: 'DO201',
store_id: 1,
warehouse_id: 100000,
items: JSON.stringify([
{
pick_location: 'A1-01-01',
barcode: '1234567890123',
quantity: 1,
},
]),
},
orderExpress: {
waybill_code: ' WB201 ',
printinfo: ' ENC_FROM_DB_PDD ',
signature: ' SIG_FROM_DB_PDD ',
},
policyConfig: {
province_name: '广东省',
city_name: '深圳市',
district_name: '南山区',
address: '科技园南区 1 号',
sender_name: '李四',
sender_tel: '13900000000',
waybill_custom_display_type: '4',
},
platformCode: 'pdd',
templateSample: {
cmd: 'print',
requestID: 'sample-request',
version: '1.0',
taskID: 'sample-task',
task: {
documents: [
{
documentID: 'sample-doc',
contents: [
{
addData: {
sender: {
address: {
province: '',
city: '',
district: '',
detail: '',
},
mobile: '',
name: '',
},
},
encryptedData: 'SAMPLE_ENCRYPTED_DATA',
signature: 'SAMPLE_SIGNATURE',
templateURL: 'https://example.com/pdd.xml',
data: {
productInfo: '#0 sample product info',
},
},
],
},
],
},
},
});
assert.equal(result.print_data.documentID, 'WB201');
assert.equal(result.print_data.contents[0].encryptedData, 'ENC_FROM_DB_PDD');
assert.equal(result.print_data.contents[0].signature, 'SIG_FROM_DB_PDD');
assert.equal(result.print_data.contents[0].addData.sender.address.province, '广东省');
assert.equal(result.print_data.contents[0].addData.sender.address.city, '深圳市');
assert.equal(result.print_data.contents[0].addData.sender.address.district, '南山区');
assert.equal(result.print_data.contents[0].addData.sender.address.detail, '科技园南区 1 号');
assert.equal(result.print_data.contents[0].addData.sender.mobile, '13900000000');
assert.equal(result.print_data.contents[0].addData.sender.name, '李四');
const customContent = result.print_data.contents.find(content => {
return content && content.data && Array.isArray(content.data.list);
});
assert.ok(customContent);
assert.equal(customContent.templateURL, 'http://www.ye4s.com/mb/pdd.xml');
assert.ok(customContent.data.list[0].text.includes('\r'));
assert.ok(customContent.data.list[0].text.includes('商品总数 【1】'));
assert.ok(customContent.data.list[0].text.includes('A1-01-01 90123 【1】'));
});
it('should query order_express with kuaishou encrypted payload columns', async () => {
const service = createOrderPrintServiceHarness();
let capturedAttributes = [];
let capturedWhere = {};
service.ctx.model.OrderExpress = {
async findAll(options) {
capturedAttributes = Array.isArray(options && options.attributes) ? options.attributes : [];
capturedWhere = options && options.where || {};
return [];
},
};
await service.queryOrderExpressByOrderIds([ 101 ]);
assert.ok(capturedAttributes.includes('printinfo'));
assert.ok(capturedAttributes.includes('signature'));
assert.ok(capturedAttributes.includes('encryptprintinfo'));
assert.equal(capturedWhere.business_key, 1);
});
it('should throw when business_key=1 waybill data is missing', async () => {
const service = createOrderPrintServiceHarness();
service.queryWavesByIds = async () => ([
{ wave_id: 1, wave_no: 'W001' },
]);
service.queryOrdersByWaveIds = async () => ([
{
id: 101,
wave_id: 1,
wave_no: 'W001',
warehouse_id: 100000,
store_id: 1,
delivery_order_code: 'O101',
post_status: 'fin',
sys_platform_code: 'kuaishou',
},
{
id: 102,
wave_id: 1,
wave_no: 'W001',
warehouse_id: 100000,
store_id: 1,
delivery_order_code: 'O102',
post_status: 'fin',
sys_platform_code: 'kuaishou',
},
]);
service.filterFullyAssignedExpressWaves = () => ({
validWaveIds: [ 1 ],
filteredWaveIds: [],
});
service.queryOrderExpressByOrderIds = async () => ([
{
order_id: 101,
business_key: 1,
waybill_code: 'WB101',
},
{
order_id: 102,
business_key: 1,
waybill_code: '',
},
]);
await assert.rejects(
async () => service.getWavePlatformPrintData({
waveIds: [ 1 ],
warehouseId: 100000,
}),
error => {
assert.ok(error instanceof Error);
assert.ok(error.message.includes('业务id为1的运单号数据缺失'));
return true;
}
);
});
it('should return waveGroups grouped by wave then platform and keep summary counters correct', async () => {
const service = createOrderPrintServiceHarness();
service.queryWavesByIds = async () => ([
{ wave_id: 2, wave_no: 'W002' },
{ wave_id: 1, wave_no: 'W001' },
]);
service.queryOrdersByWaveIds = async () => ([
{
id: 11,
wave_id: 1,
wave_no: 'W001',
warehouse_id: 100000,
store_id: 1,
delivery_order_code: 'O11',
post_status: 'fin',
sys_platform_code: 'jd',
},
{
id: 12,
wave_id: 1,
wave_no: 'W001',
warehouse_id: 100000,
store_id: 1,
delivery_order_code: 'O12',
post_status: 'fin',
sys_platform_code: 'cainiao',
},
{
id: 21,
wave_id: 2,
wave_no: 'W002',
warehouse_id: 100000,
store_id: 2,
delivery_order_code: 'O21',
post_status: 'fin',
sys_platform_code: 'jd',
},
]);
service.filterFullyAssignedExpressWaves = () => ({
validWaveIds: [ 2, 1 ],
filteredWaveIds: [],
});
service.queryOrderExpressByOrderIds = async () => ([
{
order_id: 11,
business_key: 1,
waybill_code: 'WB11',
},
{
order_id: 12,
business_key: 1,
waybill_code: 'WB12',
},
{
order_id: 21,
business_key: 1,
waybill_code: 'WB21',
},
]);
service.buildOrderExpressMap = () => new Map();
service.getPolicyConfigMapByStore = async () => new Map([
[ 1, {}],
[ 2, {}],
]);
service.resolvePrintPlatformCode = orderRow => String(orderRow && orderRow.sys_platform_code || '').trim();
service.loadTemplateSampleByPlatform = () => ({});
service.buildOrderPlatformPrintItem = ({ orderRow, platformCode }) => ({
wave_id: Number(orderRow.wave_id),
wave_no: String(orderRow.wave_no),
order_id: Number(orderRow.id),
order_no: String(orderRow.delivery_order_code),
platform: String(platformCode),
platform_name: platformCode === 'jd' ? '京东' : '菜鸟',
print_data: {
platform: String(platformCode),
order_id: Number(orderRow.id),
},
});
const result = await service.getWavePlatformPrintData({
waveIds: [ 2, 1 ],
warehouseId: 100000,
});
assert.deepEqual(result.summary, {
total_waves: 2,
total_orders: 3,
total_platforms: 3,
});
assert.equal(result.waveGroups.length, 2);
assert.deepEqual(result.waveGroups.map(item => item.wave_no), [ 'W001', 'W002' ]);
assert.deepEqual(result.waveGroups[0], {
wave_id: 1,
wave_no: 'W001',
order_count: 2,
platforms: [
{
platform: 'cainiao',
platform_name: '菜鸟',
order_count: 1,
orders: [
{
platform: 'cainiao',
order_id: 12,
},
],
},
{
platform: 'jd',
platform_name: '京东',
order_count: 1,
orders: [
{
platform: 'jd',
order_id: 11,
},
],
},
],
});
assert.deepEqual(result.waveGroups[1], {
wave_id: 2,
wave_no: 'W002',
order_count: 1,
platforms: [
{
platform: 'jd',
platform_name: '京东',
order_count: 1,
orders: [
{
platform: 'jd',
order_id: 21,
},
],
},
],
});
assert.deepEqual(result.ext, {
missing_wave_ids: [],
filtered_wave_ids: [],
});
});
});