From 72c24ab70071af2242b4b6f6d72c701c9db6bb54 Mon Sep 17 00:00:00 2001 From: Tom Butcher Date: Sat, 7 Nov 2026 19:17:28 +0000 Subject: [PATCH] Refactor stock event schema for improved ObjectId handling and error checking - Removed the unused `refId` import to streamline the code. - Enhanced the `getStockEventTotal` function to validate `parentId` directly, improving error handling. - Updated ObjectId creation logic to handle both string and ObjectId types, ensuring robustness in database queries. - Added a mock implementation for the `use` method in socket manager tests to support middleware functionality. --- src/database/schemas/inventory/stockevent.schema.js | 7 +++---- src/socket/__tests__/socketmanager.test.js | 1 + 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/src/database/schemas/inventory/stockevent.schema.js b/src/database/schemas/inventory/stockevent.schema.js index 36cc569..4d886fb 100644 --- a/src/database/schemas/inventory/stockevent.schema.js +++ b/src/database/schemas/inventory/stockevent.schema.js @@ -6,7 +6,6 @@ import { aggregateRollups, aggregateRollupsHistory, } from '../../database.js'; -import { refId } from '../../objectid.js'; const { Schema } = mongoose; const parentStockModelNames = { @@ -61,10 +60,10 @@ const buildParentState = (parentType, parentStock, currentAmount, startingAmount }; const getStockEventTotal = async (parentId, parentType) => { - const id = refId(parentId); - if (typeof id !== 'string' || !/^[a-fA-F\d]{24}$/.test(id)) return null; + if (!parentId) return null; - const objectId = new mongoose.Types.ObjectId(id); + const objectId = + parentId instanceof mongoose.Types.ObjectId ? parentId : new mongoose.Types.ObjectId(parentId); const [result] = await mongoose .model('stockEvent') diff --git a/src/socket/__tests__/socketmanager.test.js b/src/socket/__tests__/socketmanager.test.js index e805ca1..146f024 100644 --- a/src/socket/__tests__/socketmanager.test.js +++ b/src/socket/__tests__/socketmanager.test.js @@ -4,6 +4,7 @@ jest.unstable_mockModule('socket.io', () => ({ Server: jest.fn(() => ({ on: jest.fn(), emit: jest.fn(), + use: jest.fn(), })), }));