Merge remote-tracking branch 'origin/develop' into feature/level-file-upload
This commit is contained in:
@@ -80,19 +80,15 @@ export default function UserList({
|
||||
|
||||
useEffect(() => {
|
||||
(async () => {
|
||||
if (user && users) {
|
||||
const filterUsers = ["corporate", "teacher", "mastercorporate"].includes(user.type)
|
||||
? users.filter((u) => groups.flatMap((g) => g.participants).includes(u.id))
|
||||
: users;
|
||||
|
||||
const filteredUsers = filters.reduce((d, f) => d.filter(f), filterUsers);
|
||||
if (users) {
|
||||
const filteredUsers = filters.reduce((d, f) => d.filter(f), users);
|
||||
const sortedUsers = await asyncSorter<User>(filteredUsers, sortFunction);
|
||||
|
||||
setDisplayUsers([...sortedUsers]);
|
||||
}
|
||||
})();
|
||||
// eslint-disable-next-line react-hooks/exhaustive-deps
|
||||
}, [user, users, sorter, groups]);
|
||||
}, [users, sorter]);
|
||||
|
||||
const deleteAccount = (user: User) => {
|
||||
if (!confirm(`Are you sure you want to delete ${user.name}'s account?`)) return;
|
||||
|
||||
@@ -30,30 +30,10 @@ async function get(req: NextApiRequest, res: NextApiResponse) {
|
||||
participant: string;
|
||||
};
|
||||
|
||||
if (req.session?.user?.type === "mastercorporate") {
|
||||
try {
|
||||
const masterCorporateGroups = await getGroupsForUser(admin, participant);
|
||||
const participants = uniq(masterCorporateGroups.flatMap((g) => g.participants));
|
||||
const corporatesFromMaster = (await Promise.all(participants.map(getUser))).filter((x) => x.type === "corporate");
|
||||
|
||||
if (corporatesFromMaster.length === 0) return res.status(200).json(masterCorporateGroups);
|
||||
|
||||
const groups = await Promise.all(corporatesFromMaster.map((c) => getGroupsForUser(c.id, participant)));
|
||||
return res.status(200).json([...masterCorporateGroups, ...uniqBy(groups.flat(), "id")]);
|
||||
} catch (e) {
|
||||
console.error(e);
|
||||
res.status(500).json({ok: false});
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
try {
|
||||
const groups = await getGroupsForUser(admin, participant);
|
||||
res.status(200).json(groups);
|
||||
} catch (e) {
|
||||
console.error(e);
|
||||
res.status(500).json({ok: false});
|
||||
}
|
||||
const adminGroups = await getGroupsForUser(admin, participant);
|
||||
const participants = uniq(adminGroups.flatMap((g) => g.participants));
|
||||
const groups = await Promise.all(participants.map(async (c) => await getGroupsForUser(c, participant)));
|
||||
return res.status(200).json([...adminGroups, ...uniqBy(groups.flat(), "id")]);
|
||||
}
|
||||
|
||||
async function post(req: NextApiRequest, res: NextApiResponse) {
|
||||
|
||||
@@ -4,10 +4,12 @@ import {getFirestore, setDoc, doc, query, collection, where, getDocs, getDoc, de
|
||||
import {withIronSessionApiRoute} from "iron-session/next";
|
||||
import {sessionOptions} from "@/lib/session";
|
||||
import {v4} from "uuid";
|
||||
import {CorporateUser, Group} from "@/interfaces/user";
|
||||
import {CorporateUser, Group, Type} from "@/interfaces/user";
|
||||
import {createUserWithEmailAndPassword, getAuth} from "firebase/auth";
|
||||
import ShortUniqueId from "short-unique-id";
|
||||
import {getUserCorporate} from "@/utils/groups.be";
|
||||
import {getUserCorporate, getUserGroups} from "@/utils/groups.be";
|
||||
import {uniq} from "lodash";
|
||||
import {getUser} from "@/utils/users.be";
|
||||
|
||||
const DEFAULT_DESIRED_LEVELS = {
|
||||
reading: 9,
|
||||
@@ -28,6 +30,13 @@ const db = getFirestore(app);
|
||||
|
||||
export default withIronSessionApiRoute(handler, sessionOptions);
|
||||
|
||||
const getUsersOfType = async (admin: string, type: Type) => {
|
||||
const groups = await getUserGroups(admin);
|
||||
const users = await Promise.all(uniq(groups.flatMap((x) => x.participants)).map(getUser));
|
||||
|
||||
return users.filter((x) => x.type === type).map((x) => x.id);
|
||||
};
|
||||
|
||||
async function handler(req: NextApiRequest, res: NextApiResponse) {
|
||||
if (req.method === "POST") return post(req, res);
|
||||
|
||||
@@ -106,11 +115,14 @@ async function post(req: NextApiRequest, res: NextApiResponse) {
|
||||
});
|
||||
|
||||
if (type === "corporate") {
|
||||
const students = maker.type === "corporate" ? await getUsersOfType(maker.id, "student") : [];
|
||||
const teachers = maker.type === "corporate" ? await getUsersOfType(maker.id, "teacher") : [];
|
||||
|
||||
const defaultTeachersGroup: Group = {
|
||||
admin: userId,
|
||||
id: v4(),
|
||||
name: "Teachers",
|
||||
participants: [],
|
||||
participants: teachers,
|
||||
disableEditing: true,
|
||||
};
|
||||
|
||||
@@ -118,21 +130,12 @@ async function post(req: NextApiRequest, res: NextApiResponse) {
|
||||
admin: userId,
|
||||
id: v4(),
|
||||
name: "Students",
|
||||
participants: [],
|
||||
disableEditing: true,
|
||||
};
|
||||
|
||||
const defaultCorporateGroup: Group = {
|
||||
admin: userId,
|
||||
id: v4(),
|
||||
name: "Corporate",
|
||||
participants: [],
|
||||
participants: students,
|
||||
disableEditing: true,
|
||||
};
|
||||
|
||||
await setDoc(doc(db, "groups", defaultTeachersGroup.id), defaultTeachersGroup);
|
||||
await setDoc(doc(db, "groups", defaultStudentsGroup.id), defaultStudentsGroup);
|
||||
await setDoc(doc(db, "groups", defaultCorporateGroup.id), defaultCorporateGroup);
|
||||
}
|
||||
|
||||
if (!!corporate) {
|
||||
|
||||
@@ -4,6 +4,8 @@ import {app} from "@/firebase";
|
||||
import {getFirestore, collection, getDocs, query, where, doc, setDoc, addDoc, getDoc} from "firebase/firestore";
|
||||
import {withIronSessionApiRoute} from "iron-session/next";
|
||||
import {sessionOptions} from "@/lib/session";
|
||||
import {Session} from "@/hooks/useSessions";
|
||||
import moment from "moment";
|
||||
|
||||
const db = getFirestore(app);
|
||||
|
||||
@@ -24,12 +26,17 @@ async function get(req: NextApiRequest, res: NextApiResponse) {
|
||||
|
||||
const q = user ? query(collection(db, "sessions"), where("user", "==", user)) : collection(db, "sessions");
|
||||
const snapshot = await getDocs(q);
|
||||
const sessions = snapshot.docs.map((doc) => ({
|
||||
id: doc.id,
|
||||
...doc.data(),
|
||||
})) as Session[];
|
||||
|
||||
res.status(200).json(
|
||||
snapshot.docs.map((doc) => ({
|
||||
id: doc.id,
|
||||
...doc.data(),
|
||||
})),
|
||||
sessions.filter((x) => {
|
||||
if (!x.assignment) return true;
|
||||
if (x.assignment.results.filter((y) => y.user === user).length > 0) return false;
|
||||
return !moment().isAfter(moment(x.assignment.endDate));
|
||||
}),
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
@@ -4,6 +4,8 @@ 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);
|
||||
|
||||
@@ -16,11 +18,17 @@ async function handler(req: NextApiRequest, res: NextApiResponse) {
|
||||
}
|
||||
|
||||
const snapshot = await getDocs(collection(db, "users"));
|
||||
const users = snapshot.docs.map((doc) => ({
|
||||
id: doc.id,
|
||||
...doc.data(),
|
||||
}));
|
||||
|
||||
res.status(200).json(
|
||||
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);
|
||||
|
||||
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)));
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user