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 (