113 lines
3.7 KiB
JavaScript
113 lines
3.7 KiB
JavaScript
'use strict';
|
||
|
||
const assert = require('node:assert/strict');
|
||
const {
|
||
getAuthToken,
|
||
invokeSystemMethod,
|
||
} = require('../../support/system_method_client');
|
||
|
||
const EXTRA_HEADERS = {
|
||
platform: 'web',
|
||
project: 'all',
|
||
};
|
||
|
||
const ALLOWED_REVIEW_STATUS = new Set([ 'none', 'padding', 'running', 'fin', 'error' ]);
|
||
const FALLBACK_ROLE_IDS = [ 4, 6, 30 ];
|
||
|
||
async function switchToWarehouseRoleWithOrderData(token) {
|
||
let currentToken = token;
|
||
const infoRes = await invokeSystemMethod('user.info', {}, currentToken, EXTRA_HEADERS);
|
||
assert.equal(infoRes.status, 200);
|
||
assert.equal(infoRes.body.flag, 'success');
|
||
assert.equal(infoRes.body.code, 200);
|
||
|
||
const employees = Array.isArray(infoRes.body?.data?.employees) ? infoRes.body.data.employees : [];
|
||
const warehouseRoles = employees.filter(item => item && item.role_type === 'warehouse' && Number(item.id));
|
||
const candidateRoleIds = [ ...new Set([
|
||
...warehouseRoles.map(item => Number(item.id)),
|
||
...FALLBACK_ROLE_IDS,
|
||
].filter(Boolean)) ];
|
||
|
||
assert.ok(candidateRoleIds.length > 0, '当前账号没有可尝试的仓库角色');
|
||
|
||
for (const roleId of candidateRoleIds) {
|
||
const changeRes = await invokeSystemMethod('user.changeRole', {
|
||
roleId,
|
||
}, currentToken, EXTRA_HEADERS);
|
||
|
||
if (changeRes.status !== 200 || changeRes.body?.flag !== 'success' || changeRes.body?.code !== 200) {
|
||
continue;
|
||
}
|
||
|
||
currentToken = changeRes.body?.data?.token || currentToken;
|
||
|
||
const statsRes = await invokeSystemMethod('order.stats', {
|
||
where: {},
|
||
}, currentToken, EXTRA_HEADERS);
|
||
|
||
assert.equal(statsRes.status, 200);
|
||
assert.equal(statsRes.body.flag, 'success');
|
||
assert.equal(statsRes.body.code, 200);
|
||
|
||
const total = Number(statsRes.body?.data?.total || 0);
|
||
if (total > 0) {
|
||
return {
|
||
token: currentToken,
|
||
roleId,
|
||
total,
|
||
};
|
||
}
|
||
}
|
||
|
||
throw new Error('未找到有订单数据的仓库角色');
|
||
}
|
||
|
||
describe('test/app/controller/system.order.reviewOrder.test.js', () => {
|
||
it('should review order and restore inspection status', async () => {
|
||
const { token } = await getAuthToken();
|
||
const switched = await switchToWarehouseRoleWithOrderData(token);
|
||
|
||
const listRes = await invokeSystemMethod('order.list', {
|
||
pagination: {
|
||
page: 1,
|
||
pageSize: 1,
|
||
},
|
||
where: {},
|
||
}, switched.token, EXTRA_HEADERS);
|
||
|
||
assert.equal(listRes.status, 200);
|
||
assert.equal(listRes.body.flag, 'success');
|
||
assert.equal(listRes.body.code, 200);
|
||
|
||
const order = Array.isArray(listRes.body?.data?.list) ? listRes.body.data.list[0] : null;
|
||
assert.ok(order, '订单列表为空,无法测试 reviewOrder');
|
||
assert.ok(order.id, '订单缺少 id,无法测试 reviewOrder');
|
||
|
||
const originalStatus = ALLOWED_REVIEW_STATUS.has(String(order.inspection_status || ''))
|
||
? String(order.inspection_status)
|
||
: 'none';
|
||
const targetStatus = originalStatus === 'fin' ? 'running' : 'fin';
|
||
|
||
const reviewRes = await invokeSystemMethod('order.reviewOrder', {
|
||
id: Number(order.id),
|
||
review_status: targetStatus,
|
||
}, switched.token, EXTRA_HEADERS);
|
||
|
||
assert.equal(reviewRes.status, 200);
|
||
assert.equal(reviewRes.body.flag, 'success');
|
||
assert.equal(reviewRes.body.code, 200);
|
||
assert.equal(Number(reviewRes.body?.data?.id || 0), Number(order.id));
|
||
assert.equal(reviewRes.body?.data?.inspection_status, targetStatus);
|
||
|
||
const restoreRes = await invokeSystemMethod('order.reviewOrder', {
|
||
id: Number(order.id),
|
||
review_status: originalStatus,
|
||
}, switched.token, EXTRA_HEADERS);
|
||
|
||
assert.equal(restoreRes.status, 200);
|
||
assert.equal(restoreRes.body.flag, 'success');
|
||
assert.equal(restoreRes.body.code, 200);
|
||
assert.equal(restoreRes.body?.data?.inspection_status, originalStatus);
|
||
});
|
||
});
|