From dbbc2ff59a7eb73f8a4cec746c8dca672c8b0bc7 Mon Sep 17 00:00:00 2001 From: alma Date: Wed, 14 Jan 2026 16:24:52 +0100 Subject: [PATCH] Agenda Sync refactor Mig Graph --- lib/services/microsoft-graph-mail.ts | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) diff --git a/lib/services/microsoft-graph-mail.ts b/lib/services/microsoft-graph-mail.ts index 9987a94..329b3f8 100644 --- a/lib/services/microsoft-graph-mail.ts +++ b/lib/services/microsoft-graph-mail.ts @@ -17,8 +17,21 @@ const GRAPH_API_BASE = 'https://graph.microsoft.com/v1.0'; * Get a configured Axios instance for Microsoft Graph API */ async function getMicrosoftGraphClient(mailCredentialId: string): Promise { - // Get fresh access token - const tokenResult = await ensureFreshToken(mailCredentialId); + // Look up the mail credential to get userId and email for token refresh + const account = await prisma.mailCredentials.findUnique({ + where: { id: mailCredentialId }, + select: { + userId: true, + email: true, + }, + }); + + if (!account) { + throw new Error('Mail credential not found for Microsoft Graph client'); + } + + // Get fresh access token using userId + email + const tokenResult = await ensureFreshToken(account.userId, account.email); if (!tokenResult || !tokenResult.accessToken) { throw new Error('Failed to obtain valid access token for Microsoft Graph API. The account may need to be re-authenticated with Mail.Read and Mail.Send permissions.');