Scheduling infrastructure for absolutely everyone.
refactor: join teams with feature enabled directly in findNextSubscriptionBatch query (#27364)
Volnei Munhoz16cb9a31262e
diff --git a/packages/features/calendar-subscription/lib/CalendarSubscriptionService.ts b/packages/features/calendar-subscription/lib/CalendarSubscriptionService.tsindex 3c74208401a..5bdc1167112 100644--- a/packages/features/calendar-subscription/lib/CalendarSubscriptionService.ts+++ b/packages/features/calendar-subscription/lib/CalendarSubscriptionService.ts@@ -440,14 +440,10 @@ export class CalendarSubscriptionService {*/// biome-ignore lint/nursery/useExplicitType: return type is voidasync checkForNewSubscriptions() {- const teamIds = await this.deps.featuresRepository.getTeamsWithFeatureEnabled(- CalendarSubscriptionService.CALENDAR_SUBSCRIPTION_CACHE_FEATURE- );-const rows = await this.deps.selectedCalendarRepository.findNextSubscriptionBatch({take: 100,integrations: this.deps.adapterFactory.getProviders(),- teamIds,+ featureIds: [CalendarSubscriptionService.CALENDAR_SUBSCRIPTION_CACHE_FEATURE],genericCalendarSuffixes: this.deps.adapterFactory.getGenericCalendarSuffixes(),});log.debug("checkForNewSubscriptions", { count: rows.length });diff --git a/packages/features/calendar-subscription/lib/__tests__/CalendarSubscriptionService.test.ts b/packages/features/calendar-subscription/lib/__tests__/CalendarSubscriptionService.test.tsindex b8ea2d559df..774d7daafb6 100644--- a/packages/features/calendar-subscription/lib/__tests__/CalendarSubscriptionService.test.ts+++ b/packages/features/calendar-subscription/lib/__tests__/CalendarSubscriptionService.test.ts@@ -422,13 +422,10 @@ describe("CalendarSubscriptionService", () => {await service.checkForNewSubscriptions();- expect(mockFeaturesRepository.getTeamsWithFeatureEnabled).toHaveBeenCalledWith(- "calendar-subscription-cache"- );expect(mockSelectedCalendarRepository.findNextSubscriptionBatch).toHaveBeenCalledWith({take: 100,integrations: ["google_calendar", "office365_calendar"],- teamIds: [1, 2, 3],+ featureIds: ["calendar-subscription-cache"],genericCalendarSuffixes: ["@group.v.calendar.google.com","@group.calendar.google.com",@@ -439,12 +436,10 @@ describe("CalendarSubscriptionService", () => {expect(subscribeSpy).toHaveBeenCalledWith(mockSelectedCalendar.id);});- test("should handle mixed cache scenario where some teams have cache enabled and some do not", async () => {+ test("should handle multiple calendars returned from batch", async () => {const calendarWithCache = { ...mockSelectedCalendar, id: "calendar-with-cache", userId: 1 };const calendarWithCache2 = { ...mockSelectedCalendar, id: "calendar-with-cache-2", userId: 2 };- mockFeaturesRepository.getTeamsWithFeatureEnabled.mockResolvedValue([10, 20]);-mockSelectedCalendarRepository.findNextSubscriptionBatch.mockResolvedValue([calendarWithCache,calendarWithCache2,@@ -454,13 +449,10 @@ describe("CalendarSubscriptionService", () => {await service.checkForNewSubscriptions();- expect(mockFeaturesRepository.getTeamsWithFeatureEnabled).toHaveBeenCalledWith(- "calendar-subscription-cache"- );expect(mockSelectedCalendarRepository.findNextSubscriptionBatch).toHaveBeenCalledWith({take: 100,integrations: ["google_calendar", "office365_calendar"],- teamIds: [10, 20],+ featureIds: ["calendar-subscription-cache"],genericCalendarSuffixes: ["@group.v.calendar.google.com","@group.calendar.google.com",@@ -473,58 +465,17 @@ describe("CalendarSubscriptionService", () => {expect(subscribeSpy).toHaveBeenCalledWith("calendar-with-cache-2");});- test("should only fetch calendars for teams with feature enabled, not entire organization hierarchy", async () => {- const teamId = 100;- const parentOrgId = 1;-- mockFeaturesRepository.getTeamsWithFeatureEnabled.mockResolvedValue([teamId]);-- const calendarForTeamMember = { ...mockSelectedCalendar, id: "team-member-calendar", userId: 5 };- mockSelectedCalendarRepository.findNextSubscriptionBatch.mockResolvedValue([calendarForTeamMember]);-- const subscribeSpy = vi.spyOn(service, "subscribe").mockResolvedValue(undefined);-- await service.checkForNewSubscriptions();-- expect(mockFeaturesRepository.getTeamsWithFeatureEnabled).toHaveBeenCalledWith(- "calendar-subscription-cache"- );- expect(mockSelectedCalendarRepository.findNextSubscriptionBatch).toHaveBeenCalledWith({- take: 100,- integrations: ["google_calendar", "office365_calendar"],- teamIds: [teamId],- genericCalendarSuffixes: [- "@group.v.calendar.google.com",- "@group.calendar.google.com",- "@import.calendar.google.com",- "@resource.calendar.google.com",- ],- });- expect(mockSelectedCalendarRepository.findNextSubscriptionBatch).not.toHaveBeenCalledWith(- expect.objectContaining({- teamIds: expect.arrayContaining([parentOrgId]),- })- );- expect(subscribeSpy).toHaveBeenCalledTimes(1);- expect(subscribeSpy).toHaveBeenCalledWith("team-member-calendar");- });-- test("should not process any calendars when no teams have the feature enabled", async () => {- mockFeaturesRepository.getTeamsWithFeatureEnabled.mockResolvedValue([]);-+ test("should not process any calendars when no calendars are returned", async () => {mockSelectedCalendarRepository.findNextSubscriptionBatch.mockResolvedValue([]);const subscribeSpy = vi.spyOn(service, "subscribe").mockResolvedValue(undefined);await service.checkForNewSubscriptions();- expect(mockFeaturesRepository.getTeamsWithFeatureEnabled).toHaveBeenCalledWith(- "calendar-subscription-cache"- );expect(mockSelectedCalendarRepository.findNextSubscriptionBatch).toHaveBeenCalledWith({take: 100,integrations: ["google_calendar", "office365_calendar"],- teamIds: [],+ featureIds: ["calendar-subscription-cache"],genericCalendarSuffixes: ["@group.v.calendar.google.com","@group.calendar.google.com",diff --git a/packages/features/selectedCalendar/repositories/SelectedCalendarRepository.interface.ts b/packages/features/selectedCalendar/repositories/SelectedCalendarRepository.interface.tsindex 1449073d862..9defbfae5c7 100644--- a/packages/features/selectedCalendar/repositories/SelectedCalendarRepository.interface.ts+++ b/packages/features/selectedCalendar/repositories/SelectedCalendarRepository.interface.ts@@ -20,19 +20,21 @@ export interface ISelectedCalendarRepository {* Will check if syncSubscribedAt is null or channelExpiration is greater than current date* Calendars with recent subscription errors (last 24h) are skipped* Calendars with 3+ subscription errors are skipped entirely+ * Joins with TeamFeatures to filter users belonging to teams with any of the specified features enabled** @param take the number of calendars to take+ * @param featureIds the feature IDs to filter teams by (users in teams with any of these features enabled)* @param integrations the list of integrations* @param genericCalendarSuffixes the list of generic calendar suffixes to exclude*/findNextSubscriptionBatch({take,- teamIds,+ featureIds,integrations,genericCalendarSuffixes,}: {take: number;- teamIds: number[];+ featureIds: string[];integrations: string[];genericCalendarSuffixes?: string[];}): Promise<SelectedCalendar[]>;diff --git a/packages/features/selectedCalendar/repositories/SelectedCalendarRepository.test.ts b/packages/features/selectedCalendar/repositories/SelectedCalendarRepository.test.tsindex 408707b441a..9bd49dc63d7 100644--- a/packages/features/selectedCalendar/repositories/SelectedCalendarRepository.test.ts+++ b/packages/features/selectedCalendar/repositories/SelectedCalendarRepository.test.ts@@ -117,7 +117,7 @@ describe("SelectedCalendarRepository", () => {const result = await repository.findNextSubscriptionBatch({take: 10,- teamIds: [1, 2, 3],+ featureIds: ["calendar-subscription-cache"],integrations: ["google_calendar", "office365_calendar"],});@@ -127,8 +127,15 @@ describe("SelectedCalendarRepository", () => {user: {teams: {some: {- teamId: { in: [1, 2, 3] },accepted: true,+ team: {+ features: {+ some: {+ featureId: { in: ["calendar-subscription-cache"] },+ enabled: true,+ },+ },+ },},},},@@ -163,7 +170,7 @@ describe("SelectedCalendarRepository", () => {const result = await repository.findNextSubscriptionBatch({take: 5,- teamIds: [10, 20],+ featureIds: ["calendar-subscription-cache"],integrations: [],});@@ -173,8 +180,15 @@ describe("SelectedCalendarRepository", () => {user: {teams: {some: {- teamId: { in: [10, 20] },accepted: true,+ team: {+ features: {+ some: {+ featureId: { in: ["calendar-subscription-cache"] },+ enabled: true,+ },+ },+ },},},},@@ -203,52 +217,6 @@ 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"] },- user: {- teams: {- some: {- teamId: { in: [] },- accepted: true,- },- },- },- AND: [- {- OR: [- { syncSubscribedAt: null },- { channelExpiration: null },- { channelExpiration: { lte: expect.any(Date) } },- ],- },- {- OR: [- { syncSubscribedErrorAt: null },- { syncSubscribedErrorAt: { lt: expect.any(Date) } },- ],- },- {- syncSubscribedErrorCount: { lt: 3 },- },- ],- },- take: 10,- });-- expect(result).toEqual(mockCalendars);- });-test("should filter out generic calendars when genericCalendarSuffixes is provided", async () => {const mockCalendars = [mockSelectedCalendar];vi.mocked(mockPrismaClient.selectedCalendar.findMany).mockResolvedValue(mockCalendars);@@ -257,7 +225,7 @@ describe("SelectedCalendarRepository", () => {const result = await repository.findNextSubscriptionBatch({take: 10,- teamIds: [1, 2],+ featureIds: ["calendar-subscription-cache"],integrations: ["google_calendar"],genericCalendarSuffixes: genericSuffixes,});@@ -268,8 +236,15 @@ describe("SelectedCalendarRepository", () => {user: {teams: {some: {- teamId: { in: [1, 2] },accepted: true,+ team: {+ features: {+ some: {+ featureId: { in: ["calendar-subscription-cache"] },+ enabled: true,+ },+ },+ },},},},diff --git a/packages/features/selectedCalendar/repositories/SelectedCalendarRepository.ts b/packages/features/selectedCalendar/repositories/SelectedCalendarRepository.tsindex fd5d2e92fac..057463bff67 100644--- a/packages/features/selectedCalendar/repositories/SelectedCalendarRepository.ts+++ b/packages/features/selectedCalendar/repositories/SelectedCalendarRepository.ts@@ -19,12 +19,12 @@ export class SelectedCalendarRepository implements ISelectedCalendarRepository {async findNextSubscriptionBatch({take,- teamIds,+ featureIds,integrations,genericCalendarSuffixes,}: {take: number;- teamIds: number[];+ featureIds: string[];integrations: string[];genericCalendarSuffixes?: string[];}) {@@ -65,8 +65,15 @@ export class SelectedCalendarRepository implements ISelectedCalendarRepository {user: {teams: {some: {- teamId: { in: teamIds },accepted: true,+ team: {+ features: {+ some: {+ featureId: { in: featureIds },+ enabled: true,+ },+ },+ },},},},