Agenda refactor

This commit is contained in:
alma 2026-01-14 21:16:08 +01:00
parent 108fa3fb8c
commit 33b14a1e32
2 changed files with 21 additions and 8 deletions

View File

@ -448,6 +448,7 @@ export default async function CalendarPage() {
}); });
// Create sync configuration // Create sync configuration
// Use 5 minutes for Microsoft (more reactive than 15 minutes)
await prisma.calendarSync.create({ await prisma.calendarSync.create({
data: { data: {
calendarId: calendar.id, calendarId: calendar.id,
@ -456,7 +457,7 @@ export default async function CalendarPage() {
externalCalendarId: mainCalendar.id, externalCalendarId: mainCalendar.id,
externalCalendarUrl: mainCalendar.webLink || mainCalendar.id, externalCalendarUrl: mainCalendar.webLink || mainCalendar.id,
syncEnabled: true, syncEnabled: true,
syncFrequency: 15 syncFrequency: 5
} }
}); });
@ -571,17 +572,26 @@ export default async function CalendarPage() {
// Trigger sync for Microsoft calendars that need it (async, don't wait) // Trigger sync for Microsoft calendars that need it (async, don't wait)
for (const syncConfig of microsoftSyncConfigs) { for (const syncConfig of microsoftSyncConfigs) {
const needsSync = !syncConfig.lastSyncAt || // For Microsoft, use a more frequent check (2 minutes) for better reactivity
(Date.now() - syncConfig.lastSyncAt.getTime()) / (1000 * 60) >= syncConfig.syncFrequency; // This allows new events to appear faster without overloading the API
const microsoftMinSyncInterval = 2; // minutes
const minutesSinceLastSync = syncConfig.lastSyncAt
? (Date.now() - syncConfig.lastSyncAt.getTime()) / (1000 * 60)
: Infinity;
console.log(`[AGENDA] Microsoft sync config ${syncConfig.id}: lastSyncAt=${syncConfig.lastSyncAt}, needsSync=${needsSync}, syncFrequency=${syncConfig.syncFrequency}`); // Sync if never synced, or if enough time has passed (use minimum of 2 min or configured frequency)
const needsSync = !syncConfig.lastSyncAt ||
minutesSinceLastSync >= Math.min(microsoftMinSyncInterval, syncConfig.syncFrequency);
console.log(`[AGENDA] Microsoft sync config ${syncConfig.id}: lastSyncAt=${syncConfig.lastSyncAt}, minutesSinceLastSync=${minutesSinceLastSync.toFixed(1)}, needsSync=${needsSync}, syncFrequency=${syncConfig.syncFrequency}`);
if (needsSync) { if (needsSync) {
console.log(`[AGENDA] Triggering background sync for Microsoft calendar ${syncConfig.id}`); console.log(`[AGENDA] Triggering background sync for Microsoft calendar ${syncConfig.id}`);
// Trigger sync in background (don't await to avoid blocking page load) // Trigger sync in background (don't await to avoid blocking page load)
// The sync will update the database, and the next page load will show the events // The sync will update the database, and the next page load will show the events
// Use forceSync=true because we've already checked that sync is needed
import('@/lib/services/microsoft-calendar-sync').then(({ syncMicrosoftCalendar }) => { import('@/lib/services/microsoft-calendar-sync').then(({ syncMicrosoftCalendar }) => {
syncMicrosoftCalendar(syncConfig.id, false).then((result) => { syncMicrosoftCalendar(syncConfig.id, true).then((result) => {
console.log(`[AGENDA] Microsoft sync completed:`, { console.log(`[AGENDA] Microsoft sync completed:`, {
calendarSyncId: syncConfig.id, calendarSyncId: syncConfig.id,
synced: result.synced, synced: result.synced,
@ -597,7 +607,7 @@ export default async function CalendarPage() {
}); });
}); });
} else { } else {
console.log(`[AGENDA] Microsoft sync skipped - too soon since last sync (${syncConfig.syncFrequency} min)`); console.log(`[AGENDA] Microsoft sync skipped - too soon since last sync (${minutesSinceLastSync.toFixed(1)} min < ${Math.min(microsoftMinSyncInterval, syncConfig.syncFrequency)} min)`);
} }
} }

View File

@ -75,6 +75,9 @@ export async function POST(req: NextRequest) {
} }
// Create or update sync configuration // Create or update sync configuration
// Use different default frequencies: 5 min for Microsoft (more reactive), 15 min for others
const defaultSyncFrequency = detectedProvider === 'microsoft' ? 5 : (syncFrequency || 15);
const syncConfig = await prisma.calendarSync.upsert({ const syncConfig = await prisma.calendarSync.upsert({
where: { calendarId }, where: { calendarId },
create: { create: {
@ -84,14 +87,14 @@ export async function POST(req: NextRequest) {
externalCalendarId: externalCalendarId || null, externalCalendarId: externalCalendarId || null,
externalCalendarUrl, externalCalendarUrl,
syncEnabled: true, syncEnabled: true,
syncFrequency: syncFrequency || 15, syncFrequency: syncFrequency || defaultSyncFrequency,
}, },
update: { update: {
mailCredentialId, mailCredentialId,
provider: detectedProvider, provider: detectedProvider,
externalCalendarId: externalCalendarId || null, externalCalendarId: externalCalendarId || null,
externalCalendarUrl, externalCalendarUrl,
syncFrequency: syncFrequency || 15, syncFrequency: syncFrequency || defaultSyncFrequency,
syncEnabled: true, syncEnabled: true,
}, },
}); });