fix: Set teamMemberEmail server side for CRM RR Skip (#16367)
Co-authored-by: Keith Williams <[email protected]> Co-authored-by: zomars <[email protected]>
This commit is contained in:
17 files changed
+140
-114
No files matched your search
@@ -24,7 +24,7 @@ export const getScheduleSchema = z
|
||||
// whether to do team event or user event
|
||||
isTeamEvent: z.boolean().optional().default(false),
|
||||
orgSlug: z.string().optional(),
|
||||
bookerEmail: z.string().optional(),
|
||||
teamMemberEmail: z.string().nullable().optional(),
|
||||
})
|
||||
.transform((val) => {
|
||||
// Need this so we can pass a single username in the query string form public API
|
||||
|
||||
@@ -1,12 +1,10 @@
|
||||
// eslint-disable-next-line no-restricted-imports
|
||||
import { countBy } from "lodash";
|
||||
import { v4 as uuid } from "uuid";
|
||||
import type z from "zod";
|
||||
|
||||
import CrmManager from "@calcom/core/crmManager/crmManager";
|
||||
import { getAggregatedAvailability } from "@calcom/core/getAggregatedAvailability";
|
||||
import { getBusyTimesForLimitChecks } from "@calcom/core/getBusyTimes";
|
||||
import type { CurrentSeats, IFromUser, IToUser } from "@calcom/core/getUserAvailability";
|
||||
import type { CurrentSeats, IFromUser, IToUser, GetAvailabilityUser } from "@calcom/core/getUserAvailability";
|
||||
import { getUsersAvailability } from "@calcom/core/getUserAvailability";
|
||||
import type { Dayjs } from "@calcom/dayjs";
|
||||
import dayjs from "@calcom/dayjs";
|
||||
@@ -32,7 +30,6 @@ import { SchedulingType } from "@calcom/prisma/enums";
|
||||
import { BookingStatus } from "@calcom/prisma/enums";
|
||||
import { credentialForCalendarServiceSelect } from "@calcom/prisma/selects/credential";
|
||||
import { EventTypeMetaDataSchema } from "@calcom/prisma/zod-utils";
|
||||
import type { EventTypeAppMetadataSchema } from "@calcom/prisma/zod-utils";
|
||||
import type { EventBusyDate } from "@calcom/types/Calendar";
|
||||
|
||||
import { TRPCError } from "@trpc/server";
|
||||
@@ -322,57 +319,6 @@ export interface IGetAvailableSlots {
|
||||
emoji?: string | undefined;
|
||||
}[]
|
||||
>;
|
||||
teamMember?: string | undefined;
|
||||
}
|
||||
|
||||
async function getCRMContactOwnerForRRLeadSkip(
|
||||
bookerEmail: string,
|
||||
apps?: z.infer<typeof EventTypeAppMetadataSchema>
|
||||
) {
|
||||
if (!apps) return;
|
||||
const crm = await getCRMManagerWithRRLeadSkip(apps);
|
||||
|
||||
if (!crm) return;
|
||||
|
||||
const contact = await crm.getContacts(bookerEmail, true);
|
||||
if (contact?.length) {
|
||||
return contact[0].ownerEmail;
|
||||
}
|
||||
}
|
||||
|
||||
async function getCRMManagerWithRRLeadSkip(apps: z.infer<typeof EventTypeAppMetadataSchema>) {
|
||||
let crmRoundRobinLeadSkip;
|
||||
for (const appKey in apps) {
|
||||
const app = apps[appKey as keyof typeof apps];
|
||||
if (
|
||||
app.enabled &&
|
||||
typeof app.appCategories === "object" &&
|
||||
app.appCategories.some((category: string) => category === "crm") &&
|
||||
app.roundRobinLeadSkip
|
||||
) {
|
||||
crmRoundRobinLeadSkip = app;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (crmRoundRobinLeadSkip) {
|
||||
const crmCredential = await prisma.credential.findUnique({
|
||||
where: {
|
||||
id: crmRoundRobinLeadSkip.credentialId,
|
||||
},
|
||||
include: {
|
||||
user: {
|
||||
select: {
|
||||
email: true,
|
||||
},
|
||||
},
|
||||
},
|
||||
});
|
||||
if (crmCredential) {
|
||||
return new CrmManager(crmCredential);
|
||||
}
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
export async function getAvailableSlots({ input, ctx }: GetScheduleOptions): Promise<IGetAvailableSlots> {
|
||||
@@ -435,8 +381,6 @@ export async function getAvailableSlots({ input, ctx }: GetScheduleOptions): Pro
|
||||
}
|
||||
let currentSeats: CurrentSeats | undefined;
|
||||
|
||||
let teamMember: string | undefined;
|
||||
|
||||
let hosts =
|
||||
eventType.hosts?.length && eventType.schedulingType
|
||||
? eventType.hosts
|
||||
@@ -466,25 +410,18 @@ export async function getAvailableSlots({ input, ctx }: GetScheduleOptions): Pro
|
||||
hosts = hosts.filter((host) => host.user.id === originalRescheduledBooking?.userId || 0);
|
||||
}
|
||||
|
||||
let usersWithCredentials = hosts.map(({ isFixed, user }) => ({ isFixed, ...user }));
|
||||
const teamMemberHost = hosts.find((host) => host.user.email === input?.teamMemberEmail);
|
||||
|
||||
if (eventType.schedulingType === SchedulingType.ROUND_ROBIN && input.bookerEmail) {
|
||||
const crmContactOwner = await getCRMContactOwnerForRRLeadSkip(
|
||||
input.bookerEmail,
|
||||
eventType?.metadata?.apps
|
||||
);
|
||||
const contactOwnerHost = hosts.find((host) => host.user.email === crmContactOwner);
|
||||
// If the requested team member is a fixed host proceed as normal else get availability like the requested member is a fixed host
|
||||
const usersWithCredentials =
|
||||
!input.teamMemberEmail || !teamMemberHost || teamMemberHost.isFixed
|
||||
? hosts.map(({ isFixed, user }) => ({ isFixed, ...user }))
|
||||
: hosts.reduce((usersArray, host) => {
|
||||
if (host.isFixed || host.user.email === input.teamMemberEmail)
|
||||
usersArray.push({ ...host.user, isFixed: host.isFixed });
|
||||
|
||||
if (contactOwnerHost) {
|
||||
teamMember = contactOwnerHost.user.email;
|
||||
const contactOwnerIsRRHost = !contactOwnerHost.isFixed;
|
||||
|
||||
usersWithCredentials = usersWithCredentials.filter(
|
||||
(user) => user.email !== contactOwnerHost.user.email && (!contactOwnerIsRRHost || user.isFixed)
|
||||
);
|
||||
usersWithCredentials.push({ ...contactOwnerHost.user, isFixed: true });
|
||||
}
|
||||
}
|
||||
return usersArray;
|
||||
}, [] as (GetAvailabilityUser & { isFixed: boolean })[]);
|
||||
|
||||
const durationToUse = input.duration || 0;
|
||||
|
||||
@@ -894,7 +831,6 @@ export async function getAvailableSlots({ input, ctx }: GetScheduleOptions): Pro
|
||||
|
||||
return {
|
||||
slots: withinBoundsSlotsMappedToDate,
|
||||
teamMember,
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user