Solved some issues related to the BatchCreateUser

This commit is contained in:
Tiago Ribeiro
2024-08-14 20:36:47 +01:00
parent 859d9283a7
commit eb55e65d91
2 changed files with 22 additions and 18 deletions

View File

@@ -88,7 +88,7 @@ export default function BatchCreateUser({user}: {user: User}) {
return EMAIL_REGEX.test(email.toString().trim())
? {
email: email.toString().trim().toLowerCase(),
name: `${firstName ?? ""} ${lastName ?? ""}`.trim().toLowerCase(),
name: `${firstName ?? ""} ${lastName ?? ""}`.trim(),
type: type,
passport_id: passport_id?.toString().trim() || undefined,
groupName: group,
@@ -111,6 +111,7 @@ export default function BatchCreateUser({user}: {user: User}) {
return clear();
}
console.log(information);
setInfos(information);
} catch {
toast.error(
@@ -125,24 +126,22 @@ export default function BatchCreateUser({user}: {user: User}) {
const makeUsers = async () => {
const newUsers = infos.filter((x) => !users.map((u) => u.email).includes(x.email));
const confirmed = confirm(`You are about to add ${newUsers.length}, are you sure you want to continue?`);
if (!confirmed) return;
if (!confirm(`You are about to add ${newUsers.length}, are you sure you want to continue?`)) return;
if (newUsers.length > 0) {
setIsLoading(true);
Promise.all(
newUsers.map(async (user) => {
await axios.post("/api/make_user", user);
}),
)
.then((res) => {
toast.success(`Successfully added ${newUsers.length} user(s)!`);
})
.finally(() => {
return clear();
});
try {
for (const newUser of newUsers) await axios.post("/api/make_user", {...newUser, type, expiryDate});
toast.success(`Successfully added ${newUsers.length} user(s)!`);
} catch {
toast.error("Something went wrong, please try again later!");
} finally {
setIsLoading(false);
setInfos([]);
clear();
}
}
setIsLoading(false);
setInfos([]);
};
return (