fix: Duplicate calls syncing Calendar integrations (#26858)

* fix: Duplicate calls syncing Calendar integrations

* fix: TS error in handler

* Minor fixup

* Set cacheUpdatedAt at correct depth of array

* Import RouterOutputs
This commit is contained in:
Alex van Andel authored and GitHub committed 2026-01-15 00:21:40 -03:00
1 parent a708e088c9
commit 1dd2652080
8 files changed
+164 -133

No files matched your search

@@ -1,6 +1,3 @@
import * as Sentry from "@sentry/nextjs";
import { z } from "zod";
import { getCalendar } from "@calcom/app-store/_utils/getCalendar";
import type { Dayjs } from "@calcom/dayjs";
import dayjs from "@calcom/dayjs";
@@ -10,16 +7,14 @@ import {
getBusyTimesFromTeamLimits,
} from "@calcom/features/busyTimes/lib/getBusyTimesFromLimits";
import { getBusyTimesService } from "@calcom/features/di/containers/BusyTimes";
import { EventTypeRepository } from "@calcom/features/eventtypes/repositories/eventTypeRepository";
import type { EventTypeRepository } from "@calcom/features/eventtypes/repositories/eventTypeRepository";
import type { PrismaHolidayRepository } from "@calcom/features/holidays/repositories/PrismaHolidayRepository";
import type { PrismaOOORepository } from "@calcom/features/ooo/repositories/PrismaOOORepository";
import type { IRedisService } from "@calcom/features/redis/IRedisService";
import type { WorkingHours as WorkingHoursWithUserId } from "@calcom/types/schedule";
import type { DateOverride, WorkingHours } from "@calcom/features/schedules/lib/date-ranges";
import { buildDateRanges, subtract } from "@calcom/features/schedules/lib/date-ranges";
import { getWorkingHours } from "@calcom/lib/availability";
import { stringToDayjsZod } from "@calcom/lib/dayjs";
import { detectEventTypeScheduleForUser } from "./detectEventTypeScheduleForUser";
import { ErrorCode } from "@calcom/lib/errorCodes";
import { getHolidayService } from "@calcom/lib/holidays";
import { getHolidayEmoji } from "@calcom/lib/holidays/getHolidayEmoji";
@@ -31,20 +26,23 @@ import logger from "@calcom/lib/logger";
import { safeStringify } from "@calcom/lib/safeStringify";
import { withReporting } from "@calcom/lib/sentryWrapper";
import type {
Availability,
Booking,
User,
OutOfOfficeEntry,
OutOfOfficeReason,
EventType as PrismaEventType,
Availability,
SelectedCalendar,
TravelSchedule,
User,
} from "@calcom/prisma/client";
import { SchedulingType } from "@calcom/prisma/enums";
import { EventTypeMetaDataSchema } from "@calcom/prisma/zod-utils";
import type { CalendarFetchMode, EventBusyDetails, IntervalLimitUnit } from "@calcom/types/Calendar";
import type { TimeRange } from "@calcom/types/schedule";
import type { CredentialForCalendarService } from "@calcom/types/Credential";
import type { TimeRange, WorkingHours as WorkingHoursWithUserId } from "@calcom/types/schedule";
import * as Sentry from "@sentry/nextjs";
import { z } from "zod";
import { detectEventTypeScheduleForUser } from "./detectEventTypeScheduleForUser";
const log = logger.getSubLogger({ prefix: ["getUserAvailability"] });
@@ -407,7 +405,7 @@ export class UserAvailabilityService {
const getBusyTimesEnd = dateTo.toISOString();
const selectedCalendars = eventType?.useEventLevelSelectedCalendars
? EventTypeRepository.getSelectedCalendarsFromUser({ user, eventTypeId: eventType.id })
? user.allSelectedCalendars.filter((calendar) => calendar.eventTypeId === eventType.id)
: user.userLevelSelectedCalendars;
let calendarTimezone: string | null = null;
@@ -4,7 +4,6 @@ import {
getConnectedCalendars,
} from "@calcom/features/calendars/lib/CalendarManager";
import { DestinationCalendarRepository } from "@calcom/features/calendars/repositories/DestinationCalendarRepository";
import { EventTypeRepository } from "@calcom/features/eventtypes/repositories/eventTypeRepository";
import { isDelegationCredential } from "@calcom/lib/delegationCredential";
import logger from "@calcom/lib/logger";
import { SelectedCalendarRepository } from "@calcom/lib/server/repository/selectedCalendar";
@@ -44,7 +43,7 @@ const _ensureNoConflictingNonDelegatedConnectedCalendar = <
integration: { slug: string };
primary?: { email?: string | null | undefined } | undefined;
delegationCredentialId?: string | null | undefined;
}
},
>({
connectedCalendars,
loggedInUser,
@@ -216,7 +215,7 @@ function findMatchingCalendar({
calendar: DestinationCalendar;
}) {
// Check if destinationCalendar exists in connectedCalendars
const allCals = connectedCalendars.map((cal) => cal.calendars ?? []).flat();
const allCals = connectedCalendars.flatMap((cal) => cal.calendars ?? []);
const matchingCalendar = allCals.find(
(cal) => cal.externalId === calendar.externalId && cal.integration === calendar.integration
);
@@ -255,14 +254,10 @@ function getSelectedCalendars({
}: {
user: UserWithCalendars;
eventTypeId: number | null;
}) {
}): Pick<SelectedCalendar, "externalId" | "integration" | "eventTypeId" | "updatedAt" | "googleChannelId">[] {
if (eventTypeId) {
return EventTypeRepository.getSelectedCalendarsFromUser({
user,
eventTypeId: eventTypeId ?? null,
});
return user.allSelectedCalendars.filter((calendar) => calendar.eventTypeId === eventTypeId);
}
return user.userLevelSelectedCalendars;
}
@@ -1408,16 +1408,6 @@ export class EventTypeRepository {
};
}
static getSelectedCalendarsFromUser<TSelectedCalendar extends { eventTypeId: number | null }>({
user,
eventTypeId,
}: {
user: UserWithSelectedCalendars<TSelectedCalendar>;
eventTypeId: number;
}) {
return user.allSelectedCalendars.filter((calendar) => calendar.eventTypeId === eventTypeId);
}
async findByIdForUserAvailability({ id }: { id: number }) {
const eventType = await this.prismaClient.eventType.findUnique({
where: { id },