* refactor: replace shouldServeCache with mode parameter for calendar cache control Replace the boolean shouldServeCache parameter with a new CalendarFetchMode type that can have values 'slots', 'overlay', and 'booking'. This provides better control over when to serve cache vs relay on calendar providers. - 'slots' mode: Check feature flags and use cache when available (for getting actual calendar availability) - 'overlay' mode: Don't use cache (for overlay calendar availability) - 'booking' mode: Don't use cache (for booking confirmation) - undefined: Same as 'slots' for backwards compatibility The cache decision logic is now centralized in getCalendar.ts based on the mode parameter. Co-Authored-By: Volnei Munhoz <[email protected]> * fix: update CalendarService.test.ts to use mode parameter Co-Authored-By: Volnei Munhoz <[email protected]> * refactor: use shared GetAvailabilityParams type across all calendar services - Import GetAvailabilityParams and GetAvailabilityWithTimeZonesParams from @calcom/types/Calendar - Replace inline type definitions with shared types in all calendar service implementations - Update BaseCalendarService, CalendarCacheWrapper, and CalendarTelemetryWrapper - Ensures consistent typing and follows DRY principles Co-Authored-By: Volnei Munhoz <[email protected]> * fix: add missing IntegrationCalendar import and update mock getAvailability signature - Add IntegrationCalendar back to sendgrid CalendarService imports - Update bookingScenario mock getAvailability to use typed params object - Add listCalendars method to mock Calendar object Co-Authored-By: Volnei Munhoz <[email protected]> * fix: update test files to use typed params object for getAvailability methods - Update CalendarCacheWrapper.test.ts to call getAvailability/getAvailabilityWithTimeZones with params object - Update getCalendarsEvents.test.ts toHaveBeenCalledWith assertions to expect params object - All 32 tests now pass Co-Authored-By: Volnei Munhoz <[email protected]> * refactor: consolidate to single GetAvailabilityParams type for both getAvailability methods - Remove GetAvailabilityWithTimeZonesParams, use GetAvailabilityParams for both methods - Add mode parameter to getAvailabilityWithTimeZones calls - Update wrapper classes to pass mode through to underlying calendar - Update test files to include mode in getAvailabilityWithTimeZones calls and assertions Co-Authored-By: Volnei Munhoz <[email protected]> * refactor: use EventBusyDate with optional timeZone for getAvailabilityWithTimeZones - Add optional timeZone field to EventBusyDate type - Update getAvailabilityWithTimeZones return type to use EventBusyDate[] - Update Google Calendar service and wrapper classes to use the new type Co-Authored-By: Volnei Munhoz <[email protected]> * fix: type errors and remove unused calendar watching methods - Fix type errors in CalendarCacheWrapper.ts (convert null to undefined for timeZone) - Fix type errors in getCalendarsEvents.ts (ensure timeZone is always present) - Remove unused watchCalendar/unwatchCalendar from Calendar interface - Remove unused startWatchingCalendarsInGoogle/stopWatchingCalendarsInGoogle from GoogleCalendarService - Remove unused imports (uuid, uniqueBy, GOOGLE_WEBHOOK_URL, ONE_MONTH_IN_MS) Co-Authored-By: Volnei Munhoz <[email protected]> * fix: remove watchCalendar/unwatchCalendar from CalendarTelemetryWrapper Co-Authored-By: Volnei Munhoz <[email protected]> * refactor: remove verbose JSDoc param comments from wrapper classes Co-Authored-By: Volnei Munhoz <[email protected]> * refactor: make mode parameter required in getCalendar Co-Authored-By: Volnei Munhoz <[email protected]> * fix: add required mode parameter to all getCalendar callers Co-Authored-By: Volnei Munhoz <[email protected]> * fix: provide default mode value in getBusyCalendarTimes Co-Authored-By: Volnei Munhoz <[email protected]> * fix: add mode parameter to remaining getCalendar callers Co-Authored-By: Volnei Munhoz <[email protected]> * fix: add mode parameter to vital and wipemycalother reschedule Co-Authored-By: Volnei Munhoz <[email protected]> * fix: add mode parameter to credential-sync API endpoint Co-Authored-By: Volnei Munhoz <[email protected]> * refactor: add 'none' mode to CalendarFetchMode and use as default Co-Authored-By: Volnei Munhoz <[email protected]> * test: add mode parameter to getCalendarsEvents test calls Co-Authored-By: Volnei Munhoz <[email protected]> * Update packages/app-store/delegationCredential.ts Co-authored-by: cubic-dev-ai[bot] <191113872+cubic-dev-ai[bot]@users.noreply.github.com> --------- Co-authored-by: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com> Co-authored-by: cubic-dev-ai[bot] <191113872+cubic-dev-ai[bot]@users.noreply.github.com>
123 lines
3.3 KiB
TypeScript
123 lines
3.3 KiB
TypeScript
import { enrichUserWithDelegationCredentialsIncludeServiceAccountKey } from "@calcom/app-store/delegationCredential";
|
|
import dayjs from "@calcom/dayjs";
|
|
import { getBusyCalendarTimes } from "@calcom/features/calendars/lib/CalendarManager";
|
|
import { prisma } from "@calcom/prisma";
|
|
import type { EventBusyDate } from "@calcom/types/Calendar";
|
|
|
|
import { TRPCError } from "@trpc/server";
|
|
|
|
import type { TrpcSessionUser } from "../../../types";
|
|
import type { TCalendarOverlayInputSchema } from "./calendarOverlay.schema";
|
|
|
|
type ListOptions = {
|
|
ctx: {
|
|
user: NonNullable<TrpcSessionUser>;
|
|
};
|
|
input: TCalendarOverlayInputSchema;
|
|
};
|
|
|
|
export const calendarOverlayHandler = async ({ ctx, input }: ListOptions) => {
|
|
const { user } = ctx;
|
|
const { calendarsToLoad, dateFrom, dateTo } = input;
|
|
|
|
if (!dateFrom || !dateTo) {
|
|
return [] as EventBusyDate[];
|
|
}
|
|
|
|
// get all unique credentialIds from calendarsToLoad
|
|
const uniqueCredentialIds = Array.from(new Set(calendarsToLoad.map((item) => item.credentialId)));
|
|
|
|
// To call getCalendar we need
|
|
|
|
// Ensure that the user has access to all of the credentialIds
|
|
const nonDelegationCredentials = await prisma.credential.findMany({
|
|
where: {
|
|
id: {
|
|
in: uniqueCredentialIds,
|
|
},
|
|
userId: user.id,
|
|
},
|
|
select: {
|
|
id: true,
|
|
type: true,
|
|
key: true,
|
|
userId: true,
|
|
teamId: true,
|
|
appId: true,
|
|
invalid: true,
|
|
delegationCredentialId: true,
|
|
user: {
|
|
select: {
|
|
email: true,
|
|
},
|
|
},
|
|
},
|
|
});
|
|
|
|
const { credentials } = await enrichUserWithDelegationCredentialsIncludeServiceAccountKey({
|
|
user: {
|
|
...user,
|
|
credentials: nonDelegationCredentials,
|
|
},
|
|
});
|
|
|
|
if (credentials.length !== uniqueCredentialIds.length) {
|
|
throw new TRPCError({
|
|
code: "UNAUTHORIZED",
|
|
message: "Unauthorized - These credentials do not belong to you",
|
|
});
|
|
}
|
|
|
|
const composedSelectedCalendars = calendarsToLoad.map((calendar) => {
|
|
const credential = credentials.find((item) => item.id === calendar.credentialId);
|
|
if (!credential) {
|
|
throw new TRPCError({
|
|
code: "UNAUTHORIZED",
|
|
message: "Unauthorized - These credentials do not belong to you",
|
|
});
|
|
}
|
|
return {
|
|
...calendar,
|
|
userId: user.id,
|
|
integration: credential.type,
|
|
};
|
|
});
|
|
|
|
// get all calendar services
|
|
// Use "overlay" mode to bypass cache for overlay calendar availability
|
|
const calendarBusyTimesQuery = await getBusyCalendarTimes(
|
|
credentials,
|
|
dateFrom,
|
|
dateTo,
|
|
composedSelectedCalendars,
|
|
"overlay"
|
|
);
|
|
|
|
if (!calendarBusyTimesQuery.success) {
|
|
throw new TRPCError({
|
|
code: "INTERNAL_SERVER_ERROR",
|
|
message: "Failed to fetch busy calendar times",
|
|
});
|
|
}
|
|
|
|
const calendarBusyTimes = calendarBusyTimesQuery.data;
|
|
|
|
// Convert to users timezone
|
|
|
|
const userTimeZone = input.loggedInUsersTz;
|
|
const calendarBusyTimesConverted = calendarBusyTimes.map((busyTime) => {
|
|
const busyTimeStart = dayjs(busyTime.start);
|
|
const busyTimeEnd = dayjs(busyTime.end);
|
|
const busyTimeStartDate = busyTimeStart.tz(userTimeZone).toDate();
|
|
const busyTimeEndDate = busyTimeEnd.tz(userTimeZone).toDate();
|
|
|
|
return {
|
|
...busyTime,
|
|
start: busyTimeStartDate,
|
|
end: busyTimeEndDate,
|
|
} as EventBusyDate;
|
|
});
|
|
|
|
return calendarBusyTimesConverted;
|
|
};
|