refactor: join teams with feature enabled directly in findNextSubscriptionBatch query (#27364)

* refactor: join teams with feature enabled directly in findNextSubscriptionBatch query

- Changed findNextSubscriptionBatch to accept featureId instead of teamIds
- Join TeamFeatures table directly in the query instead of separate queries
- Updated CalendarSubscriptionService.checkForNewSubscriptions to pass featureId
- Removed separate getTeamsWithFeatureEnabled call
- Updated tests to reflect the new query structure

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

* refactor: change featureId to featureIds array for flexibility

- Changed findNextSubscriptionBatch to accept featureIds array instead of single featureId
- Updated Prisma query to use 'in' operator for multiple feature IDs
- Updated CalendarSubscriptionService to pass array with single feature
- Updated all tests to use featureIds array

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 2026-01-28 17:52:00 -03:00
1 parent 844380e265
commit 16cb9a3126
5 files changed
+47 -116

No files matched your search

@@ -440,14 +440,10 @@ export class CalendarSubscriptionService {
*/
// biome-ignore lint/nursery/useExplicitType: return type is void
async 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 });
@@ -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",
@@ -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[]>;
@@ -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,
},
},
},
},
},
},
@@ -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,
},
},
},
},
},
},