Feature/parallel booking availability (#3087)
This commit is contained in:
1 parent
7599f2384e
commit
2d28ca61a4
21 files changed
+242
-121
No files matched your search
@@ -1,21 +1,18 @@
|
||||
import { Credential, SelectedCalendar } from "@prisma/client";
|
||||
import { createHash } from "crypto";
|
||||
import _ from "lodash";
|
||||
import cache from "memory-cache";
|
||||
|
||||
import { getCalendar } from "@calcom/app-store/_utils/getCalendar";
|
||||
import getApps from "@calcom/app-store/utils";
|
||||
import { getUid } from "@calcom/lib/CalEventParser";
|
||||
import { getErrorFromUnknown } from "@calcom/lib/errors";
|
||||
import logger from "@calcom/lib/logger";
|
||||
import notEmpty from "@calcom/lib/notEmpty";
|
||||
import type { CalendarEvent, EventBusyDate, NewCalendarEventType } from "@calcom/types/Calendar";
|
||||
import type { Event } from "@calcom/types/Event";
|
||||
import type { EventResult } from "@calcom/types/EventManager";
|
||||
|
||||
const log = logger.getChildLogger({ prefix: ["CalendarManager"] });
|
||||
|
||||
/** TODO: Remove once all references are updated to app-store */
|
||||
export { getCalendar };
|
||||
|
||||
export const getCalendarCredentials = (credentials: Array<Credential>, userId: number) => {
|
||||
const calendarCredentials = getApps(credentials)
|
||||
.filter((app) => app.type.endsWith("_calendar"))
|
||||
@@ -77,20 +74,51 @@ export const getConnectedCalendars = async (
|
||||
return connectedCalendars;
|
||||
};
|
||||
|
||||
const CACHING_TIME = 30_000; // 30 seconds
|
||||
|
||||
const getCachedResults = (
|
||||
withCredentials: Credential[],
|
||||
dateFrom: string,
|
||||
dateTo: string,
|
||||
selectedCalendars: SelectedCalendar[]
|
||||
) => {
|
||||
const calendarCredentials = withCredentials.filter((credential) => credential.type.endsWith("_calendar"));
|
||||
const calendars = calendarCredentials.map((credential) => getCalendar(credential));
|
||||
const results = calendars.map(async (c, i) => {
|
||||
/** Filter out nulls */
|
||||
if (!c) return [];
|
||||
/** We rely on the index so we can match credentials with calendars */
|
||||
const { id, type } = calendarCredentials[i];
|
||||
/** We just pass the calendars that matched the credential type,
|
||||
* TODO: Migrate credential type or appId
|
||||
*/
|
||||
const passedSelectedCalendars = selectedCalendars.filter((sc) => sc.integration === type);
|
||||
/** We extract external Ids so we don't cache too much */
|
||||
const selectedCalendarIds = passedSelectedCalendars.map((sc) => sc.externalId);
|
||||
/** We create a unque hash key based on the input data */
|
||||
const cacheKey = createHash("md5").update(JSON.stringify({ id, selectedCalendarIds })).digest("hex");
|
||||
/** Check if we already have cached data and return */
|
||||
const cachedAvailability = cache.get(cacheKey);
|
||||
if (cachedAvailability) return cachedAvailability;
|
||||
/** If we don't then we actually fetch external calendars (which can be very slow) */
|
||||
const availability = await c.getAvailability(dateFrom, dateTo, passedSelectedCalendars);
|
||||
/** We save the availability to a few seconds so recurrent calls are nearly instant */
|
||||
cache.put(cacheKey, availability, CACHING_TIME);
|
||||
return availability;
|
||||
});
|
||||
|
||||
return Promise.all(results);
|
||||
};
|
||||
|
||||
export const getBusyCalendarTimes = async (
|
||||
withCredentials: Credential[],
|
||||
dateFrom: string,
|
||||
dateTo: string,
|
||||
selectedCalendars: SelectedCalendar[]
|
||||
) => {
|
||||
const calendars = withCredentials
|
||||
.filter((credential) => credential.type.endsWith("_calendar"))
|
||||
.map((credential) => getCalendar(credential))
|
||||
.filter(notEmpty);
|
||||
|
||||
let results: EventBusyDate[][] = [];
|
||||
try {
|
||||
results = await Promise.all(calendars.map((c) => c.getAvailability(dateFrom, dateTo, selectedCalendars)));
|
||||
results = await getCachedResults(withCredentials, dateFrom, dateTo, selectedCalendars);
|
||||
} catch (error) {
|
||||
log.warn(error);
|
||||
}
|
||||
|
||||
@@ -34,19 +34,15 @@ export type DatePickerProps = {
|
||||
isLoading?: boolean;
|
||||
};
|
||||
|
||||
const Day = ({
|
||||
export const Day = ({
|
||||
date,
|
||||
active,
|
||||
...props
|
||||
}: JSX.IntrinsicElements["button"] & { active: boolean; date: Date }) => {
|
||||
return (
|
||||
<button
|
||||
style={props.disabled ? {} : {}}
|
||||
className={classNames(
|
||||
"absolute top-0 left-0 right-0 bottom-0 mx-auto w-full rounded-sm border border-transparent text-center",
|
||||
props.disabled
|
||||
? "text-bookinglighter cursor-default font-light"
|
||||
: "hover:border-brand font-medium dark:hover:border-white",
|
||||
"hover:border-brand disabled:text-bookinglighter absolute top-0 left-0 right-0 bottom-0 mx-auto w-full rounded-sm border border-transparent text-center font-medium disabled:cursor-default disabled:border-transparent disabled:font-light dark:hover:border-white disabled:dark:border-transparent",
|
||||
active
|
||||
? "bg-brand text-brandcontrast dark:bg-darkmodebrand dark:text-darkmodebrandcontrast"
|
||||
: !props.disabled
|
||||
@@ -68,9 +64,11 @@ const Days = ({
|
||||
includedDates = [],
|
||||
browsingDate,
|
||||
weekStart,
|
||||
DayComponent = Day,
|
||||
selected,
|
||||
...props
|
||||
}: Omit<DatePickerProps, "locale" | "className" | "weekStart"> & {
|
||||
DayComponent?: React.FC<React.ComponentProps<typeof Day>>;
|
||||
browsingDate: Date;
|
||||
weekStart: number;
|
||||
}) => {
|
||||
@@ -90,16 +88,11 @@ const Days = ({
|
||||
return (
|
||||
<>
|
||||
{days.map((day, idx) => (
|
||||
<div
|
||||
key={day === null ? `e-${idx}` : `day-${day}`}
|
||||
style={{
|
||||
paddingTop: "100%",
|
||||
}}
|
||||
className="relative w-full">
|
||||
<div key={day === null ? `e-${idx}` : `day-${day}`} className="relative w-full pt-[100%]">
|
||||
{day === null ? (
|
||||
<div key={`e-${idx}`} />
|
||||
) : (
|
||||
<Day
|
||||
<DayComponent
|
||||
date={day}
|
||||
onClick={() => props.onChange(day)}
|
||||
disabled={
|
||||
@@ -139,7 +132,7 @@ const DatePicker = ({
|
||||
onMonthChange,
|
||||
isLoading = false,
|
||||
...passThroughProps
|
||||
}: DatePickerProps) => {
|
||||
}: DatePickerProps & Partial<React.ComponentProps<typeof Days>>) => {
|
||||
const [month, setMonth] = useState(selected ? selected.getMonth() : new Date().getMonth());
|
||||
|
||||
const changeMonth = (newMonth: number) => {
|
||||
|
||||
Reference in new issue
Block a user