Scheduling infrastructure for absolutely everyone.
Use user profile timezone from DB as fallback for CalDAV all-day events (#8755)
Syed Ali Shahbaz8ffb5529e6c0
diff --git a/packages/lib/CalendarService.ts b/packages/lib/CalendarService.tsindex d4c0f620492..b2f66768331 100644--- a/packages/lib/CalendarService.ts+++ b/packages/lib/CalendarService.ts@@ -276,6 +276,38 @@ export default abstract class BaseCalendarService implements Calendar {}}+ /**+ * getUserTimezoneFromDB() retrieves the timezone of a user from the database.+ *+ * @param {number} id - The user's unique identifier.+ * @returns {Promise<string | undefined>} - A Promise that resolves to the user's timezone or "Europe/London" as a default value if the timezone is not found.+ */+ getUserTimezoneFromDB = async (id: number): Promise<string | undefined> => {+ const prisma = await import("@calcom/prisma").then((mod) => mod.default);+ const user = await prisma.user.findUnique({+ where: {+ id,+ },+ select: {+ timeZone: true,+ },+ });+ return user?.timeZone;+ };++ /**+ * getUserId() extracts the user ID from the first calendar in an array of IntegrationCalendars.+ *+ * @param {IntegrationCalendar[]} selectedCalendars - An array of IntegrationCalendars.+ * @returns {number | null} - The user ID associated with the first calendar in the array, or null if the array is empty or the user ID is not found.+ */+ getUserId = (selectedCalendars: IntegrationCalendar[]): number | null => {+ if (selectedCalendars.length === 0) {+ return null;+ }+ return selectedCalendars[0].userId || null;+ };+isValidFormat = (url: string): boolean => {const allowedExtensions = ["eml", "ics"];const urlExtension = getFileExtension(url);@@ -299,6 +331,10 @@ export default abstract class BaseCalendarService implements Calendar {dateTo,headers: this.headers,});++ const userId = this.getUserId(selectedCalendars);+ // we use the userId from selectedCalendars to fetch the user's timeZone from the database primarily for all-day events without any timezone information+ const userTimeZone = userId ? await this.getUserTimezoneFromDB(userId) : "Europe/London";const events: { start: string; end: string }[] = [];objects.forEach((object) => {if (object.data == null || JSON.stringify(object.data) == "{}") return;@@ -322,26 +358,31 @@ export default abstract class BaseCalendarService implements Calendar {const isUTC = timezone === "Z";const tzid: string | undefined = vevent?.getFirstPropertyValue("tzid") || isUTC ? "UTC" : timezone;// In case of icalendar, when only tzid is available without vtimezone, we need to add vtimezone explicitly to take care of timezone diff- if (!vcalendar.getFirstSubcomponent("vtimezone") && tzid) {- try {- const timezoneComp = new ICAL.Component("vtimezone");- timezoneComp.addPropertyWithValue("tzid", tzid);- const standard = new ICAL.Component("standard");-- // get timezone offset- const tzoffsetfrom = dayjs(event.startDate.toJSDate()).tz(tzid).format("Z");- const tzoffsetto = dayjs(event.endDate.toJSDate()).tz(tzid).format("Z");-- // set timezone offset- standard.addPropertyWithValue("tzoffsetfrom", tzoffsetfrom);- standard.addPropertyWithValue("tzoffsetto", tzoffsetto);- // provide a standard dtstart- standard.addPropertyWithValue("dtstart", "1601-01-01T00:00:00");- timezoneComp.addSubcomponent(standard);- vcalendar.addSubcomponent(timezoneComp);- } catch (e) {- // Adds try-catch to ensure the code proceeds when Apple Calendar provides non-standard TZIDs- console.log("error in adding vtimezone", e);+ if (!vcalendar.getFirstSubcomponent("vtimezone")) {+ const timezoneToUse = tzid || userTimeZone;+ if (timezoneToUse) {+ try {+ const timezoneComp = new ICAL.Component("vtimezone");+ timezoneComp.addPropertyWithValue("tzid", timezoneToUse);+ const standard = new ICAL.Component("standard");++ // get timezone offset+ const tzoffsetfrom = dayjs(event.startDate.toJSDate()).tz(timezoneToUse).format("Z");+ const tzoffsetto = dayjs(event.endDate.toJSDate()).tz(timezoneToUse).format("Z");++ // set timezone offset+ standard.addPropertyWithValue("tzoffsetfrom", tzoffsetfrom);+ standard.addPropertyWithValue("tzoffsetto", tzoffsetto);+ // provide a standard dtstart+ standard.addPropertyWithValue("dtstart", "1601-01-01T00:00:00");+ timezoneComp.addSubcomponent(standard);+ vcalendar.addSubcomponent(timezoneComp);+ } catch (e) {+ // Adds try-catch to ensure the code proceeds when Apple Calendar provides non-standard TZIDs+ console.log("error in adding vtimezone", e);+ }+ } else {+ console.error("No timezone found");}}const vtimezone = vcalendar.getFirstSubcomponent("vtimezone");