Skip to content

Commit

Permalink
Refactor validateUser method to be static in ModelWithPermissions
Browse files Browse the repository at this point in the history
  • Loading branch information
Joao-vi committed Jan 23, 2025
1 parent b90a4b8 commit 786c836
Showing 1 changed file with 6 additions and 5 deletions.
11 changes: 6 additions & 5 deletions app/api/odm/ModelWithPermissions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -136,10 +136,10 @@ export class InvalidUserIdError extends Error {
}

export class ModelWithPermissions<T> extends OdmModel<WithPermissions<T>> {
private validateUser(user: DataType<UserSchema> | undefined) {
private static validateUser(user: DataType<UserSchema> | 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();
}
Expand All @@ -153,7 +153,7 @@ export class ModelWithPermissions<T> extends OdmModel<WithPermissions<T>> {
return super.save(data, appendPermissionQuery(query, AccessLevels.WRITE, user));
}

this.validateUser(user);
ModelWithPermissions.validateUser(user);
return super.save(appendPermissionData(data, user));
}

Expand All @@ -162,7 +162,8 @@ export class ModelWithPermissions<T> extends OdmModel<WithPermissions<T>> {

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);
Expand Down

0 comments on commit 786c836

Please sign in to comment.