Updated the Batch Create User to also have an expiry date

This commit is contained in:
Tiago Ribeiro
2024-08-12 19:49:18 +01:00
parent 58300e32ff
commit 8162567e12
3 changed files with 301 additions and 335 deletions

View File

@@ -1,243 +1,232 @@
import Button from "@/components/Low/Button"; import Button from "@/components/Low/Button";
import useUsers from "@/hooks/useUsers"; import useUsers from "@/hooks/useUsers";
import { Type as UserType, User } from "@/interfaces/user"; import {Type as UserType, User} from "@/interfaces/user";
import axios from "axios"; import axios from "axios";
import { uniqBy } from "lodash"; import {uniqBy} from "lodash";
import { useEffect, useState } from "react"; import {useEffect, useState} from "react";
import { toast } from "react-toastify"; import {toast} from "react-toastify";
import { useFilePicker } from "use-file-picker"; import {useFilePicker} from "use-file-picker";
import readXlsxFile from "read-excel-file"; import readXlsxFile from "read-excel-file";
import Modal from "@/components/Modal"; import Modal from "@/components/Modal";
import { BsQuestionCircleFill } from "react-icons/bs"; import {BsQuestionCircleFill} from "react-icons/bs";
import { PermissionType } from "@/interfaces/permissions"; import {PermissionType} from "@/interfaces/permissions";
const EMAIL_REGEX = new RegExp( import moment from "moment";
/^[a-zA-Z0-9]+(?:\.[a-zA-Z0-9]+)*@[a-zA-Z0-9]+(?:\.[a-zA-Z0-9]+)*$/ import {checkAccess} from "@/utils/permissions";
); import Checkbox from "@/components/Low/Checkbox";
import ReactDatePicker from "react-datepicker";
import clsx from "clsx";
const EMAIL_REGEX = new RegExp(/^[a-zA-Z0-9]+(?:\.[a-zA-Z0-9]+)*@[a-zA-Z0-9]+(?:\.[a-zA-Z0-9]+)*$/);
type Type = Exclude<UserType, "admin" | "developer" | "agent" | "mastercorporate"> type Type = Exclude<UserType, "admin" | "developer" | "agent" | "mastercorporate">;
const USER_TYPE_LABELS: {[key in Type]: string} = { const USER_TYPE_LABELS: {[key in Type]: string} = {
student: "Student", student: "Student",
teacher: "Teacher", teacher: "Teacher",
corporate: "Corporate", corporate: "Corporate",
}; };
const USER_TYPE_PERMISSIONS: { const USER_TYPE_PERMISSIONS: {
[key in Type]: { perm: PermissionType | undefined; list: Type[] }; [key in Type]: {perm: PermissionType | undefined; list: Type[]};
} = { } = {
student: { student: {
perm: "createCodeStudent", perm: "createCodeStudent",
list: [], list: [],
}, },
teacher: { teacher: {
perm: "createCodeTeacher", perm: "createCodeTeacher",
list: [], list: [],
}, },
corporate: { corporate: {
perm: "createCodeCorporate", perm: "createCodeCorporate",
list: ["student", "teacher"], list: ["student", "teacher"],
}, },
}; };
export default function BatchCreateUser({ user }: { user: User }) { export default function BatchCreateUser({user}: {user: User}) {
const [infos, setInfos] = useState< const [infos, setInfos] = useState<
{ email: string; name: string; passport_id:string, type: Type, demographicInformation: { {
country: string, email: string;
passport_id:string, name: string;
phone: string passport_id: string;
} }[] type: Type;
>([]); demographicInformation: {
const [isLoading, setIsLoading] = useState(false); country: string;
const [type, setType] = useState<Type>("student"); passport_id: string;
const [showHelp, setShowHelp] = useState(false); phone: string;
};
}[]
>([]);
const [isLoading, setIsLoading] = useState(false);
const [expiryDate, setExpiryDate] = useState<Date | null>(
user?.subscriptionExpirationDate ? moment(user.subscriptionExpirationDate).toDate() : null,
);
const [isExpiryDateEnabled, setIsExpiryDateEnabled] = useState(true);
const [type, setType] = useState<Type>("student");
const [showHelp, setShowHelp] = useState(false);
const { users } = useUsers(); const {users} = useUsers();
const { openFilePicker, filesContent, clear } = useFilePicker({ const {openFilePicker, filesContent, clear} = useFilePicker({
accept: ".xlsx", accept: ".xlsx",
multiple: false, multiple: false,
readAs: "ArrayBuffer", readAs: "ArrayBuffer",
}); });
useEffect(() => {
if (!isExpiryDateEnabled) setExpiryDate(null);
}, [isExpiryDateEnabled]);
useEffect(() => { useEffect(() => {
if (filesContent.length > 0) { if (filesContent.length > 0) {
const file = filesContent[0]; const file = filesContent[0];
readXlsxFile(file.content).then((rows) => { readXlsxFile(file.content).then((rows) => {
try { try {
const information = uniqBy( const information = uniqBy(
rows rows
.map((row) => { .map((row) => {
const [ const [firstName, lastName, country, passport_id, email, phone, group] = row as string[];
firstName, return EMAIL_REGEX.test(email.toString().trim())
lastName, ? {
country, email: email.toString().trim().toLowerCase(),
passport_id, name: `${firstName ?? ""} ${lastName ?? ""}`.trim().toLowerCase(),
email, type: type,
phone, passport_id: passport_id?.toString().trim() || undefined,
group groupName: group,
] = row as string[]; demographicInformation: {
return EMAIL_REGEX.test(email.toString().trim()) country: country,
? { passport_id: passport_id?.toString().trim() || undefined,
email: email.toString().trim().toLowerCase(), phone,
name: `${firstName ?? ""} ${lastName ?? ""}`.trim().toLowerCase(), },
type: type, }
passport_id: passport_id?.toString().trim() || undefined, : undefined;
groupName: group, })
demographicInformation: { .filter((x) => !!x) as typeof infos,
country: country, (x) => x.email,
passport_id: passport_id?.toString().trim() || undefined, );
phone,
}
}
: undefined;
})
.filter((x) => !!x) as typeof infos,
(x) => x.email
);
if (information.length === 0) { if (information.length === 0) {
toast.error( toast.error(
"Please upload an Excel file containing user information, one per line! All already registered e-mails have also been ignored!" "Please upload an Excel file containing user information, one per line! All already registered e-mails have also been ignored!",
); );
return clear(); return clear();
} }
setInfos(information); setInfos(information);
} catch { } catch {
toast.error( toast.error(
"Please upload an Excel file containing user information, one per line! All already registered e-mails have also been ignored!" "Please upload an Excel file containing user information, one per line! All already registered e-mails have also been ignored!",
); );
return clear(); return clear();
} }
}); });
} }
// eslint-disable-next-line react-hooks/exhaustive-deps // eslint-disable-next-line react-hooks/exhaustive-deps
}, [filesContent]); }, [filesContent]);
const makeUsers = async () => { const makeUsers = async () => {
const newUsers = infos.filter( const newUsers = infos.filter((x) => !users.map((u) => u.email).includes(x.email));
(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;
const confirmed = confirm( if (newUsers.length > 0) {
`You are about to add ${newUsers.length}, are you sure you want to continue?` setIsLoading(true);
) Promise.all(
if (!confirmed) newUsers.map(async (user) => {
return; await axios.post("/api/make_user", user);
if (newUsers.length > 0) }),
{ )
setIsLoading(true); .then((res) => {
Promise.all(newUsers.map(async (user) => { toast.success(`Successfully added ${newUsers.length} user(s)!`);
await axios.post("/api/make_user", user) })
})).then((res) =>{ .finally(() => {
toast.success( return clear();
`Successfully added ${newUsers.length} user(s)!` });
)}).finally(() => { }
return clear(); setIsLoading(false);
}) setInfos([]);
} };
setIsLoading(false);
setInfos([]);
};
return (
return ( <>
<> <Modal isOpen={showHelp} onClose={() => setShowHelp(false)} title="Excel File Format">
<Modal <div className="mt-4 flex flex-col gap-2">
isOpen={showHelp} <span>Please upload an Excel file with the following format:</span>
onClose={() => setShowHelp(false)} <table className="w-full">
title="Excel File Format" <thead>
> <tr>
<div className="mt-4 flex flex-col gap-2"> <th className="border border-neutral-200 px-2 py-1">First Name</th>
<span>Please upload an Excel file with the following format:</span> <th className="border border-neutral-200 px-2 py-1">Last Name</th>
<table className="w-full"> <th className="border border-neutral-200 px-2 py-1">Country</th>
<thead> <th className="border border-neutral-200 px-2 py-1">Passport/National ID</th>
<tr> <th className="border border-neutral-200 px-2 py-1">E-mail</th>
<th className="border border-neutral-200 px-2 py-1"> <th className="border border-neutral-200 px-2 py-1">Phone Number</th>
First Name <th className="border border-neutral-200 px-2 py-1">Group Name</th>
</th> </tr>
<th className="border border-neutral-200 px-2 py-1"> </thead>
Last Name </table>
</th> <span className="mt-4">
<th className="border border-neutral-200 px-2 py-1">Country</th> <b>Notes:</b>
<th className="border border-neutral-200 px-2 py-1"> <ul>
Passport/National ID <li>- All incorrect e-mails will be ignored;</li>
</th> <li>- All already registered e-mails will be ignored;</li>
<th className="border border-neutral-200 px-2 py-1">E-mail</th> <li>- You may have a header row with the format above, however, it is not necessary;</li>
<th className="border border-neutral-200 px-2 py-1"> <li>- All of the e-mails in the file will receive an e-mail to join EnCoach with the role selected below.</li>
Phone Number </ul>
</th> </span>
<th className="border border-neutral-200 px-2 py-1"> </div>
Group Name </Modal>
</th> <div className="border-mti-gray-platinum flex flex-col gap-4 rounded-xl border p-4">
</tr> <div className="flex items-end justify-between">
</thead> <label className="text-mti-gray-dim text-base font-normal">Choose an Excel file</label>
</table> <div className="tooltip cursor-pointer" data-tip="Excel File Format" onClick={() => setShowHelp(true)}>
<span className="mt-4"> <BsQuestionCircleFill />
<b>Notes:</b> </div>
<ul> </div>
<li>- All incorrect e-mails will be ignored;</li> <Button onClick={openFilePicker} isLoading={isLoading} disabled={isLoading}>
<li>- All already registered e-mails will be ignored;</li> {filesContent.length > 0 ? filesContent[0].name : "Choose a file"}
<li> </Button>
- You may have a header row with the format above, however, it {user && checkAccess(user, ["developer", "admin", "corporate", "mastercorporate"]) && (
is not necessary; <>
</li> <div className="-md:flex-row -md:items-center flex justify-between gap-2 md:flex-col 2xl:flex-row 2xl:items-center">
<li> <label className="text-mti-gray-dim text-base font-normal">Expiry Date</label>
- All of the e-mails in the file will receive an e-mail to join <Checkbox isChecked={isExpiryDateEnabled} onChange={setIsExpiryDateEnabled} disabled={!!user.subscriptionExpirationDate}>
EnCoach with the role selected below. Enabled
</li> </Checkbox>
</ul> </div>
</span> {isExpiryDateEnabled && (
</div> <ReactDatePicker
</Modal> className={clsx(
<div className="border-mti-gray-platinum flex flex-col gap-4 rounded-xl border p-4"> "flex min-h-[70px] w-full cursor-pointer justify-center rounded-full border p-6 text-sm font-normal focus:outline-none",
<div className="flex items-end justify-between"> "hover:border-mti-purple tooltip",
<label className="text-mti-gray-dim text-base font-normal"> "transition duration-300 ease-in-out",
Choose an Excel file )}
</label> filterDate={(date) =>
<div moment(date).isAfter(new Date()) &&
className="tooltip cursor-pointer" (user.subscriptionExpirationDate ? moment(date).isBefore(user.subscriptionExpirationDate) : true)
data-tip="Excel File Format" }
onClick={() => setShowHelp(true)} dateFormat="dd/MM/yyyy"
> selected={expiryDate}
<BsQuestionCircleFill /> onChange={(date) => setExpiryDate(date)}
</div> />
</div> )}
<Button </>
onClick={openFilePicker} )}
isLoading={isLoading} <label className="text-mti-gray-dim text-base font-normal">Select the type of user they should be</label>
disabled={isLoading} {user && (
> <select
{filesContent.length > 0 ? filesContent[0].name : "Choose a file"} defaultValue="student"
</Button> onChange={(e) => setType(e.target.value as Type)}
<label className="text-mti-gray-dim text-base font-normal"> className="flex min-h-[70px] w-full min-w-[350px] cursor-pointer justify-center rounded-full border bg-white p-6 text-sm font-normal focus:outline-none">
Select the type of user they should be {Object.keys(USER_TYPE_LABELS).map((type) => (
</label> <option key={type} value={type}>
{user && ( {USER_TYPE_LABELS[type as keyof typeof USER_TYPE_LABELS]}
<select </option>
defaultValue="student" ))}
onChange={(e) => setType(e.target.value as Type)} </select>
className="flex min-h-[70px] w-full min-w-[350px] cursor-pointer justify-center rounded-full border bg-white p-6 text-sm font-normal focus:outline-none" )}
> <Button className="my-auto" onClick={makeUsers} disabled={infos.length === 0}>
Create
{Object.keys(USER_TYPE_LABELS) </Button>
.map((type) => ( </div>
<option key={type} value={type}> </>
{USER_TYPE_LABELS[type as keyof typeof USER_TYPE_LABELS]} );
</option>
))}
</select>
)}
<Button
className="my-auto"
onClick={makeUsers}
disabled={
infos.length === 0
}
>
Create
</Button>
</div>
</>
);
} }

View File

@@ -1,20 +1,8 @@
import type { NextApiRequest, NextApiResponse } from "next"; import type {NextApiRequest, NextApiResponse} from "next";
import { app } from "@/firebase"; import {app} from "@/firebase";
import { import {getFirestore, setDoc, doc, query, collection, where, getDocs, getDoc, deleteDoc, limit, updateDoc} from "firebase/firestore";
getFirestore, import {withIronSessionApiRoute} from "iron-session/next";
setDoc, import {sessionOptions} from "@/lib/session";
doc,
query,
collection,
where,
getDocs,
getDoc,
deleteDoc,
limit,
updateDoc,
} from "firebase/firestore";
import { withIronSessionApiRoute } from "iron-session/next";
import { sessionOptions } from "@/lib/session";
import {v4} from "uuid"; import {v4} from "uuid";
import {Group} from "@/interfaces/user"; import {Group} from "@/interfaces/user";
import {createUserWithEmailAndPassword, getAuth} from "firebase/auth"; import {createUserWithEmailAndPassword, getAuth} from "firebase/auth";
@@ -39,114 +27,103 @@ const db = getFirestore(app);
export default withIronSessionApiRoute(handler, sessionOptions); export default withIronSessionApiRoute(handler, sessionOptions);
async function handler(req: NextApiRequest, res: NextApiResponse) { async function handler(req: NextApiRequest, res: NextApiResponse) {
if (req.method === "POST") return post(req, res);
if (req.method === "POST") return post(req, res);
return res.status(404).json({ ok: false }); return res.status(404).json({ok: false});
} }
async function post(req: NextApiRequest, res: NextApiResponse) { async function post(req: NextApiRequest, res: NextApiResponse) {
const maker = req.session.user; const maker = req.session.user;
if (!maker) { if (!maker) {
return res return res.status(401).json({ok: false, reason: "You must be logged in to make user!"});
.status(401) }
.json({ ok: false, reason: "You must be logged in to make user!" }); const {email, passport_id, type, groupName, expiryDate} = req.body as {
} email: string;
const { email, passport_id, type, groupName } = req.body as { passport_id: string;
email: string; type: string;
passport_id: string; groupName: string;
type: string, expiryDate: null | Date;
groupName: string };
}; // cleaning data
// cleaning data delete req.body.passport_id;
delete req.body.passport_id; delete req.body.groupName;
delete req.body.groupName;
await createUserWithEmailAndPassword(auth, email.toLowerCase(), passport_id) await createUserWithEmailAndPassword(auth, email.toLowerCase(), passport_id)
.then(async (userCredentials) => { .then(async (userCredentials) => {
const userId = userCredentials.user.uid; const userId = userCredentials.user.uid;
const user = { const user = {
...req.body, ...req.body,
bio: "", bio: "",
type: type, type: type,
focus: "academic", focus: "academic",
status: "paymentDue", status: "paymentDue",
desiredLevels: DEFAULT_DESIRED_LEVELS, desiredLevels: DEFAULT_DESIRED_LEVELS,
levels: DEFAULT_LEVELS, levels: DEFAULT_LEVELS,
isFirstLogin: false, isFirstLogin: false,
isVerified: true isVerified: true,
}; subscriptionExpirationDate: expiryDate || null,
await setDoc(doc(db, "users", userId), user); };
if (type === "corporate") { await setDoc(doc(db, "users", userId), user);
const defaultTeachersGroup: Group = { if (type === "corporate") {
admin: userId, const defaultTeachersGroup: Group = {
id: v4(), admin: userId,
name: "Teachers", id: v4(),
participants: [], name: "Teachers",
disableEditing: true, participants: [],
}; disableEditing: true,
};
const defaultStudentsGroup: Group = { const defaultStudentsGroup: Group = {
admin: userId, admin: userId,
id: v4(), id: v4(),
name: "Students", name: "Students",
participants: [], participants: [],
disableEditing: true, disableEditing: true,
}; };
const defaultCorporateGroup: Group = { const defaultCorporateGroup: Group = {
admin: userId, admin: userId,
id: v4(), id: v4(),
name: "Corporate", name: "Corporate",
participants: [], participants: [],
disableEditing: true, disableEditing: true,
}; };
await setDoc(doc(db, "groups", defaultTeachersGroup.id), defaultTeachersGroup);
await setDoc(doc(db, "groups", defaultStudentsGroup.id), defaultStudentsGroup);
await setDoc(doc(db, "groups", defaultCorporateGroup.id), defaultCorporateGroup);
}
await setDoc(doc(db, "groups", defaultTeachersGroup.id), defaultTeachersGroup); if (typeof groupName === "string" && groupName.trim().length > 0) {
await setDoc(doc(db, "groups", defaultStudentsGroup.id), defaultStudentsGroup); const q = query(collection(db, "groups"), where("admin", "==", maker.id), where("name", "==", groupName.trim()), limit(1));
await setDoc(doc(db, "groups", defaultCorporateGroup.id), defaultCorporateGroup); const snapshot = await getDocs(q);
}
if(typeof groupName === 'string' && groupName.trim().length > 0){
const q = query(collection(db, "groups"), where("admin", "==", maker.id), where("name", "==", groupName.trim()), limit(1))
const snapshot = await getDocs(q)
if(snapshot.empty){
const values = {
id: v4(),
admin: maker.id,
name: groupName.trim(),
participants: [userId],
disableEditing: false,
}
await setDoc(doc(db, "groups", values.id) , values)
}else{ if (snapshot.empty) {
const values = {
id: v4(),
admin: maker.id,
name: groupName.trim(),
participants: [userId],
disableEditing: false,
};
const doc = snapshot.docs[0] await setDoc(doc(db, "groups", values.id), values);
const participants : string[] = doc.get('participants'); } else {
const doc = snapshot.docs[0];
const participants: string[] = doc.get("participants");
if(!participants.includes(userId)){
updateDoc(doc.ref, {
participants: [...participants, userId]
})
}
}
}
})
.catch((error) => {
console.log(error);
return res.status(401).json({error});
});
return res.status(200).json({ ok: true });
if (!participants.includes(userId)) {
updateDoc(doc.ref, {
participants: [...participants, userId],
});
}
}
}
})
.catch((error) => {
console.log(error);
return res.status(401).json({error});
});
return res.status(200).json({ok: true});
} }

View File

@@ -60,7 +60,7 @@ export default function Admin() {
<ToastContainer /> <ToastContainer />
{user && ( {user && (
<Layout user={user} className="gap-6"> <Layout user={user} className="gap-6">
<section className="w-full flex -md:flex-col -xl:gap-2 gap-8 justify-between"> <section className="w-full grid grid-cols-2 -md:grid-cols-1 gap-8">
<ExamLoader /> <ExamLoader />
<BatchCreateUser user={user} /> <BatchCreateUser user={user} />
{checkAccess(user, getTypesOfUser(["teacher"]), permissions, "viewCodes") && ( {checkAccess(user, getTypesOfUser(["teacher"]), permissions, "viewCodes") && (