fix: Broken team events if a user with the same name exists (#10724)

* fix: Broken team events if a user with the same name exists

* Fix tests + fix usernameList optionality
This commit is contained in:
Alex van Andel authored and GitHub committed 2023-08-11 22:06:24 +01:00
1 parent 4d5697f5a5
commit ceb18167cf
5 files changed
+74 -39

No files matched your search

@@ -1,4 +1,4 @@
import { useSearchParams } from "next/navigation";
import { useSearchParams, usePathname } from "next/navigation";
import { shallow } from "zustand/shallow";
import { useSchedule } from "@calcom/features/schedules";
@@ -59,8 +59,10 @@ export const useScheduleForEvent = ({
(state) => [state.username, state.eventSlug, state.month, state.selectedDuration],
shallow
);
const serachParams = useSearchParams();
const rescheduleUid = serachParams.get("rescheduleUid");
const searchParams = useSearchParams();
const rescheduleUid = searchParams.get("rescheduleUid");
const pathname = usePathname();
return useSchedule({
username: usernameFromStore ?? username,
@@ -71,5 +73,6 @@ export const useScheduleForEvent = ({
rescheduleUid,
month: monthFromStore ?? month,
duration: durationFromStore ?? duration,
isTeamEvent: pathname.indexOf("/team/") !== -1,
});
};
@@ -11,6 +11,7 @@ type UseScheduleWithCacheArgs = {
prefetchNextMonth?: boolean;
duration?: number | null;
rescheduleUid?: string | null;
isTeamEvent?: boolean;
};
export const useSchedule = ({
@@ -22,6 +23,7 @@ export const useSchedule = ({
prefetchNextMonth,
duration,
rescheduleUid,
isTeamEvent,
}: UseScheduleWithCacheArgs) => {
const monthDayjs = month ? dayjs(month) : dayjs();
const nextMonthDayjs = monthDayjs.add(1, "month");
@@ -30,6 +32,7 @@ export const useSchedule = ({
// no satisfy typscript.
return trpc.viewer.public.slots.getSchedule.useQuery(
{
isTeamEvent,
usernameList: getUsernameList(username ?? ""),
// Prioritize slug over id, since slug is the first value we get available.
// If we have a slug, we don't need to fetch the id.
@@ -13,7 +13,7 @@ export const getScheduleSchema = z
// invitee timezone
timeZone: z.string().optional(),
// or list of users (for dynamic events)
usernameList: z.union([z.string(), z.array(z.string())]).optional(),
usernameList: z.array(z.string()).min(1).optional(),
debug: z.boolean().optional(),
// to handle event types with multiple duration options
duration: z
@@ -21,6 +21,8 @@ export const getScheduleSchema = z
.optional()
.transform((val) => val && parseInt(val)),
rescheduleUid: z.string().optional().nullable(),
// whether to do team event or user event
isTeamEvent: z.boolean().optional().default(false),
})
.transform((val) => {
// Need this so we can pass a single username in the query string form public API
@@ -69,42 +69,53 @@ export const checkIfIsAvailable = ({
});
};
async function getEventTypeId({
slug,
eventTypeSlug,
isTeamEvent,
}: {
slug?: string;
eventTypeSlug?: string;
isTeamEvent: boolean;
}) {
if (!eventTypeSlug || !slug) return null;
let teamId;
let userId;
if (isTeamEvent) {
teamId = await getTeamIdFromSlug(slug);
} else {
userId = await getUserIdFromUsername(slug);
}
const eventType = await prisma.eventType.findFirst({
where: {
slug: eventTypeSlug,
...(teamId ? { teamId } : {}),
...(userId ? { userId } : {}),
},
select: {
id: true,
},
});
if (!eventType) {
throw new TRPCError({ code: "NOT_FOUND" });
}
return eventType?.id;
}
export async function getEventType(input: TGetScheduleInputSchema) {
const { eventTypeSlug, usernameList } = input;
let eventTypeId = input.eventTypeId;
const { eventTypeSlug, usernameList, isTeamEvent } = input;
const eventTypeId =
input.eventTypeId ||
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
(await getEventTypeId({
slug: usernameList?.[0],
eventTypeSlug: eventTypeSlug,
isTeamEvent,
}));
if (eventTypeId === undefined && eventTypeSlug && usernameList && usernameList.length === 1) {
// If we only have the slug and usernameList, we need to get the id first
const username = usernameList[0];
const userId = await getUserIdFromUsername(username);
let teamId;
if (!userId) {
teamId = await getTeamIdFromSlug(username);
if (!teamId) {
throw new TRPCError({
message: "User or team not found",
code: "NOT_FOUND",
});
}
}
const eventType = await prisma.eventType.findFirst({
where: {
slug: eventTypeSlug,
...(teamId ? { teamId } : {}),
...(userId ? { userId } : {}),
},
select: {
id: true,
},
});
if (!eventType) {
throw new TRPCError({ code: "NOT_FOUND" });
}
eventTypeId = eventType.id;
if (!eventTypeId) {
return null;
}
const eventType = await prisma.eventType.findUnique({
@@ -165,7 +176,7 @@ export async function getEventType(input: TGetScheduleInputSchema) {
},
});
if (!eventType) {
return eventType;
return null;
}
return {