Agenda refactor

This commit is contained in:
alma 2026-01-15 13:00:06 +01:00
parent fd24dfbd21
commit e17cdf21d1

View File

@ -45,34 +45,29 @@ export async function discoverInfomaniakCalendars(
password: string
): Promise<CalDAVCalendar[]> {
try {
console.log(`[CALDAV] Starting calendar discovery for ${email}`);
const client = await getInfomaniakCalDAVClient(email, password);
// List all calendars using PROPFIND on root
logger.debug('Discovering Infomaniak calendars', { email });
console.log(`[CALDAV] Fetching directory contents from root (/)`);
const items = await client.getDirectoryContents('/');
logger.debug('Found items in root directory', {
email,
itemsCount: items.length,
items: items.map(item => ({ filename: item.filename, type: item.type })),
});
console.log(`[CALDAV] Found ${items.length} items in root directory:`,
items.map(item => ({ filename: item.filename, type: item.type, basename: item.basename }))
);
const calendars: CalDAVCalendar[] = [];
for (const item of items) {
// Skip non-directories, root, and special directories like /principals
if (item.type !== 'directory' || item.filename === '/' || item.filename === '/principals') {
logger.debug('Skipping item', {
filename: item.filename,
type: item.type,
reason: item.type !== 'directory' ? 'not a directory' : 'special directory',
});
console.log(`[CALDAV] Skipping item: ${item.filename} (type: ${item.type})`);
continue;
}
// Get calendar properties to verify it's actually a calendar
try {
logger.debug('Checking if item is a calendar', { filename: item.filename });
console.log(`[CALDAV] Checking if ${item.filename} is a calendar...`);
const props = await client.customRequest(item.filename, {
method: 'PROPFIND',
headers: {
@ -96,18 +91,15 @@ export async function discoverInfomaniakCalendars(
dataStr.includes('calendar') ||
dataStr.includes('urn:ietf:params:xml:ns:caldav');
logger.debug('Calendar check result', {
filename: item.filename,
console.log(`[CALDAV] Calendar check for ${item.filename}:`, {
isCalendar,
hasData: !!props.data,
dataPreview: props.data ? props.data.substring(0, 500) : 'no data',
dataLength: dataStr.length,
dataPreview: dataStr.substring(0, 300),
});
if (!isCalendar) {
logger.debug('Skipping non-calendar directory', {
filename: item.filename,
reason: 'resourcetype does not indicate calendar',
});
console.log(`[CALDAV] Skipping ${item.filename} - not a calendar (resourcetype check failed)`);
continue;
}
@ -122,38 +114,31 @@ export async function discoverInfomaniakCalendars(
color: color,
};
logger.debug('Found valid calendar', calendar);
console.log(`[CALDAV] ✅ Found valid calendar:`, calendar);
calendars.push(calendar);
} catch (error) {
logger.error('Error fetching calendar properties', {
calendar: item.filename,
error: error instanceof Error ? error.message : String(error),
});
console.error(`[CALDAV] Error fetching calendar properties for ${item.filename}:`,
error instanceof Error ? error.message : String(error)
);
// Don't add calendars that fail property fetch - they might not be calendars
}
}
logger.info('Infomaniak calendar discovery completed', {
email,
calendarsFound: calendars.length,
calendars: calendars.map(cal => ({ id: cal.id, name: cal.name, url: cal.url })),
});
console.log(`[CALDAV] Discovery completed: found ${calendars.length} calendars for ${email}`);
if (calendars.length > 0) {
console.log(`[CALDAV] Calendars:`, calendars.map(cal => ({ id: cal.id, name: cal.name, url: cal.url })));
}
return calendars;
} catch (error) {
const errorMessage = error instanceof Error ? error.message : String(error);
const errorDetails = error instanceof Error ? {
name: error.name,
message: error.message,
stack: error.stack?.substring(0, 500), // More stack for debugging
} : { raw: String(error) };
const errorStack = error instanceof Error ? error.stack?.substring(0, 500) : undefined;
// Log as error for debugging, but don't throw to avoid breaking the page
logger.error('Infomaniak calendar discovery failed', {
email,
console.error(`[CALDAV] ❌ Calendar discovery failed for ${email}:`, {
error: errorMessage,
errorDetails,
stack: errorStack,
});
// Ne pas faire échouer toute la page agenda si la découverte échoue
// On retourne simplement une liste vide -> pas de sync auto possible
return [];