diff --git a/app/agenda/page.tsx b/app/agenda/page.tsx index e8793fb..24ef9a5 100644 --- a/app/agenda/page.tsx +++ b/app/agenda/page.tsx @@ -74,7 +74,131 @@ export default async function CalendarPage() { } }); - // If no calendars exist, create default ones + // Auto-setup sync for Infomaniak accounts from courrier + // Get all Infomaniak email accounts + const infomaniakAccounts = await prisma.mailCredentials.findMany({ + where: { + userId: session?.user?.id || '', + host: { + contains: 'infomaniak' + }, + password: { + not: null + } + }, + select: { + id: true, + email: true, + display_name: true, + password: true + } + }); + + // For each Infomaniak account, ensure there's a synced calendar + for (const account of infomaniakAccounts) { + // Check if a calendar sync already exists for this account + const existingSync = await prisma.calendarSync.findFirst({ + where: { + mailCredentialId: account.id, + syncEnabled: true + }, + include: { + calendar: true + } + }); + + if (!existingSync) { + // Try to discover calendars for this account + try { + const { discoverInfomaniakCalendars } = await import('@/lib/services/caldav-sync'); + const externalCalendars = await discoverInfomaniakCalendars( + account.email, + account.password! + ); + + if (externalCalendars.length > 0) { + // Use the first calendar (usually the main calendar) + const mainCalendar = externalCalendars[0]; + + // Create a private calendar for this account + const calendar = await prisma.calendar.create({ + data: { + name: "Privée", + color: "#4F46E5", + description: `Calendrier synchronisé avec ${account.display_name || account.email}`, + userId: session?.user?.id || '', + } + }); + + // Create sync configuration + await prisma.calendarSync.create({ + data: { + calendarId: calendar.id, + mailCredentialId: account.id, + provider: 'infomaniak', + externalCalendarId: mainCalendar.id, + externalCalendarUrl: mainCalendar.url, + syncEnabled: true, + syncFrequency: 15 + } + }); + + // Trigger initial sync + try { + const { syncInfomaniakCalendar } = await import('@/lib/services/caldav-sync'); + const syncConfig = await prisma.calendarSync.findUnique({ + where: { calendarId: calendar.id }, + include: { + calendar: true, + mailCredential: true + } + }); + if (syncConfig) { + await syncInfomaniakCalendar(syncConfig.id, true); + } + } catch (syncError) { + console.error('Error during initial sync:', syncError); + // Don't fail if sync fails, calendar is still created + } + } + } catch (error) { + console.error(`Error auto-setting up sync for account ${account.email}:`, error); + // Continue with other accounts even if one fails + } + } + } + + // Refresh calendars after auto-setup + calendars = await prisma.calendar.findMany({ + where: { + userId: session?.user?.id || '', + }, + include: { + events: { + orderBy: { + start: 'asc' + } + }, + mission: { + include: { + missionUsers: true + } + }, + syncConfig: { + include: { + mailCredential: { + select: { + id: true, + email: true, + display_name: true, + } + } + } + } + } + }); + + // If no calendars exist at all, create a default one if (calendars.length === 0) { const defaultCalendars = [ {