'use strict'; const assert = require('node:assert/strict'); const Sequelize = require('sequelize'); const OrderIndexService = require('../../../../app/service/systemMethods/order/index'); const OrderBaseService = require('../../../../app/service/systemMethods/order/base'); const { buildRows, createOrderHarness, matchesWhere } = require('../../../support/order_service_test_helper'); describe('test/app/service/systemMethods/order.base.step.test.js', () => { function createService(storeId = 100000) { const ctx = { app: { Sequelize, config: {}, }, user: { store_id: storeId, }, }; return new OrderBaseService(ctx); } function createRecordModel(rows, capture) { const normalizedRows = Array.isArray(rows) ? rows.map(row => ({ ...row })) : []; return { async findOne(options = {}) { capture.findOneOptions = options; const matchedRow = normalizedRows.find(row => matchesWhere(row, options.where)); return matchedRow ? { ...matchedRow } : null; }, async findAll(options = {}) { capture.findAllOptions = options; return normalizedRows .filter(row => matchesWhere(row, options.where)) .map(row => ({ ...row })); }, }; } function createDetailHarness(options = {}) { const storeId = options.storeId || 100000; const orderRowsByType = options.orderRowsByType || {}; const itemRowsByType = options.itemRowsByType || {}; const expressRowsByOrderId = options.expressRowsByOrderId || {}; const captures = { orderFactoryCalls: [], itemFactoryCalls: [], expressQueries: [], }; const ctx = { app: { Sequelize, config: {}, }, user: { store_id: storeId, }, model: { OrderDeliveryFactory: { async getCurrentModel(warehouseId) { const capture = { method: 'getCurrentModel', warehouseId, }; captures.orderFactoryCalls.push(capture); return createRecordModel(orderRowsByType.current || [], capture); }, async getHistoryModel(warehouseId) { const capture = { method: 'getHistoryModel', warehouseId, }; captures.orderFactoryCalls.push(capture); return createRecordModel(orderRowsByType.history || [], capture); }, async getDispatchModel(warehouseId) { const capture = { method: 'getDispatchModel', warehouseId, }; captures.orderFactoryCalls.push(capture); return createRecordModel(orderRowsByType.dispatch || [], capture); }, async getOutsourcedModel(warehouseId) { const capture = { method: 'getOutsourcedModel', warehouseId, }; captures.orderFactoryCalls.push(capture); return createRecordModel(orderRowsByType.outsourced || [], capture); }, }, OrderItemsFactory: { async getCurrentModel(warehouseId) { const capture = { method: 'getCurrentModel', warehouseId, }; captures.itemFactoryCalls.push(capture); return createRecordModel(itemRowsByType.current || [], capture); }, async getHistoryModel(warehouseId) { const capture = { method: 'getHistoryModel', warehouseId, }; captures.itemFactoryCalls.push(capture); return createRecordModel(itemRowsByType.history || [], capture); }, async getDispatchModel(warehouseId) { const capture = { method: 'getDispatchModel', warehouseId, }; captures.itemFactoryCalls.push(capture); return createRecordModel(itemRowsByType.dispatch || [], capture); }, async getOrCreateModel(warehouseId, type) { const capture = { method: 'getOrCreateModel', warehouseId, type, }; captures.itemFactoryCalls.push(capture); return createRecordModel(itemRowsByType[type] || [], capture); }, }, OrderExpress: { async findAll(options = {}) { captures.expressQueries.push(options); const orderId = Number(options?.where?.order_id || 0); return (expressRowsByOrderId[orderId] || []).map(row => ({ ...row })); }, }, }, }; return { service: new OrderIndexService(ctx), ctx, captures, }; } function createAuthService(userOverrides = {}) { const ctx = { header: { platform: 'web', project: 'all', }, set() {}, app: { config: { jwt: { secret: 'test-secret', expiresIn: '3600', }, }, jwt: { verify() { return { id: 1, exp: Math.floor(Date.now() / 1000) + 3600, }; }, sign() { return 'refreshed-token'; }, }, }, service: { systems: { auth: { user: { async findById() { return { id: 1, username: 'tester', status: 1, auth_end: '2099-12-31 23:59:59', permissions: [], ...userOverrides, }; }, }, }, }, }, }; return new OrderIndexService(ctx); } it('should inject current store_id into buildWhere', () => { const service = createService(100001); const where = service.buildWhere({ step: 1 }); assert.equal(where.warehouse_id, 100001); assert.equal(where.step, 1); assert.equal(Object.prototype.hasOwnProperty.call(where, 'audit_status'), false); }); it('should build where with step in filter', () => { const service = createService(); const where = service.buildWhere({ step: { operator: 'in', value: [ 0, 1, 2 ], }, }); assert.equal(where.warehouse_id, 100000); assert.deepEqual(where.step, { [Sequelize.Op.in]: [ 0, 1, 2 ], }); }); it('should reject warehouse fields in buildWhere input', () => { const service = createService(); assert.throws(() => { service.buildWhere({ warehouse_id: 100000, }); }, /禁止传入 warehouse_id \/ warehouseId/); assert.throws(() => { service.buildWhere({ warehouseId: 100000, }); }, /禁止传入 warehouse_id \/ warehouseId/); }); it('should cap pageSize and keep deliveryOrder step filter in listByModule', async () => { const rows = buildRows(250, index => ({ step: index < 230 ? 1 : 2, dispatch_lane: 'm4', })); const { service, captures } = createOrderHarness(OrderBaseService, { rowsByModule: { deliveryOrder: rows, }, }); const result = await service.listByModule('deliveryOrder', { pagination: { page: 1, pageSize: 999, }, where: { step: 1, }, }); assert.equal(result.pagination.page, 1); assert.equal(result.pagination.pageSize, 200); assert.equal(result.pagination.total, 230); assert.equal(result.list.length, 200); assert.ok(result.list.every(item => item.step === 1)); assert.equal(captures[0].moduleName, 'deliveryOrder'); assert.equal(captures[0].findOptions.limit, 200); assert.equal(captures[0].findOptions.offset, 0); }); it('should use current store_id in listByModule and reject warehouse bypass fields', async () => { const rows = buildRows(6, () => ({ warehouse_id: 100001, step: 1, })); const { service, captures } = createOrderHarness(OrderBaseService, { storeId: 100001, rowsByModule: { order: rows, }, }); const result = await service.listByModule('order', { pagination: { page: 1, pageSize: 10, }, where: { step: 1, }, }); assert.equal(result.pagination.total, 6); assert.equal(captures[0].countWhere.warehouse_id, 100001); assert.equal(captures[0].findOptions.where.warehouse_id, 100001); assert.ok(result.list.every(item => item.warehouse_id === 100001)); await assert.rejects(() => service.listByModule('order', { where: { warehouse_id: 100001, }, }), /禁止传入 warehouse_id \/ warehouseId/); }); it('should keep pendingOrder hidden for padding rows while outboundOrder still exposes none and padding', async () => { const { service } = createOrderHarness(OrderBaseService, { rowsByModule: { pendingOrder: [ { id: 1, warehouse_id: 100000, step: 1, storage_status: 'none', outbound_status: 'none', updated_at: '2026-04-12 00:00:00', }, { id: 2, warehouse_id: 100000, step: 2, storage_status: 'none', outbound_status: 'padding', updated_at: '2026-04-12 00:00:00', }, { id: 3, warehouse_id: 100000, step: 1, storage_status: 'fin', outbound_status: 'padding', updated_at: '2026-04-12 00:00:00', }, { id: 4, warehouse_id: 100001, step: 1, storage_status: 'none', outbound_status: 'none', updated_at: '2026-04-12 00:00:00', }, ], outboundOrder: [ { id: 1, warehouse_id: 100000, step: 1, outbound_status: 'none', updated_at: '2026-04-12 00:00:00', }, { id: 2, warehouse_id: 100000, step: 2, outbound_status: 'padding', updated_at: '2026-04-12 00:00:00', }, { id: 3, warehouse_id: 100000, step: 1, outbound_status: 'padding', updated_at: '2026-04-12 00:00:00', }, { id: 4, warehouse_id: 100001, step: 1, outbound_status: 'none', updated_at: '2026-04-12 00:00:00', }, ], }, }); const pendingResult = await service.listByModule('pendingOrder', {}); const outboundResult = await service.listByModule('outboundOrder', {}); assert.deepEqual(pendingResult.list.map(item => item.id), [ 1 ]); assert.ok(pendingResult.list.every(item => item.storage_status !== 'fin')); assert.ok(pendingResult.list.every(item => item.outbound_status !== 'padding')); assert.deepEqual(outboundResult.list.map(item => item.id), [ 1, 2, 3 ]); assert.ok(outboundResult.list.some(item => item.outbound_status === 'padding')); assert.ok(outboundResult.list.some(item => item.outbound_status === 'none')); assert.ok(outboundResult.list.every(item => item.warehouse_id === 100000)); }); it('should attach express numbers and normalize sys names in all order lists', async () => { const rows = buildRows(2, index => ({ id: index + 1, warehouse_id: 100000, sys_platform_name: index === 0 ? '淘宝' : null, sys_logistics_name: index === 0 ? '申通' : undefined, })); const { service } = createOrderHarness(OrderBaseService, { rowsByModule: { order: rows, }, }); service.ctx.model = { OrderExpress: { async findAll({ where }) { const orderIds = Array.isArray(where && where.order_id && where.order_id[Sequelize.Op.in]) ? where.order_id[Sequelize.Op.in] : []; return orderIds.flatMap(orderId => { if (Number(orderId) === 1) { return [ { id: 11, order_id: 1, waybill_code: 'WB-001', }, { id: 12, order_id: 1, waybill_code: 'WB-002', }, ]; } if (Number(orderId) === 2) { return [ { id: 21, order_id: 2, waybill_code: 'WB-003', }, ]; } return []; }); }, }, }; const result = await service.listByModule('order', { pagination: { page: 1, pageSize: 10, }, where: {}, }); assert.deepEqual(result.list[0].express_numbers, [ 'WB-001', 'WB-002' ]); assert.deepEqual(result.list[0].waybill_codes, [ 'WB-001', 'WB-002' ]); assert.deepEqual(result.list[1].express_numbers, [ 'WB-003' ]); assert.deepEqual(result.list[1].waybill_codes, [ 'WB-003' ]); assert.equal(Object.prototype.hasOwnProperty.call(result.list[0], 'sys_platform_name'), true); assert.equal(Object.prototype.hasOwnProperty.call(result.list[0], 'sys_logistics_name'), true); assert.equal(result.list[0].sys_platform_name, '淘宝'); assert.equal(result.list[0].sys_logistics_name, '申通'); assert.equal(result.list[1].sys_platform_name, null); assert.equal(result.list[1].sys_logistics_name, null); }); it('should treat waybill_codes as virtual filter and only append matched order ids to order where', async () => { const rows = buildRows(3, index => ({ id: index + 1, warehouse_id: 100000, step: 1, })); const { service, captures } = createOrderHarness(OrderBaseService, { rowsByModule: { order: rows, }, }); const expressQueries = []; service.ctx.model = { OrderExpress: { async findAll(options = {}) { expressQueries.push(options); const where = options.where || {}; const waybillFilter = where.waybill_code && where.waybill_code[Sequelize.Op.in]; const orderIdFilter = where.order_id && where.order_id[Sequelize.Op.in]; if (Array.isArray(waybillFilter)) { return [ { order_id: 2 }, { order_id: 3 }, ]; } if (Array.isArray(orderIdFilter)) { return orderIdFilter .filter(orderId => Number(orderId) === 2 || Number(orderId) === 3) .map(orderId => ({ id: Number(orderId) + 100, order_id: Number(orderId), waybill_code: Number(orderId) === 2 ? 'WB-001' : 'WB-002', })); } return []; }, }, }; const result = await service.listByModule('order', { pagination: { page: 1, pageSize: 10, }, where: { waybill_codes: 'WB-001', express_numbers: [ 'WB-002' ], }, }); assert.equal(result.pagination.total, 2); assert.deepEqual(result.list.map(item => item.id), [ 2, 3 ]); assert.deepEqual(result.list[0].waybill_codes, [ 'WB-001' ]); assert.deepEqual(result.list[1].waybill_codes, [ 'WB-002' ]); assert.deepEqual(captures[0].countWhere, captures[0].findOptions.where); assert.equal(JSON.stringify(captures[0].countWhere).includes('waybill_codes'), false); assert.equal(JSON.stringify(captures[0].countWhere).includes('express_numbers'), false); assert.equal(expressQueries.length >= 1, true); assert.deepEqual(expressQueries[0].where.waybill_code[Sequelize.Op.in], [ 'WB-001', 'WB-002' ]); assert.equal(expressQueries[0].where.warehouse_id, 100000); }); it('should query detail by tableType and return orderItems, items, expresses and waybill_codes', async () => { const { service, captures } = createDetailHarness({ storeId: 100001, orderRowsByType: { history: [ { id: 501, warehouse_id: 100001, delivery_order_code: 'HIS-501', step: 2, }, ], }, itemRowsByType: { history: [ { id: 11, order_id: 501, item_code: 'SKU-001', plan_qty: 2, }, { id: 12, order_id: 501, item_code: 'SKU-002', plan_qty: 1, }, ], }, expressRowsByOrderId: { 501: [ { waybill_code: 'WB-501-A', logistics_code: 'LC-A', logistics_name: '物流A', logistics_interface_id: '88', }, { waybill_code: 'WB-501-B', logistics_code: 'LC-B', logistics_name: '物流B', logistics_interface_id: 99, }, ], }, }); const result = await service.detail({ id: 501, tableType: 'history', }); assert.equal(captures.orderFactoryCalls[0].method, 'getHistoryModel'); assert.equal(captures.orderFactoryCalls[0].warehouseId, 100001); assert.equal(captures.itemFactoryCalls[0].method, 'getHistoryModel'); assert.equal(captures.itemFactoryCalls[0].warehouseId, 100001); assert.equal(captures.expressQueries[0].where.order_id, 501); assert.deepEqual(result.order, { id: 501, warehouse_id: 100001, delivery_order_code: 'HIS-501', step: 2, }); assert.strictEqual(result.order, result.detail); assert.deepEqual(result.orderItems, [ { id: 11, order_id: 501, item_code: 'SKU-001', plan_qty: 2, }, { id: 12, order_id: 501, item_code: 'SKU-002', plan_qty: 1, }, ]); assert.deepEqual(result.items, result.orderItems); assert.deepEqual(result.expresses, [ { waybill_code: 'WB-501-A', logistics_code: 'LC-A', logistics_name: '物流A', logistics_interface_id: 88, }, { waybill_code: 'WB-501-B', logistics_code: 'LC-B', logistics_name: '物流B', logistics_interface_id: 99, }, ]); assert.deepEqual(result.waybill_codes, [ 'WB-501-A', 'WB-501-B' ]); }); it('should use current store_id as isolation condition in detail lookup', async () => { const { service, captures } = createDetailHarness({ storeId: 100002, orderRowsByType: { current: [ { id: 601, warehouse_id: 100001, delivery_order_code: 'CUR-601', step: 1, }, ], }, }); await assert.rejects(() => service.detail({ id: 601, tableType: 'current', }), /订单不存在或不属于当前仓库/); assert.equal(captures.orderFactoryCalls[0].method, 'getCurrentModel'); assert.equal(captures.orderFactoryCalls[0].findOneOptions.where.warehouse_id, 100002); assert.equal(captures.itemFactoryCalls.length, 0); assert.equal(captures.expressQueries.length, 0); }); it('should allow detail in the order permission white list', async () => { const service = createAuthService(); const result = await service.checkAuth('order', 'detail', 'Bearer test-token'); assert.equal(result.status, 200); assert.equal(result.message, 'ok'); }); });