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

@@ -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 {