275 lines
9.0 KiB
JavaScript
275 lines
9.0 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) {
|
|
const entry = {
|
|
...data,
|
|
update: async values => Object.assign(entry, values),
|
|
destroy: async () => {},
|
|
};
|
|
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.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 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);
|
|
});
|
|
});
|