Share calendar events across calendar channels

Merged
NE
2mo ago
+1382-182

Superseded from https://github.com/twentyhq/twenty/pull/27959

Differences from the #27959 design

Channel roles instead of last write wins. Copies of a shared event disagree on attendee answers: a guest copy often shows other attendees as not responded (Microsoft guest copies report none, Google does when guests can't see each other). With last write wins, a stale copy overwrites real answers and they flip on every sync. Now each copy writes only its own attendee's answer. The organizer's copy owns the fields and every answer while its calendar syncs; if it isn't connected or syncing, guest copies keep the fields current. Attendees are still never deleted on a shared event.

Locks instead of accepting the race. Two saves of the same event can both miss and both insert: an event created from Twenty races its own webhook, and two shared channels can add the same attendee. Each save takes a pg_advisory_xact_lock per sharing key, sorted, in one statement, before reading. Linking takes FOR KEY SHARE on the reused event, and the cleaner takes FOR UPDATE and re-checks before deleting.

Cost. One extra statement per batch (at most 100 keys from the cron, 50 from a webhook), held in memory and released at commit. FOR KEY SHARE only blocks deletes, never normal updates. Roles are computed in memory from the workspace's other channels of the same provider, read once per batch.

Real test run

Ran locally against real provider accounts, using dev OAuth apps only: three Google calendars and one Microsoft calendar in one workspace, synced through both webhooks and the cron. About 1,650 events imported with no duplicates. Google copies of the same meeting shared one event, and the Microsoft copy stayed separate. RSVP changes, renames and cancellations landed correctly, and the cleaner removed events once their last calendar dropped them.

Entry points

Call stack, steps 1 to 5

Call stack, steps 6 to 8

Call stack, steps 9 to 13

Channel role

What each role may write, and locks

Cleaner and ORM change