Skip to content

BT-0261: Optimize Outlook Calendar Cache with Graph Change Notifications#1

Open
sungdark wants to merge 1 commit intomainfrom
bt-0261-outlook-cache-optimization
Open

BT-0261: Optimize Outlook Calendar Cache with Graph Change Notifications#1
sungdark wants to merge 1 commit intomainfrom
bt-0261-outlook-cache-optimization

Conversation

@sungdark
Copy link
Copy Markdown
Owner

Summary

This PR enables the Microsoft Outlook (Office365) calendar cache to reduce the number of Microsoft Graph API calls when checking calendar availability on public event type pages.

Problem

Currently, for Microsoft Outlook calendars, Cal.com checks every single connected calendar whenever a user event type public page is visited. This works well for most cases but can become expensive as more calendars are added, especially for team events where multiple users' calendars need to be checked for round-robin scheduling.

Solution

This PR implements the following optimizations:

1. Enable Office365 Calendar Cache (CalendarCacheEventService.isCalendarTypeSupported)

The calendar cache was explicitly disabled for Office365 calendars. This change enables it:

static isCalendarTypeSupported(type: string | null): boolean {
  if (!type) return false;
  return ["google_calendar", "office365_calendar"].includes(type);
}

2. Expand Subscription to Individual Users (SelectedCalendarRepository.findNextSubscriptionBatch)

Previously, the subscription batch query only returned calendars for users in teams with the feature flag enabled. This change also includes users not in any team, ensuring individual users also benefit from cached calendar data:

// Include both:
// 1. Users in teams with the feature flag enabled (teamId in teamIds)
// 2. Users not in any team at all (to ensure individual users also get subscriptions)
const userTeamFilter: Prisma.SelectedCalendarWhereInput = {
  OR: [
    {
      user: {
        teams: {
          some: {
            accepted: true,
            teamId: { in: teamIds },
          },
        },
      },
    },
    {
      user: {
        teams: {
          none: {},
        },
      },
    },
  ],
};

How It Works

  1. When the calendar-subscription-cache feature flag is enabled for a user, the CalendarCacheWrapper is used instead of the raw calendar service
  2. The CalendarCacheWrapper.getAvailability() method splits calendars into two groups:
    • With sync: Calendars with both syncToken and syncSubscribedAt → served from the local database cache
    • Without sync: Calendars missing either → queried from Microsoft Graph directly
  3. Microsoft Graph change notifications are used to keep the cache up-to-date:
    • When a calendar event changes, Microsoft Graph sends a webhook notification
    • The webhook handler processes the notification and updates the local cache
  4. The cron job /api/cron/calendar-subscriptions periodically creates new subscriptions for calendars that don't have one yet

Testing

Updated the corresponding test to expect office365_calendar to return true for isCalendarTypeSupported.

Related Issues

…ndividual users

This commit enables the calendar cache for Microsoft Outlook (Office365) calendars
by updating isCalendarTypeSupported to include 'office365_calendar'.

Key changes:
1. CalendarCacheEventService.isCalendarTypeSupported: Added office365_calendar
   to the list of supported calendar types, enabling the cache wrapper for
   Outlook calendars.

2. SelectedCalendarRepository.findNextSubscriptionBatch: Modified the query to
   also return calendars for users not in any team, ensuring individual users
   also get their calendars subscribed for change notifications.

3. Updated the corresponding test to expect office365_calendar to return true.

With these changes:
- Office365 calendars will now use the CalendarCacheWrapper when the
  calendar-subscription-cache feature flag is enabled
- The cache will store calendar events and serve them from the database
  instead of querying Microsoft Graph on every public page visit
- Individual users (not just team members) will have their calendars
  subscribed for Microsoft Graph change notifications
- When a calendar change notification is received, the cache will be
  updated automatically
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Outlook Cache – Bounty-to-Hire

1 participant