Extracted the function
This commit is contained in:
@@ -1,13 +1,8 @@
|
||||
// Next.js API route support: https://nextjs.org/docs/api-routes/introduction
|
||||
import type {NextApiRequest, NextApiResponse} from "next";
|
||||
import {app} from "@/firebase";
|
||||
import {getFirestore, collection, getDocs} from "firebase/firestore";
|
||||
import {withIronSessionApiRoute} from "iron-session/next";
|
||||
import {sessionOptions} from "@/lib/session";
|
||||
import {getGroupsForUser} from "@/utils/groups.be";
|
||||
import {uniq} from "lodash";
|
||||
|
||||
const db = getFirestore(app);
|
||||
import {getLinkedUsers} from "@/utils/users.be";
|
||||
|
||||
export default withIronSessionApiRoute(handler, sessionOptions);
|
||||
|
||||
@@ -17,25 +12,6 @@ async function handler(req: NextApiRequest, res: NextApiResponse) {
|
||||
return;
|
||||
}
|
||||
|
||||
const snapshot = await getDocs(collection(db, "users"));
|
||||
const users = snapshot.docs.map((doc) => ({
|
||||
id: doc.id,
|
||||
...doc.data(),
|
||||
}));
|
||||
|
||||
if (!req.session.user) return res.status(200).json(users);
|
||||
if (req.session.user.type === "admin" || req.session.user.type === "developer") return res.status(200).json(users);
|
||||
if (req.session.user.type === "teacher") {
|
||||
const adminGroups = await getGroupsForUser(req.session.user.id);
|
||||
const belongingGroups = await getGroupsForUser(undefined, req.session.user.id);
|
||||
|
||||
const participants = uniq([...adminGroups.flatMap((x) => x.participants), ...belongingGroups.flat().flatMap((x) => x.participants)]);
|
||||
return res.status(200).json(users.filter((x) => participants.includes(x.id) && x.id !== req.session.user!.id));
|
||||
}
|
||||
|
||||
const adminGroups = await getGroupsForUser(req.session.user.id);
|
||||
const groups = await Promise.all(adminGroups.flatMap((x) => x.participants).map(async (x) => await getGroupsForUser(x)));
|
||||
const participants = uniq([...adminGroups.flatMap((x) => x.participants), ...groups.flat().flatMap((x) => x.participants)]);
|
||||
|
||||
res.status(200).json(users.filter((x) => participants.includes(x.id) && x.id !== req.session.user!.id));
|
||||
const users = await getLinkedUsers(req.session.user?.id, req.session.user?.type);
|
||||
res.status(200).json(users);
|
||||
}
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
import {app} from "@/firebase";
|
||||
|
||||
import {collection, doc, getDoc, getDocs, getFirestore, query, where} from "firebase/firestore";
|
||||
import {CorporateUser, Group, User} from "@/interfaces/user";
|
||||
import {CorporateUser, Group, Type, User} from "@/interfaces/user";
|
||||
import {getGroupsForUser} from "./groups.be";
|
||||
import {uniq, uniqBy} from "lodash";
|
||||
import {getUserCodes} from "./codes.be";
|
||||
@@ -38,6 +38,29 @@ export async function getSpecificUsers(ids: string[]) {
|
||||
return groups;
|
||||
}
|
||||
|
||||
export async function getLinkedUsers(userID?: string, type?: Type) {
|
||||
const snapshot = await getDocs(collection(db, "users"));
|
||||
const users = snapshot.docs.map((doc) => ({
|
||||
id: doc.id,
|
||||
...doc.data(),
|
||||
})) as User[];
|
||||
|
||||
if (!userID) return users;
|
||||
if (type === "admin" || type === "developer") return users;
|
||||
|
||||
const adminGroups = await getGroupsForUser(userID);
|
||||
const groups = await Promise.all(adminGroups.flatMap((x) => x.participants).map(async (x) => await getGroupsForUser(x)));
|
||||
const belongingGroups = await getGroupsForUser(undefined, userID);
|
||||
|
||||
const participants = uniq([
|
||||
...adminGroups.flatMap((x) => x.participants),
|
||||
...groups.flat().flatMap((x) => x.participants),
|
||||
...(type === "teacher" ? belongingGroups.flatMap((x) => x.participants) : []),
|
||||
]);
|
||||
|
||||
return users.filter((x) => participants.includes(x.id) && x.id !== userID);
|
||||
}
|
||||
|
||||
export async function getUserBalance(user: User) {
|
||||
const codes = await getUserCodes(user.id);
|
||||
if (user.type !== "corporate" && user.type !== "mastercorporate") return codes.length;
|
||||
|
||||
Reference in New Issue
Block a user