Feature/fixed hosts (#6423)

* Save design updates for fixed round robin support

* wip - added Host relation

* DRY hostsFixed select

* Changes to allow isFixed in the Availability page

* Allow booking with fixed hosts

* Replace users with hosts if hosts is set

* Also prefer hosts over users here

* Prevent duplicates when hosts is saved

* Accidental slot duplication

* Attempt at making isFixed optional

* Sydney and Shiraz can live in harmony again

* No fixed hosts causes every to be true..

* Make hosts undefinable

* Small handleNewBooking fixes

* Similar fix to the hosts to check for empty-ness

* Default to isFixed false instead of true

* Fix event type list avatars

* Filter availableTimeSlots, collective ts's wont magically re-enable.

* (Further) Fixes to getAggregateWorkingHours

* Weird userId artifact that preceeds this branch, investigate later

* On user delete, remove host, on event type delete, also remove host

* Dynamic event types were incorrectly marked as isFixed=false

* Fixed notFound error when there are no users (but hosts)

* Fixes userIsOwner

* Oops, fixed isFixed users being included correctly

* Fixed Button styling on secondary darkmode

* Create exclusivity in selectable options

* fix: Location dropdown is overflowing #6376 (#6415)

* add `menuPlacement` react-select's props to `getReactSelectProps` to avoid dropdown overflow on small screen.
By default, set to "auto".

* CALCOM-6362 - [CAL-728] App Sidebar. Child items aren't sized/spaced properly (#6424)

* [CAL-728] App Sidebar. Child items aren't sized/spaced properly

* fix: undo logo size

Signed-off-by: Udit Takkar <[email protected]>

Signed-off-by: Udit Takkar <[email protected]>
Co-authored-by: gitstart-calcom <[email protected]>
Co-authored-by: Udit Takkar <[email protected]>

* Fixing colors (#6429)

* Update website

* Update console

* Update yarn.lock

* Uses disable instead of filtering to be more clear

* Update EventTeamTab.tsx

* Merge conflict cleanup

* During test cases the dayjs() utcOffset is local time, this fixes that

Signed-off-by: Udit Takkar <[email protected]>
Co-authored-by: kodiakhq[bot] <49736102+kodiakhq[bot]@users.noreply.github.com>
Co-authored-by: Thomas Brodusch <[email protected]>
Co-authored-by: GitStart-Cal.com <[email protected]>
Co-authored-by: gitstart-calcom <[email protected]>
Co-authored-by: Udit Takkar <[email protected]>
Co-authored-by: sean-brydon <[email protected]>
Co-authored-by: zomars <[email protected]>
This commit is contained in:
17 files changed
+406 -151

No files matched your search

+23 -7
View File
@@ -7,16 +7,29 @@ import type { WorkingHours } from "@calcom/types/schedule";
* offsets them to UTC and intersects them for collective events.
**/
export const getAggregateWorkingHours = (
usersWorkingHoursAndBusySlots: Omit<
usersWorkingHoursAndBusySlots: (Omit<
Awaited<ReturnType<Awaited<typeof import("./getUserAvailability")>["getUserAvailability"]>>,
"currentSeats"
>[],
> & { user?: { isFixed?: boolean } })[],
// eslint-disable-next-line @typescript-eslint/no-unused-vars
schedulingType: SchedulingType | null
): WorkingHours[] => {
if (schedulingType !== SchedulingType.COLLECTIVE) {
// during personal events, just flatMap.
if (!schedulingType) {
return usersWorkingHoursAndBusySlots.flatMap((s) => s.workingHours);
}
return usersWorkingHoursAndBusySlots.reduce((currentWorkingHours: WorkingHours[], s) => {
const looseHostWorkingHours = usersWorkingHoursAndBusySlots
.filter(({ user }) => schedulingType !== SchedulingType.COLLECTIVE && user?.isFixed !== true)
.flatMap((s) => s.workingHours);
const fixedHostSchedules = usersWorkingHoursAndBusySlots.filter(
({ user }) => schedulingType === SchedulingType.COLLECTIVE || user?.isFixed
);
// return early when there are no fixed hosts.
if (!fixedHostSchedules.length) {
return looseHostWorkingHours;
}
return fixedHostSchedules.reduce((currentWorkingHours: WorkingHours[], s) => {
const updatedWorkingHours: typeof currentWorkingHours = [];
s.workingHours.forEach((workingHour) => {
@@ -31,15 +44,18 @@ export const getAggregateWorkingHours = (
updatedWorkingHours.push(
...sameDayWorkingHours.map((compare) => {
const intersect = workingHour.days.filter((day) => compare.days.includes(day));
return {
const retVal: WorkingHours & { userId?: number | null } = {
days: intersect,
startTime: Math.max(workingHour.startTime, compare.startTime),
endTime: Math.min(workingHour.endTime, compare.endTime),
};
if (schedulingType !== SchedulingType.COLLECTIVE) {
retVal.userId = compare.userId;
}
return retVal;
})
);
});
return updatedWorkingHours;
}, []);
}, looseHostWorkingHours);
};
@@ -196,6 +196,12 @@ const getEventTypesFromDB = async (eventTypeId: number) => {
timeZone: true,
},
},
hosts: {
select: {
isFixed: true,
user: userSelect,
},
},
availability: {
select: {
date: true,
@@ -216,9 +222,11 @@ const getEventTypesFromDB = async (eventTypeId: number) => {
};
};
type IsFixedAwareUser = User & { isFixed: boolean };
async function ensureAvailableUsers(
eventType: Awaited<ReturnType<typeof getEventTypesFromDB>> & {
users: User[];
users: IsFixedAwareUser[];
},
input: { dateFrom: string; dateTo: string },
recurringDatesInfo?: {
@@ -226,7 +234,7 @@ async function ensureAvailableUsers(
currentRecurringIndex: number | undefined;
}
) {
const availableUsers: typeof eventType.users = [];
const availableUsers: IsFixedAwareUser[] = [];
/** Let's start checking for availability */
for (const user of eventType.users) {
const { busy: bufferedBusyTimes, workingHours } = await getUserAvailability(
@@ -350,16 +358,25 @@ async function handler(req: NextApiRequest & { userId?: number | undefined }) {
throw new HttpError({ statusCode: 400, message: error.message });
}
let users = !eventTypeId
? await prisma.user.findMany({
where: {
username: {
in: dynamicUserList,
const loadUsers = async () =>
!eventTypeId
? await prisma.user.findMany({
where: {
username: {
in: dynamicUserList,
},
},
},
...userSelect,
})
: eventType.users;
...userSelect,
})
: !!eventType.hosts?.length
? eventType.hosts.map(({ user, isFixed }) => ({
...user,
isFixed,
}))
: eventType.users;
// loadUsers allows type inferring
let users: (Awaited<ReturnType<typeof loadUsers>>[number] & { isFixed?: boolean })[] = await loadUsers();
const isDynamicAllowed = !users.some((user) => !user.allowDynamicBooking);
if (!isDynamicAllowed && !eventTypeId) {
throw new HttpError({
@@ -383,6 +400,14 @@ async function handler(req: NextApiRequest & { userId?: number | undefined }) {
if (!users) throw new HttpError({ statusCode: 404, message: "eventTypeUser.notFound" });
users = users.map((user) => ({
...user,
isFixed:
user.isFixed === false
? false
: user.isFixed || eventType.schedulingType !== SchedulingType.ROUND_ROBIN,
}));
if (eventType && eventType.hasOwnProperty("bookingLimits") && eventType?.bookingLimits) {
const startAsDate = dayjs(reqBody.start).toDate();
await checkBookingLimits(eventType.bookingLimits, startAsDate, eventType.id);
@@ -392,7 +417,7 @@ async function handler(req: NextApiRequest & { userId?: number | undefined }) {
const availableUsers = await ensureAvailableUsers(
{
...eventType,
users,
users: users as IsFixedAwareUser[],
...(eventType.recurringEvent && {
recurringEvent: {
...eventType.recurringEvent,
@@ -410,19 +435,31 @@ async function handler(req: NextApiRequest & { userId?: number | undefined }) {
}
);
// Add an if conditional if there are no seats on the event type
// Assign to only one user when ROUND_ROBIN
if (eventType.schedulingType === SchedulingType.ROUND_ROBIN) {
users = [await getLuckyUser("MAXIMIZE_AVAILABILITY", { availableUsers, eventTypeId: eventType.id })];
} else {
// excluding ROUND_ROBIN, all users have availability required.
if (availableUsers.length !== users.length) {
throw new Error("Some users are unavailable for booking.");
const luckyUsers: typeof users = [];
const luckyUserPool = availableUsers.filter((user) => !user.isFixed);
// loop through all non-fixed hosts and get the lucky users
while (luckyUserPool.length > 0 && luckyUsers.length < 1 /* TODO: Add variable */) {
const newLuckyUser = await getLuckyUser("MAXIMIZE_AVAILABILITY", {
// find a lucky user that is not already in the luckyUsers array
availableUsers: luckyUserPool.filter(
(user) => !luckyUsers.find((existing) => existing.id === user.id)
),
eventTypeId: eventType.id,
});
if (!newLuckyUser) {
break; // prevent infinite loop
}
users = availableUsers;
luckyUsers.push(newLuckyUser);
}
// ALL fixed users must be available
if (
availableUsers.filter((user) => user.isFixed).length !== users.filter((user) => user.isFixed).length
) {
throw new Error("Some users are unavailable for booking.");
}
users = [...luckyUsers, ...availableUsers.filter((user) => user.isFixed)];
}
console.log("available users", users);
const rainbowAppData = getEventTypeAppData(eventType, "rainbow") || {};
// @TODO: use the returned address somewhere in booking creation?
@@ -451,11 +488,11 @@ async function handler(req: NextApiRequest & { userId?: number | undefined }) {
const uid = translator.fromUUID(uuidv5(seed, uuidv5.URL));
const bookingLocation = getLocationValueForDB(reqBody.location, eventType.locations);
console.log(bookingLocation, reqBody.location, eventType.locations);
const customInputs = {} as NonNullable<CalendarEvent["customInputs"]>;
const teamMemberPromises =
eventType.schedulingType === SchedulingType.COLLECTIVE
users.length > 1
? users.slice(1).map(async function (user) {
return {
email: user.email || "",
@@ -5,7 +5,7 @@ import { classNames } from "@calcom/lib";
import { useLocale } from "@calcom/lib/hooks/useLocale";
import { Avatar, Icon, Select } from "@calcom/ui";
type CheckedSelectOption = {
export type CheckedSelectOption = {
avatar: string;
label: string;
value: string;
+2
View File
@@ -82,8 +82,10 @@ const commons = {
bookingLimits: null,
hidden: false,
userId: 0,
owner: null,
workflows: [],
users: [user],
hosts: [],
metadata: EventTypeMetaDataSchema.parse({}),
};
+6
View File
@@ -125,6 +125,12 @@ export default async function getEventTypeById({
id: true,
},
},
hosts: {
select: {
isFixed: true,
userId: true,
},
},
userId: true,
price: true,
destinationCalendar: true,
+1 -1
View File
@@ -127,7 +127,7 @@ const getSlots = ({
eventLength,
}: GetSlots) => {
// current date in invitee tz
const startDate = dayjs().add(minimumBookingNotice, "minute");
const startDate = dayjs().utcOffset(inviteeDate.utcOffset()).add(minimumBookingNotice, "minute");
// This code is ran client side, startOf() does some conversions based on the
// local tz of the client. Sometimes this shifts the day incorrectly.
const startOfDayUTC = dayjs.utc().set("hour", 0).set("minute", 0).set("second", 0);
@@ -0,0 +1,15 @@
-- CreateTable
CREATE TABLE "Host" (
"id" SERIAL NOT NULL,
"userId" INTEGER NOT NULL,
"eventTypeId" INTEGER NOT NULL,
"isFixed" BOOLEAN NOT NULL DEFAULT false,
CONSTRAINT "Host_pkey" PRIMARY KEY ("id")
);
-- AddForeignKey
ALTER TABLE "Host" ADD CONSTRAINT "Host_userId_fkey" FOREIGN KEY ("userId") REFERENCES "users"("id") ON DELETE CASCADE ON UPDATE CASCADE;
-- AddForeignKey
ALTER TABLE "Host" ADD CONSTRAINT "Host_eventTypeId_fkey" FOREIGN KEY ("eventTypeId") REFERENCES "EventType"("id") ON DELETE CASCADE ON UPDATE CASCADE;
+11
View File
@@ -29,6 +29,15 @@ enum PeriodType {
RANGE @map("range")
}
model Host {
id Int @id @default(autoincrement())
user User @relation(fields: [userId], references: [id], onDelete: Cascade)
userId Int
eventType EventType @relation(fields: [eventTypeId], references: [id], onDelete: Cascade)
eventTypeId Int
isFixed Boolean @default(false)
}
model EventType {
id Int @id @default(autoincrement())
/// @zod.min(1)
@@ -41,6 +50,7 @@ model EventType {
locations Json?
length Int
hidden Boolean @default(false)
hosts Host[]
users User[] @relation("user_eventtype")
owner User? @relation("owner", fields: [userId], references: [id], onDelete: Cascade)
userId Int?
@@ -187,6 +197,7 @@ model User {
workflows Workflow[]
routingForms App_RoutingForms_Form[] @relation("routing-form")
verifiedNumbers VerifiedNumber[]
hosts Host[]
@@map(name: "users")
}
@@ -1,4 +1,4 @@
import { MembershipRole, PeriodType, Prisma } from "@prisma/client";
import { MembershipRole, PeriodType, Prisma, SchedulingType } from "@prisma/client";
import { PrismaClientKnownRequestError } from "@prisma/client/runtime";
// REVIEW: From lint error
import _ from "lodash";
@@ -84,6 +84,14 @@ const EventTypeUpdateInput = _EventTypeModel
externalId: true,
}),
users: z.array(stringOrNumber).optional(),
hosts: z
.array(
z.object({
userId: z.number(),
isFixed: z.boolean().optional(),
})
)
.optional(),
schedule: z.number().optional(),
hashedLink: z.string(),
})
@@ -187,6 +195,13 @@ export const eventTypesRouter = router({
users: {
select: baseUserSelect,
},
hosts: {
select: {
user: {
select: baseUserSelect,
},
},
},
...baseEventTypeSelect,
});
@@ -255,6 +270,7 @@ export const eventTypesRouter = router({
const mapEventType = (eventType: typeof user.eventTypes[number]) => ({
...eventType,
users: !!eventType.hosts?.length ? eventType.hosts.map((host) => host.user) : eventType.users,
// @FIXME: cc @hariombalhara This is failing with production data
// metadata: EventTypeMetaDataSchema.parse(eventType.metadata),
});
@@ -483,6 +499,7 @@ export const eventTypesRouter = router({
customInputs,
recurringEvent,
users,
hosts,
id,
hashedLink,
// Extract this from the input so it doesn't get saved in the db
@@ -557,6 +574,17 @@ export const eventTypesRouter = router({
connect: users.map((userId: number) => ({ id: userId })),
};
}
if (hosts) {
data.hosts = {
deleteMany: {
eventTypeId: id,
},
createMany: {
// when schedulingType is COLLECTIVE, remove unFixed hosts.
data: hosts.filter((host) => !(data.schedulingType === SchedulingType.COLLECTIVE && !host.isFixed)),
},
};
}
if (input?.price || input.metadata?.apps?.stripe?.price) {
data.price = input.price || input.metadata?.apps?.stripe?.price;
+46 -30
View File
@@ -144,6 +144,14 @@ async function getEventType(ctx: { prisma: typeof prisma }, input: z.infer<typeo
days: true,
},
},
hosts: {
select: {
isFixed: true,
user: {
select: availabilityUserSelect,
},
},
},
users: {
select: {
...availabilityUserSelect,
@@ -227,9 +235,17 @@ export async function getSchedule(input: z.infer<typeof getScheduleSchema>, ctx:
}
let currentSeats: CurrentSeats | undefined = undefined;
let users = eventType.users.map((user) => ({
isFixed: !eventType.schedulingType || eventType.schedulingType === SchedulingType.COLLECTIVE,
...user,
}));
// overwrite if it is a team event & hosts is set, otherwise keep using users.
if (eventType.schedulingType && !!eventType.hosts?.length) {
users = eventType.hosts.map(({ isFixed, user }) => ({ isFixed, ...user }));
}
/* We get all users working hours and busy slots */
const userAvailability = await Promise.all(
eventType.users.map(async (currentUser) => {
users.map(async (currentUser) => {
const {
busy,
workingHours,
@@ -255,16 +271,15 @@ export async function getSchedule(input: z.infer<typeof getScheduleSchema>, ctx:
workingHours,
dateOverrides,
busy,
userId: currentUser.id,
user: currentUser,
};
})
);
// flattens availability of multiple users
const dateOverrides = userAvailability.flatMap((availability) =>
availability.dateOverrides.map((override) => ({ userId: availability.userId, ...override }))
availability.dateOverrides.map((override) => ({ userId: availability.user.id, ...override }))
);
const workingHours = getAggregateWorkingHours(userAvailability, eventType.schedulingType);
const availabilityCheckProps = {
eventLength: eventType.length,
currentSeats,
@@ -304,34 +319,33 @@ export async function getSchedule(input: z.infer<typeof getScheduleSchema>, ctx:
);
}
const isCollective = !eventType.schedulingType || eventType.schedulingType === SchedulingType.COLLECTIVE;
let availableTimeSlots: typeof timeSlots = [];
if (isCollective) {
availableTimeSlots = timeSlots.filter((slot) =>
userAvailability.every((schedule) => {
const startCheckForAvailability = performance.now();
const isAvailable = checkIfIsAvailable({
time: slot.time,
...schedule,
...availabilityCheckProps,
});
const endCheckForAvailability = performance.now();
checkForAvailabilityCount++;
checkForAvailabilityTime += endCheckForAvailability - startCheckForAvailability;
return isAvailable;
})
);
} else {
availableTimeSlots = timeSlots
availableTimeSlots = timeSlots.filter((slot) => {
const fixedHosts = userAvailability.filter((availability) => availability.user.isFixed);
return fixedHosts.every((schedule) => {
const startCheckForAvailability = performance.now();
const isAvailable = checkIfIsAvailable({
time: slot.time,
...schedule,
...availabilityCheckProps,
});
const endCheckForAvailability = performance.now();
checkForAvailabilityCount++;
checkForAvailabilityTime += endCheckForAvailability - startCheckForAvailability;
return isAvailable;
});
});
// what else are you going to call it?
const looseHostAvailability = userAvailability.filter(({ user: { isFixed } }) => !isFixed);
if (looseHostAvailability.length > 0) {
availableTimeSlots = availableTimeSlots
.map((slot) => {
slot.userIds = slot.userIds?.filter((slotUserId) => {
const userSchedule = userAvailability.find(({ userId }) => userId === slotUserId);
const userSchedule = looseHostAvailability.find(
({ user: { id: userId } }) => userId === slotUserId
);
if (!userSchedule) {
throw new TRPCError({
message: "Shouldn't happen that we don't have a matching user schedule here",
code: "INTERNAL_SERVER_ERROR",
});
return false;
}
return checkIfIsAvailable({
time: slot.time,
@@ -341,7 +355,7 @@ export async function getSchedule(input: z.infer<typeof getScheduleSchema>, ctx:
});
return slot;
})
.filter((slot) => slot.userIds && slot.userIds.length > 0);
.filter((slot) => !!slot.userIds?.length);
}
availableTimeSlots = availableTimeSlots.filter((slot) => isTimeWithinBounds(slot.time));
@@ -355,7 +369,9 @@ export async function getSchedule(input: z.infer<typeof getScheduleSchema>, ctx:
r[time.format("YYYY-MM-DD")].push({
...passThroughProps,
time: time.toISOString(),
users: eventType.users.map((user) => user.username || ""),
users: (eventType.hosts ? eventType.hosts.map((host) => host.user) : eventType.users).map(
(user) => user.username || ""
),
// Conditionally add the attendees and booking id to slots object if there is already a booking during that time
...(currentSeats?.some((booking) => booking.startTime.toISOString() === time.toISOString()) && {
attendees:
+3 -3
View File
@@ -37,7 +37,7 @@ const buttonClasses = cva(
variants: {
color: {
primary: "text-white dark:text-black",
secondary: "text-gray-900 dark:text-darkgray-900 bg-white",
secondary: "text-gray-900 dark:text-darkgray-900",
minimal: "text-gray-900 dark:text-darkgray-900",
destructive: "",
},
@@ -80,7 +80,7 @@ const buttonClasses = cva(
disabled: true,
color: "secondary",
className:
"border border-gray-200 bg-opacity-30 text-gray-900/30 bg-white dark:bg-darkgray-100 dark:text-darkgray-900/30 dark:border-darkgray-200",
"border border-gray-200 bg-opacity-30 text-gray-900/30 dark:text-darkgray-900/30 dark:border-darkgray-200",
},
{
loading: true,
@@ -92,7 +92,7 @@ const buttonClasses = cva(
disabled: [undefined, false],
color: "secondary",
className:
"border border-gray-300 dark:border-darkgray-300 hover:bg-gray-50 hover:border-gray-400 focus:bg-gray-100 dark:hover:bg-darkgray-200 dark:focus:bg-darkgray-200 focus:outline-none focus:ring-2 focus:ring-offset focus:ring-gray-900 dark:focus:ring-white",
"border border-gray-300 dark:border-darkgray-300 bg-white dark:bg-darkgray-100 hover:bg-gray-50 hover:border-gray-400 focus:bg-gray-100 dark:hover:bg-darkgray-200 dark:focus:bg-darkgray-200 focus:outline-none focus:ring-2 focus:ring-offset focus:ring-gray-900 dark:focus:ring-white",
}),
// Minimal variants
{