Overview
TechCal’s calendar sync service automatically syncs bookmarked events to your Google Calendar. It handles OAuth token management, event creation/updates/deletion, and graceful error recovery.Calendar sync is triggered automatically when you bookmark an event or mark attendance. No manual API calls needed.
Architecture
Service Components
| Service | Purpose | Location |
|---|---|---|
| GoogleCalendarService | Google Calendar API wrapper | src/services/googleCalendarService.ts |
| CalendarSyncService | Orchestrates sync operations | src/services/calendarSyncService.ts |
| CalendarConnectionService | Manages OAuth connections | src/services/calendarConnectionService.ts |
Data Flow
- User bookmarks event → API route called
- CalendarSyncService checks connection status
- Token refresh if needed (GoogleCalendarService)
- Create/update calendar event (GoogleCalendarService)
- Update sync status in database
- Return success/error to user
Google Calendar API
Authentication
Uses custom OAuth credentials (not Supabase-managed):OAuth Flow
-
Initial Authorization:
- User clicks “Connect Calendar” in settings
- Redirect to Google OAuth consent screen
- Google returns authorization code
- Exchange code for access + refresh tokens
- Store tokens in
calendar_connectionstable
-
Token Refresh:
- Access tokens expire after 1 hour
- Service automatically refreshes using refresh token
- New access token stored in database
- Sync operation proceeds with fresh token
-
Re-authentication:
- If refresh fails → Mark connection as “requires reauth”
- User sees notification to reconnect
- Re-authorization flow clears error state
Calendar Event Operations
Create Event
- Adds ”📅 Added by Kure-Cal” to description
- Sets event source metadata
- Handles timezone conversion (all times in UTC)
- Includes event URL in description
Update Event
- Event details changed on TechCal
- User updated bookmark note
- Time/location changed
Delete Event
- User unbookmarks event
- User cancels attendance
- Event deleted from TechCal
Sync Triggers
Automatic Sync Events
- Bookmark Event
- Mark Attending
- Unbookmark Event
- Cancel Attendance
Trigger: User bookmarks an eventBehavior:
- Create event in Google Calendar
- Store
external_calendar_event_id - Set
calendar_sync_status = 'synced' - Set
calendar_synced_attimestamp
- Active Google Calendar connection
- Valid access token
- Event has valid start/end times
Manual Sync Operations
Bulk Sync (First Connection): When user first connects calendar, all bookmarked events are synced:- Syncs in batches of 10 events
- 2-second delay between batches
- Respects Google API rate limits
Error Handling
Error Types
| Error Code | Meaning | Action |
|---|---|---|
| 401 | Token expired | Auto-refresh and retry |
| 403 | Insufficient permissions | Mark for reauth |
| 404 | Calendar not found | Mark for reauth |
| 429 | Rate limit exceeded | Retry with exponential backoff |
| 500-504 | Google server error | Retry once after 1 second |
Retry Logic
User-Facing Errors
Friendly error messages:Database Schema
calendar_connections
user_events (sync fields)
Security Considerations
Token Storage
Scope Minimization
Only request necessary Google Calendar scopes:- Create/update/delete events ✅
- Read calendar metadata ✅
- Cannot read other users’ calendars ❌
- Cannot modify calendar settings ❌
Rate Limiting
Google Calendar API limits:- Per user: 1,000,000 queries per day
- Burst limit: 10 queries per second
- Batch processing with delays
- Exponential backoff on rate limit errors
- Bulk sync throttling (10 events per batch, 2s delay)
Monitoring
Success Metrics
Track incalendar_connections table:
Sentry Tracking
Key breadcrumbs:calendar_sync.initiated- Sync startedcalendar_sync.completed- Sync succeededcalendar_sync.token_refreshed- Token refreshedcalendar_sync.failed- Sync failed with error
Error Logs
Testing
Manual Testing
-
Connect Calendar:
- Go to
/dashboard/settings - Click “Connect Google Calendar”
- Authorize app
- Verify connection shown as active
- Go to
-
Sync Event:
- Bookmark any event
- Check Google Calendar for new event
- Verify event details match
-
Unsync Event:
- Unbookmark event
- Verify event removed from Google Calendar
-
Token Expiry:
- Manually set
token_expiryto past date - Bookmark event
- Verify automatic token refresh
- Manually set
Integration Tests
Test sync scenarios:Related Documentation
- POST /api/events/tracked - Trigger sync operations
- GET /api/profile/preferences - Calendar sync settings
- Google Calendar API Docs - Official API reference