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.');