perf: Calendar Cache Improvements (#25502)

* wip

* Filter only selectedCalendars where ff is enabled

* test: fix and add tests for calendar subscription cache feature

Co-Authored-By: Volnei Munhoz <[email protected]>

* test: fix SelectedCalendarRepository tests for new teamIds parameter

Co-Authored-By: Volnei Munhoz <[email protected]>

---------

Co-authored-by: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>
This commit is contained in:
authored and GitHub committed 2025-12-02 08:11:52 -03:00
1 parent 3bf2a8fa6c
commit 2c8ff89fac
7 files changed
+158 -34

No files matched your search

@@ -24,9 +24,11 @@ export interface ISelectedCalendarRepository {
*/
findNextSubscriptionBatch({
take,
teamIds,
integrations,
}: {
take: number;
teamIds: number[];
integrations?: string[];
}): Promise<SelectedCalendar[]>;
@@ -15,26 +15,24 @@ export class SelectedCalendarRepository implements ISelectedCalendarRepository {
return this.prismaClient.selectedCalendar.findFirst({ where: { channelId } });
}
async findNextSubscriptionBatch({ take, integrations }: { take: number; integrations: string[] }) {
async findNextSubscriptionBatch({
take,
teamIds,
integrations,
}: {
take: number;
teamIds: number[];
integrations: string[];
}) {
return this.prismaClient.selectedCalendar.findMany({
where: {
integration: { in: integrations },
OR: [{ syncSubscribedAt: null }, { channelExpiration: { lte: new Date() } }],
// initially we will run subscription only for teams that have
// the feature flags enabled and it should be removed later
user: {
teams: {
some: {
team: {
features: {
some: {
OR: [
{ featureId: "calendar-subscription-cache" },
{ featureId: "calendar-subscription-sync" },
],
},
},
},
teamId: { in: teamIds },
accepted: true,
},
},
},
@@ -116,6 +116,7 @@ describe("SelectedCalendarRepository", () => {
const result = await repository.findNextSubscriptionBatch({
take: 10,
teamIds: [1, 2, 3],
integrations: ["google_calendar", "office365_calendar"],
});
@@ -126,16 +127,8 @@ describe("SelectedCalendarRepository", () => {
user: {
teams: {
some: {
team: {
features: {
some: {
OR: [
{ featureId: "calendar-subscription-cache" },
{ featureId: "calendar-subscription-sync" },
],
},
},
},
teamId: { in: [1, 2, 3] },
accepted: true,
},
},
},
@@ -152,6 +145,7 @@ describe("SelectedCalendarRepository", () => {
const result = await repository.findNextSubscriptionBatch({
take: 5,
teamIds: [10, 20],
integrations: [],
});
@@ -162,16 +156,8 @@ describe("SelectedCalendarRepository", () => {
user: {
teams: {
some: {
team: {
features: {
some: {
OR: [
{ featureId: "calendar-subscription-cache" },
{ featureId: "calendar-subscription-sync" },
],
},
},
},
teamId: { in: [10, 20] },
accepted: true,
},
},
},
@@ -181,6 +167,35 @@ describe("SelectedCalendarRepository", () => {
expect(result).toEqual(mockCalendars);
});
test("should handle empty teamIds array", async () => {
const mockCalendars: SelectedCalendar[] = [];
vi.mocked(mockPrismaClient.selectedCalendar.findMany).mockResolvedValue(mockCalendars);
const result = await repository.findNextSubscriptionBatch({
take: 10,
teamIds: [],
integrations: ["google_calendar"],
});
expect(mockPrismaClient.selectedCalendar.findMany).toHaveBeenCalledWith({
where: {
integration: { in: ["google_calendar"] },
OR: [{ syncSubscribedAt: null }, { channelExpiration: { lte: expect.any(Date) } }],
user: {
teams: {
some: {
teamId: { in: [] },
accepted: true,
},
},
},
},
take: 10,
});
expect(result).toEqual(mockCalendars);
});
});
describe("updateSyncStatus", () => {