Files
calendar/packages/features/calendar-subscription/lib/cache/CalendarCacheEventRepository.ts
T
Volnei MunhozGitHubDevin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>
1eb6ff3e65 chore: calendar cache optmize (#24533)
* wip

* fix: regenerate Prisma types and fix GoogleCalendarSubscriptionAdapter tests

- Run yarn prisma generate to regenerate Prisma client with missing types (WorkflowType enum)
- Fix addMonthsFromNow helper to properly return the calculated date
- Update GoogleCalendarSubscriptionAdapter to use startOf/endOf day for time ranges
- Update test expectations to use Date objects instead of dayjs objects
- Fix all-day event test to use future date instead of today
- All tests now passing (3599 passed)

Co-Authored-By: Volnei Munhoz <volnei.munhoz@gmail.com>

* fix tests

* parametrize months ahead

* improve names

* Improve based on reviews

---------

Co-authored-by: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>
2025-10-17 17:20:08 +00:00

91 lines
2.5 KiB
TypeScript

import type { ICalendarCacheEventRepository } from "@calcom/features/calendar-subscription/lib/cache/CalendarCacheEventRepository.interface";
import type { PrismaClient } from "@calcom/prisma";
import type { CalendarCacheEvent } from "@calcom/prisma/client";
export class CalendarCacheEventRepository implements ICalendarCacheEventRepository {
constructor(private prismaClient: PrismaClient) {}
async findAllBySelectedCalendarIdsBetween(
selectedCalendarId: string[],
start: Date,
end: Date
): Promise<Pick<CalendarCacheEvent, "start" | "end" | "timeZone">[]> {
return this.prismaClient.calendarCacheEvent.findMany({
where: {
selectedCalendarId: {
in: selectedCalendarId,
},
AND: [{ start: { lt: end } }, { end: { gt: start } }],
},
select: {
start: true,
end: true,
timeZone: true,
},
});
}
async upsertMany(events: CalendarCacheEvent[]) {
if (events.length === 0) {
return;
}
// lack of upsertMany in prisma
return Promise.allSettled(
events.map((event) => {
return this.prismaClient.calendarCacheEvent.upsert({
where: {
selectedCalendarId_externalId: {
externalId: event.externalId,
selectedCalendarId: event.selectedCalendarId,
},
},
update: {
start: event.start,
end: event.end,
summary: event.summary,
description: event.description,
location: event.location,
isAllDay: event.isAllDay,
timeZone: event.timeZone,
},
create: event,
});
})
);
}
async deleteMany(events: Pick<CalendarCacheEvent, "externalId" | "selectedCalendarId">[]) {
// Only delete events with externalId and selectedCalendarId
const conditions = events.filter((c) => c.externalId && c.selectedCalendarId);
if (conditions.length === 0) {
return;
}
return this.prismaClient.calendarCacheEvent.deleteMany({
where: {
OR: conditions,
},
});
}
async deleteAllBySelectedCalendarId(selectedCalendarId: string) {
if (!selectedCalendarId) {
return;
}
return this.prismaClient.calendarCacheEvent.deleteMany({
where: {
selectedCalendarId,
},
});
}
async deleteStale() {
return this.prismaClient.calendarCacheEvent.deleteMany({
where: {
end: { lte: new Date() },
},
});
}
}