import Button from "@/components/Low/Button"; import axios from "axios"; 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 { 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 countryCodes from "country-codes-list"; import { User, Type as UserType } from "@/interfaces/user"; import { Type, UserImport } from "../../../interfaces/IUserImport"; import UserTable from "../../../components/Tables/UserTable"; import { EntityWithRoles } from "@/interfaces/entity"; import Select from "@/components/Low/Select"; import { IoInformationCircleOutline } from "react-icons/io5"; import { FaFileDownload } from "react-icons/fa"; import { HiOutlineDocumentText } from "react-icons/hi"; import UserImportSummary, { ExcelUserDuplicatesMap } from "@/components/ImportSummaries/User"; import { v4 } from "uuid"; const EMAIL_REGEX = new RegExp(/^[a-zA-Z0-9]+(?:\.[a-zA-Z0-9]+)*@[a-zA-Z0-9]+(?:\.[a-zA-Z0-9]+)*$/); 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; permissions: PermissionType[]; entities?: EntityWithRoles[] onFinish: () => void; } export default function BatchCreateUser({ user, entities = [], permissions, onFinish }: Props) { const [infos, setInfos] = useState([]); const [parsedExcel, setParsedExcel] = useState<{ rows?: any[]; errors?: any[] }>({ rows: undefined, errors: undefined }); const [duplicatedUsers, setDuplicatedUsers] = useState([]); const [newUsers, setNewUsers] = useState([]); const [duplicatedRows, setDuplicatedRows] = useState<{ duplicates: ExcelUserDuplicatesMap, count: number }>(); 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<{ id: string | null, label: string | null } | undefined>(() => { if (!entities?.length) { return undefined; } return { id: entities[0].id, label: entities[0].label }; }); const { openFilePicker, filesContent, clear } = useFilePicker({ accept: ".xlsx", multiple: false, readAs: "ArrayBuffer", }); useEffect(() => { if (!isExpiryDateEnabled) setExpiryDate(null); }, [isExpiryDateEnabled]); const schema = { 'First Name': { prop: 'firstName', type: String, required: true, validate: (value: string) => { if (!value || value.trim() === '') { throw new Error('First Name cannot be empty') } return true } }, 'Last Name': { prop: 'lastName', type: String, required: true, validate: (value: string) => { if (!value || value.trim() === '') { throw new Error('Last Name cannot be empty') } return true } }, 'Student ID': { prop: 'studentID', type: String, required: true, validate: (value: string) => { if (!value || value.trim() === '') { throw new Error('Student ID cannot be empty') } return true } }, 'Passport/National ID': { prop: 'passport_id', type: String, required: true, validate: (value: string) => { if (!value || value.trim() === '') { throw new Error('Passport/National ID cannot be empty') } return true } }, 'E-mail': { prop: 'email', required: true, type: (value: any) => { if (!value || value.trim() === '') { throw new Error('Email cannot be empty') } if (!EMAIL_REGEX.test(value.trim())) { throw new Error('Invalid Email') } return value } }, 'Phone Number': { prop: 'phone', type: Number, required: true, validate: (value: number) => { if (value === null || value === undefined || String(value).trim() === '') { throw new Error('Phone Number cannot be empty') } return true } }, 'Classroom Name': { prop: 'group', type: String, required: true, validate: (value: string) => { if (!value || value.trim() === '') { throw new Error('Classroom Name cannot be empty') } return true } }, 'Country': { prop: 'country', type: (value: any) => { if (!value || value.trim() === '') { throw new Error('Country cannot be empty') } const validCountry = countryCodes.findOne("countryCode" as any, value.toUpperCase()) || countryCodes.all().find((x) => x.countryNameEn.toLowerCase() === value.toLowerCase()); if (!validCountry) { throw new Error('Invalid Country/Country Code') } return validCountry; }, required: true } } useEffect(() => { if (filesContent.length > 0) { const file = filesContent[0]; readXlsxFile( file.content, { schema, ignoreEmptyRows: false }) .then((data) => { setParsedExcel(data) }); } // eslint-disable-next-line react-hooks/exhaustive-deps }, [filesContent]); useEffect(() => { if (parsedExcel.rows) { const duplicates: ExcelUserDuplicatesMap = { studentID: new Map(), email: new Map(), passport_id: new Map(), phone: new Map() }; const duplicateValues = new Set(); const duplicateRowIndices = new Set(); const errorRowIndices = new Set( parsedExcel.errors?.map(error => error.row) || [] ); parsedExcel.rows.forEach((row, index) => { if (!errorRowIndices.has(index + 2)) { (Object.keys(duplicates) as Array).forEach(field => { if (row !== null) { const value = row[field]; if (value) { if (!duplicates[field].has(value)) { duplicates[field].set(value, [index + 2]); } else { const existingRows = duplicates[field].get(value); if (existingRows) { existingRows.push(index + 2); duplicateValues.add(value); existingRows.forEach(rowNum => duplicateRowIndices.add(rowNum)); } } } } }); } }); const infos = parsedExcel.rows .map((row, index) => { if (errorRowIndices.has(index + 2) || duplicateRowIndices.has(index + 2) || row === null) { return undefined; } const { firstName, lastName, studentID, passport_id, email, phone, group, country } = row; if (!email || !EMAIL_REGEX.test(email.toString().trim())) { return undefined; } return { email: email.toString().trim().toLowerCase(), name: `${firstName ?? ""} ${lastName ?? ""}`.trim(), type: type, passport_id: passport_id?.toString().trim() || undefined, groupName: group, studentID, entity, demographicInformation: { country: country?.countryCode, passport_id: passport_id?.toString().trim() || undefined, phone: phone.toString(), }, } as UserImport; }) .filter((item): item is UserImport => item !== undefined); setDuplicatedRows({ duplicates, count: duplicateRowIndices.size }); setInfos(infos); setNewUsers([]); setDuplicatedUsers([]); } }, [entity, parsedExcel, type]); useEffect(() => { const crossReferenceEmails = async () => { try { const response = await axios.post("/api/users/controller?op=crossRefEmails", { emails: infos.map((x) => x.email) }); const crossRefEmails = response.data; if (!!crossRefEmails) { const existingEmails = new Set(crossRefEmails.map((x: any) => x.email)); const dupes = infos.filter(info => existingEmails.has(info.email)); const newUsersList = infos .filter(info => !existingEmails.has(info.email)) .map(info => ({ ...info, entityLabels: [entity!.label!] })); setNewUsers(newUsersList); const { data: emailEntityMap } = await axios.post("/api/users/controller?op=getEntities", { emails: dupes.map((x) => x.email) }); const withLabels = dupes.map((u) => ({ ...u, entityLabels: emailEntityMap.find((e: any) => e.email === u.email)?.entityLabels || [] })) setDuplicatedUsers(withLabels); } else { const withLabel = infos.map(info => ({ ...info, entityLabels: [entity!.label!] })); setNewUsers(withLabel); } } catch (error) { toast.error("Something went wrong, please try again later!"); } }; if (infos.length > 0) { crossReferenceEmails(); } }, [infos, entity]); const makeUsers = async () => { const newUsersSentence = newUsers.length > 0 ? `create ${newUsers.length} user(s)` : undefined; const existingUsersSentence = duplicatedUsers.length > 0 ? `invite ${duplicatedUsers.length} registered student(s)` : undefined; if (!confirm(`You are about to ${[newUsersSentence, existingUsersSentence].filter((x) => !!x).join(" and ")}, are you sure you want to continue?`)) return; if (newUsers.length > 0) { setIsLoading(true); try { const withIds = newUsers.map((user) => ({ ...user, type, expiryDate, id: v4() })); await axios.post("/api/batch_users", { users: withIds}); toast.success(`Successfully added ${withIds.length} user(s)!`); Promise.all(withIds.map(async (u) => await axios.post(`/api/invites`, { to: u.id, entity: entity?.id, from: user.id }))) .then(() => toast.success(`Successfully invited ${withIds.length} registered student(s)!`)) .finally(() => { if (withIds.length === 0) setIsLoading(false); }); onFinish(); } catch (e) { console.error(e) toast.error("Something went wrong, please try again later!"); } finally { setIsLoading(false); setInfos([]); clear(); } } else { setIsLoading(false); setInfos([]); clear(); } }; const handleTemplateDownload = () => { const fileName = "UsersTemplate.xlsx"; const url = `https://firebasestorage.googleapis.com/v0/b/encoach-staging.appspot.com/o/import_templates%2F${fileName}?alt=media&token=b771a535-bf95-4060-889c-a086df65d480`; const link = document.createElement('a'); link.href = url; link.download = fileName; document.body.appendChild(link); link.click(); document.body.removeChild(link); }; return ( <> setShowHelp(false)}> <>
Excel File Format

The uploaded document must:

  • be an Excel .xlsx document.
  • only have a single spreadsheet with the following exact same name columns:
    First Name Last Name Student ID Passport/National ID E-mail Phone Number Classroom Name Country

Note that:

  • all incorrect e-mails will be ignored.
  • all already registered e-mails will be ignored.
  • all rows which contain duplicate values in the columns: "Student ID", "Passport/National ID", "E-mail", will be ignored.
  • all of the e-mails in the file will receive an e-mail to join EnCoach with the role selected below.

{`The downloadable template is an example of a file that can be imported. Your document doesn't need to be a carbon copy of the template - it can have different styling but it must adhere to the previous requirements.`}

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) => ( ))} )} {parsedExcel.rows !== undefined && } {newUsers.length !== 0 && (
New Users:
)} {(duplicatedUsers.length !== 0 && newUsers.length === 0) && The imported .csv only contains duplicated users!}
); }