Skip to content

Scheduling infrastructure for absolutely everyone.

fix: Issues with calendar sync at the beginning and end of month (#9282)

Efraín Rochínb4eff256360c
diff --git a/apps/web/pages/[user]/calendar-cache/[month].tsx b/apps/web/pages/[user]/calendar-cache/[month].tsx
index 9cd9359bb52..b8b4acb7d6e 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 = () => <div />;
-
const paramsSchema = z.object({ user: z.string(), month: z.string() });
export const getStaticProps: GetStaticProps<
{ results: Awaited<ReturnType<typeof getCachedResults>> },
@@ -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<typeof getStaticProps>;
+const CalendarCache = (props: Props) =>
+ process.env.NODE_ENV === "development" ? <pre>{JSON.stringify(props, null, " ")}</pre> : <div />;
export default CalendarCache;
diff --git a/packages/core/CalendarManager.ts b/packages/core/CalendarManager.ts
index 99bc55240ea..592ff2cb5d0 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)));