Updated the code generator to only generate after the e-mails are sent

This commit is contained in:
Tiago Ribeiro
2024-01-25 12:14:12 +00:00
parent a969e90c98
commit 9b87764afb
2 changed files with 31 additions and 11 deletions

View File

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

View File

@@ -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",
);
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});
});
}