From c38c1d9ff63bf2aa8ce7aa9d5485cb61afffcf2a Mon Sep 17 00:00:00 2001 From: Tiago Ribeiro Date: Thu, 25 Jan 2024 10:28:45 +0000 Subject: [PATCH] Removed the need for it to alway use Passport ID --- src/pages/(admin)/BatchCodeGenerator.tsx | 43 ++++++++++++++---------- src/pages/api/code/index.ts | 4 +-- 2 files changed, 27 insertions(+), 20 deletions(-) diff --git a/src/pages/(admin)/BatchCodeGenerator.tsx b/src/pages/(admin)/BatchCodeGenerator.tsx index b9ba96f8..5b7d2b88 100644 --- a/src/pages/(admin)/BatchCodeGenerator.tsx +++ b/src/pages/(admin)/BatchCodeGenerator.tsx @@ -58,30 +58,37 @@ export default function BatchCodeGenerator({user}: {user: User}) { if (filesContent.length > 0) { const file = filesContent[0]; readXlsxFile(file.content).then((rows) => { - const information = uniqBy( - rows - .map((row) => { - const [firstName, lastName, country, passport_id, email, ...phone] = row as string[]; - return EMAIL_REGEX.test(email) && !users.map((u) => u.email).includes(email) - ? { - email: email.toString(), - name: `${firstName ?? ""} ${lastName ?? ""}`.trim(), - passport_id: passport_id.toString(), - } - : undefined; - }) - .filter((x) => !!x) as typeof infos, - (x) => x.email, - ); + try { + const information = uniqBy( + rows + .map((row) => { + const [firstName, lastName, country, passport_id, email, ...phone] = row as string[]; + return EMAIL_REGEX.test(email) && !users.map((u) => u.email).includes(email) + ? { + email: email.toString(), + name: `${firstName ?? ""} ${lastName ?? ""}`.trim(), + passport_id: passport_id?.toString() || undefined, + } + : undefined; + }) + .filter((x) => !!x) as typeof infos, + (x) => x.email, + ); - if (information.length === 0) { + if (information.length === 0) { + toast.error( + "Please upload an Excel file containing user information, one per line! All already registered e-mails have also been ignored!", + ); + return clear(); + } + + setInfos(information); + } catch { toast.error( "Please upload an Excel file containing user information, one per line! All already registered e-mails have also been ignored!", ); return clear(); } - - setInfos(information); }); } // eslint-disable-next-line react-hooks/exhaustive-deps diff --git a/src/pages/api/code/index.ts b/src/pages/api/code/index.ts index 39787f11..2bb1f7e0 100644 --- a/src/pages/api/code/index.ts +++ b/src/pages/api/code/index.ts @@ -42,7 +42,7 @@ async function post(req: NextApiRequest, res: NextApiResponse) { const {type, codes, infos, expiryDate} = req.body as { type: Type; codes: string[]; - infos?: {email: string; name: string; passport_id: string}[]; + infos?: {email: string; name: string; passport_id?: string}[]; expiryDate: null | Date; }; const permission = PERMISSIONS.generateCode[type]; @@ -74,7 +74,7 @@ async function post(req: NextApiRequest, res: NextApiResponse) { if (infos && infos.length > index) { const {email, name, passport_id} = infos[index]; - await setDoc(codeRef, {email: email.trim(), name: name.trim(), passport_id: passport_id.trim()}, {merge: true}); + await setDoc(codeRef, {email: email.trim(), name: name.trim(), ...(passport_id ? {passport_id: passport_id.trim()} : {})}, {merge: true}); const transport = prepareMailer(); const mailOptions = prepareMailOptions(