diff --git a/src/pages/(admin)/BatchCreateUser.tsx b/src/pages/(admin)/BatchCreateUser.tsx index b484fd12..c942a31d 100644 --- a/src/pages/(admin)/BatchCreateUser.tsx +++ b/src/pages/(admin)/BatchCreateUser.tsx @@ -1,243 +1,232 @@ import Button from "@/components/Low/Button"; 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 { uniqBy } from "lodash"; -import { useEffect, useState } from "react"; -import { toast } from "react-toastify"; -import { useFilePicker } from "use-file-picker"; +import {uniqBy} from "lodash"; +import {useEffect, useState} from "react"; +import {toast} from "react-toastify"; +import {useFilePicker} from "use-file-picker"; import readXlsxFile from "read-excel-file"; import Modal from "@/components/Modal"; -import { BsQuestionCircleFill } from "react-icons/bs"; -import { PermissionType } from "@/interfaces/permissions"; -const EMAIL_REGEX = new RegExp( - /^[a-zA-Z0-9]+(?:\.[a-zA-Z0-9]+)*@[a-zA-Z0-9]+(?:\.[a-zA-Z0-9]+)*$/ -); +import {BsQuestionCircleFill} from "react-icons/bs"; +import {PermissionType} from "@/interfaces/permissions"; +import moment from "moment"; +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 +type Type = Exclude; const USER_TYPE_LABELS: {[key in Type]: string} = { - student: "Student", - teacher: "Teacher", - corporate: "Corporate", + student: "Student", + teacher: "Teacher", + corporate: "Corporate", }; const USER_TYPE_PERMISSIONS: { - [key in Type]: { perm: PermissionType | undefined; list: Type[] }; + [key in Type]: {perm: PermissionType | undefined; list: Type[]}; } = { - student: { - perm: "createCodeStudent", - list: [], - }, - teacher: { - perm: "createCodeTeacher", - list: [], - }, - corporate: { - perm: "createCodeCorporate", - list: ["student", "teacher"], - }, + student: { + perm: "createCodeStudent", + list: [], + }, + teacher: { + perm: "createCodeTeacher", + list: [], + }, + corporate: { + perm: "createCodeCorporate", + list: ["student", "teacher"], + }, }; -export default function BatchCreateUser({ user }: { user: User }) { - const [infos, setInfos] = useState< - { email: string; name: string; passport_id:string, type: Type, demographicInformation: { - country: string, - passport_id:string, - phone: string - } }[] - >([]); - const [isLoading, setIsLoading] = useState(false); - const [type, setType] = useState("student"); - const [showHelp, setShowHelp] = useState(false); +export default function BatchCreateUser({user}: {user: User}) { + const [infos, setInfos] = useState< + { + email: string; + name: string; + passport_id: string; + type: Type; + demographicInformation: { + country: string; + passport_id: string; + phone: string; + }; + }[] + >([]); + const [isLoading, setIsLoading] = useState(false); + const [expiryDate, setExpiryDate] = useState( + user?.subscriptionExpirationDate ? moment(user.subscriptionExpirationDate).toDate() : null, + ); + const [isExpiryDateEnabled, setIsExpiryDateEnabled] = useState(true); + const [type, setType] = useState("student"); + const [showHelp, setShowHelp] = useState(false); - const { users } = useUsers(); + const {users} = useUsers(); - const { openFilePicker, filesContent, clear } = useFilePicker({ - accept: ".xlsx", - multiple: false, - readAs: "ArrayBuffer", - }); + const {openFilePicker, filesContent, clear} = useFilePicker({ + accept: ".xlsx", + multiple: false, + readAs: "ArrayBuffer", + }); + useEffect(() => { + if (!isExpiryDateEnabled) setExpiryDate(null); + }, [isExpiryDateEnabled]); - useEffect(() => { - if (filesContent.length > 0) { - const file = filesContent[0]; - readXlsxFile(file.content).then((rows) => { - try { - const information = uniqBy( - rows - .map((row) => { - const [ - firstName, - lastName, - country, - passport_id, - email, - phone, - group - ] = row as string[]; - return EMAIL_REGEX.test(email.toString().trim()) - ? { - email: email.toString().trim().toLowerCase(), - name: `${firstName ?? ""} ${lastName ?? ""}`.trim().toLowerCase(), - type: type, - passport_id: passport_id?.toString().trim() || undefined, - groupName: group, - demographicInformation: { - country: country, - passport_id: passport_id?.toString().trim() || undefined, - phone, - } - } - : undefined; - }) - .filter((x) => !!x) as typeof infos, - (x) => x.email - ); + useEffect(() => { + if (filesContent.length > 0) { + const file = filesContent[0]; + readXlsxFile(file.content).then((rows) => { + try { + const information = uniqBy( + rows + .map((row) => { + const [firstName, lastName, country, passport_id, email, phone, group] = row as string[]; + return EMAIL_REGEX.test(email.toString().trim()) + ? { + email: email.toString().trim().toLowerCase(), + name: `${firstName ?? ""} ${lastName ?? ""}`.trim().toLowerCase(), + type: type, + passport_id: passport_id?.toString().trim() || undefined, + groupName: group, + demographicInformation: { + country: country, + passport_id: passport_id?.toString().trim() || undefined, + phone, + }, + } + : undefined; + }) + .filter((x) => !!x) as typeof infos, + (x) => x.email, + ); - if (information.length === 0) { - toast.error( - "Please upload an Excel file containing user information, one per line! All already registered e-mails have also been ignored!" - ); - return clear(); - } + if (information.length === 0) { + toast.error( + "Please upload an Excel file containing user information, one per line! All already registered e-mails have also been ignored!", + ); + return clear(); + } - setInfos(information); - } catch { - toast.error( - "Please upload an Excel file containing user information, one per line! All already registered e-mails have also been ignored!" - ); - return clear(); - } - }); - } - // eslint-disable-next-line react-hooks/exhaustive-deps - }, [filesContent]); + setInfos(information); + } catch { + toast.error( + "Please upload an Excel file containing user information, one per line! All already registered e-mails have also been ignored!", + ); + return clear(); + } + }); + } + // eslint-disable-next-line react-hooks/exhaustive-deps + }, [filesContent]); - 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 (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(); - }) - } - setIsLoading(false); - setInfos([]); - }; + 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 (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(); + }); + } + setIsLoading(false); + setInfos([]); + }; - - return ( - <> - setShowHelp(false)} - title="Excel File Format" - > -
- Please upload an Excel file with the following format: - - - - - - - - - - - - -
- First Name - - Last Name - Country - Passport/National ID - E-mail - Phone Number - - Group Name -
- - Notes: -
    -
  • - All incorrect e-mails will be ignored;
  • -
  • - All already registered e-mails will be ignored;
  • -
  • - - You may have a header row with the format above, however, it - is not necessary; -
  • -
  • - - All of the e-mails in the file will receive an e-mail to join - EnCoach with the role selected below. -
  • -
-
-
-
-
-
- -
setShowHelp(true)} - > - -
-
- - - {user && ( - - )} - -
- - ); + return ( + <> + setShowHelp(false)} title="Excel File Format"> +
+ Please upload an Excel file with the following format: + + + + + + + + + + + + +
First NameLast NameCountryPassport/National IDE-mailPhone NumberGroup Name
+ + Notes: +
    +
  • - All incorrect e-mails will be ignored;
  • +
  • - All already registered e-mails will be ignored;
  • +
  • - You may have a header row with the format above, however, it is not necessary;
  • +
  • - All of the e-mails in the file will receive an e-mail to join EnCoach with the role selected below.
  • +
+
+
+
+
+
+ +
setShowHelp(true)}> + +
+
+ + {user && checkAccess(user, ["developer", "admin", "corporate", "mastercorporate"]) && ( + <> +
+ + + Enabled + +
+ {isExpiryDateEnabled && ( + + moment(date).isAfter(new Date()) && + (user.subscriptionExpirationDate ? moment(date).isBefore(user.subscriptionExpirationDate) : true) + } + dateFormat="dd/MM/yyyy" + selected={expiryDate} + onChange={(date) => setExpiryDate(date)} + /> + )} + + )} + + {user && ( + + )} + +
+ + ); } diff --git a/src/pages/api/make_user.ts b/src/pages/api/make_user.ts index 1629e184..a3446390 100644 --- a/src/pages/api/make_user.ts +++ b/src/pages/api/make_user.ts @@ -1,20 +1,8 @@ -import type { NextApiRequest, NextApiResponse } from "next"; -import { app } from "@/firebase"; -import { - getFirestore, - setDoc, - doc, - query, - collection, - where, - getDocs, - getDoc, - deleteDoc, - limit, - updateDoc, -} from "firebase/firestore"; -import { withIronSessionApiRoute } from "iron-session/next"; -import { sessionOptions } from "@/lib/session"; +import type {NextApiRequest, NextApiResponse} from "next"; +import {app} from "@/firebase"; +import {getFirestore, setDoc, 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 {Group} from "@/interfaces/user"; import {createUserWithEmailAndPassword, getAuth} from "firebase/auth"; @@ -39,114 +27,103 @@ const db = getFirestore(app); export default withIronSessionApiRoute(handler, sessionOptions); 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) { - const maker = req.session.user; - if (!maker) { - return res - .status(401) - .json({ ok: false, reason: "You must be logged in to make user!" }); - } - const { email, passport_id, type, groupName } = req.body as { - email: string; - passport_id: string; - type: string, - groupName: string - }; - // cleaning data - delete req.body.passport_id; - delete req.body.groupName; + const maker = req.session.user; + if (!maker) { + return res.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; + passport_id: string; + type: string; + groupName: string; + expiryDate: null | Date; + }; + // cleaning data + delete req.body.passport_id; + delete req.body.groupName; - await createUserWithEmailAndPassword(auth, email.toLowerCase(), passport_id) - .then(async (userCredentials) => { - const userId = userCredentials.user.uid; + await createUserWithEmailAndPassword(auth, email.toLowerCase(), passport_id) + .then(async (userCredentials) => { + const userId = userCredentials.user.uid; - const user = { - ...req.body, - bio: "", - type: type, - focus: "academic", - status: "paymentDue", - desiredLevels: DEFAULT_DESIRED_LEVELS, - levels: DEFAULT_LEVELS, - isFirstLogin: false, - isVerified: true - }; - await setDoc(doc(db, "users", userId), user); - if (type === "corporate") { - const defaultTeachersGroup: Group = { - admin: userId, - id: v4(), - name: "Teachers", - participants: [], - disableEditing: true, - }; + const user = { + ...req.body, + bio: "", + type: type, + focus: "academic", + status: "paymentDue", + desiredLevels: DEFAULT_DESIRED_LEVELS, + levels: DEFAULT_LEVELS, + isFirstLogin: false, + isVerified: true, + subscriptionExpirationDate: expiryDate || null, + }; + await setDoc(doc(db, "users", userId), user); + if (type === "corporate") { + const defaultTeachersGroup: Group = { + admin: userId, + id: v4(), + name: "Teachers", + participants: [], + disableEditing: true, + }; - const defaultStudentsGroup: Group = { - admin: userId, - id: v4(), - name: "Students", - participants: [], - disableEditing: true, - }; + const defaultStudentsGroup: Group = { + admin: userId, + id: v4(), + name: "Students", + participants: [], + disableEditing: true, + }; - const defaultCorporateGroup: Group = { - admin: userId, - id: v4(), - name: "Corporate", - participants: [], - disableEditing: true, - }; + const defaultCorporateGroup: Group = { + admin: userId, + id: v4(), + name: "Corporate", + participants: [], + 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); - await setDoc(doc(db, "groups", defaultStudentsGroup.id), defaultStudentsGroup); - await setDoc(doc(db, "groups", defaultCorporateGroup.id), defaultCorporateGroup); - } - - 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) - + 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); - }else{ - + if (snapshot.empty) { + const values = { + id: v4(), + admin: maker.id, + name: groupName.trim(), + participants: [userId], + disableEditing: false, + }; - 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 }); + await setDoc(doc(db, "groups", values.id), values); + } 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}); } diff --git a/src/pages/settings.tsx b/src/pages/settings.tsx index 3016f105..a744b2a0 100644 --- a/src/pages/settings.tsx +++ b/src/pages/settings.tsx @@ -60,7 +60,7 @@ export default function Admin() { {user && ( -
+
{checkAccess(user, getTypesOfUser(["teacher"]), permissions, "viewCodes") && (