Google Integrations
OAuth Connections
Section titled “OAuth Connections”Google integration uses a provider-agnostic OAuth system:
oauth_connections— Stores tokens per (teacherId, provider, providerAccountId)integration_settings— Stores typed JSONB config per (teacherId, integrationType)
Integration types: calendar_read, calendar_write, drive, meet
Use ConnectionService methods — never read/write tokens directly from the teachers table.
Google Calendar
Section titled “Google Calendar”Reading Availability
Section titled “Reading Availability”The Slot Engine uses Google Calendar’s freebusy.query API to check teacher availability:
- Fetch selected calendars from
integration_settings(calendar_read config) - Query FreeBusy API for the date range
- Merge busy blocks with DB sessions
- Remove overlapping slots
Writing Events
Section titled “Writing Events”When sessions are confirmed, calendar events are created/updated:
Session confirmed → Create/update Google Calendar eventSession cancelled → Cancel calendar eventSession rescheduled → Update event timeSync tracked in session_calendar_sync:
syncStatus: synced / pending / failedgoogleCalendarEventId: event referencegoogleMeetLink: auto-generated Meet link
Calendar Sync Worker
Section titled “Calendar Sync Worker”A cron job (every 5 minutes) retries pending calendar sync operations:
- Batch of 20 records at a time
- Skips if google-calendar circuit breaker is open
- Automatic retry with backoff
Google Drive
Section titled “Google Drive”Resource Integration
Section titled “Resource Integration”Teachers can add Drive files as resources via the file picker:
- Teacher opens Drive file picker
- Selects files to import as resources
- Files tracked in
teacher_resources(provider: ‘drive’)
Auto-Copy to Student Folders
Section titled “Auto-Copy to Student Folders”When Drive resources are linked to a session, files are automatically copied to student folders:
1. Resource linked to session2. DriveCopyService queues BullMQ job3. Worker copies file to student's folder (students.driveFolderId)4. Progress tracked in drive_file_copies table Status: pending → copying → copied / failedDrive Sync Worker
Section titled “Drive Sync Worker”Daily cron (3 AM) verifies Drive file integrity:
- Checks if files still exist in Drive
- Marks trashed/deleted files
- Reports inconsistencies
Circuit Breaker
Section titled “Circuit Breaker”Both Calendar and Drive API calls are protected by circuit breakers:
| Service | Failure Threshold | Window | Recovery |
|---|---|---|---|
| Google Calendar | 5 failures | 60s | 30s |
| Google Drive | 5 failures | 60s | 60s |
Only infrastructure errors (429, 5xx, timeouts) trip the circuit.
Calendar fallback: Sets syncStatus='pending', session creation never fails. Calendar-sync cron retries later.
Drive fallback: Workers skip and retry later via BullMQ DelayedError.
Rate Limiting
Section titled “Rate Limiting”Redis-backed sliding-window rate limiting at two levels:
| Service | Per-Teacher | Global |
|---|---|---|
| Google Calendar | 10 req/10s | 400/100s |
| Google Drive | 5 req/10s | 10,000/60s |
Execution order: Circuit Breaker → Rate Limiter → Token refresh → API call → 429 Retry