Added an export feature for the master statisticl screen

This commit is contained in:
Joao Ramos
2024-09-05 22:42:46 +01:00
parent 70de97766e
commit a61ad2cc7e
6 changed files with 309 additions and 89 deletions

View File

@@ -1,6 +1,7 @@
import {app} from "@/firebase";
import {Assignment} from "@/interfaces/results";
import {collection, getDocs, getFirestore, query, where} from "firebase/firestore";
import {getAllAssignersByCorporate} from "@/utils/groups.be";
const db = getFirestore(app);
@@ -34,3 +35,33 @@ 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();
};
export const getAssignmentsForCorporates = async (idsList: string[], startDate?: Date, endDate?: Date) => {
const assigners = await Promise.all(
idsList.map(async (id) => {
const assigners = await getAllAssignersByCorporate(id);
return {
corporateId: id,
assigners,
};
}),
);
const assignments = await Promise.all(
assigners.map(async (data) => {
try {
const assigners = [...new Set([...data.assigners, data.corporateId])];
const assignments = await getAssignmentsByAssigners(assigners, startDate, endDate);
return assignments.map((assignment) => ({
...assignment,
corporateId: data.corporateId,
}));
} catch (err) {
console.error(err);
return [];
}
}),
);
return assignments.flat();
}