Files
cal.diy/packages/lib/server/repository/selectedCalendar.ts
T
Omar LópezandUdit Takkar d294a74aad feat: Populate gCal calendar cache via webhooks (#11928)
* WIP

* WIP

* WIP

* WIP

* WIP

* WIP

* WIP

* WIP

* WIP

* Update CalendarService.ts

* Type fixes

* WIP

* fix: improve cache hits

* Update CalendarService.ts

* Update CalendarService.ts

* Update CalendarService.ts

* Update CalendarService.ts

* Update CalendarService.ts

* Update CalendarService.ts

* Feedback

* Update CalendarService.ts

* Update CalendarService.ts

* Update _router.ts

* feedback

* WIP

* WIP

* Update schema.prisma

* feedback

* typefixes

* Update Calendar.d.ts

* fix: watches when adding a calendar

* Discard changes to packages/app-store/googlecalendar/api/add.ts

* WIP

Signed-off-by: Omar López <[email protected]>

* WIP

Signed-off-by: Omar López <[email protected]>

* WP

Signed-off-by: Omar López <[email protected]>

* Update calendar.ts

* Update calendar.ts

* Update callback.ts

* Update callback.ts

* Conflicts

* WIP

* WIP

* Update CalendarService.ts

* Cleanup

* Discard changes to packages/features/settings/layouts/SettingsLayout.tsx

* Update calendar-cache.repository.ts

* WIP

* Update getSelectedCalendarsToWatch.sql

* WIP

* Update CalendarService.ts

* Cleanup

* Discard changes to packages/app-store/googlecalendar/lib/CalendarService.ts

* Create CalendarService.wip.ts

* WIP

* Update CalendarService.ts

* Update getSelectedCalendarsToWatch.sql

* Delete CalendarService.wip.ts

* test updates

* cleanup

Signed-off-by: Omar López <[email protected]>

* Update CalendarService.test.ts

* Update CalendarService.ts

* type fixes

* Update OAuthManager.ts

* Update CalendarService.ts

* Almost there

* Update CalendarService.test.ts

* Update calendar.ts

* Update callback.ts

* Update toggleFeatureFlag.handler.ts

* fix: feedback

* Update getSelectedCalendarsToWatch.sql

* Fix unit tests

---------

Signed-off-by: Omar López <[email protected]>
Co-authored-by: Udit Takkar <[email protected]>
2024-11-15 14:59:37 -03:00

126 lines
3.5 KiB
TypeScript

import type { Prisma } from "@prisma/client";
import { prisma } from "@calcom/prisma";
import { credentialForCalendarServiceSelect } from "@calcom/prisma/selects/credential";
type SelectedCalendarCreateInput = {
credentialId: number;
userId: number;
externalId: string;
integration: string;
};
export class SelectedCalendarRepository {
static async create(data: SelectedCalendarCreateInput) {
return await prisma.selectedCalendar.create({
data: {
...data,
},
});
}
static async upsert(data: Prisma.SelectedCalendarUncheckedCreateInput) {
return await prisma.selectedCalendar.upsert({
where: {
userId_integration_externalId: {
userId: data.userId,
integration: data.integration,
externalId: data.externalId,
},
},
create: { ...data },
update: { ...data },
});
}
/** Retrieve calendars that need to be watched */
static async getNextBatchToWatch(limit = 100) {
// Get selected calendars from users that belong to a team that has calendar cache enabled
const oneDayInMS = 24 * 60 * 60 * 1000;
const tomorrowTimestamp = String(new Date().getTime() + oneDayInMS);
const nextBatch = await prisma.selectedCalendar.findMany({
take: limit,
where: {
user: {
teams: {
some: {
team: {
features: {
some: {
featureId: "calendar-cache",
},
},
},
},
},
},
// RN we only support google calendar subscriptions for now
integration: "google_calendar",
OR: [
// Either is a calendar pending to be watched
{ googleChannelExpiration: null },
// Or is a calendar that is about to expire
{ googleChannelExpiration: { lt: tomorrowTimestamp } },
],
},
});
return nextBatch;
}
/** Retrieve calendars that are being watched but shouldn't be anymore */
static async getNextBatchToUnwatch(limit = 100) {
const nextBatch = await prisma.selectedCalendar.findMany({
take: limit,
where: {
user: {
teams: {
every: {
team: {
features: {
none: {
featureId: "calendar-cache",
},
},
},
},
},
},
// RN we only support google calendar subscriptions for now
integration: "google_calendar",
googleChannelExpiration: { not: null },
},
});
return nextBatch;
}
static async delete(data: Prisma.SelectedCalendarUncheckedCreateInput) {
return await prisma.selectedCalendar.delete({
where: {
userId_integration_externalId: {
userId: data.userId,
externalId: data.externalId,
integration: data.integration,
},
},
});
}
static async findMany(args: Prisma.SelectedCalendarFindManyArgs) {
return await prisma.selectedCalendar.findMany(args);
}
static async findByGoogleChannelId(googleChannelId: string) {
return await prisma.selectedCalendar.findUnique({
where: {
googleChannelId,
},
select: {
credential: {
select: {
...credentialForCalendarServiceSelect,
selectedCalendars: {
orderBy: {
externalId: "asc",
},
},
},
},
},
});
}
}