NeahStable/app/agenda/page.tsx
2026-01-14 17:01:21 +01:00

541 lines
17 KiB
TypeScript

import { getServerSession } from "next-auth/next";
import { authOptions } from "@/app/api/auth/options";
import { redirect } from "next/navigation";
import { prisma } from "@/lib/prisma";
import { CalendarClient } from "@/components/calendar/calendar-client";
import { Metadata } from "next";
import { CalendarDays, Users, Bookmark, Clock } from "lucide-react";
import Image from "next/image";
import { Button } from "@/components/ui/button";
import { add } from 'date-fns';
export const metadata: Metadata = {
title: "Enkun - Calendrier | Gestion d'événements professionnelle",
description: "Plateforme avancée pour la gestion de vos rendez-vous, réunions et événements professionnels",
keywords: "calendrier, rendez-vous, événements, gestion du temps, enkun",
};
interface Event {
id: string;
title: string;
description?: string | null;
start: Date;
end: Date;
location?: string | null;
isAllDay: boolean;
type?: string;
attendees?: { id: string; name: string }[];
}
interface Calendar {
id: string;
name: string;
color: string;
description?: string | null;
events: Event[];
}
export default async function CalendarPage() {
const session = await getServerSession(authOptions);
if (!session?.user) {
redirect("/api/auth/signin");
}
const userId = session.user.username || session.user.email || '';
// Get all calendars for the user with mission relation and sync configuration
// Exclude "Privée" and "Default" calendars that are not synced (they should only exist if synced from courrier)
let calendars = await prisma.calendar.findMany({
where: {
userId: session?.user?.id || '',
OR: [
// Keep calendars that are not "Privée" or "Default"
{ name: { notIn: ["Privée", "Default"] } },
// Or keep "Privée"/"Default" calendars that have active sync config
{
AND: [
{ name: { in: ["Privée", "Default"] } },
{
syncConfig: {
isNot: null
}
}
]
}
]
},
include: {
events: {
orderBy: {
start: 'asc'
}
},
mission: {
include: {
missionUsers: true
}
},
syncConfig: {
include: {
mailCredential: {
select: {
id: true,
email: true,
display_name: true,
}
}
}
}
}
});
// Auto-setup sync for email accounts from courrier (Infomaniak and Microsoft)
// 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
}
});
// Get all Microsoft email accounts (OAuth)
const microsoftAccounts = await prisma.mailCredentials.findMany({
where: {
userId: session?.user?.id || '',
host: {
contains: 'outlook.office365.com'
},
use_oauth: true,
refresh_token: {
not: null
}
},
select: {
id: true,
email: true,
display_name: true,
refresh_token: true,
use_oauth: true
}
});
// For each Infomaniak account, ensure there's a synced calendar
// Skip if no Infomaniak accounts exist (user may only have Microsoft accounts)
if (infomaniakAccounts.length > 0) {
for (const account of infomaniakAccounts) {
// Check if a calendar sync already exists for this account (enabled or disabled)
// This prevents creating duplicate calendars for the same account
const existingSync = await prisma.calendarSync.findFirst({
where: {
mailCredentialId: account.id
},
include: {
calendar: true
}
});
// If sync exists but is disabled, check if it's due to invalid credentials
// Don't re-enable if the last error was 401 (invalid credentials)
if (existingSync && !existingSync.syncEnabled) {
const isAuthError = existingSync.lastSyncError?.includes('401') ||
existingSync.lastSyncError?.includes('Unauthorized') ||
existingSync.lastSyncError?.includes('invalid');
if (!isAuthError) {
// Only re-enable if it's not an authentication error
await prisma.calendarSync.update({
where: { id: existingSync.id },
data: { syncEnabled: true }
});
} else {
// Try to discover calendars to verify if credentials are now valid
try {
const { discoverInfomaniakCalendars } = await import('@/lib/services/caldav-sync');
const externalCalendars = await discoverInfomaniakCalendars(
account.email,
account.password!
);
if (externalCalendars.length > 0) {
// Credentials are now valid, re-enable sync
await prisma.calendarSync.update({
where: { id: existingSync.id },
data: {
syncEnabled: true,
lastSyncError: null,
externalCalendarId: externalCalendars[0].id,
externalCalendarUrl: externalCalendars[0].url
}
});
}
} catch (error) {
// Credentials are still invalid, keep sync disabled
const errorMessage = error instanceof Error ? error.message : 'Unknown error';
await prisma.calendarSync.update({
where: { id: existingSync.id },
data: {
lastSyncError: `Identifiants invalides ou expirés (${errorMessage}). Veuillez vérifier vos identifiants Infomaniak dans la page courrier.`
}
});
}
}
continue; // Skip to next account
}
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) {
// Log error but don't fail the page - account may not have calendar access or credentials may be invalid
const errorMessage = error instanceof Error ? error.message : 'Unknown error';
console.log(`Infomaniak calendar sync not available for ${account.email} - ${errorMessage}`);
// If it's a 401 error, the credentials are likely invalid - update lastSyncError in existing sync if any
if (errorMessage.includes('401') || errorMessage.includes('Unauthorized')) {
// Check if there's a disabled sync for this account
const disabledSync = await prisma.calendarSync.findFirst({
where: {
mailCredentialId: account.id,
provider: 'infomaniak',
syncEnabled: false
}
});
if (disabledSync) {
// Update the error message
await prisma.calendarSync.update({
where: { id: disabledSync.id },
data: {
lastSyncError: `Identifiants invalides ou expirés (401 Unauthorized). Veuillez vérifier vos identifiants Infomaniak dans la page courrier.`
}
});
}
}
// Continue with other accounts even if one fails
}
}
}
}
// For each Microsoft account, ensure there's a synced calendar
for (const account of microsoftAccounts) {
// Check if a calendar sync already exists for this account (enabled or disabled)
// This prevents creating duplicate calendars for the same account
const existingSync = await prisma.calendarSync.findFirst({
where: {
mailCredentialId: account.id
},
include: {
calendar: true
}
});
// If sync exists but is disabled, re-enable it instead of creating a new calendar
if (existingSync && !existingSync.syncEnabled) {
await prisma.calendarSync.update({
where: { id: existingSync.id },
data: { syncEnabled: true }
});
continue; // Skip to next account
}
if (!existingSync) {
// Try to discover calendars for this account
try {
const { discoverMicrosoftCalendars } = await import('@/lib/services/microsoft-calendar-sync');
const externalCalendars = await discoverMicrosoftCalendars(
session?.user?.id || '',
account.email
);
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: "#0078D4", // Microsoft blue
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: 'microsoft',
externalCalendarId: mainCalendar.id,
externalCalendarUrl: mainCalendar.webLink || mainCalendar.id,
syncEnabled: true,
syncFrequency: 15
}
});
// Trigger initial sync
try {
const { syncMicrosoftCalendar } = await import('@/lib/services/microsoft-calendar-sync');
const syncConfig = await prisma.calendarSync.findUnique({
where: { calendarId: calendar.id },
include: {
calendar: true,
mailCredential: true
}
});
if (syncConfig) {
await syncMicrosoftCalendar(syncConfig.id, true);
}
} catch (syncError) {
console.error('Error during initial Microsoft sync:', syncError);
// Don't fail if sync fails, calendar is still created
}
}
} catch (error) {
// Microsoft sync setup failed - likely because account doesn't have calendar scope yet
// This is expected for accounts authenticated before calendar scope was added
// User will need to re-authenticate their Microsoft account to get calendar access
console.log(`Microsoft calendar sync not available for ${account.email} - account may need re-authentication with calendar permissions`);
// Don't fail the page - continue with other accounts
}
}
}
// Clean up duplicate calendars for the same mailCredentialId
// Keep only the most recent one with syncEnabled=true, delete others
const allSyncs = await prisma.calendarSync.findMany({
where: {
calendar: {
userId: session?.user?.id || ''
}
},
include: {
calendar: true,
mailCredential: true
},
orderBy: {
createdAt: 'desc'
}
});
// Group by mailCredentialId and provider
const syncsByAccount = new Map<string, typeof allSyncs>();
for (const sync of allSyncs) {
if (sync.mailCredentialId) {
const key = `${sync.mailCredentialId}-${sync.provider}`;
if (!syncsByAccount.has(key)) {
syncsByAccount.set(key, []);
}
syncsByAccount.get(key)!.push(sync);
}
}
// For each account, keep only the most recent enabled sync, disable or delete others
for (const [key, syncs] of syncsByAccount.entries()) {
if (syncs.length > 1) {
// Sort by syncEnabled first (enabled first), then by createdAt (newest first)
syncs.sort((a, b) => {
if (a.syncEnabled !== b.syncEnabled) {
return a.syncEnabled ? -1 : 1;
}
return new Date(b.createdAt).getTime() - new Date(a.createdAt).getTime();
});
const keepSync = syncs[0];
const duplicates = syncs.slice(1);
// Disable or delete duplicate syncs
for (const duplicate of duplicates) {
if (duplicate.syncEnabled) {
// Disable the duplicate sync
await prisma.calendarSync.update({
where: { id: duplicate.id },
data: { syncEnabled: false }
});
}
// Delete the calendar if it has no events
const eventCount = await prisma.event.count({
where: { calendarId: duplicate.calendarId }
});
if (eventCount === 0) {
await prisma.calendar.delete({
where: { id: duplicate.calendarId }
});
}
}
}
}
// Refresh calendars after auto-setup and cleanup
// Exclude "Privée" and "Default" calendars that are not synced
calendars = await prisma.calendar.findMany({
where: {
userId: session?.user?.id || '',
OR: [
// Keep calendars that are not "Privée" or "Default"
{ name: { notIn: ["Privée", "Default"] } },
// Or keep "Privée"/"Default" calendars that have active sync config
{
AND: [
{ name: { in: ["Privée", "Default"] } },
{
syncConfig: {
isNot: null
}
}
]
}
]
},
include: {
events: {
orderBy: {
start: 'asc'
}
},
mission: {
include: {
missionUsers: true
}
},
syncConfig: {
include: {
mailCredential: {
select: {
id: true,
email: true,
display_name: true,
}
}
}
}
}
});
// No default calendar creation - only synced calendars from courrier
// Filter out "Privée" and "Default" calendars that don't have active sync
calendars = calendars.filter(cal => {
const isPrivateOrDefault = cal.name === "Privée" || cal.name === "Default";
const hasActiveSync = cal.syncConfig?.syncEnabled === true && cal.syncConfig?.mailCredential;
// Exclude "Privée"/"Default" calendars that are not actively synced
// Log for debugging if Infomaniak calendar is missing
if (isPrivateOrDefault && cal.syncConfig?.provider === 'infomaniak') {
if (!hasActiveSync) {
console.log(`Infomaniak calendar found but sync is disabled: ${cal.id}, syncEnabled: ${cal.syncConfig?.syncEnabled}, error: ${cal.syncConfig?.lastSyncError || 'none'}`);
} else {
console.log(`Infomaniak calendar is active: ${cal.id}, email: ${cal.syncConfig?.mailCredential?.email}`);
}
}
if (isPrivateOrDefault && !hasActiveSync) {
return false;
}
return true;
});
const now = new Date();
const nextWeek = add(now, { days: 7 });
const upcomingEvents = calendars.flatMap(cal =>
cal.events.filter(event =>
new Date(event.start) >= now &&
new Date(event.start) <= nextWeek
)
).sort((a, b) => new Date(a.start).getTime() - new Date(b.start).getTime());
// Calculate statistics
const totalEvents = calendars.flatMap(cal => cal.events).length;
const totalMeetingHours = calendars
.flatMap(cal => cal.events)
.reduce((total, event) => {
const start = new Date(event.start);
const end = new Date(event.end);
const hours = (end.getTime() - start.getTime()) / (1000 * 60 * 60);
return total + (isNaN(hours) ? 0 : hours);
}, 0);
return (
<main className="w-full h-screen bg-white">
<div className="w-full h-full px-4 pt-12 pb-4 flex">
<CalendarClient
initialCalendars={calendars}
userId={session.user.id}
userProfile={{
name: session.user.name || '',
email: session.user.email || '',
avatar: session.user.image || undefined
}}
/>
</div>
</main>
);
}