From 916fa66446b9578a1f288349a13e32094fb19cb6 Mon Sep 17 00:00:00 2001 From: Joao Ramos Date: Wed, 24 Jul 2024 09:25:29 +0100 Subject: [PATCH] Added linked account --- src/dashboards/Corporate.tsx | 17 +++++++++++++++++ src/pages/index.tsx | 3 ++- src/utils/groups.ts | 32 +++++++++++++++++++------------- 3 files changed, 38 insertions(+), 14 deletions(-) diff --git a/src/dashboards/Corporate.tsx b/src/dashboards/Corporate.tsx index 94d5e5ab..06feae46 100644 --- a/src/dashboards/Corporate.tsx +++ b/src/dashboards/Corporate.tsx @@ -35,6 +35,7 @@ import GroupList from "@/pages/(admin)/Lists/GroupList"; import useFilterStore from "@/stores/listFilterStore"; import { useRouter } from "next/router"; import useCodes from "@/hooks/useCodes"; +import { getUserCorporate } from "@/utils/groups"; interface Props { user: CorporateUser; @@ -44,6 +45,8 @@ export default function CorporateDashboard({ user }: Props) { const [page, setPage] = useState(""); const [selectedUser, setSelectedUser] = useState(); const [showModal, setShowModal] = useState(false); + const [corporateUserToShow, setCorporateUserToShow] = + useState(); const { stats } = useStats(); const { users, reload } = useUsers(); @@ -57,6 +60,11 @@ export default function CorporateDashboard({ user }: Props) { setShowModal(!!selectedUser && page === ""); }, [selectedUser, page]); + useEffect(() => { + // in this case it fetches the master corporate account + getUserCorporate(user.id).then(setCorporateUserToShow); + }, [user]); + const studentFilter = (user: User) => user.type === "student" && groups.flatMap((g) => g.participants).includes(user.id); @@ -200,6 +208,15 @@ export default function CorporateDashboard({ user }: Props) { const DefaultDashboard = () => ( <> + {corporateUserToShow && ( +
+ Linked to:{" "} + + {corporateUserToShow?.corporateInformation?.companyInformation + .name || corporateUserToShow.name} + +
+ )}
setPage("students")} diff --git a/src/pages/index.tsx b/src/pages/index.tsx index 373348ae..601d4838 100644 --- a/src/pages/index.tsx +++ b/src/pages/index.tsx @@ -31,7 +31,7 @@ import MasterCorporateDashboard from "@/dashboards/MasterCorporate"; import PaymentDue from "./(status)/PaymentDue"; import {useRouter} from "next/router"; import {PayPalScriptProvider} from "@paypal/react-paypal-js"; -import {CorporateUser, Type, userTypes} from "@/interfaces/user"; +import {CorporateUser, MasterCorporateUser, Type, userTypes} from "@/interfaces/user"; import Select from "react-select"; import {USER_TYPE_LABELS} from "@/resources/user"; @@ -187,6 +187,7 @@ export default function Home(props: Props) { {selectedScreen === "student" && } {selectedScreen === "teacher" && } {selectedScreen === "corporate" && } + {selectedScreen === "mastercorporate" && } {selectedScreen === "agent" && } {selectedScreen === "admin" && } diff --git a/src/utils/groups.ts b/src/utils/groups.ts index 0c2ca04b..1987a2d5 100644 --- a/src/utils/groups.ts +++ b/src/utils/groups.ts @@ -1,4 +1,4 @@ -import { CorporateUser, Group, User } from "@/interfaces/user"; +import { CorporateUser, Group, User, Type } from "@/interfaces/user"; import axios from "axios"; export const isUserFromCorporate = async (userID: string) => { @@ -7,20 +7,12 @@ export const isUserFromCorporate = async (userID: string) => { const users = (await axios.get("/api/users/list")).data; const adminTypes = groups.map( - (g) => users.find((u) => u.id === g.admin)?.type, + (g) => users.find((u) => u.id === g.admin)?.type ); return adminTypes.includes("corporate"); }; -export const getUserCorporate = async ( - userID: string, -): Promise => { - const userRequest = await axios.get(`/api/users/${userID}`); - if (userRequest.status === 200) { - const user = userRequest.data; - if (user.type === "corporate") return user; - } - +const getAdminForGroup = async (userID: string, role: Type) => { const groups = (await axios.get(`/api/groups?participant=${userID}`)) .data; @@ -29,9 +21,23 @@ export const getUserCorporate = async ( const userRequest = await axios.get(`/api/users/${g.admin}`); if (userRequest.status === 200) return userRequest.data; return undefined; - }), + }) ); - const admins = adminRequests.filter((x) => x?.type === "corporate"); + const admins = adminRequests.filter((x) => x?.type === role); return admins.length > 0 ? (admins[0] as CorporateUser) : undefined; }; + +export const getUserCorporate = async ( + userID: string +): Promise => { + const userRequest = await axios.get(`/api/users/${userID}`); + if (userRequest.status === 200) { + const user = userRequest.data; + if (user.type === "corporate") { + return getAdminForGroup(userID, "mastercorporate"); + } + } + + return getAdminForGroup(userID, "corporate"); +};