From f093cd68ac9c6c997eb58b95e2a65c2f86a61af0 Mon Sep 17 00:00:00 2001 From: baalmart Date: Mon, 2 Dec 2024 19:17:09 +0300 Subject: [PATCH] properly handle user prehooks for role updates --- src/auth-service/models/Network.js | 2 +- src/auth-service/models/User.js | 6 +++++- 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/src/auth-service/models/Network.js b/src/auth-service/models/Network.js index c04e1c5685..22d1278bc9 100644 --- a/src/auth-service/models/Network.js +++ b/src/auth-service/models/Network.js @@ -119,7 +119,7 @@ NetworkSchema.pre( const query = this.getQuery ? this.getQuery() : { _id: this._id }; // Get the correct tenant-specific model - const tenant = this.tenant || constants.DEFAULT_TENANT; + const tenant = this.tenant || constants.DEFAULT_TENANT || "airqo"; const Network = getModelByTenant(tenant, "network", NetworkSchema); const existingDoc = await Network.findOne(query); diff --git a/src/auth-service/models/User.js b/src/auth-service/models/User.js index d1e37f3641..a519afdfeb 100644 --- a/src/auth-service/models/User.js +++ b/src/auth-service/models/User.js @@ -257,7 +257,11 @@ UserSchema.pre( // Helper function to handle role updates const handleRoleUpdates = async (fieldName, idField) => { const query = this.getQuery ? this.getQuery() : { _id: this._id }; - const doc = await this.model.findOne(query); + + // Get the correct tenant-specific model + const tenant = this.tenant || constants.DEFAULT_TENANT || "airqo"; + const User = getModelByTenant(tenant, "user", UserSchema); + const doc = await User.findOne(query); if (!doc) return; let newRoles = [];