diff --git a/src/pages/(admin)/BatchCodeGenerator.tsx b/src/pages/(admin)/BatchCodeGenerator.tsx index 03ac57f9..39bb132c 100644 --- a/src/pages/(admin)/BatchCodeGenerator.tsx +++ b/src/pages/(admin)/BatchCodeGenerator.tsx @@ -63,11 +63,11 @@ export default function BatchCodeGenerator({user}: {user: User}) { 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) + return EMAIL_REGEX.test(email.toString().trim()) && !users.map((u) => u.email).includes(email.toString().trim()) ? { - email: email.toString(), + email: email.toString().trim(), name: `${firstName ?? ""} ${lastName ?? ""}`.trim(), - passport_id: passport_id?.toString() || undefined, + passport_id: passport_id?.toString().trim() || undefined, } : undefined; }) @@ -102,10 +102,15 @@ export default function BatchCodeGenerator({user}: {user: User}) { setIsLoading(true); axios - .post("/api/code", {type, codes, infos: infos, expiryDate}) + .post<{ok: boolean; valid?: number; reason?: string}>("/api/code", {type, codes, infos: infos, expiryDate}) .then(({data, status}) => { if (data.ok) { - toast.success(`Successfully generated ${capitalize(type)} codes and they have been notified by e-mail!`, {toastId: "success"}); + toast.success( + `Successfully generated${data.valid ? ` ${data.valid}/${infos.length}` : ""} ${capitalize( + type, + )} codes and they have been notified by e-mail!`, + {toastId: "success"}, + ); return; } @@ -121,7 +126,10 @@ export default function BatchCodeGenerator({user}: {user: User}) { toast.error(`Something went wrong, please try again later!`, {toastId: "error"}); }) - .finally(() => setIsLoading(false)); + .finally(() => { + setIsLoading(false); + return clear(); + }); }; return ( diff --git a/src/pages/api/code/index.ts b/src/pages/api/code/index.ts index 2bb1f7e0..d855e1f8 100644 --- a/src/pages/api/code/index.ts +++ b/src/pages/api/code/index.ts @@ -70,11 +70,10 @@ async function post(req: NextApiRequest, res: NextApiResponse) { const codePromises = codes.map(async (code, index) => { const codeRef = doc(db, "codes", code); - await setDoc(codeRef, {type, code, creator: req.session.user!.id, expiryDate}); + const codeInformation = {type, code, creator: req.session.user!.id, expiryDate}; 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: passport_id.trim()} : {})}, {merge: true}); const transport = prepareMailer(); const mailOptions = prepareMailOptions( @@ -87,11 +86,24 @@ async function post(req: NextApiRequest, res: NextApiResponse) { "main", ); - await transport.sendMail(mailOptions); + try { + await transport.sendMail(mailOptions); + await setDoc( + codeRef, + {...codeInformation, email: email.trim(), name: name.trim(), ...(passport_id ? {passport_id: passport_id.trim()} : {})}, + {merge: true}, + ); + + return true; + } catch (e) { + return false; + } + } else { + await setDoc(codeRef, codeInformation); } }); - Promise.all(codePromises).then(() => { - res.status(200).json({ok: true}); + Promise.all(codePromises).then((results) => { + res.status(200).json({ok: true, valid: results.filter((x) => x).length}); }); }