Dynamic Links re-integrate with availability logic (#3687)
* -re init dynamic links * typing fix 001 * added missing div closing tag * added necessary DB column pull to satisfy Type errors * further type fixes * WIP * removed console logs * some revert * WIP * another approach * enabled dynamic links availability fetch * Added users to eventTypeObject for consistency * WIP: Moving user and changing map item name * Fix user list call * Removed explicit User type in map * modify default user attributes * adds explicit users to EventTypeObject in teams * Updated availability page * Updates Availability * Futher availability change * Remove explicit user type from slot router * more fixes * more fixes WIP * cleaning up more errors WIP * object assign used for typesafety * added check if dynamic booking is allowed by all users * cleaned up console logs * clean up * Improvement * resolving suggestions by alex * changes requested by Omar * Filter out empty usernames instead of accepting null Co-authored-by: Peer Richelsen <[email protected]> Co-authored-by: kodiakhq[bot] <49736102+kodiakhq[bot]@users.noreply.github.com> Co-authored-by: zomars <[email protected]>
This commit is contained in:
17 files changed
+197
-101
No files matched your search
@@ -47,7 +47,7 @@ export async function getBusyTimes(params: {
|
||||
logger.silly(`Busy Time from Cal Bookings ${JSON.stringify(busyTimes)}`);
|
||||
const endPrismaBookingGet = performance.now();
|
||||
logger.debug(`prisma booking get took ${endPrismaBookingGet - startPrismaBookingGet}ms`);
|
||||
if (credentials.length > 0) {
|
||||
if (credentials?.length > 0) {
|
||||
const calendarBusyTimes = await getBusyCalendarTimes(credentials, startTime, endTime, selectedCalendars);
|
||||
|
||||
busyTimes.push(...calendarBusyTimes); /*
|
||||
|
||||
@@ -146,6 +146,7 @@ export async function getUserAvailability(
|
||||
|
||||
const timeZone = timezone || schedule?.timeZone || eventType?.timeZone || currentUser.timeZone;
|
||||
const startGetWorkingHours = performance.now();
|
||||
|
||||
const workingHours = getWorkingHours(
|
||||
{ timeZone },
|
||||
schedule.availability ||
|
||||
|
||||
@@ -1,20 +1,9 @@
|
||||
import type { EventTypeCustomInput } from "@prisma/client";
|
||||
import { PeriodType, Prisma, SchedulingType, UserPlan } from "@prisma/client";
|
||||
|
||||
import { baseUserSelect } from "@calcom/prisma/selects";
|
||||
import { userSelect } from "@calcom/prisma/selects";
|
||||
|
||||
const userSelectData = Prisma.validator<Prisma.UserArgs>()({ select: baseUserSelect });
|
||||
type User = Prisma.UserGetPayload<typeof userSelectData>;
|
||||
|
||||
const availability = [
|
||||
{
|
||||
days: [1, 2, 3, 4, 5],
|
||||
startTime: new Date().getTime(),
|
||||
endTime: new Date().getTime(),
|
||||
date: new Date(),
|
||||
scheduleId: null,
|
||||
},
|
||||
];
|
||||
type User = Prisma.UserGetPayload<typeof userSelect>;
|
||||
|
||||
type UsernameSlugLinkProps = {
|
||||
users: {
|
||||
@@ -33,6 +22,31 @@ type UsernameSlugLinkProps = {
|
||||
slug: string;
|
||||
};
|
||||
|
||||
const user: User = {
|
||||
theme: null,
|
||||
credentials: [],
|
||||
username: "john.doe",
|
||||
timeZone: "",
|
||||
bufferTime: 0,
|
||||
availability: [],
|
||||
id: 0,
|
||||
startTime: 0,
|
||||
endTime: 0,
|
||||
selectedCalendars: [],
|
||||
schedules: [],
|
||||
defaultScheduleId: null,
|
||||
locale: "en",
|
||||
email: "[email protected]",
|
||||
name: "John doe",
|
||||
avatar: "",
|
||||
destinationCalendar: null,
|
||||
plan: UserPlan.PRO,
|
||||
hideBranding: true,
|
||||
brandColor: "#797979",
|
||||
darkBrandColor: "#efefef",
|
||||
allowDynamicBooking: true,
|
||||
};
|
||||
|
||||
const customInputs: EventTypeCustomInput[] = [];
|
||||
|
||||
const commons = {
|
||||
@@ -52,6 +66,8 @@ const commons = {
|
||||
schedule: null,
|
||||
timeZone: null,
|
||||
successRedirectUrl: "",
|
||||
teamId: null,
|
||||
scheduleId: null,
|
||||
availability: [],
|
||||
price: 0,
|
||||
currency: "usd",
|
||||
@@ -70,31 +86,7 @@ const commons = {
|
||||
hidden: false,
|
||||
userId: 0,
|
||||
workflows: [],
|
||||
users: [
|
||||
{
|
||||
id: 0,
|
||||
plan: UserPlan.PRO,
|
||||
email: "[email protected]",
|
||||
name: "John Doe",
|
||||
username: "jdoe",
|
||||
avatar: "",
|
||||
hideBranding: true,
|
||||
timeZone: "",
|
||||
destinationCalendar: null,
|
||||
credentials: [],
|
||||
bufferTime: 0,
|
||||
locale: "en",
|
||||
theme: null,
|
||||
brandColor: "#292929",
|
||||
darkBrandColor: "#fafafa",
|
||||
availability: [],
|
||||
selectedCalendars: [],
|
||||
startTime: 0,
|
||||
endTime: 0,
|
||||
schedules: [],
|
||||
defaultScheduleId: null,
|
||||
} as User,
|
||||
],
|
||||
users: [user],
|
||||
};
|
||||
|
||||
const min15Event = {
|
||||
@@ -103,6 +95,7 @@ const min15Event = {
|
||||
title: "15min",
|
||||
eventName: "Dynamic Collective 15min Event",
|
||||
description: "Dynamic Collective 15min Event",
|
||||
position: 0,
|
||||
...commons,
|
||||
};
|
||||
const min30Event = {
|
||||
@@ -111,6 +104,7 @@ const min30Event = {
|
||||
title: "30min",
|
||||
eventName: "Dynamic Collective 30min Event",
|
||||
description: "Dynamic Collective 30min Event",
|
||||
position: 1,
|
||||
...commons,
|
||||
};
|
||||
const min60Event = {
|
||||
@@ -119,6 +113,7 @@ const min60Event = {
|
||||
title: "60min",
|
||||
eventName: "Dynamic Collective 60min Event",
|
||||
description: "Dynamic Collective 60min Event",
|
||||
position: 2,
|
||||
...commons,
|
||||
};
|
||||
|
||||
|
||||
@@ -7,6 +7,7 @@ export const availabilityUserSelect = Prisma.validator<Prisma.UserSelect>()({
|
||||
availability: true,
|
||||
id: true,
|
||||
startTime: true,
|
||||
username: true,
|
||||
endTime: true,
|
||||
selectedCalendars: true,
|
||||
schedules: {
|
||||
@@ -22,7 +23,6 @@ export const availabilityUserSelect = Prisma.validator<Prisma.UserSelect>()({
|
||||
export const baseUserSelect = Prisma.validator<Prisma.UserSelect>()({
|
||||
email: true,
|
||||
name: true,
|
||||
username: true,
|
||||
destinationCalendar: true,
|
||||
locale: true,
|
||||
plan: true,
|
||||
@@ -38,7 +38,7 @@ export const userSelect = Prisma.validator<Prisma.UserArgs>()({
|
||||
select: {
|
||||
email: true,
|
||||
name: true,
|
||||
username: true,
|
||||
allowDynamicBooking: true,
|
||||
destinationCalendar: true,
|
||||
locale: true,
|
||||
plan: true,
|
||||
|
||||
@@ -4,6 +4,7 @@ import { z } from "zod";
|
||||
import type { CurrentSeats } from "@calcom/core/getUserAvailability";
|
||||
import { getUserAvailability } from "@calcom/core/getUserAvailability";
|
||||
import dayjs, { Dayjs } from "@calcom/dayjs";
|
||||
import { getDefaultEvent } from "@calcom/lib/defaultEvents";
|
||||
import isOutOfBounds from "@calcom/lib/isOutOfBounds";
|
||||
import logger from "@calcom/lib/logger";
|
||||
import { performance } from "@calcom/lib/server/perfObserver";
|
||||
@@ -23,7 +24,9 @@ const getScheduleSchema = z
|
||||
// endTime ISOString
|
||||
endTime: z.string(),
|
||||
// Event type ID
|
||||
eventTypeId: z.number().optional(),
|
||||
eventTypeId: z.number().int().optional(),
|
||||
// Event type slug
|
||||
eventTypeSlug: z.string(),
|
||||
// invitee timezone
|
||||
timeZone: z.string().optional(),
|
||||
// or list of users (for dynamic events)
|
||||
@@ -100,17 +103,7 @@ export const slotsRouter = createRouter().query("getSchedule", {
|
||||
},
|
||||
});
|
||||
|
||||
export async function getSchedule(
|
||||
input: {
|
||||
timeZone?: string | undefined;
|
||||
eventTypeId?: number | undefined;
|
||||
usernameList?: string[] | undefined;
|
||||
debug?: boolean | undefined;
|
||||
startTime: string;
|
||||
endTime: string;
|
||||
},
|
||||
ctx: { prisma: typeof prisma }
|
||||
) {
|
||||
export async function getSchedule(input: z.infer<typeof getScheduleSchema>, ctx: { prisma: typeof prisma }) {
|
||||
if (input.debug === true) {
|
||||
logger.setSettings({ minLevel: "debug" });
|
||||
}
|
||||
@@ -118,7 +111,7 @@ export async function getSchedule(
|
||||
logger.setSettings({ minLevel: "silly" });
|
||||
}
|
||||
const startPrismaEventTypeGet = performance.now();
|
||||
const eventType = await ctx.prisma.eventType.findUnique({
|
||||
const eventTypeObject = await ctx.prisma.eventType.findUnique({
|
||||
where: {
|
||||
id: input.eventTypeId,
|
||||
},
|
||||
@@ -152,12 +145,42 @@ export async function getSchedule(
|
||||
},
|
||||
users: {
|
||||
select: {
|
||||
username: true,
|
||||
...availabilityUserSelect,
|
||||
},
|
||||
},
|
||||
},
|
||||
});
|
||||
|
||||
const isDynamicBooking = !input.eventTypeId;
|
||||
// For dynamic booking, we need to get and update user credentials, schedule and availability in the eventTypeObject as they're required in the new availability logic
|
||||
const dynamicEventType = getDefaultEvent(input.eventTypeSlug);
|
||||
let dynamicEventTypeObject = dynamicEventType;
|
||||
|
||||
if (isDynamicBooking) {
|
||||
const users = await ctx.prisma.user.findMany({
|
||||
where: {
|
||||
username: {
|
||||
in: input.usernameList,
|
||||
},
|
||||
},
|
||||
select: {
|
||||
allowDynamicBooking: true,
|
||||
...availabilityUserSelect,
|
||||
},
|
||||
});
|
||||
const isDynamicAllowed = !users.some((user) => !user.allowDynamicBooking);
|
||||
if (!isDynamicAllowed) {
|
||||
throw new TRPCError({
|
||||
message: "Some of the users in this group do not allow dynamic booking",
|
||||
code: "UNAUTHORIZED",
|
||||
});
|
||||
}
|
||||
dynamicEventTypeObject = Object.assign({}, dynamicEventType, {
|
||||
users,
|
||||
});
|
||||
}
|
||||
const eventType = isDynamicBooking ? dynamicEventTypeObject : eventTypeObject;
|
||||
|
||||
const endPrismaEventTypeGet = performance.now();
|
||||
logger.debug(
|
||||
`Prisma eventType get took ${endPrismaEventTypeGet - startPrismaEventTypeGet}ms for event:${
|
||||
@@ -189,6 +212,7 @@ export async function getSchedule(
|
||||
} = await getUserAvailability(
|
||||
{
|
||||
userId: currentUser.id,
|
||||
username: currentUser.username || "",
|
||||
dateFrom: startTime.format(),
|
||||
dateTo: endTime.format(),
|
||||
eventTypeId: input.eventTypeId,
|
||||
|
||||
@@ -412,7 +412,6 @@ export const viewerTeamsRouter = createProtectedRouter()
|
||||
include: {
|
||||
user: {
|
||||
select: {
|
||||
username: true,
|
||||
...availabilityUserSelect,
|
||||
},
|
||||
},
|
||||
|
||||
Reference in new issue
Block a user