391 lines
14 KiB
JavaScript
391 lines
14 KiB
JavaScript
'use strict';
|
|
|
|
const assert = require('node:assert/strict');
|
|
const StorageInventoryService = require('../../../../app/service/systems/storage/inventory');
|
|
|
|
const DAY_MS = 24 * 60 * 60 * 1000;
|
|
|
|
/**
|
|
* 将指定偏移天数转换为用于测试的日期时间字符串。
|
|
*
|
|
* @param {number} dayOffset 相对当前时间的天数偏移
|
|
* @return {string} 日期时间字符串
|
|
*/
|
|
function toDateTimeString(dayOffset) {
|
|
const value = new Date(Date.now() + (dayOffset * DAY_MS)).toISOString().replace('T', ' ');
|
|
return value.slice(0, 19);
|
|
}
|
|
|
|
/**
|
|
* 生成带 get 方法的 Sequelize 模型行对象。
|
|
*
|
|
* @param {Object} plain 原始对象
|
|
* @return {Object} 模型行对象
|
|
*/
|
|
function createModelRow(plain) {
|
|
return {
|
|
get() {
|
|
return plain;
|
|
},
|
|
};
|
|
}
|
|
|
|
/**
|
|
* 创建库存列表服务测试支架。
|
|
*
|
|
* @param {Object} [options={}] 测试选项
|
|
* @param {Object[]} [options.batchRows=[]] 批次模式返回行
|
|
* @param {Object[]} [options.mergedRows=[]] 合并模式返回行
|
|
* @param {number} [options.batchTotal] 批次模式总数
|
|
* @param {number} [options.mergedTotal] 合并模式总数
|
|
* @return {Object} 测试支架
|
|
*/
|
|
function createServiceHarness(options = {}) {
|
|
const batchRows = Array.isArray(options.batchRows) ? options.batchRows.map(createModelRow) : [];
|
|
const mergedRows = Array.isArray(options.mergedRows) ? options.mergedRows : [];
|
|
const calls = {
|
|
findAndCountAllArgs: null,
|
|
countArgs: null,
|
|
findAllArgs: null,
|
|
stockBarcodeArgs: [],
|
|
fileReferenceArgs: [],
|
|
};
|
|
|
|
const Op = {
|
|
and: '$and',
|
|
gt: '$gt',
|
|
ne: '$ne',
|
|
in: '$in',
|
|
};
|
|
|
|
const stockBarcodes = Array.isArray(options.stockBarcodes) ? options.stockBarcodes : [];
|
|
const fileReferences = Array.isArray(options.fileReferences) ? options.fileReferences : [];
|
|
|
|
const model = {
|
|
Storage: {
|
|
async findAndCountAll(args) {
|
|
calls.findAndCountAllArgs = args;
|
|
return {
|
|
count: options.batchTotal ?? batchRows.length,
|
|
rows: batchRows,
|
|
};
|
|
},
|
|
async count(args) {
|
|
calls.countArgs = args;
|
|
return options.mergedTotal ?? mergedRows.length;
|
|
},
|
|
async findAll(args) {
|
|
calls.findAllArgs = args;
|
|
return mergedRows;
|
|
},
|
|
},
|
|
StockWarehouse: {},
|
|
StockBarcode: {
|
|
async findAll(args) {
|
|
calls.stockBarcodeArgs.push(args);
|
|
const stockIdCondition = args?.where?.stock_id || {};
|
|
const stockIds = Array.isArray(stockIdCondition[Op.in]) ? stockIdCondition[Op.in].map(Number) : [];
|
|
return stockBarcodes.filter(item => stockIds.includes(Number(item.stock_id)));
|
|
},
|
|
},
|
|
FileReference: {
|
|
async findAll(args) {
|
|
calls.fileReferenceArgs.push(args);
|
|
const businessIdCondition = args?.where?.business_id || {};
|
|
const businessIds = Array.isArray(businessIdCondition[Op.in]) ? businessIdCondition[Op.in].map(Number) : [];
|
|
return fileReferences.filter(item => businessIds.includes(Number(item.business_id)));
|
|
},
|
|
},
|
|
File: {},
|
|
LocationSpace: {},
|
|
};
|
|
|
|
const app = {
|
|
Sequelize: {
|
|
Op,
|
|
fn(name, field) {
|
|
return { fn: name, field };
|
|
},
|
|
col(value) {
|
|
return { col: value };
|
|
},
|
|
literal(value) {
|
|
return { literal: value };
|
|
},
|
|
},
|
|
model,
|
|
formatDate(value) {
|
|
return String(value).slice(0, 10);
|
|
},
|
|
};
|
|
|
|
const ctx = {
|
|
app,
|
|
model,
|
|
user: {
|
|
store_id: 100000,
|
|
},
|
|
whereParsed: {
|
|
dynamicConditions: [],
|
|
},
|
|
logger: {
|
|
info() {},
|
|
warn() {},
|
|
error() {},
|
|
},
|
|
};
|
|
|
|
return {
|
|
app,
|
|
ctx,
|
|
model,
|
|
calls,
|
|
};
|
|
}
|
|
|
|
describe('test/app/service/systems/storage.inventory.test.js', () => {
|
|
it('should return production and expiry dates with warning flag for batch list', async () => {
|
|
const productionDate = toDateTimeString(-6);
|
|
const expiryDate = toDateTimeString(4);
|
|
const expectedProductionDate = productionDate.slice(0, 10);
|
|
const expectedExpiryDate = expiryDate.slice(0, 10);
|
|
const harness = createServiceHarness({
|
|
batchRows: [{
|
|
id: 1,
|
|
stock_id: 88,
|
|
space_id: 9,
|
|
inventory_type: 'ZP',
|
|
warehouse_id: 100000,
|
|
store_id: 100001,
|
|
quantity: 12,
|
|
locked_quantity: 2,
|
|
available_quantity: 10,
|
|
created_at: toDateTimeString(-10),
|
|
updated_at: toDateTimeString(-1),
|
|
inbound_time: toDateTimeString(-3),
|
|
production_date: productionDate,
|
|
expiry_date: expiryDate,
|
|
stock: {
|
|
id: 88,
|
|
item_code: 'SKU-88',
|
|
item_name: '测试商品',
|
|
sku_property: '默认规格',
|
|
stock_unit: '件',
|
|
is_batch_mgmt: 'Y',
|
|
is_shelf_life_mgmt: 'Y',
|
|
remaining_threshold_days: 5,
|
|
store_stock: {
|
|
store_id: 100001,
|
|
store_remask: '测试货主名称',
|
|
},
|
|
},
|
|
space: {
|
|
id: 9,
|
|
space_code: 'A-01',
|
|
custom_name: '一号位',
|
|
},
|
|
}],
|
|
stockBarcodes: [
|
|
{ stock_id: 88, barcode: 'BC-88-A' },
|
|
{ stock_id: 88, barcode: 'BC-88-B' },
|
|
],
|
|
fileReferences: [
|
|
{
|
|
business_id: 88,
|
|
file_id: 70088,
|
|
file: {
|
|
file_url: 'https://example.com/stock-88-a.jpg',
|
|
},
|
|
},
|
|
{
|
|
business_id: 88,
|
|
file_id: 70089,
|
|
file: {
|
|
file_url: 'https://example.com/stock-88-b.jpg',
|
|
},
|
|
},
|
|
],
|
|
});
|
|
const service = new StorageInventoryService(harness.ctx);
|
|
|
|
const result = await service.getInventoryList(
|
|
{ page: 1, pageSize: 10 },
|
|
{ sortField: 'created_at', sortOrder: 'DESC' },
|
|
true
|
|
);
|
|
|
|
assert.equal(result.list.length, 1);
|
|
assert.equal(result.pagination.page, 1);
|
|
assert.equal(result.pagination.pageSize, 10);
|
|
assert.equal(result.pagination.total, 1);
|
|
assert.equal(Object.prototype.hasOwnProperty.call(result.list[0], 'production_date'), true);
|
|
assert.equal(Object.prototype.hasOwnProperty.call(result.list[0], 'expiry_date'), true);
|
|
assert.equal(Object.prototype.hasOwnProperty.call(result.list[0], 'is_shelf_life_warning_triggered'), true);
|
|
assert.equal(Object.prototype.hasOwnProperty.call(result.list[0].stock, 'is_batch_mgmt_enabled'), true);
|
|
assert.equal(Object.prototype.hasOwnProperty.call(result.list[0].stock, 'is_batch_mgmt'), false);
|
|
assert.equal(result.list[0].production_date, expectedProductionDate);
|
|
assert.equal(result.list[0].expiry_date, expectedExpiryDate);
|
|
assert.equal(result.list[0].is_shelf_life_warning_triggered, true);
|
|
assert.equal(result.list[0].stock.is_batch_mgmt_enabled, true);
|
|
assert.equal(result.list[0].stock.store_name, '测试货主名称');
|
|
assert.equal(Object.prototype.hasOwnProperty.call(result.list[0].stock, 'store_stock'), false);
|
|
assert.deepEqual(result.list[0].stock.barcodes, [ 'BC-88-A', 'BC-88-B' ]);
|
|
assert.deepEqual(result.list[0].stock.images, [
|
|
'https://example.com/stock-88-a.jpg',
|
|
'https://example.com/stock-88-b.jpg',
|
|
]);
|
|
assert.ok(harness.calls.findAndCountAllArgs);
|
|
assert.ok(harness.calls.findAndCountAllArgs.include[0].attributes.includes('is_shelf_life_mgmt'));
|
|
assert.ok(harness.calls.findAndCountAllArgs.include[0].attributes.includes('is_batch_mgmt'));
|
|
assert.ok(harness.calls.findAndCountAllArgs.include[0].attributes.includes('remaining_threshold_days'));
|
|
assert.ok(Array.isArray(harness.calls.findAndCountAllArgs.include[0].include));
|
|
assert.equal(harness.calls.findAndCountAllArgs.include[0].include[0].as, 'store_stock');
|
|
assert.deepEqual(harness.calls.findAndCountAllArgs.include[0].include[0].attributes, [ 'store_id', 'store_remask' ]);
|
|
assert.equal(harness.calls.stockBarcodeArgs.length, 1);
|
|
assert.equal(harness.calls.fileReferenceArgs.length, 1);
|
|
});
|
|
|
|
it('should return warning flag false when shelf-life management is disabled', async () => {
|
|
const productionDate = toDateTimeString(-4);
|
|
const expiryDate = toDateTimeString(2);
|
|
const expectedProductionDate = productionDate.slice(0, 10);
|
|
const expectedExpiryDate = expiryDate.slice(0, 10);
|
|
const harness = createServiceHarness({
|
|
batchRows: [{
|
|
id: 2,
|
|
stock_id: 89,
|
|
space_id: 10,
|
|
inventory_type: 'ZP',
|
|
warehouse_id: 100000,
|
|
store_id: 100001,
|
|
quantity: 8,
|
|
locked_quantity: 0,
|
|
available_quantity: 8,
|
|
created_at: toDateTimeString(-8),
|
|
updated_at: toDateTimeString(-2),
|
|
inbound_time: toDateTimeString(-2),
|
|
production_date: productionDate,
|
|
expiry_date: expiryDate,
|
|
stock: {
|
|
id: 89,
|
|
item_code: 'SKU-89',
|
|
item_name: '未启用保质期管理商品',
|
|
sku_property: '默认规格',
|
|
stock_unit: '箱',
|
|
is_batch_mgmt: 'N',
|
|
is_shelf_life_mgmt: 'N',
|
|
remaining_threshold_days: 5,
|
|
},
|
|
space: {
|
|
id: 10,
|
|
space_code: 'B-01',
|
|
custom_name: '二号位',
|
|
},
|
|
}],
|
|
stockBarcodes: [],
|
|
fileReferences: [],
|
|
});
|
|
const service = new StorageInventoryService(harness.ctx);
|
|
|
|
const result = await service.getInventoryList({}, {}, true);
|
|
|
|
assert.equal(result.list.length, 1);
|
|
assert.equal(result.list[0].is_shelf_life_warning_triggered, false);
|
|
assert.equal(result.list[0].stock.is_batch_mgmt_enabled, false);
|
|
assert.equal(result.list[0].stock.store_name, null);
|
|
assert.equal(Object.prototype.hasOwnProperty.call(result.list[0].stock, 'is_batch_mgmt'), false);
|
|
assert.equal(result.list[0].production_date, expectedProductionDate);
|
|
assert.equal(result.list[0].expiry_date, expectedExpiryDate);
|
|
assert.deepEqual(result.list[0].stock.barcodes, []);
|
|
assert.deepEqual(result.list[0].stock.images, []);
|
|
});
|
|
|
|
it('should return merged list with earliest production and expiry dates', async () => {
|
|
const productionDate = toDateTimeString(-9);
|
|
const expiryDate = toDateTimeString(3);
|
|
const harness = createServiceHarness({
|
|
mergedTotal: 1,
|
|
mergedRows: [{
|
|
stock_id: 90,
|
|
space_id: 11,
|
|
inventory_type: 'ZP',
|
|
warehouse_id: 100000,
|
|
store_id: 100001,
|
|
quantity: 20,
|
|
locked_quantity: 3,
|
|
available_quantity: 17,
|
|
production_date: productionDate,
|
|
expiry_date: expiryDate,
|
|
first_inbound_time: toDateTimeString(-7),
|
|
last_inbound_time: toDateTimeString(-1),
|
|
batch_count: 2,
|
|
stock: {
|
|
id: 90,
|
|
item_code: 'SKU-90',
|
|
item_name: '合并库存商品',
|
|
sku_property: '默认规格',
|
|
stock_unit: '件',
|
|
is_batch_mgmt: 'Y',
|
|
is_shelf_life_mgmt: 'Y',
|
|
remaining_threshold_days: 5,
|
|
store_stock: {
|
|
store_id: 100002,
|
|
store_remask: '合并货主名称',
|
|
},
|
|
},
|
|
space: {
|
|
id: 11,
|
|
space_code: 'C-01',
|
|
custom_name: '三号位',
|
|
},
|
|
}],
|
|
stockBarcodes: [
|
|
{ stock_id: 90, barcode: 'BC-90-A' },
|
|
],
|
|
fileReferences: [
|
|
{
|
|
business_id: 90,
|
|
file_id: 70090,
|
|
file: {
|
|
file_url: 'https://example.com/stock-90-a.jpg',
|
|
},
|
|
},
|
|
],
|
|
});
|
|
const service = new StorageInventoryService(harness.ctx);
|
|
|
|
const result = await service.getInventoryList(
|
|
{ page: 2, pageSize: 5 },
|
|
{ sortField: 'last_inbound_time', sortOrder: 'ASC' },
|
|
false
|
|
);
|
|
|
|
assert.equal(result.list.length, 1);
|
|
assert.equal(result.pagination.page, 2);
|
|
assert.equal(result.pagination.pageSize, 5);
|
|
assert.equal(result.pagination.total, 1);
|
|
assert.equal(Object.prototype.hasOwnProperty.call(result.list[0], 'production_date'), true);
|
|
assert.equal(Object.prototype.hasOwnProperty.call(result.list[0], 'expiry_date'), true);
|
|
assert.equal(Object.prototype.hasOwnProperty.call(result.list[0], 'is_shelf_life_warning_triggered'), true);
|
|
assert.equal(Object.prototype.hasOwnProperty.call(result.list[0].stock, 'is_batch_mgmt_enabled'), true);
|
|
assert.equal(Object.prototype.hasOwnProperty.call(result.list[0].stock, 'is_batch_mgmt'), false);
|
|
assert.equal(result.list[0].production_date, productionDate.slice(0, 10));
|
|
assert.equal(result.list[0].expiry_date, expiryDate.slice(0, 10));
|
|
assert.equal(result.list[0].is_shelf_life_warning_triggered, true);
|
|
assert.equal(result.list[0].stock.is_batch_mgmt_enabled, true);
|
|
assert.equal(result.list[0].stock.store_name, '合并货主名称');
|
|
assert.equal(Object.prototype.hasOwnProperty.call(result.list[0].stock, 'store_stock'), false);
|
|
assert.deepEqual(result.list[0].stock.barcodes, [ 'BC-90-A' ]);
|
|
assert.deepEqual(result.list[0].stock.images, [ 'https://example.com/stock-90-a.jpg' ]);
|
|
assert.ok(harness.calls.findAllArgs);
|
|
assert.ok(harness.calls.findAllArgs.attributes.some(item => Array.isArray(item) && item[1] === 'production_date' && item[0].fn === 'MIN'));
|
|
assert.ok(harness.calls.findAllArgs.attributes.some(item => Array.isArray(item) && item[1] === 'expiry_date' && item[0].fn === 'MIN'));
|
|
assert.ok(harness.calls.findAllArgs.include[0].attributes.includes('is_batch_mgmt'));
|
|
assert.ok(Array.isArray(harness.calls.findAllArgs.include[0].include));
|
|
assert.equal(harness.calls.findAllArgs.include[0].include[0].as, 'store_stock');
|
|
assert.deepEqual(harness.calls.findAllArgs.include[0].include[0].attributes, [ 'store_id', 'store_remask' ]);
|
|
assert.equal(harness.calls.stockBarcodeArgs.length, 1);
|
|
assert.equal(harness.calls.fileReferenceArgs.length, 1);
|
|
});
|
|
});
|