Improved some of the querying for the assignments
This commit is contained in:
@@ -14,6 +14,7 @@ import {useListSearch} from "@/hooks/useListSearch";
|
|||||||
import axios from "axios";
|
import axios from "axios";
|
||||||
import {toast} from "react-toastify";
|
import {toast} from "react-toastify";
|
||||||
import Button from "@/components/Low/Button";
|
import Button from "@/components/Low/Button";
|
||||||
|
import {getUserName} from "@/utils/users";
|
||||||
|
|
||||||
interface GroupedCorporateUsers {
|
interface GroupedCorporateUsers {
|
||||||
// list of user Ids
|
// list of user Ids
|
||||||
@@ -71,12 +72,11 @@ const MasterStatistical = (props: Props) => {
|
|||||||
|
|
||||||
const tableResults = React.useMemo(
|
const tableResults = React.useMemo(
|
||||||
() =>
|
() =>
|
||||||
assignments
|
assignments.reduce((accmA: TableData[], a: AssignmentWithCorporateId) => {
|
||||||
.reduce((accmA: TableData[], a: AssignmentWithCorporateId) => {
|
|
||||||
const userResults = a.assignees.map((assignee) => {
|
const userResults = a.assignees.map((assignee) => {
|
||||||
const userStats = a.results.find((r) => r.user === assignee)?.stats || [];
|
const userStats = a.results.find((r) => r.user === assignee)?.stats || [];
|
||||||
const userData = users.find((u) => u.id === assignee);
|
const userData = users.find((u) => u.id === assignee);
|
||||||
const corporate = users.find((u) => u.id === a.assigner)?.name || "";
|
const corporate = getUserName(users.find((u) => u.id === a.assigner));
|
||||||
const commonData = {
|
const commonData = {
|
||||||
user: userData?.name || "N/A",
|
user: userData?.name || "N/A",
|
||||||
email: userData?.email || "N/A",
|
email: userData?.email || "N/A",
|
||||||
@@ -104,11 +104,12 @@ const MasterStatistical = (props: Props) => {
|
|||||||
}) as TableData[];
|
}) as TableData[];
|
||||||
|
|
||||||
return [...accmA, ...userResults];
|
return [...accmA, ...userResults];
|
||||||
}, [])
|
}, []),
|
||||||
.filter((x) => x.user !== "N/A"),
|
|
||||||
[assignments, users],
|
[assignments, users],
|
||||||
);
|
);
|
||||||
|
|
||||||
|
useEffect(() => console.log(assignments), [assignments]);
|
||||||
|
|
||||||
const getCorporateScores = (corporateId: string): UserCount => {
|
const getCorporateScores = (corporateId: string): UserCount => {
|
||||||
const corporateAssignmentsUsers = assignments.filter((a) => a.corporateId === corporateId).reduce((acc, a) => acc + a.assignees.length, 0);
|
const corporateAssignmentsUsers = assignments.filter((a) => a.corporateId === corporateId).reduce((acc, a) => acc + a.assignees.length, 0);
|
||||||
|
|
||||||
@@ -171,13 +172,6 @@ const MasterStatistical = (props: Props) => {
|
|||||||
}),
|
}),
|
||||||
]
|
]
|
||||||
: []),
|
: []),
|
||||||
columnHelper.accessor("corporate", {
|
|
||||||
header: "Corporate",
|
|
||||||
id: "corporate",
|
|
||||||
cell: (info) => {
|
|
||||||
return <span>{info.getValue()}</span>;
|
|
||||||
},
|
|
||||||
}),
|
|
||||||
columnHelper.accessor("assignment", {
|
columnHelper.accessor("assignment", {
|
||||||
header: "Assignment",
|
header: "Assignment",
|
||||||
id: "assignment",
|
id: "assignment",
|
||||||
@@ -197,7 +191,7 @@ const MasterStatistical = (props: Props) => {
|
|||||||
},
|
},
|
||||||
}),
|
}),
|
||||||
columnHelper.accessor("correct", {
|
columnHelper.accessor("correct", {
|
||||||
header: "Correct",
|
header: "Score",
|
||||||
id: "correct",
|
id: "correct",
|
||||||
cell: (info) => {
|
cell: (info) => {
|
||||||
return <span>{info.getValue()}</span>;
|
return <span>{info.getValue()}</span>;
|
||||||
|
|||||||
@@ -83,18 +83,6 @@ export default function MasterCorporateDashboard({user}: Props) {
|
|||||||
const {assignments, isLoading: isAssignmentsLoading, reload: reloadAssignments} = useAssignments({corporate: user.id});
|
const {assignments, isLoading: isAssignmentsLoading, reload: reloadAssignments} = useAssignments({corporate: user.id});
|
||||||
|
|
||||||
const assignmentsGroups = useMemo(() => groups.filter((x) => x.admin === user.id || x.participants.includes(user.id)), [groups, user.id]);
|
const assignmentsGroups = useMemo(() => groups.filter((x) => x.admin === user.id || x.participants.includes(user.id)), [groups, user.id]);
|
||||||
const assignmentsUsers = useMemo(
|
|
||||||
() =>
|
|
||||||
[...students, ...teachers].filter((x) =>
|
|
||||||
!!selectedUser
|
|
||||||
? groups
|
|
||||||
.filter((g) => g.admin === selectedUser.id)
|
|
||||||
.flatMap((g) => g.participants)
|
|
||||||
.includes(x.id) || false
|
|
||||||
: groups.flatMap((g) => g.participants).includes(x.id),
|
|
||||||
),
|
|
||||||
[groups, selectedUser, teachers, students],
|
|
||||||
);
|
|
||||||
|
|
||||||
const appendUserFilters = useFilterStore((state) => state.appendUserFilter);
|
const appendUserFilters = useFilterStore((state) => state.appendUserFilter);
|
||||||
const router = useRouter();
|
const router = useRouter();
|
||||||
|
|||||||
@@ -26,7 +26,14 @@ export const getAssignmentsByAssignerBetweenDates = async (id: string, startDate
|
|||||||
};
|
};
|
||||||
|
|
||||||
export const getAssignmentsByAssigners = async (ids: string[], startDate?: Date, endDate?: Date) => {
|
export const getAssignmentsByAssigners = async (ids: string[], startDate?: Date, endDate?: Date) => {
|
||||||
return (await Promise.all(ids.map((id) => getAssignmentsByAssigner(id, startDate, endDate)))).flat();
|
return await db
|
||||||
|
.collection("assignments")
|
||||||
|
.find<Assignment>({
|
||||||
|
assigner: {$in: ids},
|
||||||
|
...(!!startDate ? {startDate: {$gte: startDate.toISOString()}} : {}),
|
||||||
|
...(!!endDate ? {endDate: {$lte: endDate.toISOString()}} : {}),
|
||||||
|
})
|
||||||
|
.toArray();
|
||||||
};
|
};
|
||||||
|
|
||||||
export const getAssignmentsForCorporates = async (idsList: string[], startDate?: Date, endDate?: Date) => {
|
export const getAssignmentsForCorporates = async (idsList: string[], startDate?: Date, endDate?: Date) => {
|
||||||
@@ -57,4 +64,4 @@ export const getAssignmentsForCorporates = async (idsList: string[], startDate?:
|
|||||||
);
|
);
|
||||||
|
|
||||||
return assignments.flat();
|
return assignments.flat();
|
||||||
}
|
};
|
||||||
|
|||||||
@@ -1,8 +1,9 @@
|
|||||||
import {app} from "@/firebase";
|
import {app} from "@/firebase";
|
||||||
|
import {Assignment} from "@/interfaces/results";
|
||||||
import {CorporateUser, Group, MasterCorporateUser, StudentUser, TeacherUser, User} from "@/interfaces/user";
|
import {CorporateUser, Group, MasterCorporateUser, StudentUser, TeacherUser, User} from "@/interfaces/user";
|
||||||
import client from "@/lib/mongodb";
|
import client from "@/lib/mongodb";
|
||||||
import moment from "moment";
|
import moment from "moment";
|
||||||
import {getUser} from "./users.be";
|
import {getLinkedUsers, getUser} from "./users.be";
|
||||||
import {getSpecificUsers} from "./users.be";
|
import {getSpecificUsers} from "./users.be";
|
||||||
|
|
||||||
const db = client.db(process.env.MONGODB_DB);
|
const db = client.db(process.env.MONGODB_DB);
|
||||||
@@ -71,17 +72,10 @@ export const getUsersGroups = async (ids: string[]) => {
|
|||||||
};
|
};
|
||||||
|
|
||||||
export const getAllAssignersByCorporate = async (corporateID: string): Promise<string[]> => {
|
export const getAllAssignersByCorporate = async (corporateID: string): Promise<string[]> => {
|
||||||
const groups = await getUserGroups(corporateID);
|
const linkedTeachers = await getLinkedUsers(corporateID, "mastercorporate", "teacher");
|
||||||
const groupUsers = (await Promise.all(groups.map(async (g) => await Promise.all(g.participants.map(getUser)))))
|
const linkedCorporates = await getLinkedUsers(corporateID, "mastercorporate", "corporate");
|
||||||
.flat()
|
|
||||||
.filter((x) => !!x) as User[];
|
|
||||||
const teacherPromises = await Promise.all(
|
|
||||||
groupUsers.map(async (u) =>
|
|
||||||
u.type === "teacher" ? u.id : u.type === "corporate" ? [...(await getAllAssignersByCorporate(u.id)), u.id] : undefined,
|
|
||||||
),
|
|
||||||
);
|
|
||||||
|
|
||||||
return teacherPromises.filter((x) => !!x).flat() as string[];
|
return [...linkedTeachers.users.map((x) => x.id), ...linkedCorporates.users.map((x) => x.id)];
|
||||||
};
|
};
|
||||||
|
|
||||||
export const getGroupsForUser = async (admin?: string, participant?: string) => {
|
export const getGroupsForUser = async (admin?: string, participant?: string) => {
|
||||||
|
|||||||
Reference in New Issue
Block a user