diff --git a/src/pages/(admin)/CodeGenerator.tsx b/src/pages/(admin)/CodeGenerator.tsx
index 50774f53..6a75574f 100644
--- a/src/pages/(admin)/CodeGenerator.tsx
+++ b/src/pages/(admin)/CodeGenerator.tsx
@@ -17,8 +17,9 @@ const USER_TYPE_PERMISSIONS: {[key in Type]: Type[]} = {
teacher: [],
agent: [],
corporate: ["student", "teacher"],
- admin: ["student", "teacher", "agent", "corporate", "admin"],
- developer: ["student", "teacher", "agent", "corporate", "admin", "developer"],
+ mastercorporate: ["student", "teacher", "corporate"],
+ admin: ["student", "teacher", "agent", "corporate", "admin", "mastercorporate"],
+ developer: ["student", "teacher", "agent", "corporate", "admin", "developer","mastercorporate"],
};
export default function CodeGenerator({user}: {user: User}) {
diff --git a/src/pages/(admin)/Lists/GroupList.tsx b/src/pages/(admin)/Lists/GroupList.tsx
index 5e28543d..7101d02b 100644
--- a/src/pages/(admin)/Lists/GroupList.tsx
+++ b/src/pages/(admin)/Lists/GroupList.tsx
@@ -86,7 +86,7 @@ const CreatePanel = ({user, users, group, onClose}: CreateDialogProps) => {
const emailUsers = [...new Set(emails)].map((x) => users.find((y) => y.email.toLowerCase() === x)).filter((x) => x !== undefined);
const filteredUsers = emailUsers.filter(
(x) =>
- ((user.type === "developer" || user.type === "admin" || user.type === "corporate") &&
+ ((user.type === "developer" || user.type === "admin" || user.type === "corporate" || user.type === "mastercorporate") &&
(x?.type === "student" || x?.type === "teacher")) ||
(user.type === "teacher" && x?.type === "student"),
);
@@ -197,10 +197,10 @@ export default function GroupList({user}: {user: User}) {
const [filterByUser, setFilterByUser] = useState(false);
const {users} = useUsers();
- const {groups, reload} = useGroups(user && filterTypes.includes(user?.type) ? user.id : undefined);
+ const {groups, reload} = useGroups(user && filterTypes.includes(user?.type) ? user.id : undefined, user?.type);
useEffect(() => {
- if (user && (user.type === "corporate" || user.type === "teacher")) {
+ if (user && (['corporate', 'teacher', 'mastercorporate'].includes(user.type))) {
setFilterByUser(true);
}
}, [user]);
diff --git a/src/pages/(admin)/Lists/UserList.tsx b/src/pages/(admin)/Lists/UserList.tsx
index dd351f1c..ba05b02e 100644
--- a/src/pages/(admin)/Lists/UserList.tsx
+++ b/src/pages/(admin)/Lists/UserList.tsx
@@ -92,7 +92,7 @@ export default function UserList({
const { users, reload } = useUsers();
const { groups } = useGroups(
- user && (user?.type === "corporate" || user?.type === "teacher")
+ user && (['corporate', 'teacher', 'mastercorporate'].includes(user?.type))
? user.id
: undefined
);
@@ -403,7 +403,7 @@ export default function UserList({
}),
columnHelper.accessor(
(x) =>
- x.type === "corporate"
+ x.type === "corporate" || x.type === "mastercorporate"
? x.demographicInformation?.position
: x.demographicInformation?.employment,
{
@@ -706,11 +706,11 @@ export default function UserList({
if (sorter === "employment" || sorter === reverseString("employment")) {
const aSortingItem =
- a.type === "corporate"
+ a.type === "corporate" || a.type === "mastercorporate"
? a.demographicInformation?.position
: a.demographicInformation?.employment;
const bSortingItem =
- b.type === "corporate"
+ b.type === "corporate" || b.type === "mastercorporate"
? b.demographicInformation?.position
: b.demographicInformation?.employment;
diff --git a/src/pages/api/groups/index.ts b/src/pages/api/groups/index.ts
index db32967b..d42bbae1 100644
--- a/src/pages/api/groups/index.ts
+++ b/src/pages/api/groups/index.ts
@@ -30,29 +30,74 @@ async function handler(req: NextApiRequest, res: NextApiResponse) {
if (req.method === "POST") await post(req, res);
}
+const getGroupsForUser = async (admin: string, participant: string) => {
+ try {
+ const queryConstraints = [
+ ...(admin ? [where("admin", "==", admin)] : []),
+ ...(participant
+ ? [where("participants", "array-contains", participant)]
+ : []),
+ ];
+ const snapshot = await getDocs(
+ queryConstraints.length > 0
+ ? query(collection(db, "groups"), ...queryConstraints)
+ : collection(db, "groups")
+ );
+ const groups = snapshot.docs.map((doc) => ({
+ id: doc.id,
+ ...doc.data(),
+ })) as Group[];
+
+ return groups;
+ } catch (e) {
+ console.error(e);
+ return [];
+ }
+};
async function get(req: NextApiRequest, res: NextApiResponse) {
const { admin, participant } = req.query as {
admin: string;
participant: string;
};
- const queryConstraints = [
- ...(admin ? [where("admin", "==", admin)] : []),
- ...(participant
- ? [where("participants", "array-contains", participant)]
- : []),
- ];
- const snapshot = await getDocs(
- queryConstraints.length > 0
- ? query(collection(db, "groups"), ...queryConstraints)
- : collection(db, "groups"),
- );
- const groups = snapshot.docs.map((doc) => ({
- id: doc.id,
- ...doc.data(),
- })) as Group[];
+ if (req.session?.user?.type === "mastercorporate") {
+ try {
+ const masterCorporateGroups = await getGroupsForUser(admin, participant);
+ const corporatesFromMaster = masterCorporateGroups
+ .filter((g) => g.name === "Corporate")
+ .flatMap((g) => g.participants);
- res.status(200).json(groups);
+ if (corporatesFromMaster.length === 0) {
+ res.status(200).json([]);
+ return;
+ }
+ Promise.all(
+ corporatesFromMaster.map((c) => getGroupsForUser(c, participant))
+ )
+ .then((groups) => {
+ res.status(200).json([...masterCorporateGroups, ...groups.flat()]);
+ return;
+ })
+ .catch((e) => {
+ console.error(e);
+ res.status(500).json({ ok: false });
+ return;
+ });
+ } catch (e) {
+ console.error(e);
+ res.status(500).json({ ok: false });
+ return;
+ }
+ return;
+ }
+
+ try {
+ const groups = await getGroupsForUser(admin, participant);
+ res.status(200).json(groups);
+ } catch (e) {
+ console.error(e);
+ res.status(500).json({ ok: false });
+ }
}
async function post(req: NextApiRequest, res: NextApiResponse) {
@@ -60,8 +105,8 @@ async function post(req: NextApiRequest, res: NextApiResponse) {
await Promise.all(
body.participants.map(
- async (p) => await updateExpiryDateOnGroup(p, body.admin),
- ),
+ async (p) => await updateExpiryDateOnGroup(p, body.admin)
+ )
);
await setDoc(doc(db, "groups", v4()), {
diff --git a/src/pages/api/register.ts b/src/pages/api/register.ts
index 773bfbd6..15a445d2 100644
--- a/src/pages/api/register.ts
+++ b/src/pages/api/register.ts
@@ -143,9 +143,18 @@ async function registerCorporate(req: NextApiRequest, res: NextApiResponse) {
disableEditing: true,
};
+ const defaultCorporateGroup: Group = {
+ admin: userId,
+ id: v4(),
+ name: "Corporate",
+ participants: [],
+ disableEditing: true,
+ };
+
await setDoc(doc(db, "users", userId), user);
await setDoc(doc(db, "groups", defaultTeachersGroup.id), defaultTeachersGroup);
await setDoc(doc(db, "groups", defaultStudentsGroup.id), defaultStudentsGroup);
+ await setDoc(doc(db, "groups", defaultCorporateGroup.id), defaultCorporateGroup);
req.session.user = {...user, id: userId};
await req.session.save();
diff --git a/src/pages/index.tsx b/src/pages/index.tsx
index bd3365ea..373348ae 100644
--- a/src/pages/index.tsx
+++ b/src/pages/index.tsx
@@ -27,6 +27,7 @@ import AdminDashboard from "@/dashboards/Admin";
import CorporateDashboard from "@/dashboards/Corporate";
import TeacherDashboard from "@/dashboards/Teacher";
import AgentDashboard from "@/dashboards/Agent";
+import MasterCorporateDashboard from "@/dashboards/MasterCorporate";
import PaymentDue from "./(status)/PaymentDue";
import {useRouter} from "next/router";
import {PayPalScriptProvider} from "@paypal/react-paypal-js";
@@ -172,6 +173,7 @@ export default function Home(props: Props) {
{user.type === "student" &&
}
{user.type === "teacher" &&
}
{user.type === "corporate" &&
}
+ {user.type === "mastercorporate" &&
}
{user.type === "agent" &&
}
{user.type === "admin" &&
}
{user.type === "developer" && (
diff --git a/src/pages/payment-record.tsx b/src/pages/payment-record.tsx
index 16b16b3c..c4e22ab9 100644
--- a/src/pages/payment-record.tsx
+++ b/src/pages/payment-record.tsx
@@ -42,7 +42,7 @@ export const getServerSideProps = withIronSessionSsr(({req, res}) => {
};
}
- if (shouldRedirectHome(user) || !["admin", "developer", "agent", "corporate"].includes(user.type)) {
+ if (shouldRedirectHome(user) || !["admin", "developer", "agent", "corporate", "mastercorporate"].includes(user.type)) {
return {
redirect: {
destination: "/",
@@ -940,7 +940,7 @@ export default function PaymentRecord() {
Payment Record
- {(user.type === "developer" || user.type === "admin" || user.type === "agent" || user.type === "corporate") && (
+ {(["developer", "admin", "agent", "corporate", "mastercorporate"].includes(user.type)) && (