Merge branch 'develop' of bitbucket.org:ecropdev/ielts-ui into develop

This commit is contained in:
Tiago Ribeiro
2024-12-23 09:36:19 +00:00
31 changed files with 2514 additions and 195 deletions

View File

@@ -21,6 +21,11 @@ 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]+)*$/);
@@ -74,7 +79,9 @@ export default function BatchCodeGenerator({ user, users, entities = [], permiss
const [isExpiryDateEnabled, setIsExpiryDateEnabled] = useState(true);
const [type, setType] = useState<Type>("student");
const [showHelp, setShowHelp] = useState(false);
const [entity, setEntity] = useState((entities || [])[0]?.id || undefined)
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",
@@ -86,46 +93,123 @@ export default function BatchCodeGenerator({ user, users, entities = [], permiss
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).then((rows) => {
try {
const information = uniqBy(
rows
.map((row) => {
const [firstName, lastName, country, passport_id, email, phone] = row as string[];
return EMAIL_REGEX.test(email.toString().trim())
? {
email: email.toString().trim().toLowerCase(),
name: `${firstName ?? ""} ${lastName ?? ""}`.trim(),
passport_id: passport_id?.toString().trim() || undefined,
}
: 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();
}
});
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<string>();
const duplicateRowIndices = new Set<number>();
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<keyof ExcelCodegenDuplicatesMap>).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
@@ -199,40 +283,106 @@ export default function BatchCodeGenerator({ user, users, entities = [], permiss
});
};
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 (
<>
<Modal isOpen={showHelp} onClose={() => setShowHelp(false)} title="Excel File Format">
<div className="mt-4 flex flex-col gap-2">
<span>Please upload an Excel file with the following format:</span>
<table className="w-full">
<thead>
<tr>
<th className="border border-neutral-200 px-2 py-1">First Name</th>
<th className="border border-neutral-200 px-2 py-1">Last Name</th>
<th className="border border-neutral-200 px-2 py-1">Country</th>
<th className="border border-neutral-200 px-2 py-1">Passport/National ID</th>
<th className="border border-neutral-200 px-2 py-1">E-mail</th>
<th className="border border-neutral-200 px-2 py-1">Phone Number</th>
</tr>
</thead>
</table>
<span className="mt-4">
<b>Notes:</b>
<ul>
<li>- All incorrect e-mails will be ignored;</li>
<li>- All already registered e-mails will be ignored;</li>
<li>- You may have a header row with the format above, however, it is not necessary;</li>
<li>- All of the e-mails in the file will receive an e-mail to join EnCoach with the role selected below.</li>
</ul>
</span>
</div>
<Modal isOpen={showHelp} onClose={() => setShowHelp(false)}>
<>
<div className="flex font-bold text-xl justify-center text-gray-700"><span>Excel File Format</span></div>
<div className="mt-4 flex flex-col gap-4">
<div className="flex flex-col gap-3 bg-gray-100 rounded-lg p-4">
<div className="flex items-center gap-2">
<HiOutlineDocumentText className={`w-5 h-5 text-mti-purple-light`} />
<h2 className="text-lg font-semibold">
The uploaded document must:
</h2>
</div>
<ul className="flex flex-col pl-10 gap-2">
<li className="text-gray-700 list-disc">
be an Excel .xlsx document.
</li>
<li className="text-gray-700 list-disc">
only have a single spreadsheet with the following <b>exact same name</b> columns:
<div className="py-4 pr-4">
<table className="w-full bg-white">
<thead>
<tr>
<th className="border border-neutral-200 px-2 py-1">First Name</th>
<th className="border border-neutral-200 px-2 py-1">Last Name</th>
<th className="border border-neutral-200 px-2 py-1">Passport/National ID</th>
<th className="border border-neutral-200 px-2 py-1">E-mail</th>
</tr>
</thead>
</table>
</div>
</li>
</ul>
</div>
<div className="flex flex-col gap-3 bg-gray-100 rounded-lg p-4">
<div className="flex items-center gap-2">
<IoInformationCircleOutline className={`w-5 h-5 text-mti-purple-light`} />
<h2 className="text-lg font-semibold">
Note that:
</h2>
</div>
<ul className="flex flex-col pl-10 gap-2">
<li className="text-gray-700 list-disc">
all incorrect e-mails will be ignored.
</li>
<li className="text-gray-700 list-disc">
all already registered e-mails will be ignored.
</li>
<li className="text-gray-700 list-disc">
all rows which contain duplicate values in the columns: &quot;Passport/National ID&quot;, &quot;E-mail&quot;, will be ignored.
</li>
<li className="text-gray-700 list-disc">
all of the e-mails in the file will receive an e-mail to join EnCoach with the role selected below.
</li>
</ul>
</div>
<div className="bg-gray-100 rounded-lg p-4">
<p className="text-gray-600">
{`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.`}
</p>
</div>
<div className="w-full flex justify-between mt-6 gap-8">
<Button color="purple" onClick={() => setShowHelp(false)} variant="outline" className="self-end w-full bg-white">
Close
</Button>
<Button color="purple" onClick={handleTemplateDownload} variant="solid" className="self-end w-full">
<div className="flex items-center gap-2">
<FaFileDownload size={24} />
Download Template
</div>
</Button>
</div>
</div>
</>
</Modal>
<div className="border-mti-gray-platinum flex flex-col gap-4 rounded-xl border p-4">
<div className="flex items-end justify-between">
<label className="text-mti-gray-dim text-base font-normal">Choose an Excel file</label>
<div className="tooltip cursor-pointer" data-tip="Excel File Format" onClick={() => setShowHelp(true)}>
<BsQuestionCircleFill />
</div>
<button
onClick={() => setShowHelp(true)}
className="tooltip cursor-pointer p-1.5 hover:bg-gray-200 rounded-full transition-colors duration-200"
data-tip="Excel File Format"
>
<IoInformationCircleOutline size={24} />
</button>
</div>
<Button onClick={openFilePicker} isLoading={isLoading} disabled={isLoading}>
{filesContent.length > 0 ? filesContent[0].name : "Choose a file"}
@@ -290,6 +440,13 @@ export default function BatchCodeGenerator({ user, users, entities = [], permiss
))}
</select>
)}
{infos.length > 0 && <CodeGenImportSummary infos={infos} parsedExcel={parsedExcel} duplicateRows={duplicatedRows}/>}
{infos.length !== 0 && (
<div className="flex w-full flex-col gap-4">
<span className="text-mti-gray-dim text-base font-normal">Codes will be sent to:</span>
<CodegenTable infos={infos} />
</div>
)}
{checkAccess(user, ["developer", "admin", "corporate", "mastercorporate"], permissions, "createCodes") && (
<Button onClick={generateAndInvite} disabled={infos.length === 0 || (isExpiryDateEnabled ? !expiryDate : false)}>
Generate & Send

View File

@@ -12,16 +12,16 @@ import { checkAccess, getTypesOfUser } from "@/utils/permissions";
import Checkbox from "@/components/Low/Checkbox";
import ReactDatePicker from "react-datepicker";
import clsx from "clsx";
import countryCodes, { CountryData } from "country-codes-list";
import countryCodes from "country-codes-list";
import { User, Type as UserType } from "@/interfaces/user";
import { Type, UserImport } from "../../../interfaces/IUserImport";
import UserTable from "../../../components/UserTable";
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, { DuplicatesMap } from "@/components/UserImportSummary";
import UserImportSummary, { ExcelUserDuplicatesMap } from "@/components/ImportSummaries/User";
const EMAIL_REGEX = new RegExp(/^[a-zA-Z0-9]+(?:\.[a-zA-Z0-9]+)*@[a-zA-Z0-9]+(?:\.[a-zA-Z0-9]+)*$/);
@@ -78,7 +78,7 @@ export default function BatchCreateUser({ user, entities = [], permissions, onFi
const [duplicatedUsers, setDuplicatedUsers] = useState<UserImport[]>([]);
const [newUsers, setNewUsers] = useState<UserImport[]>([]);
const [duplicatedRows, setDuplicatedRows] = useState<{ duplicates: DuplicatesMap, count: number }>();
const [duplicatedRows, setDuplicatedRows] = useState<{ duplicates: ExcelUserDuplicatesMap, count: number }>();
const [isLoading, setIsLoading] = useState(false);
const [expiryDate, setExpiryDate] = useState<Date | null>(
@@ -210,7 +210,7 @@ export default function BatchCreateUser({ user, entities = [], permissions, onFi
useEffect(() => {
if (parsedExcel.rows) {
const duplicates: DuplicatesMap = {
const duplicates: ExcelUserDuplicatesMap = {
studentID: new Map(),
email: new Map(),
passport_id: new Map(),
@@ -225,7 +225,7 @@ export default function BatchCreateUser({ user, entities = [], permissions, onFi
parsedExcel.rows.forEach((row, index) => {
if (!errorRowIndices.has(index + 2)) {
(Object.keys(duplicates) as Array<keyof DuplicatesMap>).forEach(field => {
(Object.keys(duplicates) as Array<keyof ExcelUserDuplicatesMap>).forEach(field => {
if (row !== null) {
const value = row[field];
if (value) {

View File

@@ -0,0 +1,74 @@
import { sessionOptions } from '@/lib/session';
import { withIronSessionApiRoute } from 'iron-session/next';
import type { NextApiRequest, NextApiResponse } from 'next'
import client from "@/lib/mongodb";
const db = client.db(process.env.MONGODB_DB);
export default withIronSessionApiRoute(handler, sessionOptions);
async function handler(req: NextApiRequest, res: NextApiResponse) {
const { op } = req.query
if (req.method === 'GET') {
switch (op) {
default:
res.status(400).json({ error: 'Invalid operation!' })
}
}
else if (req.method === 'POST') {
switch (op) {
case 'crossRefOwnership':
res.status(200).json(await crossRefOwnership(req.body));
break;
case 'getIds':
res.status(200).json(await getIds(req.body));
break;
default:
res.status(400).json({ error: 'Invalid operation!' })
}
} else {
res.status(400).end(`Method ${req.method} Not Allowed`)
}
}
async function crossRefOwnership(body: any): Promise<string[]> {
const { userId, classrooms } = body;
// First find which classrooms from input exist
const existingClassrooms = await db.collection('groups')
.find({ name: { $in: classrooms } })
.project({ name: 1, admin: 1, _id: 0 })
.toArray();
// From those existing classrooms, return the ones where user is NOT the admin
return existingClassrooms
.filter(classroom => classroom.admin !== userId)
.map(classroom => classroom.name);
}
async function getIds(body: any): Promise<Record<string, string>> {
const { names, userEmails } = body;
const existingGroups: any[] = await db.collection('groups')
.find({ name: { $in: names } })
.project({ name: 1, id: 1, _id: 0 })
.toArray();
const users: any[] = await db.collection('users')
.find({ email: { $in: userEmails } })
.project({ email: 1, id: 1, _id: 0 })
.toArray();
return {
groups: existingGroups.reduce((acc, group) => {
acc[group.name] = group.id;
return acc;
}, {} as Record<string, string>),
users: users.reduce((acc, user) => {
acc[user.email] = user.id;
return acc;
}, {} as Record<string, string>)
};
}

View File

@@ -0,0 +1,67 @@
// Next.js API route support: https://nextjs.org/docs/api-routes/introduction
import type { NextApiRequest, NextApiResponse } from "next";
import { withIronSessionApiRoute } from "iron-session/next";
import { sessionOptions } from "@/lib/session";
import axios from "axios";
import formidable from "formidable-serverless";
import fs from "fs";
import FormData from 'form-data';
export default withIronSessionApiRoute(handler, sessionOptions);
async function handler(req: NextApiRequest, res: NextApiResponse) {
if (!req.session.user) {
return res.status(401).json({ ok: false });
}
const form = new formidable.IncomingForm();
try {
const files = await new Promise((resolve, reject) => {
form.parse(req, (err: any, _: any, files: any) => {
if (err) reject(err);
else resolve(files);
});
});
const audioFile = (files as any).audio;
if (!audioFile) {
return res.status(400).json({ ok: false, error: 'Audio file not found in request' });
}
const formData = new FormData();
const buffer = fs.readFileSync(audioFile.path);
formData.append('audio', buffer, audioFile.name);
try {
const response = await axios.post(
`${process.env.BACKEND_URL}/listening/transcribe`,
formData,
{
headers: {
...formData.getHeaders(),
Authorization: `Bearer ${process.env.BACKEND_JWT}`,
},
}
);
return res.status(200).json(response.data);
} finally {
if (fs.existsSync(audioFile.path)) {
fs.rmSync(audioFile.path);
}
}
} catch (error) {
console.error('Error:', error);
return res.status(500).json({ ok: false });
}
}
export const config = {
api: {
bodyParser: false,
},
};

View File

@@ -19,11 +19,20 @@ async function handler(req: NextApiRequest, res: NextApiResponse) {
}
else if (req.method === 'POST') {
switch (op) {
case 'crossRefEmails':
res.status(200).json(await crossRefEmails(req.body.emails))
break;
default:
res.status(400).json({ error: 'Invalid operation!' })
case 'crossRefEmails':
res.status(200).json(await crossRefEmails(req.body.emails));
break;
case 'dontExist':
res.status(200).json(await dontExist(req.body.emails))
break;
case 'entityCheck':
res.status(200).json(await entityCheck(req.body));
break;
case 'crossRefClassrooms':
res.status(200).json(await crossRefClassrooms(req.body.sets));
break;
default:
res.status(400).json({ error: 'Invalid operation!' })
}
} else {
res.status(400).end(`Method ${req.method} Not Allowed`)
@@ -44,4 +53,126 @@ async function crossRefEmails(emails: string[]) {
}
}
]).toArray();
}
}
async function dontExist(emails: string[]): Promise<string[]> {
const existingUsers = await db.collection('users')
.find(
{ email: { $in: emails } },
{ projection: { _id: 0, email: 1 } }
)
.toArray();
const existingEmails = new Set(existingUsers.map(u => u.email));
return emails.filter(email => !existingEmails.has(email));
}
async function entityCheck(body: Record<string, any>): Promise<string[]> {
const { entities, emails } = body;
const pipeline = [
// Match users with the provided emails
{
$match: {
email: { $in: emails }
}
},
// Match users who don't have any of the entities
{
$match: {
$or: [
// Either they have no entities array
{ entities: { $exists: false } },
// Or their entities array is empty
{ entities: { $size: 0 } },
// Or none of their entities match the provided IDs
{
entities: {
$not: {
$elemMatch: {
id: { $in: entities.map((e: any) => e.id) }
}
}
}
}
]
}
},
// Project only the email field
{
$project: {
_id: 0,
email: 1
}
}
];
const results = await db.collection('users').aggregate(pipeline).toArray();
return results.map((result: any) => result.email);
}
async function crossRefClassrooms(sets: { email: string, classroom: string }[]) {
const pipeline = [
// Match users with the provided emails
{
$match: {
email: { $in: sets.map(set => set.email) }
}
},
// Lookup groups that contain the user's ID in participants
{
$lookup: {
from: 'groups',
let: { userId: '$id', userEmail: '$email' },
pipeline: [
{
$match: {
$expr: {
$and: [
{ $in: ['$$userId', '$participants'] },
{
// Match the classroom that corresponds to this user's email
$let: {
vars: {
matchingSet: {
$arrayElemAt: [
{
$filter: {
input: sets,
cond: { $eq: ['$$this.email', '$$userEmail'] }
}
},
0
]
}
},
in: { $eq: ['$name', '$$matchingSet.classroom'] }
}
}
]
}
}
}
],
as: 'matchingGroups'
}
},
// Only keep users who have matching groups
{
$match: {
matchingGroups: { $ne: [] }
}
},
// Project only the email
{
$project: {
_id: 0,
email: 1
}
}
];
const results = await db.collection('users').aggregate(pipeline).toArray();
return results.map((result: any) => result.email);
}

View File

@@ -21,6 +21,11 @@ import { getEntities, getEntitiesWithRoles } from "@/utils/entities.be";
import { useAllowedEntities } from "@/hooks/useEntityPermissions";
import { EntityWithRoles } from "@/interfaces/entity";
import { FaPersonChalkboard } from "react-icons/fa6";
import { FaFileUpload } from "react-icons/fa";
import clsx from "clsx";
import { useState } from "react";
import StudentClassroomTransfer from "@/components/Imports/StudentClassroomTransfer";
import Modal from "@/components/Modal";
export const getServerSideProps = withIronSessionSsr(async ({ req, res }) => {
const user = await requestUser(req, res)
@@ -58,7 +63,8 @@ interface Props {
entities: EntityWithRoles[]
}
export default function Home({ user, groups, entities }: Props) {
const entitiesAllowCreate = useAllowedEntities(user, entities, 'create_classroom')
const entitiesAllowCreate = useAllowedEntities(user, entities, 'create_classroom');
const [showImport, setShowImport] = useState(false);
const renderCard = (group: GroupWithUsers) => (
<Link
@@ -112,8 +118,25 @@ export default function Home({ user, groups, entities }: Props) {
<ToastContainer />
<Layout user={user} className="!gap-4">
<section className="flex flex-col gap-4 w-full h-full">
<Modal isOpen={showImport} onClose={() => setShowImport(false)}>
<StudentClassroomTransfer user={user} entities={entities} onFinish={() => setShowImport(false)} />
</Modal>
<div className="flex flex-col gap-4">
<h2 className="font-bold text-2xl">Classrooms</h2>
<div className="flex justify-between">
<h2 className="font-bold text-2xl">Classrooms</h2>
{entitiesAllowCreate.length !== 0 && <button
className={clsx(
"flex flex-row gap-3 items-center py-1.5 px-4 text-lg",
"bg-mti-purple-light border border-mti-purple-light rounded-xl text-white",
"hover:bg-white hover:text-mti-purple-light transition duration-300 ease-in-out",
)}
onClick={() => setShowImport(true)}
>
<FaFileUpload className="w-5 h-5" />
Transfer Students
</button>
}
</div>
<Separator />
</div>

View File

@@ -167,7 +167,6 @@ export default function Generation({ id, user, exam, examModule, permissions }:
defaultValue={title}
required
/>
{/*<MultipleAudioUploader />*/}
<label className="font-normal text-base text-mti-gray-dim">Module</label>
<RadioGroup
value={currentModule}