From 75c2ccff964b027c9c341634932be510b92e6b95 Mon Sep 17 00:00:00 2001 From: Hariom Balhara Date: Tue, 12 Apr 2022 22:40:18 +0530 Subject: [PATCH] RoundRobin Booking Error in a particular case (#2471) * users can be array of user when roundrobin team booking is there with availablity of multiple people * Return empty array * Add comments * checktype fix * removed extra condition Co-authored-by: Syed Ali Shahbaz --- apps/web/pages/api/book/event.ts | 2 +- packages/lib/defaultEvents.ts | 32 +++++++++++++++++++++++--------- 2 files changed, 24 insertions(+), 10 deletions(-) diff --git a/apps/web/pages/api/book/event.ts b/apps/web/pages/api/book/event.ts index 87d2c9f9..ec99dfca 100644 --- a/apps/web/pages/api/book/event.ts +++ b/apps/web/pages/api/book/event.ts @@ -223,7 +223,7 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse) const reqBody = req.body as BookingCreateBody; // handle dynamic user - const dynamicUserList = getUsernameList(reqBody.user as string); + const dynamicUserList = getUsernameList(reqBody?.user); const eventTypeSlug = reqBody.eventTypeSlug; const eventTypeId = reqBody.eventTypeId; const tAttendees = await getTranslation(reqBody.language ?? "en", "common"); diff --git a/packages/lib/defaultEvents.ts b/packages/lib/defaultEvents.ts index 0b9cb639..879804ff 100644 --- a/packages/lib/defaultEvents.ts +++ b/packages/lib/defaultEvents.ts @@ -139,15 +139,29 @@ export const getUsernameSlugLink = ({ users, slug }: UsernameSlugLinkProps): str return slugLink; }; -export const getUsernameList = (users: string): string[] => { - return users - ?.toLowerCase() - .replace(/ /g, "+") - .replace(/%20/g, "+") - .split("+") - .filter((el) => { - return el.length != 0; - }); +export const getUsernameList = (users: string | string[] | undefined): string[] => { + if (!users) { + return []; + } + if (!(users instanceof Array)) { + users = [users]; + } + const allUsers: string[] = []; + // Multiple users can come in case of a team round-robin booking and in that case dynamic link won't be a user. + // So, even though this code handles even if individual user is dynamic link, that isn't a possibility right now. + users.forEach((user) => { + allUsers.push( + ...user + ?.toLowerCase() + .replace(/ /g, "+") + .replace(/%20/g, "+") + .split("+") + .filter((el) => { + return el.length != 0; + }) + ); + }); + return allUsers; }; export default defaultEvents;