From ff3113cc30167247ebf754847a3ba9862c9593e1 Mon Sep 17 00:00:00 2001 From: Nils T <40796616+tollercode@users.noreply.github.com> Date: Fri, 19 Dec 2025 10:01:28 +0100 Subject: [PATCH 1/4] Update user ID handling in MicrosoftTeams service --- .../MicrosoftTeams/MicrosoftTeams.ts | 31 ++++++++++++++++--- 1 file changed, 27 insertions(+), 4 deletions(-) diff --git a/Common/Server/Utils/Workspace/MicrosoftTeams/MicrosoftTeams.ts b/Common/Server/Utils/Workspace/MicrosoftTeams/MicrosoftTeams.ts index 021e10524b..5016e56790 100644 --- a/Common/Server/Utils/Workspace/MicrosoftTeams/MicrosoftTeams.ts +++ b/Common/Server/Utils/Workspace/MicrosoftTeams/MicrosoftTeams.ts @@ -54,6 +54,10 @@ import ScheduledMaintenanceService from "../../../Services/ScheduledMaintenanceS import IncidentStateService from "../../../Services/IncidentStateService"; import AlertStateService from "../../../Services/AlertStateService"; +// Import user services +import User from "../../Models/DatabaseModels/User"; +import UserService from "../../../Services/UserService"; + // Import database utilities import QueryHelper from "../../../Types/Database/QueryHelper"; import SortOrder from "../../../../Types/BaseDatabase/SortOrder"; @@ -2938,7 +2942,7 @@ All monitoring checks are passing normally.`; logger.debug("Using app-scoped token to fetch joined teams for user"); const userTeams: Record = await this.getUserJoinedTeams({ - userId: data.userId.toString(), + userId: data.userId, projectId: data.projectId, }); allTeams = Object.values(userTeams) as any; @@ -3063,14 +3067,33 @@ All monitoring checks are passing normally.`; // Method to get user's joined teams using app-scoped token @CaptureSpan() public static async getUserJoinedTeams(data: { - userId: string; + userId: ObjectID; projectId: ObjectID; }): Promise> { logger.debug("=== getUserJoinedTeams called ==="); - logger.debug(`User ID: ${data.userId}`); + logger.debug(`User ID: ${data.userId.toString()}`); logger.debug(`Project ID: ${data.projectId.toString()}`); try { + // Fetch user email from UserService + const user:User = await UserService.findOneById({ + id: data.userId, + select: { + email: true, + }, + props: { + isRoot: true, + }, + }); + if (!user || !user.email) { + logger.error("User or user email not found"); + throw new BadDataException( + "User email not found for Microsoft Teams integration", + ); + } + const userEmail: string = user.email.toString(); + logger.debug(`Retrieved user email: ${userEmail}`); + // Get a valid app access token (refreshed if needed) logger.debug("Refreshing app access token before fetching teams"); const accessToken: string = await this.getValidAccessToken({ @@ -3083,7 +3106,7 @@ All monitoring checks are passing normally.`; const teamsResponse: HTTPErrorResponse | HTTPResponse = await API.get({ url: URL.fromString( - `https://graph.microsoft.com/v1.0/users/${data.userId}/joinedTeams`, + `https://graph.microsoft.com/v1.0/users/${userEmail}/joinedTeams`, ), headers: { Authorization: `Bearer ${accessToken}`, From c4f21561ff7eba4b36897abfebb4e0fe477b2fbf Mon Sep 17 00:00:00 2001 From: Nils T <40796616+tollercode@users.noreply.github.com> Date: Fri, 19 Dec 2025 10:22:50 +0100 Subject: [PATCH 2/4] Fix formatting of User type declaration --- Common/Server/Utils/Workspace/MicrosoftTeams/MicrosoftTeams.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Common/Server/Utils/Workspace/MicrosoftTeams/MicrosoftTeams.ts b/Common/Server/Utils/Workspace/MicrosoftTeams/MicrosoftTeams.ts index 5016e56790..f6aa3da106 100644 --- a/Common/Server/Utils/Workspace/MicrosoftTeams/MicrosoftTeams.ts +++ b/Common/Server/Utils/Workspace/MicrosoftTeams/MicrosoftTeams.ts @@ -3076,7 +3076,7 @@ All monitoring checks are passing normally.`; try { // Fetch user email from UserService - const user:User = await UserService.findOneById({ + const user: User = await UserService.findOneById({ id: data.userId, select: { email: true, From 2eed0cbfb2e5edd342ce0511c57f59ec7667043a Mon Sep 17 00:00:00 2001 From: Nils T <40796616+tollercode@users.noreply.github.com> Date: Fri, 19 Dec 2025 10:33:41 +0100 Subject: [PATCH 3/4] Update import path for User model --- Common/Server/Utils/Workspace/MicrosoftTeams/MicrosoftTeams.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Common/Server/Utils/Workspace/MicrosoftTeams/MicrosoftTeams.ts b/Common/Server/Utils/Workspace/MicrosoftTeams/MicrosoftTeams.ts index f6aa3da106..9bbab987ac 100644 --- a/Common/Server/Utils/Workspace/MicrosoftTeams/MicrosoftTeams.ts +++ b/Common/Server/Utils/Workspace/MicrosoftTeams/MicrosoftTeams.ts @@ -55,7 +55,7 @@ import IncidentStateService from "../../../Services/IncidentStateService"; import AlertStateService from "../../../Services/AlertStateService"; // Import user services -import User from "../../Models/DatabaseModels/User"; +import User from "../../../../Models/DatabaseModels/User"; import UserService from "../../../Services/UserService"; // Import database utilities From 3a575f86668b6949ca3cfba11aeb02d29c64570d Mon Sep 17 00:00:00 2001 From: Nils T <40796616+tollercode@users.noreply.github.com> Date: Fri, 19 Dec 2025 10:37:22 +0100 Subject: [PATCH 4/4] Handle potential null user in UserService call --- Common/Server/Utils/Workspace/MicrosoftTeams/MicrosoftTeams.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Common/Server/Utils/Workspace/MicrosoftTeams/MicrosoftTeams.ts b/Common/Server/Utils/Workspace/MicrosoftTeams/MicrosoftTeams.ts index 9bbab987ac..c7b9bfc7b4 100644 --- a/Common/Server/Utils/Workspace/MicrosoftTeams/MicrosoftTeams.ts +++ b/Common/Server/Utils/Workspace/MicrosoftTeams/MicrosoftTeams.ts @@ -3076,7 +3076,7 @@ All monitoring checks are passing normally.`; try { // Fetch user email from UserService - const user: User = await UserService.findOneById({ + const user: User | null = await UserService.findOneById({ id: data.userId, select: { email: true,