'use strict'; const assert = require('node:assert/strict'); const ReceivingService = require('../../../../app/service/systems/storage/receiving'); describe('test/app/service/systems/storage.receiving.getDetail.test.js', () => { it('should include box_remainder and box_weight in receiving detail items', async () => { const receivingRow = { id: 123, warehouse_id: 100000, receiving_number: 'RCV202605060001', dataValues: {}, }; const detailRows = [ { id: 501, receiving_id: 123, space_id: 3001, product_name: '测试商品A', box_remainder: 2, box_weight: 18.5, receiving_space: { id: 3001, custom_name: 'A区定制位', space_code: 'A-01-01', }, toJSON() { return { id: this.id, receiving_id: this.receiving_id, space_id: this.space_id, product_name: this.product_name, box_remainder: this.box_remainder, box_weight: this.box_weight, receiving_space: this.receiving_space, }; }, }, { id: 502, receiving_id: 123, space_id: 3002, product_name: '测试商品B', box_remainder: null, box_weight: null, receiving_space: { id: 3002, custom_name: null, space_code: 'B-02-03', }, toJSON() { return { id: this.id, receiving_id: this.receiving_id, space_id: this.space_id, product_name: this.product_name, box_remainder: this.box_remainder, box_weight: this.box_weight, receiving_space: this.receiving_space, }; }, }, ]; const model = { Receiving: { async findOne() { return receivingRow; }, }, ReceivingDetail: { async findAll() { return detailRows; }, async count() { return detailRows.length; }, }, FileReference: { async findAll({ where }) { if (where.business_type === 'receivingItem') { return [ { business_id: 501, file_id: 9001, file: { file_url: 'https://example.com/item-501.png', }, }, ]; } return []; }, }, File: {}, }; const ctx = { user: { store_id: 100000, }, app: { Sequelize: { Op: { in: 'in', }, }, model, }, model, }; const service = new ReceivingService(ctx); const result = await service.getReceivingDetail({ id: 123, pagination: { page: 1, pageSize: 10, }, }); assert.equal(result.code, 200); const receivingPayload = result.data.receiving; const details = Array.isArray(receivingPayload.details) ? receivingPayload.details : receivingPayload.dataValues.details; assert.ok(Array.isArray(details)); assert.equal(Number(details[0].box_remainder), 2); assert.equal(Number(details[0].box_weight), 18.5); assert.equal(Number(details[1].box_remainder), 0); assert.equal(Number(details[1].box_weight), 0); assert.equal(details[0].space_id, 'A区定制位'); assert.equal(details[0].space_name, 'A区定制位'); assert.equal(details[0].custom_space_name, 'A区定制位'); assert.equal(Number(details[0].space_id_raw), 3001); assert.equal(details[1].space_id, 'B-02-03'); assert.equal(details[1].space_name, 'B-02-03'); assert.equal(details[1].custom_space_name, null); assert.equal(Number(details[1].space_id_raw), 3002); }); it('should resolve store_name from contract store_remask with company_name fallback', async () => { const receivingRow = { id: 123, warehouse_id: 100000, store_id: 77, store_name: '', receiving_number: 'RCV202605060001', dataValues: {}, }; const detailRows = []; const model = { Receiving: { async findOne() { return receivingRow; }, }, ReceivingDetail: { async findAll() { return detailRows; }, async count() { return 0; }, }, FileReference: { async findAll() { return []; }, }, File: {}, StoreWarehouse: { async findAll() { return []; }, }, Store: { async findAll() { return [{ id: 77, company_name: '货主公司全称' }]; }, }, }; const ctx = { user: { store_id: 100000, }, app: { Sequelize: { Op: { in: 'in', }, }, model, }, model, }; const service = new ReceivingService(ctx); const result = await service.getReceivingDetail({ id: 123, pagination: { page: 1, pageSize: 10, }, }); assert.equal(result.code, 200); assert.equal(result.data.receiving.store_name, '货主公司全称'); }); it('should resolve store_name remask first over company_name', async () => { const model = { StoreWarehouse: { async findAll({ where }) { if (where.store_id.in.includes(1)) { return [{ store_id: 1, store_remask: '货主备注A' }]; } return []; }, }, Store: { async findAll({ where }) { return where.id.in.map(id => ({ id, company_name: id === 1 ? '公司A' : '公司B' })); }, }, }; const ctx = { app: { Sequelize: { Op: { in: 'in', }, }, }, model, }; const service = new ReceivingService(ctx); const map = await service._resolveReceivingStoreNameMap([ 1, 2 ], 100000); assert.equal(map.get(1), '货主备注A'); assert.equal(map.get(2), '公司B'); }); });