199 lines
6.1 KiB
JavaScript
199 lines
6.1 KiB
JavaScript
'use strict';
|
|
|
|
const assert = require('node:assert/strict');
|
|
const { Op } = require('sequelize');
|
|
const LocationZoneService = require('../../../../app/service/systems/location_zone');
|
|
|
|
function createHarness(options = {}) {
|
|
const calls = {
|
|
transactionCalls: 0,
|
|
zoneDestroyQueries: [],
|
|
spaceDestroyQueries: [],
|
|
storageCountQueries: [],
|
|
};
|
|
|
|
const zoneRows = (options.zones || []).map(item => ({
|
|
...item,
|
|
deleted_at: item.deleted_at || null,
|
|
}));
|
|
const spaceRows = (options.spaces || []).map(item => ({
|
|
...item,
|
|
deleted_at: item.deleted_at || null,
|
|
}));
|
|
const storageRows = (options.storages || []).map(item => ({
|
|
delete_at: null,
|
|
is_enabled: 1,
|
|
quantity: 0,
|
|
locked_quantity: 0,
|
|
available_quantity: 0,
|
|
...item,
|
|
}));
|
|
|
|
const ctx = {
|
|
logger: {
|
|
error() {},
|
|
},
|
|
app: {
|
|
Sequelize: {
|
|
Op,
|
|
},
|
|
},
|
|
model: {
|
|
async transaction(handler) {
|
|
calls.transactionCalls += 1;
|
|
const transaction = {
|
|
async commit() {},
|
|
async rollback() {},
|
|
};
|
|
|
|
if (typeof handler === 'function') {
|
|
return await handler(transaction);
|
|
}
|
|
|
|
return transaction;
|
|
},
|
|
LocationZone: {
|
|
async findOne({ where = {} }) {
|
|
return zoneRows.find(row => (
|
|
Number(row.id) === Number(where.id) &&
|
|
(where.deleted_at !== null ? row.deleted_at === null : true)
|
|
)) || null;
|
|
},
|
|
async findAll({ where = {}, raw = false }) {
|
|
let rows = zoneRows.filter(row => row.deleted_at === null);
|
|
|
|
if (where.parent_id && where.parent_id[Op.in]) {
|
|
const parentIds = where.parent_id[Op.in].map(item => Number(item));
|
|
rows = rows.filter(row => parentIds.includes(Number(row.parent_id)));
|
|
} else if (Object.prototype.hasOwnProperty.call(where, 'parent_id')) {
|
|
rows = rows.filter(row => Number(row.parent_id) === Number(where.parent_id));
|
|
}
|
|
|
|
return raw ? rows.map(row => ({ ...row })) : rows.map(row => ({ ...row }));
|
|
},
|
|
async destroy({ where = {} }) {
|
|
calls.zoneDestroyQueries.push(where);
|
|
const ids = where.id && where.id[Op.in]
|
|
? where.id[Op.in].map(item => Number(item))
|
|
: [];
|
|
|
|
zoneRows.forEach(row => {
|
|
if (ids.includes(Number(row.id))) {
|
|
row.deleted_at = new Date();
|
|
}
|
|
});
|
|
|
|
return ids.length;
|
|
},
|
|
},
|
|
LocationSpace: {
|
|
async findAll({ where = {}, raw = false }) {
|
|
let rows = spaceRows.filter(row => row.deleted_at === null);
|
|
|
|
if (where.zone_id && where.zone_id[Op.in]) {
|
|
const zoneIds = where.zone_id[Op.in].map(item => Number(item));
|
|
rows = rows.filter(row => zoneIds.includes(Number(row.zone_id)));
|
|
} else if (Object.prototype.hasOwnProperty.call(where, 'zone_id')) {
|
|
rows = rows.filter(row => Number(row.zone_id) === Number(where.zone_id));
|
|
}
|
|
|
|
return raw ? rows.map(row => ({ ...row })) : rows.map(row => ({ ...row }));
|
|
},
|
|
async destroy({ where = {} }) {
|
|
calls.spaceDestroyQueries.push(where);
|
|
const ids = where.id && where.id[Op.in]
|
|
? where.id[Op.in].map(item => Number(item))
|
|
: [];
|
|
|
|
spaceRows.forEach(row => {
|
|
if (ids.includes(Number(row.id))) {
|
|
row.deleted_at = new Date();
|
|
}
|
|
});
|
|
|
|
return ids.length;
|
|
},
|
|
},
|
|
Storage: {
|
|
async count({ where = {} }) {
|
|
calls.storageCountQueries.push(where);
|
|
const spaceIds = where.space_id && where.space_id[Op.in]
|
|
? where.space_id[Op.in].map(item => Number(item))
|
|
: [];
|
|
|
|
return storageRows.filter(row => {
|
|
if (!spaceIds.includes(Number(row.space_id))) {
|
|
return false;
|
|
}
|
|
if (row.delete_at !== null) {
|
|
return false;
|
|
}
|
|
if (Number(row.is_enabled) !== 1) {
|
|
return false;
|
|
}
|
|
|
|
return Number(row.quantity) > 0 || Number(row.locked_quantity) > 0 || Number(row.available_quantity) > 0;
|
|
}).length;
|
|
},
|
|
},
|
|
},
|
|
};
|
|
|
|
return {
|
|
service: new LocationZoneService(ctx),
|
|
zoneRows,
|
|
spaceRows,
|
|
calls,
|
|
};
|
|
}
|
|
|
|
describe('test/app/service/systems/location_zone.deleteZone.test.js', () => {
|
|
it('should cascade soft delete descendant racks and spaces when deleting an area', async () => {
|
|
const { service, zoneRows, spaceRows, calls } = createHarness({
|
|
zones: [
|
|
{ id: 10, parent_id: 1, zone_type: 'area' },
|
|
{ id: 11, parent_id: 10, zone_type: 'rack' },
|
|
{ id: 12, parent_id: 11, zone_type: 'obstacle' },
|
|
],
|
|
spaces: [
|
|
{ id: 101, zone_id: 11 },
|
|
{ id: 102, zone_id: 11 },
|
|
],
|
|
});
|
|
|
|
const result = await service.deleteZone(10);
|
|
|
|
assert.equal(result.success, true);
|
|
assert.equal(result.message, '删除成功');
|
|
assert.equal(calls.transactionCalls, 1);
|
|
assert.deepEqual(calls.spaceDestroyQueries[0].id[Op.in], [ 101, 102 ]);
|
|
assert.deepEqual(calls.zoneDestroyQueries[0].id[Op.in], [ 10, 11, 12 ]);
|
|
assert.equal(zoneRows.every(row => row.deleted_at instanceof Date), true);
|
|
assert.equal(spaceRows.every(row => row.deleted_at instanceof Date), true);
|
|
});
|
|
|
|
it('should reject deleting a rack when any affected space still has stock', async () => {
|
|
const { service, zoneRows, spaceRows, calls } = createHarness({
|
|
zones: [
|
|
{ id: 20, parent_id: 2, zone_type: 'rack' },
|
|
],
|
|
spaces: [
|
|
{ id: 201, zone_id: 20 },
|
|
],
|
|
storages: [
|
|
{ id: 1, space_id: 201, quantity: 3 },
|
|
],
|
|
});
|
|
|
|
await assert.rejects(async () => {
|
|
await service.deleteZone(20);
|
|
}, /删除范围内库位有库存,禁止删除,请先移库/);
|
|
|
|
assert.equal(calls.transactionCalls, 1);
|
|
assert.equal(calls.spaceDestroyQueries.length, 0);
|
|
assert.equal(calls.zoneDestroyQueries.length, 0);
|
|
assert.equal(zoneRows[0].deleted_at, null);
|
|
assert.equal(spaceRows[0].deleted_at, null);
|
|
});
|
|
});
|