Merge branch 'develop'

This commit is contained in:
Tiago Ribeiro
2024-05-05 12:03:36 +01:00
7 changed files with 653 additions and 487 deletions

View File

@@ -1,126 +1,105 @@
import ProgressBar from "@/components/Low/ProgressBar"; import ProgressBar from "@/components/Low/ProgressBar";
import useUsers from "@/hooks/useUsers"; import useUsers from "@/hooks/useUsers";
import { Module } from "@/interfaces"; import {Module} from "@/interfaces";
import { Assignment } from "@/interfaces/results"; import {Assignment} from "@/interfaces/results";
import { calculateBandScore } from "@/utils/score"; import {calculateBandScore} from "@/utils/score";
import clsx from "clsx"; import clsx from "clsx";
import moment from "moment"; import moment from "moment";
import { import {BsBook, BsClipboard, BsHeadphones, BsMegaphone, BsPen} from "react-icons/bs";
BsBook, import {usePDFDownload} from "@/hooks/usePDFDownload";
BsClipboard, import {useAssignmentArchive} from "@/hooks/useAssignmentArchive";
BsHeadphones, import {uniqBy} from "lodash";
BsMegaphone, import {useAssignmentUnarchive} from "@/hooks/useAssignmentUnarchive";
BsPen,
} from "react-icons/bs";
import { usePDFDownload } from "@/hooks/usePDFDownload";
import { useAssignmentArchive } from "@/hooks/useAssignmentArchive";
import { uniqBy } from "lodash";
interface Props { interface Props {
onClick?: () => void; onClick?: () => void;
allowDownload?: boolean; allowDownload?: boolean;
reload?: Function; reload?: Function;
allowArchive?: boolean; allowArchive?: boolean;
allowUnarchive?: boolean;
} }
export default function AssignmentCard({ export default function AssignmentCard({
id, id,
name, name,
assigner, assigner,
startDate, startDate,
endDate, endDate,
assignees, assignees,
results, results,
exams, exams,
archived, archived,
onClick, onClick,
allowDownload, allowDownload,
reload, reload,
allowArchive, allowArchive,
allowUnarchive,
}: Assignment & Props) { }: Assignment & Props) {
const renderPdfIcon = usePDFDownload("assignments"); const renderPdfIcon = usePDFDownload("assignments");
const renderArchiveIcon = useAssignmentArchive(id, reload); const renderArchiveIcon = useAssignmentArchive(id, reload);
const renderUnarchiveIcon = useAssignmentUnarchive(id, reload);
const calculateAverageModuleScore = (module: Module) => { const calculateAverageModuleScore = (module: Module) => {
const resultModuleBandScores = results.map((r) => { const resultModuleBandScores = results.map((r) => {
const moduleStats = r.stats.filter((s) => s.module === module); const moduleStats = r.stats.filter((s) => s.module === module);
const correct = moduleStats.reduce( const correct = moduleStats.reduce((acc, curr) => acc + curr.score.correct, 0);
(acc, curr) => acc + curr.score.correct, const total = moduleStats.reduce((acc, curr) => acc + curr.score.total, 0);
0 return calculateBandScore(correct, total, module, r.type);
); });
const total = moduleStats.reduce(
(acc, curr) => acc + curr.score.total,
0
);
return calculateBandScore(correct, total, module, r.type);
});
return resultModuleBandScores.length === 0 return resultModuleBandScores.length === 0 ? -1 : resultModuleBandScores.reduce((acc, curr) => acc + curr, 0) / results.length;
? -1 };
: resultModuleBandScores.reduce((acc, curr) => acc + curr, 0) /
results.length;
};
return ( return (
<div <div
onClick={onClick} onClick={onClick}
className="border-mti-gray-platinum flex h-fit w-[350px] cursor-pointer flex-col gap-6 rounded-xl border bg-white p-4 transition duration-300 ease-in-out hover:drop-shadow" className="border-mti-gray-platinum flex h-fit w-[350px] cursor-pointer flex-col gap-6 rounded-xl border bg-white p-4 transition duration-300 ease-in-out hover:drop-shadow">
> <div className="flex flex-col gap-3">
<div className="flex flex-col gap-3"> <div className="flex flex-row justify-between">
<div className="flex flex-row justify-between"> <h3 className="text-xl font-semibold">{name}</h3>
<h3 className="text-xl font-semibold">{name}</h3> <div className="flex gap-2">
<div className="flex gap-2"> {allowDownload && renderPdfIcon(id, "text-mti-gray-dim", "text-mti-gray-dim")}
{allowDownload && {allowArchive && !archived && renderArchiveIcon("text-mti-gray-dim", "text-mti-gray-dim")}
renderPdfIcon(id, "text-mti-gray-dim", "text-mti-gray-dim")} {allowUnarchive && archived && renderUnarchiveIcon("text-mti-gray-dim", "text-mti-gray-dim")}
{allowArchive && </div>
!archived && </div>
renderArchiveIcon("text-mti-gray-dim", "text-mti-gray-dim")} <ProgressBar
</div> color={results.length / assignees.length < 0.5 ? "red" : "purple"}
</div> percentage={(results.length / assignees.length) * 100}
<ProgressBar label={`${results.length}/${assignees.length}`}
color={results.length / assignees.length < 0.5 ? "red" : "purple"} className="h-5"
percentage={(results.length / assignees.length) * 100} textClassName={results.length / assignees.length < 0.5 ? "!text-mti-gray-dim font-light" : "text-white"}
label={`${results.length}/${assignees.length}`} />
className="h-5" </div>
textClassName={ <span className="flex justify-between gap-1">
results.length / assignees.length < 0.5 <span>{moment(startDate).format("DD/MM/YY, HH:mm")}</span>
? "!text-mti-gray-dim font-light" <span>-</span>
: "text-white" <span>{moment(endDate).format("DD/MM/YY, HH:mm")}</span>
} </span>
/> <div className="-md:mt-2 grid w-full grid-cols-4 place-items-start gap-2">
</div> {uniqBy(exams, (x) => x.module).map(({module}) => (
<span className="flex justify-between gap-1"> <div
<span>{moment(startDate).format("DD/MM/YY, HH:mm")}</span> key={module}
<span>-</span> className={clsx(
<span>{moment(endDate).format("DD/MM/YY, HH:mm")}</span> "-md:px-4 flex w-fit items-center gap-2 rounded-xl py-2 text-white md:px-2 xl:px-4",
</span> module === "reading" && "bg-ielts-reading",
<div className="-md:mt-2 grid w-full grid-cols-4 place-items-start gap-2"> module === "listening" && "bg-ielts-listening",
{uniqBy(exams, (x) => x.module).map(({ module }) => ( module === "writing" && "bg-ielts-writing",
<div module === "speaking" && "bg-ielts-speaking",
key={module} module === "level" && "bg-ielts-level",
className={clsx( )}>
"-md:px-4 flex w-fit items-center gap-2 rounded-xl py-2 text-white md:px-2 xl:px-4", {module === "reading" && <BsBook className="h-4 w-4" />}
module === "reading" && "bg-ielts-reading", {module === "listening" && <BsHeadphones className="h-4 w-4" />}
module === "listening" && "bg-ielts-listening", {module === "writing" && <BsPen className="h-4 w-4" />}
module === "writing" && "bg-ielts-writing", {module === "speaking" && <BsMegaphone className="h-4 w-4" />}
module === "speaking" && "bg-ielts-speaking", {module === "level" && <BsClipboard className="h-4 w-4" />}
module === "level" && "bg-ielts-level" {calculateAverageModuleScore(module) > -1 && (
)} <span className="text-sm">{calculateAverageModuleScore(module).toFixed(1)}</span>
> )}
{module === "reading" && <BsBook className="h-4 w-4" />} </div>
{module === "listening" && <BsHeadphones className="h-4 w-4" />} ))}
{module === "writing" && <BsPen className="h-4 w-4" />} </div>
{module === "speaking" && <BsMegaphone className="h-4 w-4" />} </div>
{module === "level" && <BsClipboard className="h-4 w-4" />} );
{calculateAverageModuleScore(module) > -1 && (
<span className="text-sm">
{calculateAverageModuleScore(module).toFixed(1)}
</span>
)}
</div>
))}
</div>
</div>
);
} }

View File

@@ -151,8 +151,10 @@ export default function TeacherDashboard({user}: Props) {
}; };
const AssignmentsPage = () => { const AssignmentsPage = () => {
const activeFilter = (a: Assignment) => moment(a.endDate).isAfter(moment()) && moment(a.startDate).isBefore(moment()) && a.assignees.length > a.results.length; const activeFilter = (a: Assignment) =>
moment(a.endDate).isAfter(moment()) && moment(a.startDate).isBefore(moment()) && a.assignees.length > a.results.length;
const pastFilter = (a: Assignment) => (moment(a.endDate).isBefore(moment()) || a.assignees.length === a.results.length) && !a.archived; const pastFilter = (a: Assignment) => (moment(a.endDate).isBefore(moment()) || a.assignees.length === a.results.length) && !a.archived;
const archivedFilter = (a: Assignment) => a.archived;
const futureFilter = (a: Assignment) => moment(a.startDate).isAfter(moment()); const futureFilter = (a: Assignment) => moment(a.startDate).isAfter(moment());
return ( return (
@@ -234,7 +236,29 @@ export default function TeacherDashboard({user}: Props) {
<h2 className="text-2xl font-semibold">Past Assignments ({assignments.filter(pastFilter).length})</h2> <h2 className="text-2xl font-semibold">Past Assignments ({assignments.filter(pastFilter).length})</h2>
<div className="flex flex-wrap gap-2"> <div className="flex flex-wrap gap-2">
{assignments.filter(pastFilter).map((a) => ( {assignments.filter(pastFilter).map((a) => (
<AssignmentCard {...a} onClick={() => setSelectedAssignment(a)} key={a.id} allowDownload reload={reloadAssignments} allowArchive/> <AssignmentCard
{...a}
onClick={() => setSelectedAssignment(a)}
key={a.id}
allowDownload
reload={reloadAssignments}
allowArchive
/>
))}
</div>
</section>
<section className="flex flex-col gap-4">
<h2 className="text-2xl font-semibold">Archived Assignments ({assignments.filter(archivedFilter).length})</h2>
<div className="flex flex-wrap gap-2">
{assignments.filter(archivedFilter).map((a) => (
<AssignmentCard
{...a}
onClick={() => setSelectedAssignment(a)}
key={a.id}
allowDownload
reload={reloadAssignments}
allowUnarchive
/>
))} ))}
</div> </div>
</section> </section>

View File

@@ -1,45 +1,42 @@
import React from "react"; import React from "react";
import axios from "axios"; import axios from "axios";
import { toast } from "react-toastify"; import {toast} from "react-toastify";
import { BsArchive } from "react-icons/bs"; import {BsArchive} from "react-icons/bs";
export const useAssignmentArchive = ( export const useAssignmentArchive = (assignmentId: string, reload?: Function) => {
assignmentId: string, const [loading, setLoading] = React.useState(false);
reload?: Function const archive = () => {
) => { // archive assignment
const [loading, setLoading] = React.useState(false); setLoading(true);
const archive = () => { axios
// archive assignment .post(`/api/assignments/${assignmentId}/archive`)
setLoading(true); .then((res) => {
axios toast.success("Assignment archived!");
.post(`/api/assignments/${assignmentId}/archive`) if (reload) reload();
.then((res) => { setLoading(false);
toast.success("Assignment archived!"); })
if(reload) reload(); .catch((err) => {
setLoading(false); toast.error("Failed to archive the assignment!");
}) setLoading(false);
.catch((err) => { });
toast.error("Failed to archive the assignment!"); };
setLoading(false);
});
};
const renderIcon = (downloadClasses: string, loadingClasses: string) => { const renderIcon = (downloadClasses: string, loadingClasses: string) => {
if (loading) { if (loading) {
return ( return <span className={`${loadingClasses} loading loading-infinity w-6`} />;
<span className={`${loadingClasses} loading loading-infinity w-6`} /> }
); return (
} <div
return ( className="tooltip flex items-center justify-center w-fit h-fit"
<BsArchive data-tip="Archive assignment"
className={`${downloadClasses} text-2xl cursor-pointer`} onClick={(e) => {
onClick={(e) => { e.stopPropagation();
e.stopPropagation(); archive();
archive(); }}>
}} <BsArchive className={`${downloadClasses} text-2xl cursor-pointer tooltip`} />
/> </div>
); );
}; };
return renderIcon; return renderIcon;
}; };

View File

@@ -0,0 +1,42 @@
import React from "react";
import axios from "axios";
import {toast} from "react-toastify";
import {BsArchive, BsFileEarmarkCheck, BsFileEarmarkCheckFill} from "react-icons/bs";
export const useAssignmentUnarchive = (assignmentId: string, reload?: Function) => {
const [loading, setLoading] = React.useState(false);
const archive = () => {
// archive assignment
setLoading(true);
axios
.post(`/api/assignments/${assignmentId}/unarchive`)
.then((res) => {
toast.success("Assignment unarchived!");
if (reload) reload();
setLoading(false);
})
.catch((err) => {
toast.error("Failed to unarchive the assignment!");
setLoading(false);
});
};
const renderIcon = (downloadClasses: string, loadingClasses: string) => {
if (loading) {
return <span className={`${loadingClasses} loading loading-infinity w-6`} />;
}
return (
<div
className="tooltip flex items-center justify-center w-fit h-fit"
data-tip="Unarchive assignment"
onClick={(e) => {
e.stopPropagation();
archive();
}}>
<BsFileEarmarkCheck className={`${downloadClasses} text-2xl cursor-pointer tooltip`} />
</div>
);
};
return renderIcon;
};

View File

@@ -4,259 +4,319 @@ import Select from "@/components/Low/Select";
import useCodes from "@/hooks/useCodes"; import useCodes from "@/hooks/useCodes";
import useUser from "@/hooks/useUser"; import useUser from "@/hooks/useUser";
import useUsers from "@/hooks/useUsers"; import useUsers from "@/hooks/useUsers";
import {Code, User} from "@/interfaces/user"; import { Code, User } from "@/interfaces/user";
import {USER_TYPE_LABELS} from "@/resources/user"; import { USER_TYPE_LABELS } from "@/resources/user";
import {createColumnHelper, flexRender, getCoreRowModel, useReactTable} from "@tanstack/react-table"; import {
createColumnHelper,
flexRender,
getCoreRowModel,
useReactTable,
} from "@tanstack/react-table";
import axios from "axios"; import axios from "axios";
import moment from "moment"; import moment from "moment";
import {useEffect, useState} from "react"; import { useEffect, useState } from "react";
import {BsTrash} from "react-icons/bs"; import { BsTrash } from "react-icons/bs";
import {toast} from "react-toastify"; import { toast } from "react-toastify";
const columnHelper = createColumnHelper<Code>(); const columnHelper = createColumnHelper<Code>();
const CreatorCell = ({id, users}: {id: string; users: User[]}) => { const CreatorCell = ({ id, users }: { id: string; users: User[] }) => {
const [creatorUser, setCreatorUser] = useState<User>(); const [creatorUser, setCreatorUser] = useState<User>();
useEffect(() => { useEffect(() => {
setCreatorUser(users.find((x) => x.id === id)); setCreatorUser(users.find((x) => x.id === id));
}, [id, users]); }, [id, users]);
return ( return (
<> <>
{(creatorUser?.type === "corporate" ? creatorUser?.corporateInformation?.companyInformation?.name : creatorUser?.name || "N/A") || "N/A"}{" "} {(creatorUser?.type === "corporate"
{creatorUser && `(${USER_TYPE_LABELS[creatorUser.type]})`} ? creatorUser?.corporateInformation?.companyInformation?.name
</> : creatorUser?.name || "N/A") || "N/A"}{" "}
); {creatorUser && `(${USER_TYPE_LABELS[creatorUser.type]})`}
</>
);
}; };
export default function CodeList({user}: {user: User}) { export default function CodeList({ user }: { user: User }) {
const [selectedCodes, setSelectedCodes] = useState<string[]>([]); const [selectedCodes, setSelectedCodes] = useState<string[]>([]);
const [filteredCorporate, setFilteredCorporate] = useState<User | undefined>(user?.type === "corporate" ? user : undefined); const [filteredCorporate, setFilteredCorporate] = useState<User | undefined>(
const [filterAvailability, setFilterAvailability] = useState<"in-use" | "unused">(); user?.type === "corporate" ? user : undefined,
);
const [filterAvailability, setFilterAvailability] = useState<
"in-use" | "unused"
>();
const [filteredCodes, setFilteredCodes] = useState<Code[]>([]); const [filteredCodes, setFilteredCodes] = useState<Code[]>([]);
const {users} = useUsers(); const { users } = useUsers();
const {codes, reload} = useCodes(user?.type === "corporate" ? user?.id : undefined); const { codes, reload } = useCodes(
user?.type === "corporate" ? user?.id : undefined,
);
useEffect(() => { useEffect(() => {
let result = [...codes]; let result = [...codes];
if (filteredCorporate) result = result.filter((x) => x.creator === filteredCorporate.id); if (filteredCorporate)
if (filterAvailability) result = result.filter((x) => (filterAvailability === "in-use" ? !!x.userId : !x.userId)); result = result.filter((x) => x.creator === filteredCorporate.id);
if (filterAvailability)
result = result.filter((x) =>
filterAvailability === "in-use" ? !!x.userId : !x.userId,
);
setFilteredCodes(result); setFilteredCodes(result);
}, [codes, filteredCorporate, filterAvailability]); }, [codes, filteredCorporate, filterAvailability]);
const toggleCode = (id: string) => { const toggleCode = (id: string) => {
setSelectedCodes((prev) => (prev.includes(id) ? prev.filter((x) => x !== id) : [...prev, id])); setSelectedCodes((prev) =>
}; prev.includes(id) ? prev.filter((x) => x !== id) : [...prev, id],
);
};
const toggleAllCodes = (checked: boolean) => { const toggleAllCodes = (checked: boolean) => {
if (checked) return setSelectedCodes(filteredCodes.filter((x) => !x.userId).map((x) => x.code)); if (checked)
return setSelectedCodes(
filteredCodes.filter((x) => !x.userId).map((x) => x.code),
);
return setSelectedCodes([]); return setSelectedCodes([]);
}; };
const deleteCodes = async (codes: string[]) => { const deleteCodes = async (codes: string[]) => {
if (!confirm(`Are you sure you want to delete these ${codes.length} code(s)?`)) return; if (
!confirm(`Are you sure you want to delete these ${codes.length} code(s)?`)
)
return;
const params = new URLSearchParams(); const params = new URLSearchParams();
codes.forEach((code) => params.append("code", code)); codes.forEach((code) => params.append("code", code));
axios axios
.delete(`/api/code?${params.toString()}`) .delete(`/api/code?${params.toString()}`)
.then(() => toast.success(`Deleted the codes!`)) .then(() => {
.catch((reason) => { toast.success(`Deleted the codes!`);
if (reason.response.status === 404) { setSelectedCodes([]);
toast.error("Code not found!"); })
return; .catch((reason) => {
} if (reason.response.status === 404) {
toast.error("Code not found!");
return;
}
if (reason.response.status === 403) { if (reason.response.status === 403) {
toast.error("You do not have permission to delete this code!"); toast.error("You do not have permission to delete this code!");
return; return;
} }
toast.error("Something went wrong, please try again later."); toast.error("Something went wrong, please try again later.");
}) })
.finally(reload); .finally(reload);
}; };
const deleteCode = async (code: Code) => { const deleteCode = async (code: Code) => {
if (!confirm(`Are you sure you want to delete this "${code.code}" code?`)) return; if (!confirm(`Are you sure you want to delete this "${code.code}" code?`))
return;
axios axios
.delete(`/api/code/${code.code}`) .delete(`/api/code/${code.code}`)
.then(() => toast.success(`Deleted the "${code.code}" exam`)) .then(() => toast.success(`Deleted the "${code.code}" exam`))
.catch((reason) => { .catch((reason) => {
if (reason.response.status === 404) { if (reason.response.status === 404) {
toast.error("Code not found!"); toast.error("Code not found!");
return; return;
} }
if (reason.response.status === 403) { if (reason.response.status === 403) {
toast.error("You do not have permission to delete this code!"); toast.error("You do not have permission to delete this code!");
return; return;
} }
toast.error("Something went wrong, please try again later."); toast.error("Something went wrong, please try again later.");
}) })
.finally(reload); .finally(reload);
}; };
const defaultColumns = [ const defaultColumns = [
columnHelper.accessor("code", { columnHelper.accessor("code", {
id: "code", id: "code",
header: () => ( header: () => (
<Checkbox <Checkbox
disabled={filteredCodes.filter((x) => !x.userId).length === 0} disabled={filteredCodes.filter((x) => !x.userId).length === 0}
isChecked={ isChecked={
selectedCodes.length === filteredCodes.filter((x) => !x.userId).length && filteredCodes.filter((x) => !x.userId).length > 0 selectedCodes.length ===
} filteredCodes.filter((x) => !x.userId).length &&
onChange={(checked) => toggleAllCodes(checked)}> filteredCodes.filter((x) => !x.userId).length > 0
{""} }
</Checkbox> onChange={(checked) => toggleAllCodes(checked)}
), >
cell: (info) => {""}
!info.row.original.userId ? ( </Checkbox>
<Checkbox isChecked={selectedCodes.includes(info.getValue())} onChange={() => toggleCode(info.getValue())}> ),
{""} cell: (info) =>
</Checkbox> !info.row.original.userId ? (
) : null, <Checkbox
}), isChecked={selectedCodes.includes(info.getValue())}
columnHelper.accessor("code", { onChange={() => toggleCode(info.getValue())}
header: "Code", >
cell: (info) => info.getValue(), {""}
}), </Checkbox>
columnHelper.accessor("creationDate", { ) : null,
header: "Creation Date", }),
cell: (info) => (info.getValue() ? moment(info.getValue()).format("DD/MM/YYYY") : "N/A"), columnHelper.accessor("code", {
}), header: "Code",
columnHelper.accessor("email", { cell: (info) => info.getValue(),
header: "Invited E-mail", }),
cell: (info) => info.getValue() || "N/A", columnHelper.accessor("creationDate", {
}), header: "Creation Date",
columnHelper.accessor("creator", { cell: (info) =>
header: "Creator", info.getValue() ? moment(info.getValue()).format("DD/MM/YYYY") : "N/A",
cell: (info) => <CreatorCell id={info.getValue()} users={users} />, }),
}), columnHelper.accessor("email", {
columnHelper.accessor("userId", { header: "Invited E-mail",
header: "Availability", cell: (info) => info.getValue() || "N/A",
cell: (info) => }),
info.getValue() ? ( columnHelper.accessor("creator", {
<span className="flex gap-1 items-center text-mti-green"> header: "Creator",
<div className="w-2 h-2 rounded-full bg-mti-green" /> In Use cell: (info) => <CreatorCell id={info.getValue()} users={users} />,
</span> }),
) : ( columnHelper.accessor("userId", {
<span className="flex gap-1 items-center text-mti-red"> header: "Availability",
<div className="w-2 h-2 rounded-full bg-mti-red" /> Unused cell: (info) =>
</span> info.getValue() ? (
), <span className="flex gap-1 items-center text-mti-green">
}), <div className="w-2 h-2 rounded-full bg-mti-green" /> In Use
{ </span>
header: "", ) : (
id: "actions", <span className="flex gap-1 items-center text-mti-red">
cell: ({row}: {row: {original: Code}}) => { <div className="w-2 h-2 rounded-full bg-mti-red" /> Unused
return ( </span>
<div className="flex gap-4"> ),
{!row.original.userId && ( }),
<div data-tip="Delete" className="cursor-pointer tooltip" onClick={() => deleteCode(row.original)}> {
<BsTrash className="hover:text-mti-purple-light transition ease-in-out duration-300" /> header: "",
</div> id: "actions",
)} cell: ({ row }: { row: { original: Code } }) => {
</div> return (
); <div className="flex gap-4">
}, {!row.original.userId && (
}, <div
]; data-tip="Delete"
className="cursor-pointer tooltip"
onClick={() => deleteCode(row.original)}
>
<BsTrash className="hover:text-mti-purple-light transition ease-in-out duration-300" />
</div>
)}
</div>
);
},
},
];
const table = useReactTable({ const table = useReactTable({
data: filteredCodes, data: filteredCodes,
columns: defaultColumns, columns: defaultColumns,
getCoreRowModel: getCoreRowModel(), getCoreRowModel: getCoreRowModel(),
}); });
return ( return (
<> <>
<div className="flex items-center justify-between pb-4 pt-1"> <div className="flex items-center justify-between pb-4 pt-1">
<div className="flex items-center gap-4"> <div className="flex items-center gap-4">
<Select <Select
className="!w-96 !py-1" className="!w-96 !py-1"
disabled={user?.type === "corporate"} disabled={user?.type === "corporate"}
isClearable isClearable
placeholder="Corporate" placeholder="Corporate"
value={ value={
filteredCorporate filteredCorporate
? { ? {
label: `${ label: `${
filteredCorporate.type === "corporate" filteredCorporate.type === "corporate"
? filteredCorporate.corporateInformation?.companyInformation?.name || filteredCorporate.name ? filteredCorporate.corporateInformation
: filteredCorporate.name ?.companyInformation?.name || filteredCorporate.name
} (${USER_TYPE_LABELS[filteredCorporate.type]})`, : filteredCorporate.name
value: filteredCorporate.id, } (${USER_TYPE_LABELS[filteredCorporate.type]})`,
} value: filteredCorporate.id,
: null }
} : null
options={users }
.filter((x) => ["admin", "developer", "corporate"].includes(x.type)) options={users
.map((x) => ({ .filter((x) =>
label: `${x.type === "corporate" ? x.corporateInformation?.companyInformation?.name || x.name : x.name} (${ ["admin", "developer", "corporate"].includes(x.type),
USER_TYPE_LABELS[x.type] )
})`, .map((x) => ({
value: x.id, label: `${x.type === "corporate" ? x.corporateInformation?.companyInformation?.name || x.name : x.name} (${
user: x, USER_TYPE_LABELS[x.type]
}))} })`,
onChange={(value) => setFilteredCorporate(value ? users.find((x) => x.id === value?.value) : undefined)} value: x.id,
/> user: x,
<Select }))}
className="!w-96 !py-1" onChange={(value) =>
placeholder="Availability" setFilteredCorporate(
isClearable value ? users.find((x) => x.id === value?.value) : undefined,
options={[ )
{label: "In Use", value: "in-use"}, }
{label: "Unused", value: "unused"}, />
]} <Select
onChange={(value) => setFilterAvailability(value ? (value.value as typeof filterAvailability) : undefined)} className="!w-96 !py-1"
/> placeholder="Availability"
</div> isClearable
<div className="flex gap-4 items-center"> options={[
<span>{selectedCodes.length} code(s) selected</span> { label: "In Use", value: "in-use" },
<Button { label: "Unused", value: "unused" },
disabled={selectedCodes.length === 0} ]}
variant="outline" onChange={(value) =>
color="red" setFilterAvailability(
className="!py-1 px-10" value ? (value.value as typeof filterAvailability) : undefined,
onClick={() => deleteCodes(selectedCodes)}> )
Delete }
</Button> />
</div> </div>
</div> <div className="flex gap-4 items-center">
<table className="rounded-xl bg-mti-purple-ultralight/40 w-full"> <span>{selectedCodes.length} code(s) selected</span>
<thead> <Button
{table.getHeaderGroups().map((headerGroup) => ( disabled={selectedCodes.length === 0}
<tr key={headerGroup.id}> variant="outline"
{headerGroup.headers.map((header) => ( color="red"
<th className="p-4 text-left" key={header.id}> className="!py-1 px-10"
{header.isPlaceholder ? null : flexRender(header.column.columnDef.header, header.getContext())} onClick={() => deleteCodes(selectedCodes)}
</th> >
))} Delete
</tr> </Button>
))} </div>
</thead> </div>
<tbody className="px-2"> <table className="rounded-xl bg-mti-purple-ultralight/40 w-full">
{table.getRowModel().rows.map((row) => ( <thead>
<tr className="odd:bg-white even:bg-mti-purple-ultralight/40 rounded-lg py-2" key={row.id}> {table.getHeaderGroups().map((headerGroup) => (
{row.getVisibleCells().map((cell) => ( <tr key={headerGroup.id}>
<td className="px-4 py-2" key={cell.id}> {headerGroup.headers.map((header) => (
{flexRender(cell.column.columnDef.cell, cell.getContext())} <th className="p-4 text-left" key={header.id}>
</td> {header.isPlaceholder
))} ? null
</tr> : flexRender(
))} header.column.columnDef.header,
</tbody> header.getContext(),
</table> )}
</> </th>
); ))}
</tr>
))}
</thead>
<tbody className="px-2">
{table.getRowModel().rows.map((row) => (
<tr
className="odd:bg-white even:bg-mti-purple-ultralight/40 rounded-lg py-2"
key={row.id}
>
{row.getVisibleCells().map((cell) => (
<td className="px-4 py-2" key={cell.id}>
{flexRender(cell.column.columnDef.cell, cell.getContext())}
</td>
))}
</tr>
))}
</tbody>
</table>
</>
);
} }

View File

@@ -0,0 +1,33 @@
import type {NextApiRequest, NextApiResponse} from "next";
import {app} from "@/firebase";
import {getFirestore, doc, getDoc, setDoc} from "firebase/firestore";
import {withIronSessionApiRoute} from "iron-session/next";
import {sessionOptions} from "@/lib/session";
const db = getFirestore(app);
export default withIronSessionApiRoute(handler, sessionOptions);
async function post(req: NextApiRequest, res: NextApiResponse) {
// verify if it's a logged user that is trying to archive
if (req.session.user) {
const {id} = req.query as {id: string};
const docSnap = await getDoc(doc(db, "assignments", id));
if (!docSnap.exists()) {
res.status(404).json({ok: false});
return;
}
await setDoc(docSnap.ref, {archived: false}, {merge: true});
res.status(200).json({ok: true});
return;
}
res.status(401).json({ok: false});
}
async function handler(req: NextApiRequest, res: NextApiResponse) {
if (req.method === "POST") return post(req, res);
res.status(404).json({ok: false});
}

View File

@@ -1,143 +1,174 @@
// Next.js API route support: https://nextjs.org/docs/api-routes/introduction // Next.js API route support: https://nextjs.org/docs/api-routes/introduction
import type {NextApiRequest, NextApiResponse} from "next"; import type { NextApiRequest, NextApiResponse } from "next";
import {app} from "@/firebase"; import { app } from "@/firebase";
import {getFirestore, setDoc, doc, query, collection, where, getDocs, getDoc, deleteDoc} from "firebase/firestore"; import {
import {withIronSessionApiRoute} from "iron-session/next"; getFirestore,
import {sessionOptions} from "@/lib/session"; setDoc,
import {Type} from "@/interfaces/user"; doc,
import {PERMISSIONS} from "@/constants/userPermissions"; query,
import {uuidv4} from "@firebase/util"; collection,
import {prepareMailer, prepareMailOptions} from "@/email"; where,
getDocs,
getDoc,
deleteDoc,
} from "firebase/firestore";
import { withIronSessionApiRoute } from "iron-session/next";
import { sessionOptions } from "@/lib/session";
import { Code, Type } from "@/interfaces/user";
import { PERMISSIONS } from "@/constants/userPermissions";
import { uuidv4 } from "@firebase/util";
import { prepareMailer, prepareMailOptions } from "@/email";
const db = getFirestore(app); const db = getFirestore(app);
export default withIronSessionApiRoute(handler, sessionOptions); export default withIronSessionApiRoute(handler, sessionOptions);
async function handler(req: NextApiRequest, res: NextApiResponse) { async function handler(req: NextApiRequest, res: NextApiResponse) {
if (req.method === "GET") return get(req, res); if (req.method === "GET") return get(req, res);
if (req.method === "POST") return post(req, res); if (req.method === "POST") return post(req, res);
if (req.method === "DELETE") return del(req, res); if (req.method === "DELETE") return del(req, res);
return res.status(404).json({ok: false}); return res.status(404).json({ ok: false });
} }
async function get(req: NextApiRequest, res: NextApiResponse) { async function get(req: NextApiRequest, res: NextApiResponse) {
if (!req.session.user) { if (!req.session.user) {
res.status(401).json({ok: false, reason: "You must be logged in to generate a code!"}); res
return; .status(401)
} .json({ ok: false, reason: "You must be logged in to generate a code!" });
return;
}
const {creator} = req.query as {creator?: string}; const { creator } = req.query as { creator?: string };
const q = query(collection(db, "codes"), where("creator", "==", creator || "")); const q = query(
const snapshot = await getDocs(creator ? q : collection(db, "codes")); collection(db, "codes"),
where("creator", "==", creator || ""),
);
const snapshot = await getDocs(creator ? q : collection(db, "codes"));
res.status(200).json(snapshot.docs.map((doc) => doc.data())); res.status(200).json(snapshot.docs.map((doc) => doc.data()));
} }
async function post(req: NextApiRequest, res: NextApiResponse) { async function post(req: NextApiRequest, res: NextApiResponse) {
if (!req.session.user) { if (!req.session.user) {
res.status(401).json({ok: false, reason: "You must be logged in to generate a code!"}); res
return; .status(401)
} .json({ ok: false, reason: "You must be logged in to generate a code!" });
return;
}
const {type, codes, infos, expiryDate} = req.body as { const { type, codes, infos, expiryDate } = req.body as {
type: Type; type: Type;
codes: string[]; codes: string[];
infos?: {email: string; name: string; passport_id?: string}[]; infos?: { email: string; name: string; passport_id?: string }[];
expiryDate: null | Date; expiryDate: null | Date;
}; };
const permission = PERMISSIONS.generateCode[type]; const permission = PERMISSIONS.generateCode[type];
if (!permission.includes(req.session.user.type)) { if (!permission.includes(req.session.user.type)) {
res.status(403).json({ res.status(403).json({
ok: false, ok: false,
reason: "Your account type does not have permissions to generate a code for that type of user!", reason:
}); "Your account type does not have permissions to generate a code for that type of user!",
return; });
} return;
}
if (req.session.user.type === "corporate") { const codesGeneratedByUserSnapshot = await getDocs(
const codesGeneratedByUserSnapshot = await getDocs(query(collection(db, "codes"), where("creator", "==", req.session.user.id))); query(collection(db, "codes"), where("creator", "==", req.session.user.id)),
const totalCodes = codesGeneratedByUserSnapshot.docs.length + codes.length; );
const allowedCodes = req.session.user.corporateInformation?.companyInformation.userAmount || 0; const userCodes = codesGeneratedByUserSnapshot.docs.map((x) => ({
...x.data(),
}));
if (totalCodes > allowedCodes) { if (req.session.user.type === "corporate") {
res.status(403).json({ const totalCodes = codesGeneratedByUserSnapshot.docs.length + codes.length;
ok: false, const allowedCodes =
reason: `You have or would have exceeded your amount of allowed codes, you currently are allowed to generate ${ req.session.user.corporateInformation?.companyInformation.userAmount || 0;
allowedCodes - codesGeneratedByUserSnapshot.docs.length
} codes.`,
});
return;
}
}
const codePromises = codes.map(async (code, index) => { if (totalCodes > allowedCodes) {
const codeRef = doc(db, "codes", code); res.status(403).json({
const codeInformation = { ok: false,
type, reason: `You have or would have exceeded your amount of allowed codes, you currently are allowed to generate ${
code, allowedCodes - codesGeneratedByUserSnapshot.docs.length
creator: req.session.user!.id, } codes.`,
creationDate: new Date().toISOString(), });
expiryDate, return;
}; }
}
if (infos && infos.length > index) { const codePromises = codes.map(async (code, index) => {
const {email, name, passport_id} = infos[index]; const codeRef = doc(db, "codes", code);
let codeInformation = {
type,
code,
creator: req.session.user!.id,
creationDate: new Date().toISOString(),
expiryDate,
};
const transport = prepareMailer(); if (infos && infos.length > index) {
const mailOptions = prepareMailOptions( const { email, name, passport_id } = infos[index];
{ const previousCode = userCodes.find((x) => x.email === email) as Code;
type,
code,
environment: process.env.ENVIRONMENT,
},
[email.toLowerCase().trim()],
"EnCoach Registration",
"main",
);
try { const transport = prepareMailer();
await transport.sendMail(mailOptions); const mailOptions = prepareMailOptions(
await setDoc( {
codeRef, type,
{ code: previousCode ? previousCode.code : code,
...codeInformation, environment: process.env.ENVIRONMENT,
email: email.trim().toLowerCase(), },
name: name.trim(), [email.toLowerCase().trim()],
...(passport_id ? {passport_id: passport_id.trim()} : {}), "EnCoach Registration",
}, "main",
{merge: true}, );
);
return true; try {
} catch (e) { await transport.sendMail(mailOptions);
return false;
}
} else {
await setDoc(codeRef, codeInformation);
}
});
Promise.all(codePromises).then((results) => { if (!previousCode) {
res.status(200).json({ok: true, valid: results.filter((x) => x).length}); await setDoc(
}); codeRef,
{
...codeInformation,
email: email.trim().toLowerCase(),
name: name.trim(),
...(passport_id ? { passport_id: passport_id.trim() } : {}),
},
{ merge: true },
);
}
return true;
} catch (e) {
return false;
}
} else {
await setDoc(codeRef, codeInformation);
}
});
Promise.all(codePromises).then((results) => {
res.status(200).json({ ok: true, valid: results.filter((x) => x).length });
});
} }
async function del(req: NextApiRequest, res: NextApiResponse) { async function del(req: NextApiRequest, res: NextApiResponse) {
if (!req.session.user) { if (!req.session.user) {
res.status(401).json({ok: false, reason: "You must be logged in to generate a code!"}); res
return; .status(401)
} .json({ ok: false, reason: "You must be logged in to generate a code!" });
return;
}
const codes = req.query.code as string[]; const codes = req.query.code as string[];
for (const code of codes) { for (const code of codes) {
const snapshot = await getDoc(doc(db, "codes", code as string)); const snapshot = await getDoc(doc(db, "codes", code as string));
if (!snapshot.exists()) continue; if (!snapshot.exists()) continue;
await deleteDoc(snapshot.ref); await deleteDoc(snapshot.ref);
} }
res.status(200).json({codes}); res.status(200).json({ codes });
} }