From 786c8367774e65d465fa18ef58d9cefc3ebf7f27 Mon Sep 17 00:00:00 2001 From: Joao Date: Thu, 23 Jan 2025 14:16:36 -0400 Subject: [PATCH] Refactor validateUser method to be static in ModelWithPermissions --- app/api/odm/ModelWithPermissions.ts | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/app/api/odm/ModelWithPermissions.ts b/app/api/odm/ModelWithPermissions.ts index 26479cf66a..87a40c5684 100644 --- a/app/api/odm/ModelWithPermissions.ts +++ b/app/api/odm/ModelWithPermissions.ts @@ -136,10 +136,10 @@ export class InvalidUserIdError extends Error { } export class ModelWithPermissions extends OdmModel> { - private validateUser(user: DataType | undefined) { + private static validateUser(user: DataType | undefined) { try { - // eslint-disable-next-line no-new - new ObjectId(user?._id?.toString()); + if (typeof user === 'undefined') return; + ObjectId.createFromHexString(user?._id?.toString()!); } catch (e) { throw new InvalidUserIdError(); } @@ -153,7 +153,7 @@ export class ModelWithPermissions extends OdmModel> { return super.save(data, appendPermissionQuery(query, AccessLevels.WRITE, user)); } - this.validateUser(user); + ModelWithPermissions.validateUser(user); return super.save(appendPermissionData(data, user)); } @@ -162,7 +162,8 @@ export class ModelWithPermissions extends OdmModel> { const dataArrayWithPermissions = dataArray.map(data => { if (data._id || data.permissions) return data; - this.validateUser(user); + + ModelWithPermissions.validateUser(user); return appendPermissionData(data, user); }); const query = appendPermissionQuery({}, AccessLevels.WRITE, user);