feat: Skip contact owner even if it matches attribute routing (#17015)
Co-authored-by: Omar López <[email protected]>
This commit is contained in:
1 parent
de503add83
commit
03d0b98155
38 files changed
+608
-159
No files matched your search
@@ -2,6 +2,7 @@ import { v4 as uuidv4 } from "uuid";
|
||||
|
||||
import dayjs from "@calcom/dayjs";
|
||||
import { getRoutedTeamMemberIdsFromSearchParams } from "@calcom/lib/bookings/getRoutedTeamMemberIdsFromSearchParams";
|
||||
import { fromEntriesWithDuplicateKeys } from "@calcom/lib/fromEntriesWithDuplicateKeys";
|
||||
import { parseRecurringDates } from "@calcom/lib/parse-dates";
|
||||
|
||||
import type { BookerEvent, BookingCreateBody, RecurringBookingCreateBody } from "../../types";
|
||||
@@ -46,6 +47,8 @@ export const mapBookingToMutationInput = ({
|
||||
const routedTeamMemberIds = getRoutedTeamMemberIdsFromSearchParams(searchParams);
|
||||
const routingFormResponseIdParam = searchParams.get("cal.routingFormResponseId");
|
||||
const routingFormResponseId = routingFormResponseIdParam ? Number(routingFormResponseIdParam) : undefined;
|
||||
const skipContactOwner = searchParams.get("cal.skipContactOwner") === "true";
|
||||
|
||||
return {
|
||||
...values,
|
||||
user: username,
|
||||
@@ -68,7 +71,8 @@ export const mapBookingToMutationInput = ({
|
||||
teamMemberEmail,
|
||||
orgSlug,
|
||||
routedTeamMemberIds,
|
||||
routingFormResponseId
|
||||
routingFormResponseId,
|
||||
skipContactOwner,
|
||||
};
|
||||
};
|
||||
|
||||
|
||||
@@ -150,6 +150,36 @@ type BookingDataSchemaGetter =
|
||||
| typeof getBookingDataSchema
|
||||
| typeof import("@calcom/features/bookings/lib/getBookingDataSchemaForApi").default;
|
||||
|
||||
/**
|
||||
* Adds the contact owner to be the only lucky user
|
||||
* @returns
|
||||
*/
|
||||
function buildLuckyUsersWithJustContactOwner({
|
||||
contactOwnerEmail,
|
||||
availableUsers,
|
||||
fixedUserPool,
|
||||
}: {
|
||||
contactOwnerEmail: string | null;
|
||||
availableUsers: IsFixedAwareUser[];
|
||||
fixedUserPool: IsFixedAwareUser[];
|
||||
}) {
|
||||
const luckyUsers: Awaited<ReturnType<typeof loadAndValidateUsers>> = [];
|
||||
if (!contactOwnerEmail) {
|
||||
return luckyUsers;
|
||||
}
|
||||
|
||||
const isContactOwnerAFixedHostAlready = fixedUserPool.some((user) => user.email === contactOwnerEmail);
|
||||
if (isContactOwnerAFixedHostAlready) {
|
||||
return luckyUsers;
|
||||
}
|
||||
|
||||
const teamMember = availableUsers.find((user) => user.email === contactOwnerEmail);
|
||||
if (teamMember) {
|
||||
luckyUsers.push(teamMember);
|
||||
}
|
||||
return luckyUsers;
|
||||
}
|
||||
|
||||
async function handler(
|
||||
req: NextApiRequest & {
|
||||
userId?: number | undefined;
|
||||
@@ -271,6 +301,10 @@ async function handler(
|
||||
logger: loggerWithEventDetails,
|
||||
});
|
||||
|
||||
const contactOwnerFromReq = reqBody.teamMemberEmail ?? null;
|
||||
const skipContactOwner = reqBody.skipContactOwner ?? false;
|
||||
const contactOwnerEmail = skipContactOwner ? null : contactOwnerFromReq;
|
||||
|
||||
let users = await loadAndValidateUsers({
|
||||
req,
|
||||
eventType,
|
||||
@@ -278,6 +312,7 @@ async function handler(
|
||||
dynamicUserList,
|
||||
logger: loggerWithEventDetails,
|
||||
routedTeamMemberIds: routedTeamMemberIds ?? null,
|
||||
contactOwnerEmail,
|
||||
});
|
||||
|
||||
let { locationBodyString, organizerOrFirstDynamicGroupMemberDefaultLocationUrl } = getLocationValuesForDb(
|
||||
@@ -384,7 +419,6 @@ async function handler(
|
||||
},
|
||||
loggerWithEventDetails
|
||||
);
|
||||
const luckyUsers: typeof users = [];
|
||||
const luckyUserPool: IsFixedAwareUser[] = [];
|
||||
const fixedUserPool: IsFixedAwareUser[] = [];
|
||||
availableUsers.forEach((user) => {
|
||||
@@ -401,17 +435,14 @@ async function handler(
|
||||
})
|
||||
);
|
||||
|
||||
if (reqBody.teamMemberEmail) {
|
||||
// If requested user is not a fixed host, assign the lucky user as the team member
|
||||
if (!fixedUserPool.some((user) => user.email === reqBody.teamMemberEmail)) {
|
||||
const teamMember = availableUsers.find((user) => user.email === reqBody.teamMemberEmail);
|
||||
if (teamMember) {
|
||||
luckyUsers.push(teamMember);
|
||||
}
|
||||
}
|
||||
}
|
||||
const luckyUsers: typeof users = buildLuckyUsersWithJustContactOwner({
|
||||
contactOwnerEmail: contactOwnerEmail,
|
||||
availableUsers,
|
||||
fixedUserPool,
|
||||
});
|
||||
|
||||
// loop through all non-fixed hosts and get the lucky users
|
||||
// This logic doesn't run when contactOwner is used because in that case, luckUsers.length === 1
|
||||
while (luckyUserPool.length > 0 && luckyUsers.length < 1 /* TODO: Add variable */) {
|
||||
const freeUsers = luckyUserPool.filter(
|
||||
(user) => !luckyUsers.concat(notAvailableLuckyUsers).find((existing) => existing.id === user.id)
|
||||
@@ -632,7 +663,6 @@ async function handler(
|
||||
const attendeesList = [...invitee, ...guests];
|
||||
|
||||
const responses = reqBody.responses || null;
|
||||
|
||||
const evtName = !eventType?.isDynamic ? eventType.eventName : responses?.title;
|
||||
const eventNameObject = {
|
||||
//TODO: Can we have an unnamed attendee? If not, I would really like to throw an error here.
|
||||
|
||||
@@ -27,6 +27,7 @@ type InputProps = {
|
||||
dynamicUserList: string[];
|
||||
logger: Logger<unknown>;
|
||||
routedTeamMemberIds: number[] | null;
|
||||
contactOwnerEmail: string | null;
|
||||
};
|
||||
|
||||
export async function loadAndValidateUsers({
|
||||
@@ -36,8 +37,9 @@ export async function loadAndValidateUsers({
|
||||
dynamicUserList,
|
||||
logger,
|
||||
routedTeamMemberIds,
|
||||
contactOwnerEmail,
|
||||
}: InputProps): Promise<Users> {
|
||||
let users: Users = await loadUsers({ eventType, dynamicUserList, req, routedTeamMemberIds });
|
||||
let users: Users = await loadUsers({ eventType, dynamicUserList, req, routedTeamMemberIds, contactOwnerEmail });
|
||||
const isDynamicAllowed = !users.some((user) => !user.allowDynamicBooking);
|
||||
if (!isDynamicAllowed && !eventTypeId) {
|
||||
logger.warn({
|
||||
|
||||
@@ -2,7 +2,7 @@ import { Prisma } from "@prisma/client";
|
||||
import type { IncomingMessage } from "http";
|
||||
|
||||
import { orgDomainConfig } from "@calcom/features/ee/organizations/lib/orgDomains";
|
||||
import { getRoutedUsers } from "@calcom/lib/bookings/getRoutedUsers";
|
||||
import { getRoutedUsersWithContactOwnerAndFixedUsers } from "@calcom/lib/bookings/getRoutedUsers";
|
||||
import { HttpError } from "@calcom/lib/http-error";
|
||||
import logger from "@calcom/lib/logger";
|
||||
import { safeStringify } from "@calcom/lib/safeStringify";
|
||||
@@ -21,18 +21,20 @@ export const loadUsers = async ({
|
||||
dynamicUserList,
|
||||
req,
|
||||
routedTeamMemberIds,
|
||||
contactOwnerEmail,
|
||||
}: {
|
||||
eventType: EventType;
|
||||
dynamicUserList: string[];
|
||||
req: IncomingMessage;
|
||||
routedTeamMemberIds: number[] | null;
|
||||
contactOwnerEmail: string | null;
|
||||
}) => {
|
||||
try {
|
||||
const { currentOrgDomain } = orgDomainConfig(req);
|
||||
const users = eventType.id
|
||||
? await loadUsersByEventType(eventType)
|
||||
: await loadDynamicUsers(dynamicUserList, currentOrgDomain);
|
||||
return getRoutedUsers({ users, routedTeamMemberIds });
|
||||
return getRoutedUsersWithContactOwnerAndFixedUsers({ users, routedTeamMemberIds, contactOwnerEmail });
|
||||
} catch (error) {
|
||||
log.error("Unable to load users", safeStringify(error));
|
||||
if (error instanceof HttpError || error instanceof Prisma.PrismaClientKnownRequestError) {
|
||||
|
||||
@@ -46,8 +46,8 @@ export const useSchedule = ({
|
||||
selectedDate,
|
||||
});
|
||||
const searchParams = useSearchParams();
|
||||
|
||||
const routedTeamMemberIds = searchParams ? getRoutedTeamMemberIdsFromSearchParams(searchParams) : null;
|
||||
const skipContactOwner = searchParams ? searchParams.get("cal.skipContactOwner") === "true" : false;
|
||||
return trpc.viewer.public.slots.getSchedule.useQuery(
|
||||
{
|
||||
isTeamEvent,
|
||||
@@ -67,6 +67,7 @@ export const useSchedule = ({
|
||||
orgSlug,
|
||||
teamMemberEmail,
|
||||
routedTeamMemberIds,
|
||||
skipContactOwner,
|
||||
},
|
||||
{
|
||||
trpc: {
|
||||
|
||||
Reference in new issue
Block a user