'use strict'; const assert = require('node:assert/strict'); const ReceivingService = require('../../../../app/service/systems/storage/receiving'); function createReceivingRow(overrides = {}) { return { id: 501, warehouse_id: 100000, receiving_number: 'RCV-501', status: 'completed', is_review: 0, storage_status: 'none', creator_snapshot: JSON.stringify({ id: 3001, employees_id: 3001, name: '创建人A', }), ...overrides, }; } function createHarness(options = {}) { const rows = (options.rows || []).map(item => ({ ...item })); const noticeCalls = []; const operationLogs = []; const updateCalls = []; const ctx = { app: { config: {}, }, user: { store_id: 100000, store_name: '测试仓库', employees_id: 9001, employees_name: '审核员A', id: 9001, }, logger: { warn() {}, error() {}, info() {}, }, service: { systems: { operationLog: { async createOperationLog(payload) { operationLogs.push(payload); }, }, notice: { async send(payload) { noticeCalls.push(payload); return { success: true }; }, }, }, }, model: { Sequelize: { Op: { in: 'in', }, }, Receiving: { async findAll() { return rows; }, async update(values) { updateCalls.push({ ...values }); rows.forEach(row => Object.assign(row, values)); return [ rows.length ]; }, }, }, }; return { service: new ReceivingService(ctx), rows, noticeCalls, operationLogs, updateCalls, }; } describe('test/app/service/systems/storage.receiving.reject.test.js', () => { it('should send one reject notice per receiving with creator info and identifiers', async () => { const { service, noticeCalls, operationLogs, updateCalls } = createHarness({ rows: [ createReceivingRow({ id: 501, receiving_number: 'RCV-501' }), createReceivingRow({ id: 502, receiving_number: 'RCV-502', creator_snapshot: JSON.stringify({ id: 3002, employees_id: 3002, name: '创建人B', }), }), ], }); const result = await service.rejectReceiving({ ids: [ 501, 502 ], remark: '资料不完整', }); assert.equal(result.code, 200); assert.equal(result.message, '驳回成功'); assert.equal(updateCalls.length, 1); assert.equal(Object.prototype.hasOwnProperty.call(updateCalls[0], 'is_review'), false); assert.equal(operationLogs.length, 2); assert.equal(noticeCalls.length, 2); const firstNotice = noticeCalls.find(item => Number(item.businessId) === 501); const secondNotice = noticeCalls.find(item => Number(item.businessId) === 502); assert.ok(firstNotice); assert.ok(secondNotice); assert.equal(firstNotice.event, 'receiving_rejected'); assert.match(String(firstNotice.title || ''), /RCV-501|501/); assert.match(String(firstNotice.content || ''), /收货单ID:501/); assert.match(String(firstNotice.content || ''), /收货单号:RCV-501/); assert.match(String(firstNotice.content || ''), /收货创建人:创建人A/); assert.deepEqual(firstNotice.metadata.receiving_ids, [ 501 ]); assert.deepEqual(firstNotice.metadata.receiving_numbers, [ 'RCV-501' ]); assert.deepEqual(firstNotice.metadata.receiving_creators, [ { id: 3001, employees_id: 3001, name: '创建人A' }, ]); assert.deepEqual(firstNotice.metadata.receivings, [ { id: 501, receiving_number: 'RCV-501', creator: { id: 3001, employees_id: 3001, name: '创建人A' }, }, ]); assert.equal(secondNotice.event, 'receiving_rejected'); assert.match(String(secondNotice.title || ''), /RCV-502|502/); assert.match(String(secondNotice.content || ''), /收货单ID:502/); assert.match(String(secondNotice.content || ''), /收货单号:RCV-502/); assert.match(String(secondNotice.content || ''), /收货创建人:创建人B/); assert.deepEqual(secondNotice.metadata.receiving_ids, [ 502 ]); assert.deepEqual(secondNotice.metadata.receiving_numbers, [ 'RCV-502' ]); assert.deepEqual(secondNotice.metadata.receiving_creators, [ { id: 3002, employees_id: 3002, name: '创建人B' }, ]); assert.deepEqual(secondNotice.metadata.receivings, [ { id: 502, receiving_number: 'RCV-502', creator: { id: 3002, employees_id: 3002, name: '创建人B' }, }, ]); }); });