Files
calendar/packages/trpc/server/routers/viewer/availability/calendarOverlay.handler.ts
T
3c1297aa72 fix: calcom trpc sessio circle dep (#19893)
* fix trpc session circle dep

* fixes trpc session cirlce dep

* fix relative imports

* fix more imports to use types and not trpc

* fix exports

* Fixed types

---------

Co-authored-by: Keith Williams <keithwillcode@gmail.com>
2025-03-19 05:50:22 -03:00

111 lines
2.9 KiB
TypeScript

import dayjs from "@calcom/dayjs";
import { getBusyCalendarTimes } from "@calcom/lib/CalendarManager";
import { enrichUserWithDelegationCredentialsWithoutOrgId } from "@calcom/lib/delegationCredential/server";
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,
user: {
select: {
email: true,
},
},
},
});
const { credentials } = await enrichUserWithDelegationCredentialsWithoutOrgId({
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 clanedar services
const calendarBusyTimes = await getBusyCalendarTimes(
credentials,
dateFrom,
dateTo,
composedSelectedCalendars
);
// 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;
};