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 {toast} from "react-toastify";
|
||||
import Button from "@/components/Low/Button";
|
||||
import {getUserName} from "@/utils/users";
|
||||
|
||||
interface GroupedCorporateUsers {
|
||||
// list of user Ids
|
||||
@@ -71,12 +72,11 @@ const MasterStatistical = (props: Props) => {
|
||||
|
||||
const tableResults = React.useMemo(
|
||||
() =>
|
||||
assignments
|
||||
.reduce((accmA: TableData[], a: AssignmentWithCorporateId) => {
|
||||
assignments.reduce((accmA: TableData[], a: AssignmentWithCorporateId) => {
|
||||
const userResults = a.assignees.map((assignee) => {
|
||||
const userStats = a.results.find((r) => r.user === assignee)?.stats || [];
|
||||
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 = {
|
||||
user: userData?.name || "N/A",
|
||||
email: userData?.email || "N/A",
|
||||
@@ -104,11 +104,12 @@ const MasterStatistical = (props: Props) => {
|
||||
}) as TableData[];
|
||||
|
||||
return [...accmA, ...userResults];
|
||||
}, [])
|
||||
.filter((x) => x.user !== "N/A"),
|
||||
}, []),
|
||||
[assignments, users],
|
||||
);
|
||||
|
||||
useEffect(() => console.log(assignments), [assignments]);
|
||||
|
||||
const getCorporateScores = (corporateId: string): UserCount => {
|
||||
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", {
|
||||
header: "Assignment",
|
||||
id: "assignment",
|
||||
@@ -197,7 +191,7 @@ const MasterStatistical = (props: Props) => {
|
||||
},
|
||||
}),
|
||||
columnHelper.accessor("correct", {
|
||||
header: "Correct",
|
||||
header: "Score",
|
||||
id: "correct",
|
||||
cell: (info) => {
|
||||
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 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 router = useRouter();
|
||||
|
||||
@@ -26,7 +26,14 @@ export const getAssignmentsByAssignerBetweenDates = async (id: string, startDate
|
||||
};
|
||||
|
||||
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) => {
|
||||
@@ -57,4 +64,4 @@ export const getAssignmentsForCorporates = async (idsList: string[], startDate?:
|
||||
);
|
||||
|
||||
return assignments.flat();
|
||||
}
|
||||
};
|
||||
|
||||
@@ -1,8 +1,9 @@
|
||||
import {app} from "@/firebase";
|
||||
import {Assignment} from "@/interfaces/results";
|
||||
import {CorporateUser, Group, MasterCorporateUser, StudentUser, TeacherUser, User} from "@/interfaces/user";
|
||||
import client from "@/lib/mongodb";
|
||||
import moment from "moment";
|
||||
import {getUser} from "./users.be";
|
||||
import {getLinkedUsers, getUser} from "./users.be";
|
||||
import {getSpecificUsers} from "./users.be";
|
||||
|
||||
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[]> => {
|
||||
const groups = await getUserGroups(corporateID);
|
||||
const groupUsers = (await Promise.all(groups.map(async (g) => await Promise.all(g.participants.map(getUser)))))
|
||||
.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,
|
||||
),
|
||||
);
|
||||
const linkedTeachers = await getLinkedUsers(corporateID, "mastercorporate", "teacher");
|
||||
const linkedCorporates = await getLinkedUsers(corporateID, "mastercorporate", "corporate");
|
||||
|
||||
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) => {
|
||||
|
||||
Reference in New Issue
Block a user