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
|
* 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 {
|
try {
|
||||||
// Resolve accountId if it's 'default'
|
// Resolve accountId if it's 'default'
|
||||||
let resolvedAccountId = accountId;
|
let resolvedAccountId = accountId;
|
||||||
|
|||||||
@ -3,7 +3,7 @@ import { logger } from '@/lib/logger';
|
|||||||
import { Notification, NotificationCount } from '@/lib/types/notification';
|
import { Notification, NotificationCount } from '@/lib/types/notification';
|
||||||
import { getRedisClient } from '@/lib/redis';
|
import { getRedisClient } from '@/lib/redis';
|
||||||
import { prisma } from '@/lib/prisma';
|
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';
|
import { getGraphUnreadCount, fetchGraphEmails } from '@/lib/services/microsoft-graph-mail';
|
||||||
|
|
||||||
export class EmailAdapter implements NotificationAdapter {
|
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
|
// Check if this is a Microsoft account that should use Graph API
|
||||||
const mailCredential = await prisma.mailCredentials.findUnique({
|
const graphCheck = await shouldUseGraphAPI(userId, accountId);
|
||||||
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;
|
|
||||||
|
|
||||||
unreadCounts[accountId] = {};
|
unreadCounts[accountId] = {};
|
||||||
|
|
||||||
if (isMicrosoftAccount) {
|
if (graphCheck.useGraph && graphCheck.mailCredentialId) {
|
||||||
// Use Graph API for Microsoft accounts
|
// Use Graph API for Microsoft accounts
|
||||||
try {
|
try {
|
||||||
const unreadCount = await getGraphUnreadCount(mailCredential.id, 'Inbox');
|
const unreadCount = await getGraphUnreadCount(graphCheck.mailCredentialId, 'Inbox');
|
||||||
unreadCounts[accountId]['INBOX'] = unreadCount;
|
unreadCounts[accountId]['INBOX'] = unreadCount;
|
||||||
|
|
||||||
logger.debug('[EMAIL_ADAPTER] Unread count (Graph API)', {
|
logger.debug('[EMAIL_ADAPTER] Unread count (Graph API)', {
|
||||||
@ -268,26 +255,13 @@ export class EmailAdapter implements NotificationAdapter {
|
|||||||
for (const account of accounts) {
|
for (const account of accounts) {
|
||||||
try {
|
try {
|
||||||
// Check if this is a Microsoft account that should use Graph API
|
// Check if this is a Microsoft account that should use Graph API
|
||||||
const mailCredential = await prisma.mailCredentials.findUnique({
|
const graphCheck = await shouldUseGraphAPI(userId, account.id);
|
||||||
where: { id: account.id },
|
|
||||||
select: {
|
|
||||||
id: true,
|
|
||||||
host: true,
|
|
||||||
use_oauth: true,
|
|
||||||
refresh_token: true,
|
|
||||||
},
|
|
||||||
});
|
|
||||||
|
|
||||||
const isMicrosoftAccount = mailCredential &&
|
if (graphCheck.useGraph && graphCheck.mailCredentialId) {
|
||||||
mailCredential.host === 'outlook.office365.com' &&
|
|
||||||
mailCredential.use_oauth &&
|
|
||||||
mailCredential.refresh_token;
|
|
||||||
|
|
||||||
if (isMicrosoftAccount) {
|
|
||||||
// Use Graph API for Microsoft accounts
|
// Use Graph API for Microsoft accounts
|
||||||
try {
|
try {
|
||||||
const graphResult = await fetchGraphEmails(
|
const graphResult = await fetchGraphEmails(
|
||||||
mailCredential.id,
|
graphCheck.mailCredentialId,
|
||||||
'Inbox',
|
'Inbox',
|
||||||
limit * 3, // Get more than limit to have enough after filtering
|
limit * 3, // Get more than limit to have enough after filtering
|
||||||
0,
|
0,
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user