Skip to main content
← All notes
Building

The booking sync cron matches calendar attendees to clients by building an email lookup map

software

The syncBookingsForAllClients function in lib/portal/calendar-sync.ts starts by loading every non-archived client and building a Map from lowercased email to client ID. Then it pulls Google Calendar events from ninety days ago to thirty days ahead, paginating through up to 250 events per page. For each event, it lowercases every attendee email, maps each through the client email Map, and takes the first match. If no attendee matches a client, the event is skipped. If one does, the function upserts a booking record keyed on calendarEventId — updating the title, times, and Meet link if the record exists, creating it if it does not. The Map construction is the critical decision. A naive implementation would query the database for each event's attendees, making one round trip per calendar event. The Map makes it zero — one query loads all clients, one in-memory lookup per attendee resolves the match. The Meet link extraction walks two paths. First it checks conferenceData.entryPoints for a video type entry point. If that fails, it falls back to hangoutLink. The fallback covers older events created before Google migrated to the conferenceData structure. The upsert on calendarEventId means events booked through the site's BookCall component and events created manually in Google Calendar both end up in the same portal view. The cron route itself supports both POST and GET — the GET handler just calls POST, so a developer can trigger a sync by visiting the URL in a browser with the right Bearer token. The result object returns a synced count and an errors array. Individual event failures push to the array without breaking the loop, so one corrupt event does not prevent the other two hundred and forty-nine from syncing.

Comments coming soon

Sign in with TikTok to leave a comment. Coming soon.