Agenda refactor

This commit is contained in:
alma 2026-01-14 20:51:22 +01:00
parent 9904370ac8
commit d9c78d8125
2 changed files with 5 additions and 67 deletions

View File

@ -178,9 +178,7 @@ export default async function CalendarPage() {
// 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) {
console.log(`[AGENDA] Processing ${infomaniakAccounts.length} Infomaniak account(s)`);
for (const account of infomaniakAccounts) {
console.log(`[AGENDA] Processing Infomaniak account: ${account.email}, hasPassword: ${!!account.password}, passwordLength: ${account.password?.length || 0}`);
// Check if a calendar sync already exists for this account (enabled or disabled)
// This prevents creating duplicate calendars for the same account
let existingSync = await prisma.calendarSync.findFirst({
@ -341,21 +339,11 @@ export default async function CalendarPage() {
// No sync exists for this account - try to discover and create calendar
// Only create calendar if discovery succeeds
try {
// Verify password is available
if (!account.password) {
console.log(`[AGENDA] Skipping Infomaniak account ${account.email} - no password available`);
continue;
}
console.log(`[AGENDA] Attempting to discover calendars for Infomaniak account ${account.email} (password length: ${account.password.length})`);
const { discoverInfomaniakCalendars } = await import('@/lib/services/caldav-sync');
const externalCalendars = await discoverInfomaniakCalendars(
account.email,
account.password
account.password!
);
console.log(`[AGENDA] Discovered ${externalCalendars.length} calendars for Infomaniak account ${account.email}`);
if (externalCalendars.length > 0) {
// Use the first calendar (usually the main calendar)

View File

@ -27,19 +27,8 @@ export async function getInfomaniakCalDAVClient(
password: string
): Promise<WebDAVClient> {
// Infomaniak CalDAV base URL (from Infomaniak sync assistant)
// The actual CalDAV endpoint is at /caldav path
const baseUrl = 'https://sync.infomaniak.com/caldav';
if (!password || password.trim() === '') {
throw new Error('Password is required for Infomaniak CalDAV authentication');
}
logger.debug('Creating Infomaniak CalDAV client', {
email,
baseUrl,
hasPassword: !!password,
passwordLength: password.length,
});
// Base URL is https://sync.infomaniak.com, CalDAV endpoint is accessed via /caldav path
const baseUrl = 'https://sync.infomaniak.com';
const client = createClient(baseUrl, {
username: email,
@ -57,49 +46,10 @@ export async function discoverInfomaniakCalendars(
password: string
): Promise<CalDAVCalendar[]> {
try {
logger.debug('Starting Infomaniak calendar discovery', {
email,
hasPassword: !!password,
passwordLength: password?.length || 0,
});
const client = await getInfomaniakCalDAVClient(email, password);
logger.debug('CalDAV client created, attempting to list directory contents');
// List all calendars using PROPFIND on root
let items;
try {
items = await client.getDirectoryContents('/');
logger.debug(`Successfully retrieved ${items.length} items from CalDAV server`);
} catch (dirError: any) {
// Log more details about the error
const errorDetails: any = {
email,
error: dirError?.message || String(dirError),
status: dirError?.status,
statusText: dirError?.statusText,
};
// Try to extract more details from the error
if (dirError?.response) {
errorDetails.responseStatus = dirError.response.status;
errorDetails.responseStatusText = dirError.response.statusText;
errorDetails.responseHeaders = dirError.response.headers;
}
// Check if it's a 401 error specifically
if (dirError?.status === 401 || dirError?.response?.status === 401) {
logger.error('CalDAV authentication failed (401 Unauthorized)', {
...errorDetails,
hint: 'This usually means: 1) Password is incorrect, 2) Password has changed, 3) 2FA is enabled and requires an app-specific password, 4) Account credentials are invalid',
});
} else {
logger.error('Error listing directory contents', errorDetails);
}
throw dirError;
}
// List all calendars using PROPFIND on /caldav path
const items = await client.getDirectoryContents('/caldav');
const calendars: CalDAVCalendar[] = [];