240 lines
8.3 KiB
JavaScript
240 lines
8.3 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 FALLBACK_ROLE_IDS = [ 4, 6, 30 ];
|
|
|
|
function normalizeListPayload(data) {
|
|
if (data && Array.isArray(data.list)) return data;
|
|
if (data && data.data && Array.isArray(data.data.list)) return data.data;
|
|
return null;
|
|
}
|
|
|
|
function extractDataObject(data) {
|
|
if (!data) return null;
|
|
if (data && typeof data === 'object' && data.data && typeof data.data === 'object') {
|
|
return data.data;
|
|
}
|
|
return data;
|
|
}
|
|
|
|
async function switchToWarehouseRole(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 listRes = await invokeSystemMethod('storage.entry.list', {
|
|
pagination: { page: 1, pageSize: 10 },
|
|
where: {},
|
|
}, currentToken, EXTRA_HEADERS);
|
|
|
|
if (listRes.status === 200 && listRes.body?.flag === 'success' && listRes.body?.code === 200) {
|
|
return {
|
|
token: currentToken,
|
|
roleId,
|
|
};
|
|
}
|
|
}
|
|
|
|
throw new Error('未找到可访问入库单的仓库角色');
|
|
}
|
|
|
|
describe('test/app/controller/system.storage.entry.test.js', () => {
|
|
it('should query entry list', async () => {
|
|
const { token } = await getAuthToken();
|
|
const switched = await switchToWarehouseRole(token);
|
|
|
|
const res = await invokeSystemMethod('storage.entry.list', {
|
|
pagination: {
|
|
page: 1,
|
|
pageSize: 10,
|
|
},
|
|
where: {},
|
|
}, switched.token, EXTRA_HEADERS);
|
|
|
|
assert.equal(res.status, 200);
|
|
assert.equal(res.body.flag, 'success');
|
|
assert.equal(res.body.code, 200);
|
|
|
|
const payload = normalizeListPayload(res.body.data);
|
|
assert.ok(payload, '返回结构中缺少入库单列表 payload');
|
|
assert.ok(Array.isArray(payload.list), '入库单 list 不是数组');
|
|
assert.ok(payload.pagination, '入库单 pagination 缺失');
|
|
assert.equal(typeof payload.pagination.total, 'number', 'pagination.total 不是 number');
|
|
});
|
|
|
|
it('should create entry and run detail lifecycle then cancel', async () => {
|
|
const { token } = await getAuthToken();
|
|
const switched = await switchToWarehouseRole(token);
|
|
|
|
const createRes = await invokeSystemMethod('storage.entry.create', {
|
|
order_type: 'QTRK',
|
|
purchase_order_code: `PO-${Date.now()}`,
|
|
remark: 'integration-test-cancel',
|
|
}, switched.token, EXTRA_HEADERS);
|
|
|
|
assert.equal(createRes.status, 200);
|
|
assert.equal(createRes.body.flag, 'success');
|
|
assert.equal(createRes.body.code, 200);
|
|
|
|
const createData = extractDataObject(createRes.body.data) || {};
|
|
const orderId = Number(createData.id || createRes.body.id || 0);
|
|
assert.ok(orderId > 0, `创建入库单未返回有效 id: ${JSON.stringify(createRes.body)}`);
|
|
|
|
const getRes = await invokeSystemMethod('storage.entry.get', {
|
|
id: orderId,
|
|
}, switched.token, EXTRA_HEADERS);
|
|
|
|
assert.equal(getRes.status, 200);
|
|
assert.equal(getRes.body.flag, 'success');
|
|
assert.equal(getRes.body.code, 200);
|
|
|
|
const getData = extractDataObject(getRes.body.data) || {};
|
|
const entryOrder = getData.entryOrder || {};
|
|
assert.equal(Number(entryOrder.id), orderId);
|
|
|
|
const addRes = await invokeSystemMethod('storage.entry.addDetail', {
|
|
order_id: orderId,
|
|
count: 2,
|
|
item_code: `SKU-${Date.now().toString().slice(-6)}`,
|
|
inventory_type: 'ZP',
|
|
}, switched.token, EXTRA_HEADERS);
|
|
|
|
assert.equal(addRes.status, 200);
|
|
assert.equal(addRes.body.flag, 'success');
|
|
assert.equal(addRes.body.code, 200);
|
|
|
|
const addData = extractDataObject(addRes.body.data) || {};
|
|
const detailId = Number(addData.id || addRes.body.id || 0);
|
|
assert.ok(detailId > 0, `新增明细未返回有效 id: ${JSON.stringify(addRes.body)}`);
|
|
|
|
const changeRes = await invokeSystemMethod('storage.entry.detailChange', {
|
|
id: detailId,
|
|
count: 3,
|
|
item_code: `SKU-CHG-${Date.now().toString().slice(-6)}`,
|
|
}, switched.token, EXTRA_HEADERS);
|
|
|
|
assert.equal(changeRes.status, 200);
|
|
assert.equal(changeRes.body.flag, 'success');
|
|
assert.equal(changeRes.body.code, 200);
|
|
|
|
const deleteRes = await invokeSystemMethod('storage.entry.deleteDetail', {
|
|
id: detailId,
|
|
}, switched.token, EXTRA_HEADERS);
|
|
|
|
assert.equal(deleteRes.status, 200);
|
|
assert.equal(deleteRes.body.flag, 'success');
|
|
assert.equal(deleteRes.body.code, 200);
|
|
|
|
const cancelRes = await invokeSystemMethod('storage.entry.cancel', {
|
|
id: orderId,
|
|
}, switched.token, EXTRA_HEADERS);
|
|
|
|
assert.equal(cancelRes.status, 200);
|
|
assert.equal(cancelRes.body.flag, 'success');
|
|
assert.equal(cancelRes.body.code, 200);
|
|
});
|
|
|
|
it('should complete entry after adding detail', async () => {
|
|
const { token } = await getAuthToken();
|
|
const switched = await switchToWarehouseRole(token);
|
|
|
|
const createRes = await invokeSystemMethod('storage.entry.create', {
|
|
order_type: 'QTRK',
|
|
purchase_order_code: `PO-COMP-${Date.now()}`,
|
|
remark: 'integration-test-complete',
|
|
}, switched.token, EXTRA_HEADERS);
|
|
|
|
assert.equal(createRes.status, 200);
|
|
assert.equal(createRes.body.flag, 'success');
|
|
assert.equal(createRes.body.code, 200);
|
|
|
|
const createData = extractDataObject(createRes.body.data) || {};
|
|
const orderId = Number(createData.id || createRes.body.id || 0);
|
|
assert.ok(orderId > 0, `创建入库单未返回有效 id: ${JSON.stringify(createRes.body)}`);
|
|
|
|
const addRes = await invokeSystemMethod('storage.entry.addDetail', {
|
|
order_id: orderId,
|
|
count: 1,
|
|
item_code: `SKU-COMP-${Date.now().toString().slice(-6)}`,
|
|
inventory_type: 'ZP',
|
|
}, switched.token, EXTRA_HEADERS);
|
|
|
|
assert.equal(addRes.status, 200);
|
|
assert.equal(addRes.body.flag, 'success');
|
|
assert.equal(addRes.body.code, 200);
|
|
|
|
const completeRes = await invokeSystemMethod('storage.entry.completeEntry', {
|
|
id: orderId,
|
|
}, switched.token, EXTRA_HEADERS);
|
|
|
|
assert.equal(completeRes.status, 200);
|
|
assert.equal(completeRes.body.flag, 'success');
|
|
assert.equal(completeRes.body.code, 200);
|
|
});
|
|
|
|
it('should reject invalid params for create/get/addDetail', async () => {
|
|
const { token } = await getAuthToken();
|
|
const switched = await switchToWarehouseRole(token);
|
|
|
|
const createBadRes = await invokeSystemMethod('storage.entry.create', {
|
|
purchase_order_code: `PO-BAD-${Date.now()}`,
|
|
}, switched.token, EXTRA_HEADERS);
|
|
|
|
assert.equal(createBadRes.status, 200);
|
|
assert.equal(createBadRes.body.flag, 'success');
|
|
assert.equal(createBadRes.body.code, 400);
|
|
assert.match(String(createBadRes.body.message || ''), /order_type 必填/);
|
|
|
|
const getBadRes = await invokeSystemMethod('storage.entry.get', {}, switched.token, EXTRA_HEADERS);
|
|
|
|
assert.equal(getBadRes.status, 200);
|
|
assert.equal(getBadRes.body.flag, 'success');
|
|
assert.equal(getBadRes.body.code, 400);
|
|
assert.match(String(getBadRes.body.message || ''), /至少传一个/);
|
|
|
|
const addBadRes = await invokeSystemMethod('storage.entry.addDetail', {
|
|
order_id: 0,
|
|
count: 0,
|
|
}, switched.token, EXTRA_HEADERS);
|
|
|
|
assert.equal(addBadRes.status, 200);
|
|
assert.equal(addBadRes.body.flag, 'success');
|
|
assert.equal(addBadRes.body.code, 400);
|
|
assert.match(String(addBadRes.body.message || ''), /order_id 必填|count 必须大于 0/);
|
|
});
|
|
});
|