172 lines
4.7 KiB
JavaScript
172 lines
4.7 KiB
JavaScript
'use strict';
|
|
|
|
const assert = require('node:assert/strict');
|
|
const EntryService = require('../../../../app/service/systems/storage/entry');
|
|
|
|
function createOrder(id, orderStatus = 'CREATED') {
|
|
return {
|
|
id,
|
|
warehouse_id: 1001,
|
|
is_enabled: 1,
|
|
deleted_at: null,
|
|
order_status: orderStatus,
|
|
updateCalls: [],
|
|
async update(values, options = {}) {
|
|
this.updateCalls.push({
|
|
values,
|
|
options,
|
|
});
|
|
Object.assign(this, values);
|
|
return this;
|
|
},
|
|
};
|
|
}
|
|
|
|
function createServiceHarness(orders = []) {
|
|
const orderMap = new Map(orders.map(order => [ Number(order.id), order ]));
|
|
const transaction = {
|
|
commitCalls: 0,
|
|
rollbackCalls: 0,
|
|
async commit() {
|
|
this.commitCalls += 1;
|
|
},
|
|
async rollback() {
|
|
this.rollbackCalls += 1;
|
|
},
|
|
};
|
|
|
|
const ctx = {
|
|
user: {
|
|
store_id: 1001,
|
|
},
|
|
app: {
|
|
Sequelize: {
|
|
Op: {},
|
|
},
|
|
coreLogger: {
|
|
warn() {},
|
|
error() {},
|
|
},
|
|
},
|
|
model: {
|
|
async transaction() {
|
|
return transaction;
|
|
},
|
|
EntryOrder: {
|
|
async findOne({ where = {} }) {
|
|
const order = orderMap.get(Number(where.id));
|
|
if (!order) {
|
|
return null;
|
|
}
|
|
|
|
if (Number(where.warehouse_id) !== Number(order.warehouse_id)) {
|
|
return null;
|
|
}
|
|
|
|
if (Object.prototype.hasOwnProperty.call(where, 'is_enabled') && Number(where.is_enabled) !== Number(order.is_enabled)) {
|
|
return null;
|
|
}
|
|
|
|
if (Object.prototype.hasOwnProperty.call(where, 'deleted_at') && where.deleted_at === null && order.deleted_at !== null) {
|
|
return null;
|
|
}
|
|
|
|
return order;
|
|
},
|
|
},
|
|
},
|
|
};
|
|
|
|
const service = new EntryService(ctx);
|
|
|
|
return {
|
|
service,
|
|
transaction,
|
|
orders: orderMap,
|
|
};
|
|
}
|
|
|
|
describe('test/app/service/systems/storage.entry.cancel.test.js', () => {
|
|
it('should cancel single entry order by id', async () => {
|
|
const order = createOrder(11);
|
|
const { service, transaction } = createServiceHarness([ order ]);
|
|
|
|
const result = await service.cancel({
|
|
id: 11,
|
|
});
|
|
|
|
assert.equal(result.code, 200);
|
|
assert.equal(result.message, '作废成功');
|
|
assert.deepEqual(result.data, { id: 11 });
|
|
assert.equal(order.order_status, 'CANCELLED');
|
|
assert.equal(order.updateCalls.length, 1);
|
|
assert.equal(transaction.commitCalls, 1);
|
|
assert.equal(transaction.rollbackCalls, 0);
|
|
});
|
|
|
|
it('should cancel batch entry orders by ids', async () => {
|
|
const orderA = createOrder(21);
|
|
const orderB = createOrder(22);
|
|
const { service, transaction } = createServiceHarness([ orderA, orderB ]);
|
|
|
|
const result = await service.cancel({
|
|
ids: [ 21, 22 ],
|
|
});
|
|
|
|
assert.equal(result.code, 200);
|
|
assert.equal(result.message, '作废成功');
|
|
assert.deepEqual(result.data, { ids: [ 21, 22 ] });
|
|
assert.equal(orderA.order_status, 'CANCELLED');
|
|
assert.equal(orderB.order_status, 'CANCELLED');
|
|
assert.equal(orderA.updateCalls.length, 1);
|
|
assert.equal(orderB.updateCalls.length, 1);
|
|
assert.equal(transaction.commitCalls, 1);
|
|
assert.equal(transaction.rollbackCalls, 0);
|
|
});
|
|
|
|
it('should reject empty or illegal ids', async () => {
|
|
const { service } = createServiceHarness();
|
|
|
|
const emptyResult = await service.cancel({
|
|
ids: [],
|
|
});
|
|
|
|
assert.equal(emptyResult.code, 400);
|
|
assert.match(String(emptyResult.message || ''), /ids 必须是非空数组/);
|
|
|
|
const illegalResult = await service.cancel({
|
|
ids: [ 'abc' ],
|
|
});
|
|
|
|
assert.equal(illegalResult.code, 400);
|
|
assert.match(String(illegalResult.message || ''), /ids 必须为正整数数组/);
|
|
});
|
|
|
|
it('should reject batch cancel when any order is missing or completed', async () => {
|
|
const existingOrder = createOrder(31);
|
|
const completedOrder = createOrder(32, 'COMPLETED');
|
|
const { service, transaction } = createServiceHarness([ existingOrder, completedOrder ]);
|
|
|
|
const missingResult = await service.cancel({
|
|
ids: [ 31, 999 ],
|
|
});
|
|
|
|
assert.equal(missingResult.code, 404);
|
|
assert.match(String(missingResult.message || ''), /入库单不存在或无权访问/);
|
|
assert.equal(existingOrder.updateCalls.length, 0);
|
|
assert.equal(transaction.commitCalls, 0);
|
|
assert.equal(transaction.rollbackCalls, 0);
|
|
|
|
const statusResult = await service.cancel({
|
|
ids: [ 31, 32 ],
|
|
});
|
|
|
|
assert.equal(statusResult.code, 400);
|
|
assert.match(String(statusResult.message || ''), /已完成入库单不允许作废/);
|
|
assert.equal(existingOrder.updateCalls.length, 0);
|
|
assert.equal(completedOrder.updateCalls.length, 0);
|
|
assert.equal(transaction.commitCalls, 0);
|
|
assert.equal(transaction.rollbackCalls, 0);
|
|
});
|
|
});
|