diff --git a/src/auth-service/models/Preference.js b/src/auth-service/models/Preference.js index 78b7894169..8ee2d51161 100644 --- a/src/auth-service/models/Preference.js +++ b/src/auth-service/models/Preference.js @@ -232,9 +232,14 @@ PreferenceSchema.pre( // Utility function to validate and process ObjectIds const processObjectId = (id) => { - return id instanceof mongoose.Types.ObjectId - ? id - : mongoose.Types.ObjectId(id); + if (!id) return null; + if (id instanceof mongoose.Types.ObjectId) return id; + try { + return mongoose.Types.ObjectId(id); + } catch (error) { + logger.error(`Invalid ObjectId: ${id}`); + throw new Error(`Invalid ObjectId: ${id}`); + } }; // Comprehensive ID fields processing