Merged develop into ENCOA-82_Permissions

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

View File

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