Updated the pagination on the useUsers and migrading the grading

This commit is contained in:
Tiago Ribeiro
2024-09-07 16:53:58 +01:00
parent 417c9176fe
commit 56f374bbfe
5 changed files with 45 additions and 41 deletions

View File

@@ -14,10 +14,11 @@ import {getUserCorporate} from "@/utils/groups.be";
import {Grading} from "@/interfaces";
import {getGroupsForUser} from "@/utils/groups.be";
import {uniq} from "lodash";
import {getUser} from "@/utils/users.be";
import {getSpecificUsers, getUser} from "@/utils/users.be";
import {getGradingSystem} from "@/utils/grading.be";
import client from "@/lib/mongodb";
const db = getFirestore(app);
const db = client.db(process.env.MONGODB_DB);
export default withIronSessionApiRoute(handler, sessionOptions);
@@ -36,6 +37,14 @@ async function get(req: NextApiRequest, res: NextApiResponse) {
return res.status(200).json(gradingSystem);
}
async function updateGrading(id: string, body: Grading) {
if (await db.collection("grading").findOne({id})) {
await db.collection("grading").updateOne({id}, {$set: body});
} else {
await db.collection("grading").insertOne({id, ...body});
}
}
async function post(req: NextApiRequest, res: NextApiResponse) {
if (!req.session.user) {
res.status(401).json({ok: false});
@@ -49,16 +58,16 @@ async function post(req: NextApiRequest, res: NextApiResponse) {
});
const body = req.body as Grading;
await setDoc(doc(db, "grading", req.session.user.id), body);
await updateGrading(req.session.user.id, body);
if (req.session.user.type === "mastercorporate") {
const groups = await getGroupsForUser(req.session.user.id);
const participants = uniq(groups.flatMap((x) => x.participants));
const participantUsers = await Promise.all(participants.map(getUser));
const participantUsers = await getSpecificUsers(participants);
const corporateUsers = participantUsers.filter((x) => x?.type === "corporate") as CorporateUser[];
await Promise.all(corporateUsers.map(async (g) => await setDoc(doc(db, "grading", g.id), body)));
await Promise.all(corporateUsers.map(async (g) => await updateGrading(g.id, body)));
}
res.status(200).json({ok: true});