'use strict'; const assert = require('node:assert/strict'); const ReceivingService = require('../../../../app/service/systems/storage/receiving'); function createReceivingRow(overrides = {}) { return { id: 101, warehouse_id: 100000, receiving_number: 'RCV202604300001', status: 'receiving', delivery_method: 'truck', license_plate: '粤A12345', loading_method: 'without_pallet', remark: 'old remark', store_id: 200001, store_name: '旧货主', total_boxes: 1, total_pallets: 0, tracking_number: null, video_device_id: null, is_deleted: false, ...overrides, }; } function createHarness(options = {}) { const receivingRow = createReceivingRow(options.receiving || {}); const calls = { transactionCommitCount: 0, transactionRollbackCount: 0, updateArgs: [], operationLogs: [], }; const transaction = { LOCK: { UPDATE: 'UPDATE', }, async commit() { calls.transactionCommitCount += 1; }, async rollback() { calls.transactionRollbackCount += 1; }, }; const storeRelation = options.storeRelation === undefined ? { store_id: 300001, store_remask: '新货主' } : options.storeRelation; const ctx = { app: { config: {}, }, user: { store_id: 100000, employees_id: 9001, employees_name: '测试员A', id: 9001, }, logger: { warn() {}, error() {}, info() {}, }, service: { systems: { operationLog: { async createOperationLog(payload) { calls.operationLogs.push(payload); }, }, }, }, model: { async transaction() { return transaction; }, Receiving: { async findOne({ where = {} }) { if (Number(where.id) !== Number(receivingRow.id) || Number(where.warehouse_id) !== Number(receivingRow.warehouse_id) || receivingRow.is_deleted) { return null; } return receivingRow; }, async update(values, optionsArg = {}) { calls.updateArgs.push({ values: { ...values }, where: optionsArg.where, }); Object.assign(receivingRow, values); return [ 1 ]; }, }, StoreWarehouse: { async findOne() { return storeRelation; }, }, }, }; return { service: new ReceivingService(ctx), receivingRow, calls, }; } describe('test/app/service/systems/storage.receiving.save.test.js', () => { it('should update receiving by id', async () => { const { service, receivingRow, calls } = createHarness(); const result = await service.save({ id: 101, delivery_method: 'express', tracking_number: 'YT123456789', loading_method: 'with_pallet', remark: 'new remark', store_id: 300001, total_boxes: 8, total_pallets: 2, video_device_id: 'CAM-01', }); assert.equal(result.code, 200); assert.equal(result.id, 101); assert.equal(result.receiving_number, 'RCV202604300001'); assert.equal(result.message, '收货单修改成功'); assert.equal(receivingRow.delivery_method, 'express'); assert.equal(receivingRow.license_plate, null); assert.equal(receivingRow.tracking_number, 'YT123456789'); assert.equal(receivingRow.loading_method, 'with_pallet'); assert.equal(receivingRow.remark, 'new remark'); assert.equal(receivingRow.store_id, 300001); assert.equal(receivingRow.store_name, '新货主'); assert.equal(receivingRow.total_boxes, 8); assert.equal(receivingRow.total_pallets, 2); assert.equal(receivingRow.video_device_id, 'CAM-01'); assert.equal(calls.transactionCommitCount, 1); assert.equal(calls.transactionRollbackCount, 0); assert.equal(calls.operationLogs.length, 1); }); it('should reject invalid store relation', async () => { const { service, receivingRow, calls } = createHarness({ storeRelation: null, }); const result = await service.save({ id: 101, store_id: 999999, }); assert.equal(result.code, 404); assert.match(String(result.message || ''), /货主不存在|签约关系/); assert.equal(receivingRow.store_id, 200001); assert.equal(calls.transactionCommitCount, 0); assert.equal(calls.transactionRollbackCount, 1); }); });