Did some fixes related to master corporates
This commit is contained in:
@@ -1,273 +1,272 @@
|
|||||||
import Button from "@/components/Low/Button";
|
import Button from "@/components/Low/Button";
|
||||||
import Checkbox from "@/components/Low/Checkbox";
|
import Checkbox from "@/components/Low/Checkbox";
|
||||||
import { PERMISSIONS } from "@/constants/userPermissions";
|
import {PERMISSIONS} from "@/constants/userPermissions";
|
||||||
import { CorporateUser, TeacherUser, Type, User } from "@/interfaces/user";
|
import {CorporateUser, TeacherUser, Type, User} from "@/interfaces/user";
|
||||||
import { USER_TYPE_LABELS } from "@/resources/user";
|
import {USER_TYPE_LABELS} from "@/resources/user";
|
||||||
import axios from "axios";
|
import axios from "axios";
|
||||||
import clsx from "clsx";
|
import clsx from "clsx";
|
||||||
import { capitalize, uniqBy } from "lodash";
|
import {capitalize, uniqBy} from "lodash";
|
||||||
import moment from "moment";
|
import moment from "moment";
|
||||||
import { useEffect, useState } from "react";
|
import {useEffect, useState} from "react";
|
||||||
import ReactDatePicker from "react-datepicker";
|
import ReactDatePicker from "react-datepicker";
|
||||||
import { toast } from "react-toastify";
|
import {toast} from "react-toastify";
|
||||||
import ShortUniqueId from "short-unique-id";
|
import ShortUniqueId from "short-unique-id";
|
||||||
import { checkAccess, getTypesOfUser } from "@/utils/permissions";
|
import {checkAccess, getTypesOfUser} from "@/utils/permissions";
|
||||||
import { PermissionType } from "@/interfaces/permissions";
|
import {PermissionType} from "@/interfaces/permissions";
|
||||||
import usePermissions from "@/hooks/usePermissions";
|
import usePermissions from "@/hooks/usePermissions";
|
||||||
import Input from "@/components/Low/Input";
|
import Input from "@/components/Low/Input";
|
||||||
import CountrySelect from "@/components/Low/CountrySelect";
|
import CountrySelect from "@/components/Low/CountrySelect";
|
||||||
import useGroups from "@/hooks/useGroups";
|
import useGroups from "@/hooks/useGroups";
|
||||||
import useUsers from "@/hooks/useUsers";
|
import useUsers from "@/hooks/useUsers";
|
||||||
import { getUserName } from "@/utils/users";
|
import {getUserName} from "@/utils/users";
|
||||||
import Select from "@/components/Low/Select";
|
import Select from "@/components/Low/Select";
|
||||||
import { EntityWithRoles } from "@/interfaces/entity";
|
import {EntityWithRoles} from "@/interfaces/entity";
|
||||||
import useEntitiesGroups from "@/hooks/useEntitiesGroups";
|
import useEntitiesGroups from "@/hooks/useEntitiesGroups";
|
||||||
|
import {mapBy} from "@/utils";
|
||||||
|
|
||||||
const USER_TYPE_PERMISSIONS: {
|
const USER_TYPE_PERMISSIONS: {
|
||||||
[key in Type]: { perm: PermissionType | undefined; list: Type[] };
|
[key in Type]: {perm: PermissionType | undefined; list: Type[]};
|
||||||
} = {
|
} = {
|
||||||
student: {
|
student: {
|
||||||
perm: "createCodeStudent",
|
perm: "createCodeStudent",
|
||||||
list: [],
|
list: [],
|
||||||
},
|
},
|
||||||
teacher: {
|
teacher: {
|
||||||
perm: "createCodeTeacher",
|
perm: "createCodeTeacher",
|
||||||
list: [],
|
list: [],
|
||||||
},
|
},
|
||||||
agent: {
|
agent: {
|
||||||
perm: "createCodeCountryManager",
|
perm: "createCodeCountryManager",
|
||||||
list: ["student", "teacher", "corporate", "mastercorporate"],
|
list: ["student", "teacher", "corporate", "mastercorporate"],
|
||||||
},
|
},
|
||||||
corporate: {
|
corporate: {
|
||||||
perm: "createCodeCorporate",
|
perm: "createCodeCorporate",
|
||||||
list: ["student", "teacher"],
|
list: ["student", "teacher"],
|
||||||
},
|
},
|
||||||
mastercorporate: {
|
mastercorporate: {
|
||||||
perm: undefined,
|
perm: undefined,
|
||||||
list: ["student", "teacher", "corporate"],
|
list: ["student", "teacher", "corporate"],
|
||||||
},
|
},
|
||||||
admin: {
|
admin: {
|
||||||
perm: "createCodeAdmin",
|
perm: "createCodeAdmin",
|
||||||
list: ["student", "teacher", "agent", "corporate", "admin", "mastercorporate"],
|
list: ["student", "teacher", "agent", "corporate", "admin", "mastercorporate"],
|
||||||
},
|
},
|
||||||
developer: {
|
developer: {
|
||||||
perm: undefined,
|
perm: undefined,
|
||||||
list: ["student", "teacher", "agent", "corporate", "admin", "developer", "mastercorporate"],
|
list: ["student", "teacher", "agent", "corporate", "admin", "developer", "mastercorporate"],
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
|
|
||||||
interface Props {
|
interface Props {
|
||||||
user: User;
|
user: User;
|
||||||
users: User[];
|
users: User[];
|
||||||
entities: EntityWithRoles[]
|
entities: EntityWithRoles[];
|
||||||
permissions: PermissionType[];
|
permissions: PermissionType[];
|
||||||
onFinish: () => void;
|
onFinish: () => void;
|
||||||
}
|
}
|
||||||
|
|
||||||
export default function UserCreator({ user, users, entities = [], permissions, onFinish }: Props) {
|
export default function UserCreator({user, users, entities = [], permissions, onFinish}: Props) {
|
||||||
const [name, setName] = useState<string>();
|
const [name, setName] = useState<string>();
|
||||||
const [email, setEmail] = useState<string>();
|
const [email, setEmail] = useState<string>();
|
||||||
const [phone, setPhone] = useState<string>();
|
const [phone, setPhone] = useState<string>();
|
||||||
const [passportID, setPassportID] = useState<string>();
|
const [passportID, setPassportID] = useState<string>();
|
||||||
const [studentID, setStudentID] = useState<string>();
|
const [studentID, setStudentID] = useState<string>();
|
||||||
const [country, setCountry] = useState(user?.demographicInformation?.country);
|
const [country, setCountry] = useState(user?.demographicInformation?.country);
|
||||||
const [group, setGroup] = useState<string | null>();
|
const [group, setGroup] = useState<string | null>();
|
||||||
const [password, setPassword] = useState<string>();
|
const [password, setPassword] = useState<string>();
|
||||||
const [confirmPassword, setConfirmPassword] = useState<string>();
|
const [confirmPassword, setConfirmPassword] = useState<string>();
|
||||||
const [expiryDate, setExpiryDate] = useState<Date | null>(
|
const [expiryDate, setExpiryDate] = useState<Date | null>(
|
||||||
user?.subscriptionExpirationDate ? moment(user?.subscriptionExpirationDate).toDate() : null,
|
user?.subscriptionExpirationDate ? moment(user?.subscriptionExpirationDate).toDate() : null,
|
||||||
);
|
);
|
||||||
const [isExpiryDateEnabled, setIsExpiryDateEnabled] = useState(true);
|
const [isExpiryDateEnabled, setIsExpiryDateEnabled] = useState(true);
|
||||||
const [isLoading, setIsLoading] = useState(false);
|
const [isLoading, setIsLoading] = useState(false);
|
||||||
const [type, setType] = useState<Type>("student");
|
const [type, setType] = useState<Type>("student");
|
||||||
const [position, setPosition] = useState<string>();
|
const [position, setPosition] = useState<string>();
|
||||||
const [entity, setEntity] = useState((entities || [])[0]?.id || undefined)
|
const [entity, setEntity] = useState((entities || [])[0]?.id || undefined);
|
||||||
|
|
||||||
const { groups } = useEntitiesGroups();
|
const {groups} = useEntitiesGroups();
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
if (!isExpiryDateEnabled) setExpiryDate(null);
|
if (!isExpiryDateEnabled) setExpiryDate(null);
|
||||||
}, [isExpiryDateEnabled]);
|
}, [isExpiryDateEnabled]);
|
||||||
|
|
||||||
const createUser = () => {
|
const createUser = () => {
|
||||||
if (!name || name.trim().length === 0) return toast.error("Please enter a valid name!");
|
if (!name || name.trim().length === 0) return toast.error("Please enter a valid name!");
|
||||||
if (!email || email.trim().length === 0) return toast.error("Please enter a valid e-mail address!");
|
if (!email || email.trim().length === 0) return toast.error("Please enter a valid e-mail address!");
|
||||||
if (users.map((x) => x.email).includes(email.trim())) return toast.error("That e-mail is already in use!");
|
if (users.map((x) => x.email).includes(email.trim())) return toast.error("That e-mail is already in use!");
|
||||||
if (!password || password.trim().length < 6) return toast.error("Please enter a valid password!");
|
if (!password || password.trim().length < 6) return toast.error("Please enter a valid password!");
|
||||||
if (password !== confirmPassword) return toast.error("The passwords do not match!");
|
if (password !== confirmPassword) return toast.error("The passwords do not match!");
|
||||||
|
|
||||||
setIsLoading(true);
|
setIsLoading(true);
|
||||||
|
|
||||||
const body = {
|
const body = {
|
||||||
name,
|
name,
|
||||||
email,
|
email,
|
||||||
password,
|
password,
|
||||||
groupID: group,
|
groupID: group,
|
||||||
entity,
|
entity,
|
||||||
type,
|
type,
|
||||||
studentID: type === "student" ? studentID : undefined,
|
studentID: type === "student" ? studentID : undefined,
|
||||||
expiryDate,
|
expiryDate,
|
||||||
demographicInformation: {
|
demographicInformation: {
|
||||||
passport_id: type === "student" ? passportID : undefined,
|
passport_id: type === "student" ? passportID : undefined,
|
||||||
phone,
|
phone,
|
||||||
country,
|
country,
|
||||||
position,
|
position,
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
|
|
||||||
axios
|
axios
|
||||||
.post("/api/make_user", body)
|
.post("/api/make_user", body)
|
||||||
.then(() => {
|
.then(() => {
|
||||||
toast.success("That user has been created!");
|
toast.success("That user has been created!");
|
||||||
onFinish();
|
onFinish();
|
||||||
|
|
||||||
setName("");
|
setName("");
|
||||||
setEmail("");
|
setEmail("");
|
||||||
setPhone("");
|
setPhone("");
|
||||||
setPassportID("");
|
setPassportID("");
|
||||||
setStudentID("");
|
setStudentID("");
|
||||||
setCountry(user?.demographicInformation?.country);
|
setCountry(user?.demographicInformation?.country);
|
||||||
setGroup(null);
|
setGroup(null);
|
||||||
setEntity((entities || [])[0]?.id || undefined)
|
setEntity((entities || [])[0]?.id || undefined);
|
||||||
setExpiryDate(user?.subscriptionExpirationDate ? moment(user?.subscriptionExpirationDate).toDate() : null);
|
setExpiryDate(user?.subscriptionExpirationDate ? moment(user?.subscriptionExpirationDate).toDate() : null);
|
||||||
setIsExpiryDateEnabled(true);
|
setIsExpiryDateEnabled(true);
|
||||||
setType("student");
|
setType("student");
|
||||||
setPosition(undefined);
|
setPosition(undefined);
|
||||||
})
|
})
|
||||||
.catch((error) => {
|
.catch((error) => {
|
||||||
const data = error?.response?.data;
|
const data = error?.response?.data;
|
||||||
if (!!data?.message) return toast.error(data.message);
|
if (!!data?.message) return toast.error(data.message);
|
||||||
toast.error("Something went wrong! Please try again later!");
|
toast.error("Something went wrong! Please try again later!");
|
||||||
})
|
})
|
||||||
.finally(() => setIsLoading(false));
|
.finally(() => setIsLoading(false));
|
||||||
};
|
};
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<div className="flex flex-col gap-4 border p-4 border-mti-gray-platinum rounded-xl">
|
<div className="flex flex-col gap-4 border p-4 border-mti-gray-platinum rounded-xl">
|
||||||
<div className="grid grid-cols-2 gap-4">
|
<div className="grid grid-cols-2 gap-4">
|
||||||
<Input required label="Name" value={name} onChange={setName} type="text" name="name" placeholder="Name" />
|
<Input required label="Name" value={name} onChange={setName} type="text" name="name" placeholder="Name" />
|
||||||
<Input label="E-mail" required value={email} onChange={setEmail} type="email" name="email" placeholder="E-mail" />
|
<Input label="E-mail" required value={email} onChange={setEmail} type="email" name="email" placeholder="E-mail" />
|
||||||
|
|
||||||
<Input type="password" name="password" label="Password" value={password} onChange={setPassword} placeholder="Password" required />
|
<Input type="password" name="password" label="Password" value={password} onChange={setPassword} placeholder="Password" required />
|
||||||
<Input
|
<Input
|
||||||
type="password"
|
type="password"
|
||||||
name="confirmPassword"
|
name="confirmPassword"
|
||||||
label="Confirm Password"
|
label="Confirm Password"
|
||||||
value={confirmPassword}
|
value={confirmPassword}
|
||||||
onChange={setConfirmPassword}
|
onChange={setConfirmPassword}
|
||||||
placeholder="ConfirmPassword"
|
placeholder="ConfirmPassword"
|
||||||
required
|
required
|
||||||
/>
|
/>
|
||||||
|
|
||||||
<div className="flex flex-col gap-4">
|
<div className="flex flex-col gap-4">
|
||||||
<label className="font-normal text-base text-mti-gray-dim">Country *</label>
|
<label className="font-normal text-base text-mti-gray-dim">Country *</label>
|
||||||
<CountrySelect value={country} onChange={setCountry} />
|
<CountrySelect value={country} onChange={setCountry} />
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<Input type="tel" name="phone" label="Phone number" value={phone} onChange={setPhone} placeholder="Phone number" required />
|
<Input type="tel" name="phone" label="Phone number" value={phone} onChange={setPhone} placeholder="Phone number" required />
|
||||||
|
|
||||||
{type === "student" && (
|
{type === "student" && (
|
||||||
<>
|
<>
|
||||||
<Input
|
<Input
|
||||||
type="text"
|
type="text"
|
||||||
name="passport_id"
|
name="passport_id"
|
||||||
label="Passport/National ID"
|
label="Passport/National ID"
|
||||||
onChange={setPassportID}
|
onChange={setPassportID}
|
||||||
value={passportID}
|
value={passportID}
|
||||||
placeholder="National ID or Passport number"
|
placeholder="National ID or Passport number"
|
||||||
required
|
required
|
||||||
/>
|
/>
|
||||||
<Input type="text" name="studentID" label="Student ID" onChange={setStudentID} value={studentID} placeholder="Student ID" />
|
<Input type="text" name="studentID" label="Student ID" onChange={setStudentID} value={studentID} placeholder="Student ID" />
|
||||||
</>
|
</>
|
||||||
)}
|
)}
|
||||||
|
|
||||||
<div className={clsx("flex flex-col gap-4")}>
|
<div className={clsx("flex flex-col gap-4")}>
|
||||||
<label className="font-normal text-base text-mti-gray-dim">Entity</label>
|
<label className="font-normal text-base text-mti-gray-dim">Entity</label>
|
||||||
<Select
|
<Select
|
||||||
defaultValue={{ value: (entities || [])[0]?.id, label: (entities || [])[0]?.label }}
|
defaultValue={{value: (entities || [])[0]?.id, label: (entities || [])[0]?.label}}
|
||||||
options={entities.map((e) => ({ value: e.id, label: e.label }))}
|
options={entities.map((e) => ({value: e.id, label: e.label}))}
|
||||||
onChange={(e) => setEntity(e?.value || undefined)}
|
onChange={(e) => setEntity(e?.value || undefined)}
|
||||||
isClearable={checkAccess(user, ["admin", "developer"])}
|
isClearable={checkAccess(user, ["admin", "developer"])}
|
||||||
/>
|
/>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
{["corporate", "mastercorporate"].includes(type) && (
|
{["corporate", "mastercorporate"].includes(type) && (
|
||||||
<Input type="text" name="department" label="Department" onChange={setPosition} value={position} placeholder="Department" />
|
<Input type="text" name="department" label="Department" onChange={setPosition} value={position} placeholder="Department" />
|
||||||
)}
|
)}
|
||||||
|
|
||||||
<div className={clsx("flex flex-col gap-4")}>
|
<div className={clsx("flex flex-col gap-4")}>
|
||||||
<label className="font-normal text-base text-mti-gray-dim">Classroom</label>
|
<label className="font-normal text-base text-mti-gray-dim">Classroom</label>
|
||||||
<Select
|
<Select
|
||||||
options={groups
|
options={groups.filter((x) => x.entity?.id === entity).map((g) => ({value: g.id, label: g.name}))}
|
||||||
.filter((x) => x.entity?.id === entity)
|
onChange={(e) => setGroup(e?.value || undefined)}
|
||||||
.map((g) => ({ value: g.id, label: g.name }))}
|
isClearable
|
||||||
onChange={(e) => setGroup(e?.value || undefined)}
|
/>
|
||||||
isClearable
|
</div>
|
||||||
/>
|
|
||||||
</div>
|
|
||||||
|
|
||||||
<div
|
<div
|
||||||
className={clsx(
|
className={clsx(
|
||||||
"flex flex-col gap-4",
|
"flex flex-col gap-4",
|
||||||
!checkAccess(user, ["developer", "admin", "corporate", "mastercorporate"]) && "col-span-2",
|
!checkAccess(user, ["developer", "admin", "corporate", "mastercorporate"]) && "col-span-2",
|
||||||
)}>
|
)}>
|
||||||
<label className="font-normal text-base text-mti-gray-dim">Type</label>
|
<label className="font-normal text-base text-mti-gray-dim">Type</label>
|
||||||
{user && (
|
{user && (
|
||||||
<select
|
<select
|
||||||
defaultValue="student"
|
defaultValue="student"
|
||||||
value={type}
|
value={type}
|
||||||
onChange={(e) => setType(e.target.value as Type)}
|
onChange={(e) => setType(e.target.value as Type)}
|
||||||
className="p-6 w-full min-w-[350px] min-h-[70px] flex justify-center text-sm font-normal rounded-full border focus:outline-none cursor-pointer bg-white">
|
className="p-6 w-full min-w-[350px] min-h-[70px] flex justify-center text-sm font-normal rounded-full border focus:outline-none cursor-pointer bg-white">
|
||||||
{Object.keys(USER_TYPE_LABELS)
|
{Object.keys(USER_TYPE_LABELS)
|
||||||
.filter((x) => {
|
.filter((x) => {
|
||||||
const { list, perm } = USER_TYPE_PERMISSIONS[x as Type];
|
const {list, perm} = USER_TYPE_PERMISSIONS[x as Type];
|
||||||
return checkAccess(user, getTypesOfUser(list), permissions, perm);
|
return checkAccess(user, getTypesOfUser(list), permissions, perm);
|
||||||
})
|
})
|
||||||
.map((type) => (
|
.map((type) => (
|
||||||
<option key={type} value={type}>
|
<option key={type} value={type}>
|
||||||
{USER_TYPE_LABELS[type as keyof typeof USER_TYPE_LABELS]}
|
{USER_TYPE_LABELS[type as keyof typeof USER_TYPE_LABELS]}
|
||||||
</option>
|
</option>
|
||||||
))}
|
))}
|
||||||
</select>
|
</select>
|
||||||
)}
|
)}
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<div className="flex flex-col gap-4">
|
<div className="flex flex-col gap-4">
|
||||||
{user && checkAccess(user, ["developer", "admin", "corporate", "mastercorporate"]) && (
|
{user && checkAccess(user, ["developer", "admin", "corporate", "mastercorporate"]) && (
|
||||||
<>
|
<>
|
||||||
<div className="-md:flex-row -md:items-center flex justify-between gap-2 md:flex-col 2xl:flex-row 2xl:items-center">
|
<div className="-md:flex-row -md:items-center flex justify-between gap-2 md:flex-col 2xl:flex-row 2xl:items-center">
|
||||||
<label className="text-mti-gray-dim text-base font-normal">Expiry Date</label>
|
<label className="text-mti-gray-dim text-base font-normal">Expiry Date</label>
|
||||||
<Checkbox
|
<Checkbox
|
||||||
isChecked={isExpiryDateEnabled}
|
isChecked={isExpiryDateEnabled}
|
||||||
onChange={setIsExpiryDateEnabled}
|
onChange={setIsExpiryDateEnabled}
|
||||||
disabled={!!user?.subscriptionExpirationDate}>
|
disabled={!!user?.subscriptionExpirationDate}>
|
||||||
Enabled
|
Enabled
|
||||||
</Checkbox>
|
</Checkbox>
|
||||||
</div>
|
</div>
|
||||||
{isExpiryDateEnabled && (
|
{isExpiryDateEnabled && (
|
||||||
<ReactDatePicker
|
<ReactDatePicker
|
||||||
className={clsx(
|
className={clsx(
|
||||||
"flex min-h-[70px] w-full cursor-pointer justify-center rounded-full border p-6 text-sm font-normal focus:outline-none",
|
"flex min-h-[70px] w-full cursor-pointer justify-center rounded-full border p-6 text-sm font-normal focus:outline-none",
|
||||||
"hover:border-mti-purple tooltip",
|
"hover:border-mti-purple tooltip",
|
||||||
"transition duration-300 ease-in-out",
|
"transition duration-300 ease-in-out",
|
||||||
)}
|
)}
|
||||||
filterDate={(date) =>
|
filterDate={(date) =>
|
||||||
moment(date).isAfter(new Date()) &&
|
moment(date).isAfter(new Date()) &&
|
||||||
(user?.subscriptionExpirationDate ? moment(date).isBefore(user?.subscriptionExpirationDate) : true)
|
(user?.subscriptionExpirationDate ? moment(date).isBefore(user?.subscriptionExpirationDate) : true)
|
||||||
}
|
}
|
||||||
dateFormat="dd/MM/yyyy"
|
dateFormat="dd/MM/yyyy"
|
||||||
selected={expiryDate}
|
selected={expiryDate}
|
||||||
onChange={(date) => setExpiryDate(date)}
|
onChange={(date) => setExpiryDate(date)}
|
||||||
/>
|
/>
|
||||||
)}
|
)}
|
||||||
</>
|
</>
|
||||||
)}
|
)}
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<Button onClick={createUser} isLoading={isLoading} disabled={(isExpiryDateEnabled ? !expiryDate : false) || isLoading}>
|
<Button onClick={createUser} isLoading={isLoading} disabled={(isExpiryDateEnabled ? !expiryDate : false) || isLoading}>
|
||||||
Create User
|
Create User
|
||||||
</Button>
|
</Button>
|
||||||
</div>
|
</div>
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,269 +1,216 @@
|
|||||||
/* eslint-disable @next/next/no-img-element */
|
/* eslint-disable @next/next/no-img-element */
|
||||||
import UserDisplayList from "@/components/UserDisplayList";
|
import UserDisplayList from "@/components/UserDisplayList";
|
||||||
import IconCard from "@/components/IconCard";
|
import IconCard from "@/components/IconCard";
|
||||||
import { useAllowedEntities } from "@/hooks/useEntityPermissions";
|
import {useAllowedEntities} from "@/hooks/useEntityPermissions";
|
||||||
import { EntityWithRoles } from "@/interfaces/entity";
|
import {EntityWithRoles} from "@/interfaces/entity";
|
||||||
import { Stat, StudentUser, Type, User } from "@/interfaces/user";
|
import {Stat, StudentUser, Type, User} from "@/interfaces/user";
|
||||||
import { sessionOptions } from "@/lib/session";
|
import {sessionOptions} from "@/lib/session";
|
||||||
import { filterBy, mapBy, redirect, serialize } from "@/utils";
|
import {filterBy, mapBy, redirect, serialize} from "@/utils";
|
||||||
import { requestUser } from "@/utils/api";
|
import {requestUser} from "@/utils/api";
|
||||||
import { countEntitiesAssignments } from "@/utils/assignments.be";
|
import {countEntitiesAssignments} from "@/utils/assignments.be";
|
||||||
import { getEntitiesWithRoles } from "@/utils/entities.be";
|
import {getEntitiesWithRoles} from "@/utils/entities.be";
|
||||||
import { countGroupsByEntities } from "@/utils/groups.be";
|
import {countGroupsByEntities} from "@/utils/groups.be";
|
||||||
import {
|
import {checkAccess, groupAllowedEntitiesByPermissions} from "@/utils/permissions";
|
||||||
checkAccess,
|
import {groupByExam} from "@/utils/stats";
|
||||||
groupAllowedEntitiesByPermissions,
|
import {countAllowedUsers, getUsers} from "@/utils/users.be";
|
||||||
} from "@/utils/permissions";
|
import {clsx} from "clsx";
|
||||||
import { groupByExam } from "@/utils/stats";
|
import {withIronSessionSsr} from "iron-session/next";
|
||||||
import { countAllowedUsers, getUsers } from "@/utils/users.be";
|
|
||||||
import { clsx } from "clsx";
|
|
||||||
import { withIronSessionSsr } from "iron-session/next";
|
|
||||||
import moment from "moment";
|
import moment from "moment";
|
||||||
import Head from "next/head";
|
import Head from "next/head";
|
||||||
import { useRouter } from "next/router";
|
import {useRouter} from "next/router";
|
||||||
import { useMemo } from "react";
|
import {useMemo} from "react";
|
||||||
import {
|
import {BsBank, BsClock, BsEnvelopePaper, BsPencilSquare, BsPeople, BsPeopleFill, BsPersonFill, BsPersonFillGear} from "react-icons/bs";
|
||||||
BsBank,
|
import {ToastContainer} from "react-toastify";
|
||||||
BsClock,
|
import {isAdmin} from "@/utils/users";
|
||||||
BsEnvelopePaper,
|
|
||||||
BsPencilSquare,
|
|
||||||
BsPeople,
|
|
||||||
BsPeopleFill,
|
|
||||||
BsPersonFill,
|
|
||||||
BsPersonFillGear,
|
|
||||||
} from "react-icons/bs";
|
|
||||||
import { ToastContainer } from "react-toastify";
|
|
||||||
import { isAdmin } from "@/utils/users";
|
|
||||||
|
|
||||||
interface Props {
|
interface Props {
|
||||||
user: User;
|
user: User;
|
||||||
students: StudentUser[];
|
students: StudentUser[];
|
||||||
latestStudents: User[];
|
latestStudents: User[];
|
||||||
latestTeachers: User[];
|
latestTeachers: User[];
|
||||||
userCounts: { [key in Type]: number };
|
userCounts: {[key in Type]: number};
|
||||||
entities: EntityWithRoles[];
|
entities: EntityWithRoles[];
|
||||||
assignmentsCount: number;
|
assignmentsCount: number;
|
||||||
stats: Stat[];
|
stats: Stat[];
|
||||||
groupsCount: number;
|
groupsCount: number;
|
||||||
}
|
}
|
||||||
|
|
||||||
export const getServerSideProps = withIronSessionSsr(async ({ req, res }) => {
|
export const getServerSideProps = withIronSessionSsr(async ({req, res}) => {
|
||||||
const user = await requestUser(req, res);
|
const user = await requestUser(req, res);
|
||||||
if (!user || !user.isVerified) return redirect("/login");
|
if (!user || !user.isVerified) return redirect("/login");
|
||||||
|
|
||||||
if (!checkAccess(user, ["admin", "developer", "mastercorporate"]))
|
if (!checkAccess(user, ["admin", "developer", "mastercorporate"])) return redirect("/");
|
||||||
return redirect("/");
|
|
||||||
|
|
||||||
const entityIDS = mapBy(user.entities, "id") || [];
|
const entityIDS = mapBy(user.entities, "id") || [];
|
||||||
const entities = await getEntitiesWithRoles(
|
const entities = await getEntitiesWithRoles(isAdmin(user) ? undefined : entityIDS);
|
||||||
isAdmin(user) ? undefined : entityIDS
|
const {["view_students"]: allowedStudentEntities, ["view_teachers"]: allowedTeacherEntities} = groupAllowedEntitiesByPermissions(user, entities, [
|
||||||
);
|
"view_students",
|
||||||
const {
|
"view_teachers",
|
||||||
["view_students"]: allowedStudentEntities,
|
]);
|
||||||
["view_teachers"]: allowedTeacherEntities,
|
|
||||||
} = groupAllowedEntitiesByPermissions(user, entities, [
|
|
||||||
"view_students",
|
|
||||||
"view_teachers",
|
|
||||||
]);
|
|
||||||
|
|
||||||
const allowedStudentEntitiesIDS = mapBy(allowedStudentEntities, "id");
|
const allowedStudentEntitiesIDS = mapBy(allowedStudentEntities, "id");
|
||||||
|
|
||||||
const entitiesIDS = mapBy(entities, "id") || [];
|
const entitiesIDS = mapBy(entities, "id") || [];
|
||||||
|
|
||||||
const [
|
const [students, latestStudents, latestTeachers, userCounts, assignmentsCount, groupsCount] = await Promise.all([
|
||||||
students,
|
getUsers(
|
||||||
latestStudents,
|
{type: "student", "entities.id": {$in: allowedStudentEntitiesIDS}},
|
||||||
latestTeachers,
|
10,
|
||||||
userCounts,
|
{averageLevel: -1},
|
||||||
assignmentsCount,
|
{_id: 0, id: 1, name: 1, email: 1, profilePicture: 1},
|
||||||
groupsCount,
|
),
|
||||||
] = await Promise.all([
|
getUsers(
|
||||||
getUsers(
|
{type: "student", "entities.id": {$in: allowedStudentEntitiesIDS}},
|
||||||
{ type: "student", "entities.id": { $in: allowedStudentEntitiesIDS } },
|
10,
|
||||||
10,
|
{registrationDate: -1},
|
||||||
{ averageLevel: -1 },
|
{_id: 0, id: 1, name: 1, email: 1, profilePicture: 1},
|
||||||
{ _id: 0, id: 1, name: 1, email: 1, profilePicture: 1 }
|
),
|
||||||
),
|
getUsers(
|
||||||
getUsers(
|
{
|
||||||
{ type: "student", "entities.id": { $in: allowedStudentEntitiesIDS } },
|
type: "teacher",
|
||||||
10,
|
"entities.id": {$in: mapBy(allowedTeacherEntities, "id")},
|
||||||
{ registrationDate: -1 },
|
},
|
||||||
{ _id: 0, id: 1, name: 1, email: 1, profilePicture: 1 }
|
10,
|
||||||
),
|
{registrationDate: -1},
|
||||||
getUsers(
|
{_id: 0, id: 1, name: 1, email: 1, profilePicture: 1},
|
||||||
{
|
),
|
||||||
type: "teacher",
|
countAllowedUsers(user, entities),
|
||||||
"entities.id": { $in: mapBy(allowedTeacherEntities, "id") },
|
countEntitiesAssignments(entitiesIDS, {archived: {$ne: true}}),
|
||||||
},
|
countGroupsByEntities(entitiesIDS),
|
||||||
10,
|
]);
|
||||||
{ registrationDate: -1 },
|
|
||||||
{ _id: 0, id: 1, name: 1, email: 1, profilePicture: 1 }
|
|
||||||
),
|
|
||||||
countAllowedUsers(user, entities),
|
|
||||||
countEntitiesAssignments(entitiesIDS, { archived: { $ne: true } }),
|
|
||||||
countGroupsByEntities(entitiesIDS),
|
|
||||||
]);
|
|
||||||
|
|
||||||
return {
|
return {
|
||||||
props: serialize({
|
props: serialize({
|
||||||
user,
|
user,
|
||||||
students,
|
students,
|
||||||
latestStudents,
|
latestStudents,
|
||||||
latestTeachers,
|
latestTeachers,
|
||||||
userCounts,
|
userCounts,
|
||||||
entities,
|
entities,
|
||||||
assignmentsCount,
|
assignmentsCount,
|
||||||
groupsCount,
|
groupsCount,
|
||||||
}),
|
}),
|
||||||
};
|
};
|
||||||
}, sessionOptions);
|
}, sessionOptions);
|
||||||
|
|
||||||
export default function Dashboard({
|
export default function Dashboard({
|
||||||
user,
|
user,
|
||||||
students,
|
students,
|
||||||
latestStudents,
|
latestStudents,
|
||||||
latestTeachers,
|
latestTeachers,
|
||||||
userCounts,
|
userCounts,
|
||||||
entities,
|
entities,
|
||||||
assignmentsCount,
|
assignmentsCount,
|
||||||
stats = [],
|
stats = [],
|
||||||
groupsCount,
|
groupsCount,
|
||||||
}: Props) {
|
}: Props) {
|
||||||
|
const totalCount = useMemo(() => userCounts.corporate + userCounts.mastercorporate + userCounts.student + userCounts.teacher, [userCounts]);
|
||||||
const totalCount = useMemo(
|
|
||||||
() =>
|
|
||||||
userCounts.corporate +
|
|
||||||
userCounts.mastercorporate +
|
|
||||||
userCounts.student +
|
|
||||||
userCounts.teacher,
|
|
||||||
[userCounts]
|
|
||||||
);
|
|
||||||
|
|
||||||
const totalLicenses = useMemo(
|
const totalLicenses = useMemo(() => entities.reduce((acc, curr) => acc + parseInt(curr.licenses.toString()), 0), [entities]);
|
||||||
() =>
|
|
||||||
entities.reduce(
|
|
||||||
(acc, curr) => acc + parseInt(curr.licenses.toString()),
|
|
||||||
0
|
|
||||||
),
|
|
||||||
[entities]
|
|
||||||
);
|
|
||||||
|
|
||||||
const router = useRouter();
|
const router = useRouter();
|
||||||
|
|
||||||
const allowedEntityStatistics = useAllowedEntities(
|
const allowedEntityStatistics = useAllowedEntities(user, entities, "view_entity_statistics");
|
||||||
user,
|
const allowedStudentPerformance = useAllowedEntities(user, entities, "view_student_performance");
|
||||||
entities,
|
|
||||||
"view_entity_statistics"
|
|
||||||
);
|
|
||||||
const allowedStudentPerformance = useAllowedEntities(
|
|
||||||
user,
|
|
||||||
entities,
|
|
||||||
"view_student_performance"
|
|
||||||
);
|
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<>
|
<>
|
||||||
<Head>
|
<Head>
|
||||||
<title>EnCoach</title>
|
<title>EnCoach</title>
|
||||||
<meta
|
<meta
|
||||||
name="description"
|
name="description"
|
||||||
content="A training platform for the IELTS exam provided by the Muscat Training Institute and developed by eCrop."
|
content="A training platform for the IELTS exam provided by the Muscat Training Institute and developed by eCrop."
|
||||||
/>
|
/>
|
||||||
<meta name="viewport" content="width=device-width, initial-scale=1" />
|
<meta name="viewport" content="width=device-width, initial-scale=1" />
|
||||||
<link rel="icon" href="/favicon.ico" />
|
<link rel="icon" href="/favicon.ico" />
|
||||||
</Head>
|
</Head>
|
||||||
<ToastContainer />
|
<ToastContainer />
|
||||||
<>
|
<>
|
||||||
<section className="grid grid-cols-5 place-items-center -md:grid-cols-2 gap-4 text-center">
|
<section className="grid grid-cols-5 place-items-center -md:grid-cols-2 gap-4 text-center">
|
||||||
<IconCard
|
<IconCard
|
||||||
onClick={() => router.push("/users?type=student")}
|
onClick={() => router.push("/users?type=student")}
|
||||||
Icon={BsPersonFill}
|
Icon={BsPersonFill}
|
||||||
label="Students"
|
label="Students"
|
||||||
value={userCounts.student}
|
value={userCounts.student}
|
||||||
color="purple"
|
color="purple"
|
||||||
/>
|
/>
|
||||||
<IconCard
|
<IconCard
|
||||||
onClick={() => router.push("/users?type=teacher")}
|
onClick={() => router.push("/users?type=teacher")}
|
||||||
Icon={BsPencilSquare}
|
Icon={BsPencilSquare}
|
||||||
label="Teachers"
|
label="Teachers"
|
||||||
value={userCounts.teacher}
|
value={userCounts.teacher}
|
||||||
color="purple"
|
color="purple"
|
||||||
/>
|
/>
|
||||||
<IconCard
|
<IconCard
|
||||||
onClick={() => router.push("/users?type=corporate")}
|
onClick={() => router.push("/users?type=corporate")}
|
||||||
Icon={BsBank}
|
Icon={BsBank}
|
||||||
label="Corporate Accounts"
|
label="Corporate Accounts"
|
||||||
value={userCounts.corporate}
|
value={userCounts.corporate}
|
||||||
color="purple"
|
color="purple"
|
||||||
/>
|
/>
|
||||||
<IconCard
|
<IconCard
|
||||||
Icon={BsPeople}
|
Icon={BsBank}
|
||||||
onClick={() => router.push("/classrooms")}
|
onClick={() => router.push("/users?type=mastercorporate")}
|
||||||
label="Classrooms"
|
label="Master Corporates"
|
||||||
value={groupsCount}
|
value={userCounts.mastercorporate}
|
||||||
color="purple"
|
color="purple"
|
||||||
/>
|
/>
|
||||||
<IconCard
|
<IconCard Icon={BsPeople} onClick={() => router.push("/classrooms")} label="Classrooms" value={groupsCount} color="purple" />
|
||||||
Icon={BsPeopleFill}
|
<IconCard
|
||||||
onClick={() => router.push("/entities")}
|
Icon={BsPeopleFill}
|
||||||
label="Entities"
|
onClick={() => router.push("/entities")}
|
||||||
value={`${entities.length} - ${totalCount}/${totalLicenses}`}
|
label="Entities"
|
||||||
color="purple"
|
value={`${entities.length} - ${totalCount}/${totalLicenses}`}
|
||||||
/>
|
color="purple"
|
||||||
{allowedStudentPerformance.length > 0 && (
|
/>
|
||||||
<IconCard
|
{allowedStudentPerformance.length > 0 && (
|
||||||
Icon={BsPersonFillGear}
|
<IconCard
|
||||||
onClick={() => router.push("/users/performance")}
|
Icon={BsPersonFillGear}
|
||||||
label="Student Performance"
|
onClick={() => router.push("/users/performance")}
|
||||||
value={userCounts.student}
|
label="Student Performance"
|
||||||
color="purple"
|
value={userCounts.student}
|
||||||
/>
|
color="purple"
|
||||||
)}
|
/>
|
||||||
{allowedEntityStatistics.length > 0 && (
|
)}
|
||||||
<IconCard
|
{allowedEntityStatistics.length > 0 && (
|
||||||
Icon={BsPersonFillGear}
|
<IconCard
|
||||||
onClick={() => router.push("/statistical")}
|
Icon={BsPersonFillGear}
|
||||||
label="Entity Statistics"
|
onClick={() => router.push("/statistical")}
|
||||||
value={allowedEntityStatistics.length}
|
label="Entity Statistics"
|
||||||
color="purple"
|
value={allowedEntityStatistics.length}
|
||||||
/>
|
color="purple"
|
||||||
)}
|
/>
|
||||||
<IconCard
|
)}
|
||||||
Icon={BsEnvelopePaper}
|
<IconCard
|
||||||
onClick={() => router.push("/assignments")}
|
Icon={BsEnvelopePaper}
|
||||||
label="Assignments"
|
onClick={() => router.push("/assignments")}
|
||||||
value={assignmentsCount}
|
label="Assignments"
|
||||||
className={clsx(
|
value={assignmentsCount}
|
||||||
allowedEntityStatistics.length === 0 && "col-span-2"
|
className={clsx(allowedEntityStatistics.length === 0 && "col-span-2")}
|
||||||
)}
|
color="purple"
|
||||||
color="purple"
|
/>
|
||||||
/>
|
<IconCard
|
||||||
<IconCard
|
Icon={BsClock}
|
||||||
Icon={BsClock}
|
label="Expiration Date"
|
||||||
label="Expiration Date"
|
value={user.subscriptionExpirationDate ? moment(user.subscriptionExpirationDate).format("DD/MM/yyyy") : "Unlimited"}
|
||||||
value={
|
color="rose"
|
||||||
user.subscriptionExpirationDate
|
/>
|
||||||
? moment(user.subscriptionExpirationDate).format("DD/MM/yyyy")
|
</section>
|
||||||
: "Unlimited"
|
|
||||||
}
|
|
||||||
color="rose"
|
|
||||||
/>
|
|
||||||
</section>
|
|
||||||
|
|
||||||
<section className="grid grid-cols-1 md:grid-cols-2 gap-4 w-full justify-between">
|
<section className="grid grid-cols-1 md:grid-cols-2 gap-4 w-full justify-between">
|
||||||
<UserDisplayList users={latestStudents} title="Latest Students" />
|
<UserDisplayList users={latestStudents} title="Latest Students" />
|
||||||
<UserDisplayList users={latestTeachers} title="Latest Teachers" />
|
<UserDisplayList users={latestTeachers} title="Latest Teachers" />
|
||||||
<UserDisplayList users={students} title="Highest level students" />
|
<UserDisplayList users={students} title="Highest level students" />
|
||||||
<UserDisplayList
|
<UserDisplayList
|
||||||
users={students.sort(
|
users={students.sort(
|
||||||
(a, b) =>
|
(a, b) =>
|
||||||
Object.keys(groupByExam(filterBy(stats, "user", b))).length -
|
Object.keys(groupByExam(filterBy(stats, "user", b))).length -
|
||||||
Object.keys(groupByExam(filterBy(stats, "user", a))).length
|
Object.keys(groupByExam(filterBy(stats, "user", a))).length,
|
||||||
)}
|
)}
|
||||||
title="Highest exam count students"
|
title="Highest exam count students"
|
||||||
/>
|
/>
|
||||||
</section>
|
</section>
|
||||||
</>
|
</>
|
||||||
</>
|
</>
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,268 +1,201 @@
|
|||||||
/* eslint-disable @next/next/no-img-element */
|
/* eslint-disable @next/next/no-img-element */
|
||||||
import Head from "next/head";
|
import Head from "next/head";
|
||||||
import { withIronSessionSsr } from "iron-session/next";
|
import {withIronSessionSsr} from "iron-session/next";
|
||||||
import { sessionOptions } from "@/lib/session";
|
import {sessionOptions} from "@/lib/session";
|
||||||
import { ToastContainer } from "react-toastify";
|
import {ToastContainer} from "react-toastify";
|
||||||
import CodeGenerator from "./(admin)/CodeGenerator";
|
import CodeGenerator from "./(admin)/CodeGenerator";
|
||||||
import ExamLoader from "./(admin)/ExamLoader";
|
import ExamLoader from "./(admin)/ExamLoader";
|
||||||
import Lists from "./(admin)/Lists";
|
import Lists from "./(admin)/Lists";
|
||||||
import BatchCodeGenerator from "./(admin)/BatchCodeGenerator";
|
import BatchCodeGenerator from "./(admin)/BatchCodeGenerator";
|
||||||
import { shouldRedirectHome } from "@/utils/navigation.disabled";
|
import {shouldRedirectHome} from "@/utils/navigation.disabled";
|
||||||
import BatchCreateUser from "./(admin)/Lists/BatchCreateUser";
|
import BatchCreateUser from "./(admin)/Lists/BatchCreateUser";
|
||||||
import { checkAccess, getTypesOfUser } from "@/utils/permissions";
|
import {checkAccess, getTypesOfUser} from "@/utils/permissions";
|
||||||
import { useState } from "react";
|
import {useEffect, useState} from "react";
|
||||||
import Modal from "@/components/Modal";
|
import Modal from "@/components/Modal";
|
||||||
import IconCard from "@/components/IconCard";
|
import IconCard from "@/components/IconCard";
|
||||||
import {
|
import {BsCode, BsCodeSquare, BsGearFill, BsPeopleFill, BsPersonFill} from "react-icons/bs";
|
||||||
BsCode,
|
|
||||||
BsCodeSquare,
|
|
||||||
BsGearFill,
|
|
||||||
BsPeopleFill,
|
|
||||||
BsPersonFill,
|
|
||||||
} from "react-icons/bs";
|
|
||||||
import UserCreator from "./(admin)/UserCreator";
|
import UserCreator from "./(admin)/UserCreator";
|
||||||
import CorporateGradingSystem from "./(admin)/CorporateGradingSystem";
|
import CorporateGradingSystem from "./(admin)/CorporateGradingSystem";
|
||||||
import { CEFR_STEPS } from "@/resources/grading";
|
import {CEFR_STEPS} from "@/resources/grading";
|
||||||
import { User } from "@/interfaces/user";
|
import {User} from "@/interfaces/user";
|
||||||
import { getUserPermissions } from "@/utils/permissions.be";
|
import {getUserPermissions} from "@/utils/permissions.be";
|
||||||
import { PermissionType } from "@/interfaces/permissions";
|
import {PermissionType} from "@/interfaces/permissions";
|
||||||
import { getUsers } from "@/utils/users.be";
|
import {getUsers} from "@/utils/users.be";
|
||||||
import { getEntitiesWithRoles } from "@/utils/entities.be";
|
import {getEntitiesWithRoles} from "@/utils/entities.be";
|
||||||
import { mapBy, serialize, redirect } from "@/utils";
|
import {mapBy, serialize, redirect, filterBy} from "@/utils";
|
||||||
import { EntityWithRoles } from "@/interfaces/entity";
|
import {EntityWithRoles} from "@/interfaces/entity";
|
||||||
import { requestUser } from "@/utils/api";
|
import {requestUser} from "@/utils/api";
|
||||||
import { isAdmin } from "@/utils/users";
|
import {isAdmin} from "@/utils/users";
|
||||||
import {
|
import {getGradingSystemByEntities, getGradingSystemByEntity} from "@/utils/grading.be";
|
||||||
getGradingSystemByEntities,
|
import {Grading} from "@/interfaces";
|
||||||
getGradingSystemByEntity,
|
import {useRouter} from "next/router";
|
||||||
} from "@/utils/grading.be";
|
import {useAllowedEntities} from "@/hooks/useEntityPermissions";
|
||||||
import { Grading } from "@/interfaces";
|
|
||||||
import { useRouter } from "next/router";
|
|
||||||
import { useAllowedEntities } from "@/hooks/useEntityPermissions";
|
|
||||||
|
|
||||||
export const getServerSideProps = withIronSessionSsr(async ({ req, res }) => {
|
export const getServerSideProps = withIronSessionSsr(async ({req, res}) => {
|
||||||
const user = await requestUser(req, res);
|
const user = await requestUser(req, res);
|
||||||
if (!user) return redirect("/login");
|
if (!user) return redirect("/login");
|
||||||
|
|
||||||
if (
|
if (shouldRedirectHome(user) || !checkAccess(user, ["admin", "developer", "corporate", "teacher", "mastercorporate"])) return redirect("/");
|
||||||
shouldRedirectHome(user) ||
|
const [permissions, entities, allUsers] = await Promise.all([
|
||||||
!checkAccess(user, [
|
getUserPermissions(user.id),
|
||||||
"admin",
|
isAdmin(user) ? await getEntitiesWithRoles() : await getEntitiesWithRoles(mapBy(user.entities, "id")),
|
||||||
"developer",
|
getUsers(),
|
||||||
"corporate",
|
]);
|
||||||
"teacher",
|
const gradingSystems = await getGradingSystemByEntities(mapBy(entities, "id"));
|
||||||
"mastercorporate",
|
const entitiesGrading = entities.map(
|
||||||
])
|
(e) =>
|
||||||
)
|
gradingSystems.find((g) => g.entity === e.id) || {
|
||||||
return redirect("/");
|
entity: e.id,
|
||||||
const [permissions, entities, allUsers] = await Promise.all([
|
steps: CEFR_STEPS,
|
||||||
getUserPermissions(user.id),
|
},
|
||||||
isAdmin(user)
|
);
|
||||||
? await getEntitiesWithRoles()
|
|
||||||
: await getEntitiesWithRoles(mapBy(user.entities, "id")),
|
|
||||||
getUsers(),
|
|
||||||
]);
|
|
||||||
const gradingSystems = await getGradingSystemByEntities(
|
|
||||||
mapBy(entities, "id")
|
|
||||||
);
|
|
||||||
const entitiesGrading = entities.map(
|
|
||||||
(e) =>
|
|
||||||
gradingSystems.find((g) => g.entity === e.id) || {
|
|
||||||
entity: e.id,
|
|
||||||
steps: CEFR_STEPS,
|
|
||||||
}
|
|
||||||
);
|
|
||||||
|
|
||||||
return {
|
return {
|
||||||
props: serialize({
|
props: serialize({
|
||||||
user,
|
user,
|
||||||
permissions,
|
permissions,
|
||||||
entities,
|
entities,
|
||||||
allUsers,
|
allUsers,
|
||||||
entitiesGrading,
|
entitiesGrading,
|
||||||
}),
|
}),
|
||||||
};
|
};
|
||||||
}, sessionOptions);
|
}, sessionOptions);
|
||||||
|
|
||||||
interface Props {
|
interface Props {
|
||||||
user: User;
|
user: User;
|
||||||
permissions: PermissionType[];
|
permissions: PermissionType[];
|
||||||
entities: EntityWithRoles[];
|
entities: EntityWithRoles[];
|
||||||
allUsers: User[];
|
allUsers: User[];
|
||||||
entitiesGrading: Grading[];
|
entitiesGrading: Grading[];
|
||||||
}
|
}
|
||||||
|
|
||||||
export default function Admin({
|
export default function Admin({user, entities, permissions, allUsers, entitiesGrading}: Props) {
|
||||||
user,
|
const [modalOpen, setModalOpen] = useState<string>();
|
||||||
entities,
|
const router = useRouter();
|
||||||
permissions,
|
|
||||||
allUsers,
|
|
||||||
entitiesGrading,
|
|
||||||
}: Props) {
|
|
||||||
const [modalOpen, setModalOpen] = useState<string>();
|
|
||||||
const router = useRouter();
|
|
||||||
|
|
||||||
const entitiesAllowCreateUser = useAllowedEntities(
|
const entitiesAllowCreateUser = useAllowedEntities(user, entities, "create_user");
|
||||||
user,
|
const entitiesAllowCreateUsers = useAllowedEntities(user, entities, "create_user_batch");
|
||||||
entities,
|
const entitiesAllowCreateCode = useAllowedEntities(user, entities, "create_code");
|
||||||
"create_user"
|
const entitiesAllowCreateCodes = useAllowedEntities(user, entities, "create_code_batch");
|
||||||
);
|
const entitiesAllowEditGrading = useAllowedEntities(user, entities, "edit_grading_system");
|
||||||
const entitiesAllowCreateUsers = useAllowedEntities(
|
|
||||||
user,
|
|
||||||
entities,
|
|
||||||
"create_user_batch"
|
|
||||||
);
|
|
||||||
const entitiesAllowCreateCode = useAllowedEntities(
|
|
||||||
user,
|
|
||||||
entities,
|
|
||||||
"create_code"
|
|
||||||
);
|
|
||||||
const entitiesAllowCreateCodes = useAllowedEntities(
|
|
||||||
user,
|
|
||||||
entities,
|
|
||||||
"create_code_batch"
|
|
||||||
);
|
|
||||||
const entitiesAllowEditGrading = useAllowedEntities(
|
|
||||||
user,
|
|
||||||
entities,
|
|
||||||
"edit_grading_system"
|
|
||||||
);
|
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<>
|
<>
|
||||||
<Head>
|
<Head>
|
||||||
<title>Settings Panel | EnCoach</title>
|
<title>Settings Panel | EnCoach</title>
|
||||||
<meta
|
<meta
|
||||||
name="description"
|
name="description"
|
||||||
content="A training platform for the IELTS exam provided by the Muscat Training Institute and developed by eCrop."
|
content="A training platform for the IELTS exam provided by the Muscat Training Institute and developed by eCrop."
|
||||||
/>
|
/>
|
||||||
<meta name="viewport" content="width=device-width, initial-scale=1" />
|
<meta name="viewport" content="width=device-width, initial-scale=1" />
|
||||||
<link rel="icon" href="/favicon.ico" />
|
<link rel="icon" href="/favicon.ico" />
|
||||||
</Head>
|
</Head>
|
||||||
<ToastContainer />
|
<ToastContainer />
|
||||||
<>
|
<>
|
||||||
<Modal
|
<Modal isOpen={modalOpen === "batchCreateUser"} onClose={() => setModalOpen(undefined)} maxWidth="max-w-[85%]">
|
||||||
isOpen={modalOpen === "batchCreateUser"}
|
<BatchCreateUser
|
||||||
onClose={() => setModalOpen(undefined)}
|
user={user}
|
||||||
maxWidth="max-w-[85%]"
|
entities={entitiesAllowCreateUsers.filter(
|
||||||
>
|
(e) =>
|
||||||
<BatchCreateUser
|
e.licenses > 0 &&
|
||||||
user={user}
|
e.licenses > allUsers.filter((u) => !isAdmin(u) && (u.entities || []).some((ent) => ent.id === e.id)).length,
|
||||||
entities={entitiesAllowCreateUser}
|
)}
|
||||||
permissions={permissions}
|
permissions={permissions}
|
||||||
onFinish={() => setModalOpen(undefined)}
|
onFinish={() => setModalOpen(undefined)}
|
||||||
/>
|
/>
|
||||||
</Modal>
|
</Modal>
|
||||||
<Modal
|
<Modal isOpen={modalOpen === "batchCreateCode"} onClose={() => setModalOpen(undefined)}>
|
||||||
isOpen={modalOpen === "batchCreateCode"}
|
<BatchCodeGenerator
|
||||||
onClose={() => setModalOpen(undefined)}
|
entities={entitiesAllowCreateCodes}
|
||||||
>
|
user={user}
|
||||||
<BatchCodeGenerator
|
users={allUsers}
|
||||||
entities={entitiesAllowCreateCodes}
|
permissions={permissions}
|
||||||
user={user}
|
onFinish={() => setModalOpen(undefined)}
|
||||||
users={allUsers}
|
/>
|
||||||
permissions={permissions}
|
</Modal>
|
||||||
onFinish={() => setModalOpen(undefined)}
|
<Modal isOpen={modalOpen === "createCode"} onClose={() => setModalOpen(undefined)}>
|
||||||
/>
|
<CodeGenerator
|
||||||
</Modal>
|
entities={entitiesAllowCreateCode}
|
||||||
<Modal
|
user={user}
|
||||||
isOpen={modalOpen === "createCode"}
|
permissions={permissions}
|
||||||
onClose={() => setModalOpen(undefined)}
|
onFinish={() => setModalOpen(undefined)}
|
||||||
>
|
/>
|
||||||
<CodeGenerator
|
</Modal>
|
||||||
entities={entitiesAllowCreateCode}
|
<Modal isOpen={modalOpen === "createUser"} onClose={() => setModalOpen(undefined)}>
|
||||||
user={user}
|
<UserCreator
|
||||||
permissions={permissions}
|
user={user}
|
||||||
onFinish={() => setModalOpen(undefined)}
|
entities={entitiesAllowCreateUser.filter(
|
||||||
/>
|
(e) =>
|
||||||
</Modal>
|
e.licenses > 0 &&
|
||||||
<Modal
|
e.licenses > allUsers.filter((u) => !isAdmin(u) && (u.entities || []).some((ent) => ent.id === e.id)).length,
|
||||||
isOpen={modalOpen === "createUser"}
|
)}
|
||||||
onClose={() => setModalOpen(undefined)}
|
users={allUsers}
|
||||||
>
|
permissions={permissions}
|
||||||
<UserCreator
|
onFinish={() => setModalOpen(undefined)}
|
||||||
user={user}
|
/>
|
||||||
entities={entitiesAllowCreateUsers}
|
</Modal>
|
||||||
users={allUsers}
|
<Modal isOpen={modalOpen === "gradingSystem"} onClose={() => setModalOpen(undefined)}>
|
||||||
permissions={permissions}
|
<CorporateGradingSystem
|
||||||
onFinish={() => setModalOpen(undefined)}
|
user={user}
|
||||||
/>
|
entitiesGrading={entitiesGrading}
|
||||||
</Modal>
|
entities={entitiesAllowEditGrading}
|
||||||
<Modal
|
mutate={() => router.replace(router.asPath)}
|
||||||
isOpen={modalOpen === "gradingSystem"}
|
/>
|
||||||
onClose={() => setModalOpen(undefined)}
|
</Modal>
|
||||||
>
|
|
||||||
<CorporateGradingSystem
|
|
||||||
user={user}
|
|
||||||
entitiesGrading={entitiesGrading}
|
|
||||||
entities={entitiesAllowEditGrading}
|
|
||||||
mutate={() => router.replace(router.asPath)}
|
|
||||||
/>
|
|
||||||
</Modal>
|
|
||||||
|
|
||||||
<section className="w-full grid grid-cols-2 -md:grid-cols-1 gap-8">
|
<section className="w-full grid grid-cols-2 -md:grid-cols-1 gap-8">
|
||||||
<ExamLoader />
|
<ExamLoader />
|
||||||
{checkAccess(
|
{checkAccess(user, getTypesOfUser(["teacher"]), permissions, "viewCodes") && (
|
||||||
user,
|
<div className="w-full grid grid-cols-2 gap-4">
|
||||||
getTypesOfUser(["teacher"]),
|
<IconCard
|
||||||
permissions,
|
Icon={BsCode}
|
||||||
"viewCodes"
|
label="Generate Single Code"
|
||||||
) && (
|
color="purple"
|
||||||
<div className="w-full grid grid-cols-2 gap-4">
|
className="w-full h-full"
|
||||||
<IconCard
|
onClick={() => setModalOpen("createCode")}
|
||||||
Icon={BsCode}
|
disabled={entitiesAllowCreateCode.length === 0}
|
||||||
label="Generate Single Code"
|
/>
|
||||||
color="purple"
|
<IconCard
|
||||||
className="w-full h-full"
|
Icon={BsCodeSquare}
|
||||||
onClick={() => setModalOpen("createCode")}
|
label="Generate Codes in Batch"
|
||||||
disabled={entitiesAllowCreateCode.length === 0}
|
color="purple"
|
||||||
/>
|
className="w-full h-full"
|
||||||
<IconCard
|
onClick={() => setModalOpen("batchCreateCode")}
|
||||||
Icon={BsCodeSquare}
|
disabled={entitiesAllowCreateCodes.length === 0}
|
||||||
label="Generate Codes in Batch"
|
/>
|
||||||
color="purple"
|
<IconCard
|
||||||
className="w-full h-full"
|
Icon={BsPersonFill}
|
||||||
onClick={() => setModalOpen("batchCreateCode")}
|
label="Create Single User"
|
||||||
disabled={entitiesAllowCreateCodes.length === 0}
|
color="purple"
|
||||||
/>
|
className="w-full h-full"
|
||||||
<IconCard
|
onClick={() => setModalOpen("createUser")}
|
||||||
Icon={BsPersonFill}
|
disabled={entitiesAllowCreateUser.length === 0}
|
||||||
label="Create Single User"
|
/>
|
||||||
color="purple"
|
<IconCard
|
||||||
className="w-full h-full"
|
Icon={BsPeopleFill}
|
||||||
onClick={() => setModalOpen("createUser")}
|
label="Create Users in Batch"
|
||||||
disabled={entitiesAllowCreateUser.length === 0}
|
color="purple"
|
||||||
/>
|
className="w-full h-full"
|
||||||
<IconCard
|
onClick={() => setModalOpen("batchCreateUser")}
|
||||||
Icon={BsPeopleFill}
|
disabled={entitiesAllowCreateUsers.length === 0}
|
||||||
label="Create Users in Batch"
|
/>
|
||||||
color="purple"
|
{checkAccess(user, ["admin", "corporate", "developer", "mastercorporate"]) && (
|
||||||
className="w-full h-full"
|
<IconCard
|
||||||
onClick={() => setModalOpen("batchCreateUser")}
|
Icon={BsGearFill}
|
||||||
disabled={entitiesAllowCreateUsers.length === 0}
|
label="Grading System"
|
||||||
/>
|
color="purple"
|
||||||
{checkAccess(user, [
|
className="w-full h-full col-span-2"
|
||||||
"admin",
|
onClick={() => setModalOpen("gradingSystem")}
|
||||||
"corporate",
|
disabled={entitiesAllowEditGrading.length === 0}
|
||||||
"developer",
|
/>
|
||||||
"mastercorporate",
|
)}
|
||||||
]) && (
|
</div>
|
||||||
<IconCard
|
)}
|
||||||
Icon={BsGearFill}
|
</section>
|
||||||
label="Grading System"
|
<section className="w-full">
|
||||||
color="purple"
|
<Lists user={user} entities={entities} permissions={permissions} />
|
||||||
className="w-full h-full col-span-2"
|
</section>
|
||||||
onClick={() => setModalOpen("gradingSystem")}
|
</>
|
||||||
disabled={entitiesAllowEditGrading.length === 0}
|
</>
|
||||||
/>
|
);
|
||||||
)}
|
|
||||||
</div>
|
|
||||||
)}
|
|
||||||
</section>
|
|
||||||
<section className="w-full">
|
|
||||||
<Lists user={user} entities={entities} permissions={permissions} />
|
|
||||||
</section>
|
|
||||||
</>
|
|
||||||
</>
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user