Solved a bug for the master statistical

This commit is contained in:
Tiago Ribeiro
2024-09-10 10:55:02 +01:00
parent 85c8f622ee
commit 33a46c227b
6 changed files with 13 additions and 13 deletions

View File

@@ -74,10 +74,8 @@ const MasterStatistical = (props: Props) => {
() => () =>
assignments.reduce((accmA: TableData[], a: AssignmentWithCorporateId) => { assignments.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 userData = users.find((u) => u.id === assignee); const userData = users.find((u) => u.id === assignee);
if (!!userData) console.log(assignee, userData.name); const userStats = !!userData ? a.results.find((r) => r.user === assignee)?.stats || [] : [];
const corporate = getUserName(users.find((u) => u.id === a.assigner)); const corporate = getUserName(users.find((u) => u.id === a.assigner));
const commonData = { const commonData = {
user: userData, user: userData,
@@ -88,6 +86,7 @@ const MasterStatistical = (props: Props) => {
corporate, corporate,
assignment: a.name, assignment: a.name,
}; };
if (userStats.length === 0) { if (userStats.length === 0) {
return { return {
...commonData, ...commonData,
@@ -167,7 +166,7 @@ const MasterStatistical = (props: Props) => {
header: "Student ID", header: "Student ID",
id: "studentID", id: "studentID",
cell: (info) => { cell: (info) => {
return <span>{(info.getValue() as StudentUser).studentID || "N/A"}</span>; return <span>{(info.getValue() as StudentUser)?.studentID || "N/A"}</span>;
}, },
}), }),
...(displaySelection ...(displaySelection

View File

@@ -22,7 +22,7 @@ async function handler(req: NextApiRequest, res: NextApiResponse) {
async function GET(req: NextApiRequest, res: NextApiResponse) { async function GET(req: NextApiRequest, res: NextApiResponse) {
const {id} = req.query as {id: string}; const {id} = req.query as {id: string};
const assigners = await getAllAssignersByCorporate(id); const assigners = await getAllAssignersByCorporate(id, req.session.user!.type);
const assignments = await getAssignmentsByAssigners([...assigners, id]); const assignments = await getAssignmentsByAssigners([...assigners, id]);
res.status(200).json(uniqBy(assignments, "id")); res.status(200).json(uniqBy(assignments, "id"));

View File

@@ -29,7 +29,7 @@ async function GET(req: NextApiRequest, res: NextApiResponse) {
try { try {
const idsList = ids.split(","); const idsList = ids.split(",");
const assignments = await getAssignmentsForCorporates(idsList, startDateParsed, endDateParsed); const assignments = await getAssignmentsForCorporates(req.session.user!.type, idsList, startDateParsed, endDateParsed);
res.status(200).json(assignments); res.status(200).json(assignments);
} catch (err: any) { } catch (err: any) {
res.status(500).json({error: err.message}); res.status(500).json({error: err.message});

View File

@@ -64,7 +64,7 @@ async function post(req: NextApiRequest, res: NextApiResponse) {
}; };
const startDateParsed = startDate ? new Date(startDate) : undefined; const startDateParsed = startDate ? new Date(startDate) : undefined;
const endDateParsed = endDate ? new Date(endDate) : undefined; const endDateParsed = endDate ? new Date(endDate) : undefined;
const assignments = await getAssignmentsForCorporates(ids, startDateParsed, endDateParsed); const assignments = await getAssignmentsForCorporates(req.session.user.type, ids, startDateParsed, endDateParsed);
const assignmentUsers = [...new Set(assignments.flatMap((a) => a.assignees))]; const assignmentUsers = [...new Set(assignments.flatMap((a) => a.assignees))];
const assigners = [...new Set(assignments.map((a) => a.assigner))]; const assigners = [...new Set(assignments.map((a) => a.assigner))];

View File

@@ -1,6 +1,7 @@
import client from "@/lib/mongodb"; import client from "@/lib/mongodb";
import {Assignment} from "@/interfaces/results"; import {Assignment} from "@/interfaces/results";
import {getAllAssignersByCorporate} from "@/utils/groups.be"; import {getAllAssignersByCorporate} from "@/utils/groups.be";
import {Type} from "@/interfaces/user";
const db = client.db(process.env.MONGODB_DB); const db = client.db(process.env.MONGODB_DB);
@@ -36,10 +37,10 @@ export const getAssignmentsByAssigners = async (ids: string[], startDate?: Date,
.toArray(); .toArray();
}; };
export const getAssignmentsForCorporates = async (idsList: string[], startDate?: Date, endDate?: Date) => { export const getAssignmentsForCorporates = async (userType: Type, idsList: string[], startDate?: Date, endDate?: Date) => {
const assigners = await Promise.all( const assigners = await Promise.all(
idsList.map(async (id) => { idsList.map(async (id) => {
const assigners = await getAllAssignersByCorporate(id); const assigners = await getAllAssignersByCorporate(id, userType);
return { return {
corporateId: id, corporateId: id,
assigners, assigners,

View File

@@ -1,6 +1,6 @@
import {app} from "@/firebase"; import {app} from "@/firebase";
import {Assignment} from "@/interfaces/results"; import {Assignment} from "@/interfaces/results";
import {CorporateUser, Group, MasterCorporateUser, StudentUser, TeacherUser, User} from "@/interfaces/user"; import {CorporateUser, Group, MasterCorporateUser, StudentUser, TeacherUser, Type, User} from "@/interfaces/user";
import client from "@/lib/mongodb"; import client from "@/lib/mongodb";
import moment from "moment"; import moment from "moment";
import {getLinkedUsers, getUser} from "./users.be"; import {getLinkedUsers, getUser} from "./users.be";
@@ -71,9 +71,9 @@ export const getUsersGroups = async (ids: string[]) => {
.toArray(); .toArray();
}; };
export const getAllAssignersByCorporate = async (corporateID: string): Promise<string[]> => { export const getAllAssignersByCorporate = async (corporateID: string, type: Type): Promise<string[]> => {
const linkedTeachers = await getLinkedUsers(corporateID, "mastercorporate", "teacher"); const linkedTeachers = await getLinkedUsers(corporateID, type, "teacher");
const linkedCorporates = await getLinkedUsers(corporateID, "mastercorporate", "corporate"); const linkedCorporates = await getLinkedUsers(corporateID, type, "corporate");
return [...linkedTeachers.users.map((x) => x.id), ...linkedCorporates.users.map((x) => x.id)]; return [...linkedTeachers.users.map((x) => x.id), ...linkedCorporates.users.map((x) => x.id)];
}; };