feat: api v2 getSchedules that are applied to team event type (#25912)

* chore: update teams schedules module

* chore: update schedules and team events repository

* chore: input for teams schedules

* feat: add logic to filter schedules based on event type id

* fix: bad import

* chore: add input for get user schedules

* feat: add logic in user schedule to filter schedules based on event type id

* chore: update e2e tests

* fix: add EventTypesModule_2024_06_14 import to SchedulesModule_2024_06_11

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

* fix: import path

* chore: implement cubic feedback

* fix: replace .finally() with try/finally in E2E tests

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

* chore: implement PR feedback

* fix: correct schedule precedence order for team hosts

Fix the effective schedule resolution to follow the intended precedence:
host schedule > user default schedule > event type schedule

This addresses Cubic AI review feedback with confidence 9/10:
- schedules.service.ts: Fix precedence in getUserSchedulesForEventType
- teams-schedules.service.ts: Fix precedence in getTeamSchedulesForEventType

Co-Authored-By: unknown <>

* fixup

---------

Co-authored-by: Keith Williams <[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-20 17:55:38 +05:30
1 parent 2c105aceb8
commit 14e2fa6893
9 files changed
+478 -38

No files matched your search

@@ -157,4 +157,28 @@ export class TeamsEventTypesRepository {
},
});
}
async getByIdIncludeHostsAndUserDefaultSchedule(eventTypeId: number, teamId: number) {
return this.dbRead.prisma.eventType.findUnique({
where: {
id: eventTypeId,
teamId,
},
select: {
id: true,
scheduleId: true,
hosts: {
select: {
scheduleId: true,
userId: true,
user: {
select: {
defaultScheduleId: true,
},
},
},
},
},
});
}
}
@@ -1,20 +1,73 @@
import type { ScheduleOutput_2024_06_11 } from "@calcom/platform-types";
import type { Availability, Schedule } from "@calcom/prisma/client";
import { Injectable, NotFoundException } from "@nestjs/common";
import { SchedulesRepository_2024_06_11 } from "@/ee/schedules/schedules_2024_06_11/schedules.repository";
import { OutputSchedulesService_2024_06_11 } from "@/ee/schedules/schedules_2024_06_11/services/output-schedules.service";
import { TeamsEventTypesRepository } from "@/modules/teams/event-types/teams-event-types.repository";
import { TeamsRepository } from "@/modules/teams/teams/teams.repository";
import { Injectable } from "@nestjs/common";
@Injectable()
export class TeamsSchedulesService {
constructor(
private readonly teamsRepository: TeamsRepository,
private readonly schedulesRepository: SchedulesRepository_2024_06_11,
private readonly outputSchedulesService: OutputSchedulesService_2024_06_11
private readonly outputSchedulesService: OutputSchedulesService_2024_06_11,
private readonly teamsEventTypesRepository: TeamsEventTypesRepository
) {}
async getTeamSchedules(teamId: number, skip = 0, take = 250) {
const userIds = await this.teamsRepository.getTeamUsersIds(teamId);
const schedules = await this.schedulesRepository.getSchedulesByUserIds(userIds, skip, take);
async getTeamSchedules(
teamId: number,
skip = 0,
take = 250,
eventTypeId?: number
): Promise<ScheduleOutput_2024_06_11[]> {
if (!eventTypeId) {
const userIds = await this.teamsRepository.getTeamUsersIds(teamId);
const schedules = await this.schedulesRepository.getSchedulesByUserIds(userIds, skip, take);
return this.outputSchedulesService.getResponseSchedules(schedules);
return this.outputSchedulesService.getResponseSchedules(schedules);
}
return this.getTeamSchedulesForEventType(teamId, eventTypeId, skip, take);
}
private async getTeamSchedulesForEventType(
teamId: number,
eventTypeId: number,
skip: number,
take: number
): Promise<ScheduleOutput_2024_06_11[]> {
const eventType = await this.teamsEventTypesRepository.getByIdIncludeHostsAndUserDefaultSchedule(
eventTypeId,
teamId
);
if (!eventType) {
throw new NotFoundException(`Event type with id ${eventTypeId} not found in team ${teamId}`);
}
const effectiveScheduleIds = new Set<number>();
for (const host of eventType.hosts) {
const effectiveScheduleId = eventType.scheduleId ?? host.scheduleId ?? host.user.defaultScheduleId;
if (effectiveScheduleId) {
effectiveScheduleIds.add(effectiveScheduleId);
}
}
if (effectiveScheduleIds.size === 0) {
return [];
}
const schedules = await this.schedulesRepository.getSchedulesByIds(
Array.from(effectiveScheduleIds),
skip,
take
);
return this.outputSchedulesService.getResponseSchedules(
schedules as (Schedule & { availability: Availability[] })[]
);
}
}
@@ -8,6 +8,7 @@ import { OrganizationSchedulesRepository } from "@/modules/organizations/schedul
import { OrganizationsTeamsRepository } from "@/modules/organizations/teams/index/organizations-teams.repository";
import { PrismaModule } from "@/modules/prisma/prisma.module";
import { RedisModule } from "@/modules/redis/redis.module";
import { TeamsEventTypesModule } from "@/modules/teams/event-types/teams-event-types.module";
import { StripeService } from "@/modules/stripe/stripe.service";
import { TeamsMembershipsService } from "@/modules/teams/memberships/services/teams-memberships.service";
import { TeamsMembershipsRepository } from "@/modules/teams/memberships/teams-memberships.repository";
@@ -20,7 +21,7 @@ import { UsersRepository } from "@/modules/users/users.repository";
import { Module } from "@nestjs/common";
@Module({
imports: [PrismaModule, MembershipsModule, RedisModule],
imports: [PrismaModule, MembershipsModule, RedisModule, TeamsEventTypesModule],
providers: [
TeamsRepository,
TeamsService,