From 56d52c50fae9c4032ade90bf796582f70636ec8b Mon Sep 17 00:00:00 2001 From: alma Date: Wed, 14 Jan 2026 16:17:56 +0100 Subject: [PATCH] Agenda Sync refactor Mig Graph --- lib/services/email-service.ts | 3 +- lib/services/notifications/email-adapter.ts | 40 ++++----------------- 2 files changed, 9 insertions(+), 34 deletions(-) diff --git a/lib/services/email-service.ts b/lib/services/email-service.ts index 774f251..418e13c 100644 --- a/lib/services/email-service.ts +++ b/lib/services/email-service.ts @@ -787,8 +787,9 @@ interface FetchOptions { /** * Check if an account should use Microsoft Graph API instead of IMAP + * Exported for use in other modules */ -async function shouldUseGraphAPI(userId: string, accountId?: string): Promise<{ useGraph: boolean; mailCredentialId?: string }> { +export async function shouldUseGraphAPI(userId: string, accountId?: string): Promise<{ useGraph: boolean; mailCredentialId?: string }> { try { // Resolve accountId if it's 'default' let resolvedAccountId = accountId; diff --git a/lib/services/notifications/email-adapter.ts b/lib/services/notifications/email-adapter.ts index 1c797a0..dc2ace4 100644 --- a/lib/services/notifications/email-adapter.ts +++ b/lib/services/notifications/email-adapter.ts @@ -3,7 +3,7 @@ import { logger } from '@/lib/logger'; import { Notification, NotificationCount } from '@/lib/types/notification'; import { getRedisClient } from '@/lib/redis'; import { prisma } from '@/lib/prisma'; -import { getImapConnection } from '@/lib/services/email-service'; +import { getImapConnection, shouldUseGraphAPI } from '@/lib/services/email-service'; import { getGraphUnreadCount, fetchGraphEmails } from '@/lib/services/microsoft-graph-mail'; export class EmailAdapter implements NotificationAdapter { @@ -52,27 +52,14 @@ export class EmailAdapter implements NotificationAdapter { }); // Check if this is a Microsoft account that should use Graph API - const mailCredential = await prisma.mailCredentials.findUnique({ - where: { id: accountId }, - select: { - id: true, - host: true, - use_oauth: true, - refresh_token: true, - }, - }); - - const isMicrosoftAccount = mailCredential && - mailCredential.host === 'outlook.office365.com' && - mailCredential.use_oauth && - mailCredential.refresh_token; + const graphCheck = await shouldUseGraphAPI(userId, accountId); unreadCounts[accountId] = {}; - if (isMicrosoftAccount) { + if (graphCheck.useGraph && graphCheck.mailCredentialId) { // Use Graph API for Microsoft accounts try { - const unreadCount = await getGraphUnreadCount(mailCredential.id, 'Inbox'); + const unreadCount = await getGraphUnreadCount(graphCheck.mailCredentialId, 'Inbox'); unreadCounts[accountId]['INBOX'] = unreadCount; logger.debug('[EMAIL_ADAPTER] Unread count (Graph API)', { @@ -268,26 +255,13 @@ export class EmailAdapter implements NotificationAdapter { for (const account of accounts) { try { // Check if this is a Microsoft account that should use Graph API - const mailCredential = await prisma.mailCredentials.findUnique({ - where: { id: account.id }, - select: { - id: true, - host: true, - use_oauth: true, - refresh_token: true, - }, - }); + const graphCheck = await shouldUseGraphAPI(userId, account.id); - const isMicrosoftAccount = mailCredential && - mailCredential.host === 'outlook.office365.com' && - mailCredential.use_oauth && - mailCredential.refresh_token; - - if (isMicrosoftAccount) { + if (graphCheck.useGraph && graphCheck.mailCredentialId) { // Use Graph API for Microsoft accounts try { const graphResult = await fetchGraphEmails( - mailCredential.id, + graphCheck.mailCredentialId, 'Inbox', limit * 3, // Get more than limit to have enough after filtering 0,