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 (

View File

@@ -47,6 +47,7 @@ async function post(req: NextApiRequest, res: NextApiResponse) {
// cleaning data
delete req.body.passport_id;
delete req.body.groupName;
delete req.body.expiryDate;
await createUserWithEmailAndPassword(auth, email.toLowerCase(), passport_id)
.then(async (userCredentials) => {
@@ -57,11 +58,12 @@ async function post(req: NextApiRequest, res: NextApiResponse) {
bio: "",
type: type,
focus: "academic",
status: "paymentDue",
status: "active",
desiredLevels: DEFAULT_DESIRED_LEVELS,
levels: DEFAULT_LEVELS,
isFirstLogin: false,
isVerified: true,
registrationDate: new Date(),
subscriptionExpirationDate: expiryDate || null,
};
await setDoc(doc(db, "users", userId), user);
@@ -120,10 +122,13 @@ async function post(req: NextApiRequest, res: NextApiResponse) {
}
}
}
console.log(`Returning - ${email}`);
return res.status(200).json({ok: true});
})
.catch((error) => {
console.log(`Failing - ${email}`);
console.log(error);
return res.status(401).json({error});
});
return res.status(200).json({ok: true});
}