From b4eff256360c07c532339526d47d1fced01d55ca Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Efra=C3=ADn=20Roch=C3=ADn?= Date: Fri, 2 Jun 2023 13:17:53 -0700 Subject: [PATCH] fix: Issues with calendar sync at the beginning and end of month (#9282) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * increases the time range to cover all time zones * remove console.log --------- Co-authored-by: Omar López --- .../pages/[user]/calendar-cache/[month].tsx | 19 ++++++++++--------- packages/core/CalendarManager.ts | 6 +++++- 2 files changed, 15 insertions(+), 10 deletions(-) diff --git a/apps/web/pages/[user]/calendar-cache/[month].tsx b/apps/web/pages/[user]/calendar-cache/[month].tsx index 9cd9359bb5..b8b4acb7d6 100644 --- a/apps/web/pages/[user]/calendar-cache/[month].tsx +++ b/apps/web/pages/[user]/calendar-cache/[month].tsx @@ -3,15 +3,13 @@ * caching system that NextJS uses SSG pages. * TODO: Redirect to user profile on browser */ -import type { GetStaticPaths, GetStaticProps } from "next"; +import type { GetStaticPaths, GetStaticProps, InferGetStaticPropsType } from "next"; import { z } from "zod"; import { getCachedResults } from "@calcom/core"; import dayjs from "@calcom/dayjs"; import prisma from "@calcom/prisma"; -const CalendarCache = () =>
; - const paramsSchema = z.object({ user: z.string(), month: z.string() }); export const getStaticProps: GetStaticProps< { results: Awaited> }, @@ -29,16 +27,16 @@ export const getStaticProps: GetStaticProps< selectedCalendars: true, }, }); - const startDate = ( - dayjs(month, "YYYY-MM").isSame(dayjs(), "month") ? dayjs.utc() : dayjs.utc(month, "YYYY-MM") - ).startOf("day"); - const endDate = startDate.endOf("month"); + // Subtract 11 hours from the start date to avoid problems in UTC- time zones. + const startDate = dayjs.utc(month, "YYYY-MM").startOf("day").subtract(11, "hours").format(); + // Add 14 hours from the start date to avoid problems in UTC+ time zones. + const endDate = dayjs.utc(month, "YYYY-MM").endOf("month").add(14, "hours").format(); try { const results = userWithCredentials?.credentials ? await getCachedResults( userWithCredentials?.credentials, - startDate.format(), - endDate.format(), + startDate, + endDate, userWithCredentials?.selectedCalendars ) : []; @@ -64,5 +62,8 @@ export const getStaticPaths: GetStaticPaths = () => { fallback: "blocking", }; }; +type Props = InferGetStaticPropsType; +const CalendarCache = (props: Props) => + process.env.NODE_ENV === "development" ?
{JSON.stringify(props, null, "  ")}
:
; export default CalendarCache; diff --git a/packages/core/CalendarManager.ts b/packages/core/CalendarManager.ts index 99bc55240e..592ff2cb5d 100644 --- a/packages/core/CalendarManager.ts +++ b/packages/core/CalendarManager.ts @@ -231,7 +231,11 @@ export const getBusyCalendarTimes = async ( const months = getMonths(dateFrom, dateTo); try { if (coldStart) { - results = await getCachedResults(withCredentials, dateFrom, dateTo, selectedCalendars); + // Subtract 11 hours from the start date to avoid problems in UTC- time zones. + const startDate = dayjs(dateFrom).subtract(11, "hours").format(); + // Add 14 hours from the start date to avoid problems in UTC+ time zones. + const endDate = dayjs(dateTo).endOf("month").add(14, "hours").format(); + results = await getCachedResults(withCredentials, startDate, endDate, selectedCalendars); logger.info("Generating calendar cache in background"); // on cold start the calendar cache page generated in the background Promise.all(months.map((month) => createCalendarCachePage(username, month)));