Agenda Sync refactor Mig Graph
This commit is contained in:
parent
c665d45766
commit
56d52c50fa
@ -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;
|
||||
|
||||
@ -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,
|
||||
|
||||
Loading…
Reference in New Issue
Block a user