Agenda refactor
This commit is contained in:
parent
108fa3fb8c
commit
33b14a1e32
@ -448,6 +448,7 @@ export default async function CalendarPage() {
|
||||
});
|
||||
|
||||
// Create sync configuration
|
||||
// Use 5 minutes for Microsoft (more reactive than 15 minutes)
|
||||
await prisma.calendarSync.create({
|
||||
data: {
|
||||
calendarId: calendar.id,
|
||||
@ -456,7 +457,7 @@ export default async function CalendarPage() {
|
||||
externalCalendarId: mainCalendar.id,
|
||||
externalCalendarUrl: mainCalendar.webLink || mainCalendar.id,
|
||||
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)
|
||||
for (const syncConfig of microsoftSyncConfigs) {
|
||||
const needsSync = !syncConfig.lastSyncAt ||
|
||||
(Date.now() - syncConfig.lastSyncAt.getTime()) / (1000 * 60) >= syncConfig.syncFrequency;
|
||||
// For Microsoft, use a more frequent check (2 minutes) for better reactivity
|
||||
// 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) {
|
||||
console.log(`[AGENDA] Triggering background sync for Microsoft calendar ${syncConfig.id}`);
|
||||
// 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
|
||||
// Use forceSync=true because we've already checked that sync is needed
|
||||
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:`, {
|
||||
calendarSyncId: syncConfig.id,
|
||||
synced: result.synced,
|
||||
@ -597,7 +607,7 @@ export default async function CalendarPage() {
|
||||
});
|
||||
});
|
||||
} 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)`);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@ -75,6 +75,9 @@ export async function POST(req: NextRequest) {
|
||||
}
|
||||
|
||||
// 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({
|
||||
where: { calendarId },
|
||||
create: {
|
||||
@ -84,14 +87,14 @@ export async function POST(req: NextRequest) {
|
||||
externalCalendarId: externalCalendarId || null,
|
||||
externalCalendarUrl,
|
||||
syncEnabled: true,
|
||||
syncFrequency: syncFrequency || 15,
|
||||
syncFrequency: syncFrequency || defaultSyncFrequency,
|
||||
},
|
||||
update: {
|
||||
mailCredentialId,
|
||||
provider: detectedProvider,
|
||||
externalCalendarId: externalCalendarId || null,
|
||||
externalCalendarUrl,
|
||||
syncFrequency: syncFrequency || 15,
|
||||
syncFrequency: syncFrequency || defaultSyncFrequency,
|
||||
syncEnabled: true,
|
||||
},
|
||||
});
|
||||
|
||||
Loading…
Reference in New Issue
Block a user