WMS_MAIN/test/app/service/systems/storage.receiving.review.test.js

152 lines
3.9 KiB
JavaScript

'use strict';
const assert = require('node:assert/strict');
const { Op } = require('sequelize');
const ReceivingService = require('../../../../app/service/systems/storage/receiving');
function createReceivingRow(overrides = {}) {
return {
id: 101,
warehouse_id: 100000,
receiving_number: 'RCV202604300001',
status: 'completed',
detail_status: 'all_matched',
storage_status: 'none',
store_id: 200001,
is_review: 0,
...overrides,
};
}
function createHarness(options = {}) {
const rows = (options.rows || []).map(item => ({ ...item }));
const calls = {
updateArgs: [],
transactionCommitCount: 0,
transactionRollbackCount: 0,
operationLogs: [],
};
const transaction = {
LOCK: {
UPDATE: 'UPDATE',
},
async commit() {
calls.transactionCommitCount += 1;
},
async rollback() {
calls.transactionRollbackCount += 1;
},
};
const ctx = {
app: {
Sequelize: {
Op,
},
},
user: {
store_id: 100000,
employees_id: 9001,
employees_name: '审核员A',
id: 9001,
},
logger: {
info() {},
warn() {},
error() {},
},
service: {
systems: {
operationLog: {
async createOperationLog(payload) {
calls.operationLogs.push(payload);
},
},
},
},
model: {
Sequelize: {
Op,
},
async transaction() {
return transaction;
},
Receiving: {
async findAll() {
return rows;
},
async update(values, optionsArg = {}) {
calls.updateArgs.push({
values: { ...values },
where: optionsArg.where,
});
const affectedRows = options.updatedRows !== undefined
? Number(options.updatedRows)
: rows.length;
if (affectedRows > 0) {
rows.slice(0, affectedRows).forEach(row => {
Object.assign(row, values);
});
}
return [ affectedRows ];
},
},
},
};
const service = new ReceivingService(ctx);
service.sendReviewNotice = async () => ({ success: true, data: { sentCount: 0, failedCount: 0, matchedHookCount: 0 } });
service.uploadReviewExcelReport = async receiving => ({
fileKey: `${receiving.receiving_number}.xlsx`,
url: `https://cdn.example.com/${receiving.receiving_number}.xlsx`,
});
return {
service,
rows,
calls,
};
}
describe('test/app/service/systems/storage.receiving.review.test.js', () => {
it('should review completed receivings even when legacy is_review is null', async () => {
const { service, rows, calls } = createHarness({
rows: [ createReceivingRow({ id: 201, receiving_number: 'RCV-201', is_review: null }) ],
});
const result = await service.reviewReceiving({ ids: [ 201 ] });
assert.equal(result.code, 200);
assert.equal(result.message, '审核成功');
assert.equal(Number(rows[0].is_review), 1);
assert.equal(rows[0].reviewer_name, '审核员A');
assert.equal(Number(rows[0].reviewer_employee_id), 9001);
assert.equal(calls.transactionCommitCount, 1);
assert.equal(calls.transactionRollbackCount, 0);
assert.equal(calls.updateArgs.length, 1);
assert.deepEqual(calls.updateArgs[0].where[Op.or], [
{ is_review: 0 },
{ is_review: null },
]);
});
it('should fail when affected rows are fewer than requested ids', async () => {
const { service, rows, calls } = createHarness({
rows: [ createReceivingRow({ id: 301, receiving_number: 'RCV-301', is_review: null }) ],
updatedRows: 0,
});
const result = await service.reviewReceiving({ ids: [ 301 ] });
assert.equal(result.code, 0);
assert.match(String(result.message || ''), /审核状态已变化/);
assert.equal(rows[0].is_review, null);
assert.equal(calls.transactionCommitCount, 0);
assert.equal(calls.transactionRollbackCount, 1);
});
});