* WIP * add frontend * backend to update weight * UI improvements * WIP weight algorithm * enable weights switch + algorithm improvements * fix weightDescription * clean up code * remove OOOEntryHost from schema.prisma * implement logic (not yet tested) * add tests for weight algorithm * add test with weight adjustment * finish unit tests * fix type error * fix type error * add migration * fix type error * fix event type update handler * fix failing test * UI fixes for saving hosts * make sure weightAdjustment is not lost on host changes * fix weightadjustment for new hosts * add weightAdjustment to availableUsers * fix type errors * fix default value for weight * make weight and weightAdjustment optional * fix type errors from schema changes * type fix * clean up code * improve comments * remove comment * clean up code * add tests & weight adjustment improvments * better variable naming * fixes for weight adjustments * make weightAdjustments proportional to weights * fix previous host weight adjustments * improved tests for weight adjustments * save weight and priority + sort hosts correctly * fix type error * code clean up * remove console.log * use BookingRepository to fetch bookings of users * use BookingRepository to fetch bookings in getLuckyUser * fix type errors * fix weightAdjustment if changed from fixed to rr host * disable weights when 'assign all' is enabled * typo * allow 0 weight * set min (and max) for weight and priority * use useWatch * code clean up * fix type error * only count accepted bookings for RR * fix type error * improve data fetching of bookings * only filter bookings of availableUsers * code clean up form feedback * fix tests * don't count no show bookings * code clean up * choose user with highest weight * use one reduce instead of two * use reduce instead of filter and map * don't show weights toggle when 'assign all' is enabled * design fixes * fix type errors * fix: type check * Update packages/features/eventtypes/components/AddMembersWithSwitch.tsx --------- Co-authored-by: CarinaWolli <wollencarina@gmail.com> Co-authored-by: Alex van Andel <me@alexvanandel.com>
92 lines
3.0 KiB
TypeScript
92 lines
3.0 KiB
TypeScript
import { Prisma } from "@prisma/client";
|
|
import type { IncomingMessage } from "http";
|
|
|
|
import { orgDomainConfig } from "@calcom/features/ee/organizations/lib/orgDomains";
|
|
import { HttpError } from "@calcom/lib/http-error";
|
|
import logger from "@calcom/lib/logger";
|
|
import { UserRepository } from "@calcom/lib/server/repository/user";
|
|
import prisma, { userSelect } from "@calcom/prisma";
|
|
import { credentialForCalendarServiceSelect } from "@calcom/prisma/selects/credential";
|
|
|
|
import type { NewBookingEventType } from "./types";
|
|
|
|
const log = logger.getSubLogger({ prefix: ["[loadUsers]:handleNewBooking "] });
|
|
|
|
type EventType = Pick<NewBookingEventType, "hosts" | "users" | "id">;
|
|
|
|
export const loadUsers = async (eventType: EventType, dynamicUserList: string[], req: IncomingMessage) => {
|
|
try {
|
|
const { currentOrgDomain } = orgDomainConfig(req);
|
|
|
|
return eventType.id
|
|
? await loadUsersByEventType(eventType)
|
|
: await loadDynamicUsers(dynamicUserList, currentOrgDomain);
|
|
} catch (error) {
|
|
if (error instanceof HttpError || error instanceof Prisma.PrismaClientKnownRequestError) {
|
|
throw new HttpError({ statusCode: 400, message: error.message });
|
|
}
|
|
throw new HttpError({ statusCode: 500, message: "Unable to load users" });
|
|
}
|
|
};
|
|
|
|
const loadUsersByEventType = async (eventType: EventType): Promise<NewBookingEventType["users"]> => {
|
|
const hosts = eventType.hosts || [];
|
|
const users = hosts.map(({ user, isFixed, priority, weight, weightAdjustment }) => ({
|
|
...user,
|
|
isFixed,
|
|
priority,
|
|
weight,
|
|
weightAdjustment,
|
|
}));
|
|
return users.length ? users : eventType.users;
|
|
};
|
|
|
|
const loadDynamicUsers = async (dynamicUserList: string[], currentOrgDomain: string | null) => {
|
|
if (!Array.isArray(dynamicUserList) || dynamicUserList.length === 0) {
|
|
throw new Error("dynamicUserList is not properly defined or empty.");
|
|
}
|
|
return findUsersByUsername({
|
|
usernameList: dynamicUserList,
|
|
orgSlug: !!currentOrgDomain ? currentOrgDomain : null,
|
|
});
|
|
};
|
|
|
|
/**
|
|
* This method is mostly same as the one in UserRepository but it includes a lot more relations which are specific requirement here
|
|
* TODO: Figure out how to keep it in UserRepository and use it here
|
|
*/
|
|
export const findUsersByUsername = async ({
|
|
usernameList,
|
|
orgSlug,
|
|
}: {
|
|
orgSlug: string | null;
|
|
usernameList: string[];
|
|
}) => {
|
|
log.debug("findUsersByUsername", { usernameList, orgSlug });
|
|
const { where, profiles } = await UserRepository._getWhereClauseForFindingUsersByUsername({
|
|
orgSlug,
|
|
usernameList,
|
|
});
|
|
return (
|
|
await prisma.user.findMany({
|
|
where,
|
|
select: {
|
|
...userSelect.select,
|
|
credentials: {
|
|
select: credentialForCalendarServiceSelect,
|
|
},
|
|
metadata: true,
|
|
},
|
|
})
|
|
).map((user) => {
|
|
const profile = profiles?.find((profile) => profile.user.id === user.id) ?? null;
|
|
return {
|
|
...user,
|
|
organizationId: profile?.organizationId ?? null,
|
|
profile,
|
|
};
|
|
});
|
|
};
|
|
|
|
export type AwaitedLoadUsers = Awaited<ReturnType<typeof loadUsers>>;
|