feat: Calendar Cache and Sync (#23876)
* feat: calendar cache and sync - wip * Add env.example * refactor on CalendarCacheEventService * remove test console.log * Fix type checks errors * chore: remove pt comment * add route.ts * chore: fix tests * Improve cache impl * chore: update recurring event id * chore: small improvements * calendar cache improvements * Fix remove dynamic imports * Add cleanup stale cache * Fix tests * add event update * type fixes * feat: add comprehensive tests for new calendar subscription API routes - Add tests for /api/cron/calendar-subscriptions-cleanup route (9 tests) - Add tests for /api/cron/calendar-subscriptions route (10 tests) - Add tests for /api/webhooks/calendar-subscription/[provider] route (11 tests) - Add missing feature flags for calendar-subscription-cache and calendar-subscription-sync - All 30 tests pass with comprehensive coverage of authentication, feature flags, error handling, and service instantiation Tests cover: - Authentication scenarios (API key validation, Bearer tokens, query parameters) - Feature flag combinations (cache/sync enabled/disabled states) - Success and error handling (including non-Error exceptions) - Service instantiation with proper dependency injection - Provider validation for webhook endpoints Co-Authored-By: Volnei Munhoz <[email protected]> * feat: add comprehensive tests for calendar subscription services, repositories, and adapters - Add unit tests for CalendarSubscriptionService with subscription, webhook, and event processing - Add unit tests for CalendarCacheEventService with cache operations and cleanup - Add unit tests for CalendarSyncService with Cal.com event filtering and booking operations - Add unit tests for CalendarCacheEventRepository with CRUD operations - Add unit tests for SelectedCalendarRepository with calendar selection management - Add unit tests for GoogleCalendarSubscriptionAdapter with subscription and event fetching - Add unit tests for Office365CalendarSubscriptionAdapter with placeholder implementation - Add unit tests for AdaptersFactory with provider management and adapter creation - Fix lint issues by removing explicit 'any' type casting and unused variables - All tests follow Cal.com conventions using Vitest framework with proper mocking Co-Authored-By: Volnei Munhoz <[email protected]> * fix: improve calendar-subscriptions-cleanup test performance by adding missing mocks - Add comprehensive mocks for defaultResponderForAppDir, logger, performance monitoring, and Sentry - Fix slow test execution (933ms -> <100ms) caused by missing dependency mocks - Ensure consistent test performance across different environments Co-Authored-By: Volnei Munhoz <[email protected]> * Fix tests * Fix tests * type fix * Fix coderabbit comments * Fix types * Fix test * Update apps/web/app/api/cron/calendar-subscriptions/route.ts Co-authored-by: Alex van Andel <[email protected]> * Fixes by first review * feat: add database migrations for calendar cache and sync fields - Add CalendarCacheEventStatus enum with confirmed, tentative, cancelled values - Add new fields to SelectedCalendar: channelId, channelKind, channelResourceId, channelResourceUri, channelExpiration, syncSubscribedAt, syncToken, syncedAt, syncErrorAt, syncErrorCount - Create CalendarCacheEvent table with foreign key to SelectedCalendar - Add necessary indexes and constraints for performance and data integrity Fixes database schema issues causing e2e test failures with 'column does not exist' errors. Co-Authored-By: Volnei Munhoz <[email protected]> * only google-calendar for now * docs: add Calendar Cache and Sync feature documentation - Add comprehensive feature overview and motivation - Document feature flags with SQL examples - Include SQL examples for enabling features for users and teams - Reference technical documentation files Addresses PR #23876 documentation requirements Co-Authored-By: Volnei Munhoz <[email protected]> * docs: update calendar subscription README with comprehensive documentation - Undo incorrect changes to main README.md - Update packages/features/calendar-subscription/README.md with: - Feature overview and motivation - Environment variables section - Complete feature flags documentation with SQL examples - SQL examples for enabling features for users and teams - Detailed architecture documentation Addresses PR #23876 documentation requirements Co-Authored-By: Volnei Munhoz <[email protected]> * fix docs * Fix test to available calendars * Fix test to available calendars * add migration and sync boilerplate * fix typo * remove double log * sync boilerplate --------- Co-authored-by: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com> Co-authored-by: Alex van Andel <[email protected]> Co-authored-by: Keith Williams <[email protected]>
This commit is contained in:
42 files changed
+4723
-42
No files matched your search
+58
@@ -0,0 +1,58 @@
|
||||
/*
|
||||
Warnings:
|
||||
|
||||
- Added calendar cache and sync fields to SelectedCalendar table
|
||||
- Created CalendarCacheEvent table with CalendarCacheEventStatus enum
|
||||
|
||||
*/
|
||||
|
||||
CREATE TYPE "CalendarCacheEventStatus" AS ENUM ('confirmed', 'tentative', 'cancelled');
|
||||
|
||||
-- AlterTable
|
||||
ALTER TABLE "SelectedCalendar" ADD COLUMN "channelId" TEXT,
|
||||
ADD COLUMN "channelKind" TEXT,
|
||||
ADD COLUMN "channelResourceId" TEXT,
|
||||
ADD COLUMN "channelResourceUri" TEXT,
|
||||
ADD COLUMN "channelExpiration" TIMESTAMP(3),
|
||||
ADD COLUMN "syncSubscribedAt" TIMESTAMP(3),
|
||||
ADD COLUMN "syncToken" TEXT,
|
||||
ADD COLUMN "syncedAt" TIMESTAMP(3),
|
||||
ADD COLUMN "syncErrorAt" TIMESTAMP(3),
|
||||
ADD COLUMN "syncErrorCount" INTEGER DEFAULT 0;
|
||||
|
||||
CREATE TABLE "CalendarCacheEvent" (
|
||||
"id" TEXT NOT NULL,
|
||||
"selectedCalendarId" TEXT NOT NULL,
|
||||
"externalId" TEXT NOT NULL,
|
||||
"externalEtag" TEXT NOT NULL,
|
||||
"iCalUID" TEXT,
|
||||
"iCalSequence" INTEGER NOT NULL DEFAULT 0,
|
||||
"summary" TEXT,
|
||||
"description" TEXT,
|
||||
"location" TEXT,
|
||||
"start" TIMESTAMP(3) NOT NULL,
|
||||
"end" TIMESTAMP(3) NOT NULL,
|
||||
"isAllDay" BOOLEAN NOT NULL DEFAULT false,
|
||||
"timeZone" TEXT,
|
||||
"status" "CalendarCacheEventStatus" NOT NULL DEFAULT 'confirmed',
|
||||
"recurringEventId" TEXT,
|
||||
"originalStartTime" TIMESTAMP(3),
|
||||
"createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
|
||||
"updatedAt" TIMESTAMP(3) NOT NULL,
|
||||
"externalCreatedAt" TIMESTAMP(3),
|
||||
"externalUpdatedAt" TIMESTAMP(3),
|
||||
|
||||
CONSTRAINT "CalendarCacheEvent_pkey" PRIMARY KEY ("id")
|
||||
);
|
||||
|
||||
-- CreateIndex
|
||||
CREATE UNIQUE INDEX "CalendarCacheEvent_selectedCalendarId_externalId_key" ON "CalendarCacheEvent"("selectedCalendarId", "externalId");
|
||||
|
||||
-- CreateIndex
|
||||
CREATE INDEX "CalendarCacheEvent_start_end_status_idx" ON "CalendarCacheEvent"("start", "end", "status");
|
||||
|
||||
-- CreateIndex
|
||||
CREATE INDEX "CalendarCacheEvent_selectedCalendarId_iCalUID_idx" ON "CalendarCacheEvent"("selectedCalendarId", "iCalUID");
|
||||
|
||||
-- AddForeignKey
|
||||
ALTER TABLE "CalendarCacheEvent" ADD CONSTRAINT "CalendarCacheEvent_selectedCalendarId_fkey" FOREIGN KEY ("selectedCalendarId") REFERENCES "SelectedCalendar"("id") ON DELETE CASCADE ON UPDATE CASCADE;
|
||||
@@ -0,0 +1,8 @@
|
||||
-- FeatureFlags
|
||||
INSERT INTO "Feature" (slug, enabled, description, "type")
|
||||
VALUES ('calendar-subscription-sync', false, 'Enable calendar subscription synchronization', 'OPERATIONAL')
|
||||
ON CONFLICT (slug) DO NOTHING;
|
||||
|
||||
INSERT INTO "Feature" (slug, enabled, description, "type")
|
||||
VALUES ('calendar-subscription-cache', false, 'Enable calendar subscription cache', 'OPERATIONAL')
|
||||
ON CONFLICT (slug) DO NOTHING;
|
||||
@@ -904,21 +904,44 @@ model Availability {
|
||||
}
|
||||
|
||||
model SelectedCalendar {
|
||||
id String @id @default(uuid())
|
||||
user User @relation(fields: [userId], references: [id], onDelete: Cascade)
|
||||
userId Int
|
||||
integration String
|
||||
externalId String
|
||||
credential Credential? @relation(fields: [credentialId], references: [id], onDelete: Cascade)
|
||||
credentialId Int?
|
||||
createdAt DateTime? @default(now())
|
||||
updatedAt DateTime? @updatedAt
|
||||
id String @id @default(uuid())
|
||||
user User @relation(fields: [userId], references: [id], onDelete: Cascade)
|
||||
userId Int
|
||||
integration String
|
||||
externalId String
|
||||
credential Credential? @relation(fields: [credentialId], references: [id], onDelete: Cascade)
|
||||
credentialId Int?
|
||||
createdAt DateTime? @default(now())
|
||||
updatedAt DateTime? @updatedAt
|
||||
// Used to identify a watched calendar channel in Google Calendar
|
||||
googleChannelId String?
|
||||
googleChannelKind String?
|
||||
googleChannelResourceId String?
|
||||
// @deprecated use channelId instead
|
||||
googleChannelId String?
|
||||
|
||||
// @deprecated use channelKind instead
|
||||
googleChannelKind String?
|
||||
|
||||
// @deprecated use channelResourceId instead
|
||||
googleChannelResourceId String?
|
||||
|
||||
// @deprecated use channelResourceUri instead
|
||||
googleChannelResourceUri String?
|
||||
googleChannelExpiration String?
|
||||
|
||||
// @deprecated use channelExpoiration instead
|
||||
googleChannelExpiration String?
|
||||
|
||||
// Used to identify a watched calendar
|
||||
channelId String?
|
||||
channelKind String?
|
||||
channelResourceId String?
|
||||
channelResourceUri String?
|
||||
channelExpiration DateTime? @db.Timestamp(3)
|
||||
|
||||
// Used to calendar cache and sync
|
||||
syncSubscribedAt DateTime? @db.Timestamp(3)
|
||||
syncToken String?
|
||||
syncedAt DateTime?
|
||||
syncErrorAt DateTime?
|
||||
syncErrorCount Int? @default(0)
|
||||
|
||||
delegationCredential DelegationCredential? @relation(fields: [delegationCredentialId], references: [id], onDelete: Cascade)
|
||||
delegationCredentialId String?
|
||||
@@ -935,6 +958,8 @@ model SelectedCalendar {
|
||||
eventTypeId Int?
|
||||
eventType EventType? @relation(fields: [eventTypeId], references: [id])
|
||||
|
||||
calendarCacheEvents CalendarCacheEvent[]
|
||||
|
||||
// It could still allow multiple user-level(eventTypeId is null) selected calendars for same userId, integration, externalId because NULL is not equal to NULL
|
||||
// We currently ensure uniqueness by checking for the existence of the record before creating a new one
|
||||
// Think about introducing a generated unique key ${userId}_${integration}_${externalId}_${eventTypeId}
|
||||
@@ -2571,3 +2596,41 @@ model CalAiPhoneNumber {
|
||||
@@index([inboundAgentId])
|
||||
@@index([outboundAgentId])
|
||||
}
|
||||
|
||||
enum CalendarCacheEventStatus {
|
||||
confirmed @map("confirmed")
|
||||
tentative @map("tentative")
|
||||
cancelled @map("cancelled")
|
||||
}
|
||||
|
||||
model CalendarCacheEvent {
|
||||
id String @id @default(uuid())
|
||||
selectedCalendarId String
|
||||
externalId String
|
||||
externalEtag String
|
||||
iCalUID String?
|
||||
iCalSequence Int @default(0)
|
||||
|
||||
// Event details
|
||||
summary String?
|
||||
description String?
|
||||
location String?
|
||||
start DateTime
|
||||
end DateTime
|
||||
isAllDay Boolean @default(false)
|
||||
timeZone String?
|
||||
status CalendarCacheEventStatus @default(confirmed)
|
||||
recurringEventId String?
|
||||
originalStartTime DateTime?
|
||||
|
||||
createdAt DateTime @default(now())
|
||||
updatedAt DateTime @updatedAt
|
||||
externalCreatedAt DateTime?
|
||||
externalUpdatedAt DateTime?
|
||||
|
||||
selectedCalendar SelectedCalendar @relation(fields: [selectedCalendarId], references: [id], onDelete: Cascade)
|
||||
|
||||
@@unique([selectedCalendarId, externalId])
|
||||
@@index([start, end, status])
|
||||
@@index([selectedCalendarId, iCalUID])
|
||||
}
|
||||
Reference in new issue
Block a user