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