Refactor ObjectId handling in utils.js to use mongoose.Types.ObjectId for consistency across the codebase.
This commit is contained in:
parent
ecb888f692
commit
7541612d67
11
src/utils.js
11
src/utils.js
@ -1,4 +1,4 @@
|
||||
import { ObjectId } from 'mongodb';
|
||||
import { mongoose } from 'mongoose';
|
||||
import { auditLogModel } from './database/schemas/management/auditlog.schema.js';
|
||||
import exifr from 'exifr';
|
||||
import { natsServer } from './database/nats.js';
|
||||
@ -20,6 +20,9 @@ function buildWildcardRegexPattern(input) {
|
||||
}
|
||||
|
||||
function parseFilter(property, value) {
|
||||
if (value?._id !== undefined && value?._id !== null) {
|
||||
return { [property]: { _id: new mongoose.Types.ObjectId(value._id) } };
|
||||
}
|
||||
if (typeof value === 'string') {
|
||||
var trimmed = value.trim();
|
||||
if (trimmed.charAt(3) == ':') {
|
||||
@ -33,7 +36,7 @@ function parseFilter(property, value) {
|
||||
// Handle ObjectId (24-char hex)
|
||||
|
||||
if (/^[a-f\d]{24}$/i.test(trimmed) && trimmed.length >= 24) {
|
||||
return { [property]: new ObjectId(trimmed) };
|
||||
return { [property]: new mongoose.Types.ObjectId(trimmed) };
|
||||
}
|
||||
|
||||
// Handle numbers
|
||||
@ -514,7 +517,7 @@ function expandObjectIds(input) {
|
||||
// Helper to check if a value is an ObjectId or a 24-char hex string
|
||||
function isObjectId(val) {
|
||||
// Check for MongoDB ObjectId instance
|
||||
if (val instanceof ObjectId) return true;
|
||||
if (val instanceof mongoose.Types.ObjectId) return true;
|
||||
// Check for exactly 24 hex characters (no special characters)
|
||||
if (typeof val === 'string' && /^[a-fA-F\d]{24}$/.test(val)) return true;
|
||||
return false;
|
||||
@ -524,7 +527,7 @@ function expandObjectIds(input) {
|
||||
function expand(value) {
|
||||
if (Array.isArray(value)) {
|
||||
return value.map(expand);
|
||||
} else if (value && typeof value === 'object' && !(value instanceof ObjectId)) {
|
||||
} else if (value && typeof value === 'object' && !(value instanceof mongoose.Types.ObjectId)) {
|
||||
var result = {};
|
||||
for (const [key, val] of Object.entries(value)) {
|
||||
if (key === '_id') {
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user