批量审核对每一单都串行请求店管家「平台是否已发货」校验,20 单就要 8~13 秒, 50 单约 25 秒;前端 axios 超时 15 秒必然中断(nginx 日志大量 499),用户看到"审核无响应"。 - 新增 externalPrintOrder.checkPlatformFinishedBatch:同一「账号 + 订单域名 + 分区库」 的订单用一次 LogicOrderId Contract=in 批量回读;接口没返回的订单才逐单兜底 (时间窗回扫,与单条校验同口径),兜底并发 4 且受总时间预算约束 (DGJ_VERIFY_BUDGET_MS,默认 5 秒),超预算按"校验失败放行"处理,与原有口径一致 - 批量审核先整批校验平台状态、再逐单落库,落库时不再重复请求平台; 自动作废仍保留在逐单流程里,避免并发写同一批库存行 - dgj traceId 改为进程级缓存(key = 账号 + token):业务侧逐单 new 服务实例, 实例级缓存命中不了,每单都会多打一次 GetUserInfo Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
110 lines
4.0 KiB
JavaScript
110 lines
4.0 KiB
JavaScript
'use strict';
|
|
|
|
const { app, mock, assert } = require('egg-mock/bootstrap');
|
|
|
|
/**
|
|
* 批量审核的平台状态预校验
|
|
*
|
|
* 背景:校验是外部请求(店管家),逐单串行会把十几单的审核拖到前端 15 秒超时;
|
|
* 所以批量审核先按批次校验一次,再逐单落库,且落库时不再重复请求平台。
|
|
*/
|
|
describe('service/orderStatus 批量审核平台预校验', () => {
|
|
const PRINCIPAL_ID = 3;
|
|
|
|
/**
|
|
* 构造一个只够跑预校验的服务实例
|
|
* @param {Object} [options] 桩配置
|
|
* @param {Function} [options.check] 批量平台状态校验桩
|
|
* @return {Object} 服务实例
|
|
*/
|
|
function buildService(options = {}) {
|
|
const ctx = app.mockContext({ state: { principalId: PRINCIPAL_ID } });
|
|
ctx.userId = null;
|
|
ctx.service.externalPrintOrder.checkPlatformFinishedBatch = options.check;
|
|
const service = ctx.service.orderStatus;
|
|
service._loadIndexRows = async ids => ids.map(id => ({ id, order_no: `NO-${id}`, current_status: 'pending' }));
|
|
service._model = () => ({ findByPk: async id => ({ id, shop_id: 2 }) });
|
|
return service;
|
|
}
|
|
|
|
it('只送待审核/异常且有明细的订单,结果按订单 id 归档(未返回按放行)', async () => {
|
|
const seen = [];
|
|
const service = buildService({
|
|
check: async items => {
|
|
seen.push(...items.map(item => Number(item.index.id)));
|
|
return new Map([[ 2, { finished: true, reason: '店管家侧已发货' }]]);
|
|
},
|
|
});
|
|
service._model = () => ({ findByPk: async id => (id === 3 ? null : { id, shop_id: 2 }) });
|
|
const loadIndex = service._loadIndexRows;
|
|
service._loadIndexRows = async ids => (await loadIndex(ids)).map(row => ({
|
|
...row,
|
|
current_status: row.id === 4 ? 'queued' : 'pending',
|
|
}));
|
|
|
|
const checks = await service._precheckPlatformFinished([ 1, 2, 3, 4 ], PRINCIPAL_ID);
|
|
|
|
assert.deepStrictEqual(seen, [ 1, 2 ]);
|
|
assert.strictEqual(checks.get(2).finished, true);
|
|
assert.strictEqual(checks.get(1), null);
|
|
assert.ok(!checks.has(3), '明细缺失的订单不预校验');
|
|
assert.ok(!checks.has(4), '状态不符的订单不预校验');
|
|
});
|
|
|
|
it('批量校验整体失败时放行整批,不阻断审核', async () => {
|
|
const service = buildService({
|
|
check: async () => {
|
|
throw new Error('平台超时');
|
|
},
|
|
});
|
|
|
|
const checks = await service._precheckPlatformFinished([ 1, 2 ], PRINCIPAL_ID);
|
|
|
|
assert.strictEqual(checks.size, 0);
|
|
});
|
|
|
|
it('DGJ_AUDIT_PLATFORM_CHECK=0 时不请求平台', async () => {
|
|
let called = false;
|
|
const service = buildService({
|
|
check: async () => {
|
|
called = true;
|
|
return new Map();
|
|
},
|
|
});
|
|
const previous = process.env.DGJ_AUDIT_PLATFORM_CHECK;
|
|
process.env.DGJ_AUDIT_PLATFORM_CHECK = '0';
|
|
try {
|
|
const checks = await service._precheckPlatformFinished([ 1 ], PRINCIPAL_ID);
|
|
assert.strictEqual(checks.size, 0);
|
|
assert.strictEqual(called, false);
|
|
} finally {
|
|
if (previous === undefined) delete process.env.DGJ_AUDIT_PLATFORM_CHECK;
|
|
else process.env.DGJ_AUDIT_PLATFORM_CHECK = previous;
|
|
}
|
|
});
|
|
|
|
it('已预校验的单据落库时不再请求平台', async () => {
|
|
const service = buildService();
|
|
const applied = [];
|
|
const guarded = [];
|
|
service._applyPlatformGuard = async (row, check) => { applied.push([ row.id, check ]); };
|
|
service._guardPlatformFinished = async row => { guarded.push(row.id); };
|
|
mock(app.model.OrderStatusItem, 'count', async () => 0);
|
|
|
|
const check = { finished: false };
|
|
await assert.rejects(
|
|
() => service._auditOne(1, PRINCIPAL_ID, '审核人', { platformCheck: check }),
|
|
/无商品明细/
|
|
);
|
|
assert.deepStrictEqual(applied, [[ 1, check ]]);
|
|
assert.deepStrictEqual(guarded, []);
|
|
|
|
// 未预校验(单条审核)时才走完整守卫
|
|
await assert.rejects(
|
|
() => service._auditOne(1, PRINCIPAL_ID, '审核人'),
|
|
/无商品明细/
|
|
);
|
|
assert.deepStrictEqual(guarded, [ 1 ]);
|
|
});
|
|
});
|