import Button from "@/components/Low/Button"; import Checkbox from "@/components/Low/Checkbox"; import { PERMISSIONS } from "@/constants/userPermissions"; import useUsers from "@/hooks/useUsers"; import { Type, User } from "@/interfaces/user"; import { USER_TYPE_LABELS } from "@/resources/user"; import axios from "axios"; import clsx from "clsx"; import { capitalize, uniqBy } from "lodash"; import moment from "moment"; import { useEffect, useState } from "react"; import ReactDatePicker from "react-datepicker"; import { toast } from "react-toastify"; import ShortUniqueId from "short-unique-id"; import { useFilePicker } from "use-file-picker"; import readXlsxFile from "read-excel-file"; import Modal from "@/components/Modal"; import { BsFileEarmarkEaselFill, BsQuestionCircleFill } from "react-icons/bs"; import { checkAccess, getTypesOfUser } from "@/utils/permissions"; import { PermissionType } from "@/interfaces/permissions"; import usePermissions from "@/hooks/usePermissions"; import { EntityWithRoles } from "@/interfaces/entity"; import Select from "@/components/Low/Select"; import CodeGenImportSummary, { ExcelCodegenDuplicatesMap } from "@/components/ImportSummaries/Codegen"; import { FaFileDownload } from "react-icons/fa"; import { IoInformationCircleOutline } from "react-icons/io5"; import { HiOutlineDocumentText } from "react-icons/hi"; import CodegenTable from "@/components/Tables/CodeGenTable"; const EMAIL_REGEX = new RegExp(/^[a-zA-Z0-9]+(?:\.[a-zA-Z0-9]+)*@[a-zA-Z0-9]+(?:\.[a-zA-Z0-9]+)*$/); const USER_TYPE_PERMISSIONS: { [key in Type]: { perm: PermissionType | undefined; list: Type[] }; } = { 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", "admin", "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 BatchCodeGenerator({ user, users, entities = [], permissions, onFinish }: Props) { const [infos, setInfos] = useState<{ email: string; name: string; passport_id: 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 [parsedExcel, setParsedExcel] = useState<{ rows?: any[]; errors?: any[] }>({ rows: undefined, errors: undefined }); const [duplicatedRows, setDuplicatedRows] = useState<{ duplicates: ExcelCodegenDuplicatesMap, count: number }>(); 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 } }, '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 } } } 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: ExcelCodegenDuplicatesMap = { email: new Map(), passport_id: 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 info = 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(), passport_id: passport_id?.toString().trim() || undefined, }; }).filter((x) => !!x) as typeof infos; setInfos(info); } }, [entity, parsedExcel, type]); const generateAndInvite = async () => { const newUsers = infos.filter((x) => !users.map((u) => u.email).includes(x.email)); const existingUsers = infos .filter((x) => users.map((u) => u.email).includes(x.email)) .map((i) => users.find((u) => u.email === i.email)) .filter((x) => !!x && x.type === "student") as User[]; const newUsersSentence = newUsers.length > 0 ? `generate ${newUsers.length} code(s)` : undefined; const existingUsersSentence = existingUsers.length > 0 ? `invite ${existingUsers.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; setIsLoading(true); Promise.all(existingUsers.map(async (u) => await axios.post(`/api/invites`, { to: u.id, from: user.id }))) .then(() => toast.success(`Successfully invited ${existingUsers.length} registered student(s)!`)) .finally(() => { if (newUsers.length === 0) setIsLoading(false); }); if (newUsers.length > 0) generateCode(type, newUsers); setInfos([]); }; const generateCode = (type: Type, informations: typeof infos) => { const uid = new ShortUniqueId(); const codes = informations.map(() => uid.randomUUID(6)); setIsLoading(true); axios .post<{ ok: boolean; valid?: number; reason?: string }>("/api/code", { type, codes, infos: informations.map((info, index) => ({ ...info, code: codes[index] })), expiryDate, entity }) .then(({ data, status }) => { if (data.ok) { toast.success( `Successfully generated${data.valid ? ` ${data.valid}/${informations.length}` : ""} ${capitalize( type, )} codes and they have been notified by e-mail!`, { toastId: "success" }, ); onFinish(); return; } if (status === 403) { toast.error(data.reason, { toastId: "forbidden" }); } }) .catch(({ response: { status, data } }) => { if (status === 403) { toast.error(data.reason, { toastId: "forbidden" }); return; } toast.error(`Something went wrong, please try again later!`, { toastId: "error", }); }) .finally(() => { setIsLoading(false); return clear(); }); }; const handleTemplateDownload = () => { const fileName = "BatchCodeTemplate.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 Passport/National ID E-mail

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: "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.`}

{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)} /> )} )}
setType(e.target.value as typeof user.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]; return checkAccess(user, getTypesOfUser(list), permissions, perm); }) .map((type) => ( ))} )} {infos.length > 0 && } {infos.length !== 0 && (
Codes will be sent to:
)} {checkAccess(user, ["developer", "admin", "corporate", "mastercorporate"], permissions, "createCodes") && ( )}
); }