import Button from "@/components/Low/Button"; import useUsers from "@/hooks/useUsers"; 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 readXlsxFile from "read-excel-file"; import Modal from "@/components/Modal"; import {BsQuestionCircleFill} from "react-icons/bs"; import {PermissionType} from "@/interfaces/permissions"; import moment from "moment"; import {checkAccess, getTypesOfUser} from "@/utils/permissions"; import Checkbox from "@/components/Low/Checkbox"; import ReactDatePicker from "react-datepicker"; import clsx from "clsx"; import usePermissions from "@/hooks/usePermissions"; import countryCodes from "country-codes-list"; import { EntityWithRoles } from "@/interfaces/entity"; import Select from "@/components/Low/Select"; const EMAIL_REGEX = new RegExp(/^[a-zA-Z0-9]+(?:\.[a-zA-Z0-9]+)*@[a-zA-Z0-9]+(?:\.[a-zA-Z0-9]+)*$/); type Type = Exclude; const USER_TYPE_LABELS: {[key in Type]: string} = { student: "Student", teacher: "Teacher", corporate: "Corporate", }; const USER_TYPE_PERMISSIONS: { [key in UserType]: {perm: PermissionType | undefined; list: UserType[]}; } = { student: { perm: "createCodeStudent", list: [], }, teacher: { perm: "createCodeTeacher", list: [], }, agent: { perm: "createCodeCountryManager", list: ["student", "teacher", "corporate", "mastercorporate"], }, corporate: { perm: "createCodeCorporate", list: ["student", "teacher"], }, mastercorporate: { perm: undefined, list: ["student", "teacher", "corporate"], }, admin: { perm: "createCodeAdmin", list: ["student", "teacher", "agent", "corporate", "mastercorporate"], }, developer: { perm: undefined, list: ["student", "teacher", "agent", "corporate", "admin", "developer", "mastercorporate"], }, }; interface Props { user: User; users: User[]; permissions: PermissionType[]; entities: EntityWithRoles[] onFinish: () => void; } export default function BatchCreateUser({user, users, entities = [], permissions, onFinish}: Props) { 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 [entity, setEntity] = useState((entities || [])[0]?.id || undefined) 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, studentID, passport_id, email, phone, corporate, group, country] = row as string[]; const countryItem = countryCodes.findOne("countryCode" as any, country.toUpperCase()) || countryCodes.all().find((x) => x.countryNameEn.toLowerCase() === country.toLowerCase()); return EMAIL_REGEX.test(email.toString().trim()) ? { email: email.toString().trim().toLowerCase(), name: `${firstName ?? ""} ${lastName ?? ""}`.trim(), type: type, passport_id: passport_id?.toString().trim() || undefined, groupName: group, corporate, studentID, entity, demographicInformation: { country: countryItem?.countryCode, passport_id: passport_id?.toString().trim() || undefined, phone: phone.toString(), }, } : 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(); } 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)); if (!confirm(`You are about to add ${newUsers.length}, are you sure you want to continue?`)) return; if (newUsers.length > 0) { setIsLoading(true); try { await axios.post("/api/batch_users", {users: newUsers.map((user) => ({...user, type, expiryDate}))}); toast.success(`Successfully added ${newUsers.length} user(s)!`); onFinish(); } catch { toast.error("Something went wrong, please try again later!"); } finally { setIsLoading(false); setInfos([]); clear(); } } }; return ( <> setShowHelp(false)} title="Excel File Format">
Please upload an Excel file with the following format: {user?.type !== "corporate" && }
First Name Last Name Student ID Passport/National ID E-mail Phone NumberCorporate (e-mail)Classroom Name Country
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)}>
setType(e.target.value as Type)} 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"> {Object.keys(USER_TYPE_LABELS) .filter((x) => { const {list, perm} = USER_TYPE_PERMISSIONS[x as Type]; // if (x === "corporate") console.log(list, perm, checkAccess(user, list, permissions, perm)); return checkAccess(user, getTypesOfUser(list), permissions, perm); }) .map((type) => ( ))} )}
); }