* refactor: replace FeaturesRepository with DI-based feature repositories Migrate from the legacy FeaturesRepository pattern to separate DI-based repositories: - Add checkIfFeatureIsEnabledGlobally to IFeatureRepository - Add getTeamsWithFeatureEnabled to ITeamFeatureRepository - Update CalendarSubscriptionService to use featureRepository, teamFeatureRepository, and userFeatureRepository - Update SelectedCalendarRepository.findNextSubscriptionBatch to filter by teamIds instead of featureIds - Update OnboardingPathService to use DI via getFeatureRepository() - Remove prisma parameter from OnboardingPathService callsites Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com> * test: update tests to use new DI-based feature repositories Update CalendarSubscriptionService and SelectedCalendarRepository tests to use the new separate repository interfaces: - featureRepository for global feature checks - teamFeatureRepository for team-level feature checks - userFeatureRepository for user-level feature checks Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com> * fix: update API routes to use DI-based feature repositories Update cron and webhook routes to use the new separate repository interfaces instead of the combined FeaturesRepository. Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com> * fix: remove prisma arg from getGettingStartedPathWithParams call The OnboardingPathService method no longer requires a prisma argument as it now uses DI containers internally. Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com> * test: update route tests to expect new DI-based feature repositories Update service instantiation tests to expect featureRepository, teamFeatureRepository, and userFeatureRepository instead of the old featuresRepository. Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com> * fix: optimize global feature check and add guard in checkForNewSubscriptions - Use targeted select for only `enabled` field in checkIfFeatureIsEnabledGlobally - Add global feature flag guard in checkForNewSubscriptions to avoid unnecessary DB queries and API calls when the cache feature is globally disabled Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com> * remove redundant comment --------- Co-authored-by: Claude Opus 4.5 <noreply@anthropic.com>
Calendar Cache and Sync
The Calendar Cache and Sync feature provides efficient calendar synchronization with intelligent caching to reduce API calls and ensure real-time updates across your Cal.com instance.
Feature Overview
This feature introduces two complementary capabilities:
- Calendar Cache: Stores calendar availability data locally to reduce external API calls and improve performance
- Calendar Sync: Uses webhooks to automatically listen for calendar updates and apply changes in real-time
Key Benefits:
- Efficiency: Reduces API calls with optimized caching strategies
- Reliability: Guarantees updates through webhook event delivery
- Real-Time Sync: Ensures calendars are always up-to-date with minimal latency
- Scalability: Supports multiple calendars and handles high-volume updates seamlessly
Motivation: By subscribing to calendars via webhooks and implementing intelligent caching, you gain a smarter, faster, and more resource-friendly way to keep your data in sync. This eliminates the need for constant polling and reduces the load on external calendar APIs while ensuring data consistency.
Environment Variables
- GOOGLE_WEBHOOK_URL: Optional, only used for local tests. Default points to application url.
- GOOGLE_WEBHOOK_TOKEN: Required, token to validate Google Webhook incoming.
- MICROSOFT_WEBHOOK_URL: Optional, only used for local tests. Default points to application url.
- MICROSOFT_WEBHOOK_TOKEN: Required, token to validate Microsoft Webhook incoming.
Feature Flags
This feature is controlled by three feature flags that can be enabled independently:
1. calendar-subscription-cache
Enables calendar cache recording and usage through calendars. This flag should be managed individually by teams.
INSERT INTO "Feature" ("slug", "enabled", "description", "type", "stale", "lastUsedAt", "createdAt", "updatedAt", "updatedBy")
VALUES ('calendar-subscription-cache', false, 'Allow calendar cache to be recorded and used through calendars.', 'OPERATIONAL', false, NULL, NOW(), NOW(), NULL)
ON CONFLICT (slug) DO NOTHING;
2. calendar-subscription-sync
Enables calendar sync globally for all users regardless of team or organization.
INSERT INTO "Feature" ("slug", "enabled", "description", "type", "stale", "lastUsedAt", "createdAt", "updatedAt", "updatedBy")
VALUES ('calendar-subscription-sync', false, 'Enable calendar sync for all calendars globally.', 'OPERATIONAL', false, NULL, NOW(), NOW(), NULL)
ON CONFLICT (slug) DO NOTHING;
Enabling Features for Specific Users
To enable calendar cache features for specific users, add entries to the UserFeatures table:
-- Enable calendar-subscription-cache for user ID 123
INSERT INTO "UserFeatures" ("userId", "featureId", "assignedAt", "assignedBy", "updatedAt")
VALUES (123, 'calendar-subscription-cache', NOW(), 'admin', NOW())
ON CONFLICT ("userId", "featureId") DO NOTHING;
-- Enable calendar-subscription-sync for user ID 123
INSERT INTO "UserFeatures" ("userId", "featureId", "assignedAt", "assignedBy", "updatedAt")
VALUES (123, 'calendar-subscription-sync', NOW(), 'admin', NOW())
ON CONFLICT ("userId", "featureId") DO NOTHING;
Enabling Features for Specific Teams
To enable calendar cache features for specific teams, add entries to the TeamFeatures table:
-- Enable calendar-subscription-cache for team ID 456
INSERT INTO "TeamFeatures" ("teamId", "featureId", "assignedAt", "assignedBy", "updatedAt")
VALUES (456, 'calendar-subscription-cache', NOW(), 'admin', NOW())
ON CONFLICT ("teamId", "featureId") DO NOTHING;
-- Enable calendar-subscription-sync for team ID 456
INSERT INTO "TeamFeatures" ("teamId", "featureId", "assignedAt", "assignedBy", "updatedAt")
VALUES (456, 'calendar-subscription-sync', NOW(), 'admin', NOW())
ON CONFLICT ("teamId", "featureId") DO NOTHING;
Architecture
The calendar cache and sync system consists of several key components:
Database Schema
- CalendarCacheEvent Table: Stores cached calendar events with status tracking
- SelectedCalendar Extensions: Additional fields for sync state and webhook management including:
channelId: Webhook channel identifierchannelResourceId: Resource ID for webhook subscriptionschannelResourceUri: URI for webhook notificationschannelKind: Type of webhook channelchannelExpiration: Webhook subscription expiration timesyncToken: Token for incremental syncsyncedAt: Last successful sync timestampsyncErrorAt: Last sync error timestampsyncErrorCount: Number of consecutive sync errorssyncSubscribedAt: Webhook subscription timestamp
Core Services
- CalendarCacheEventRepository: Manages cached event storage and retrieval
- CalendarSubscriptionService: Orchestrates webhook subscriptions and event processing
- Provider-specific Adapters: Handle calendar-specific sync logic (Google, Office365)
Background Processes
- Cron Jobs: Automated processes for cache cleanup and calendar watching
- Webhook Handlers: Real-time event processing for calendar updates
Integration Points
- Calendar Providers: Google Calendar, Office365, and other supported integrations
- Webhook Endpoints: Receive real-time notifications from calendar providers
- Cache Layer: Optimized storage for frequently accessed calendar data
For detailed technical implementation, see:
- Database migrations in
packages/prisma/migrations/