Updated the MasterCorporate with the improved queries
This commit is contained in:
@@ -1,6 +1,20 @@
|
||||
import {app} from "@/firebase";
|
||||
|
||||
import {collection, doc, getDoc, getDocs, getFirestore, query, where} from "firebase/firestore";
|
||||
import {
|
||||
collection,
|
||||
doc,
|
||||
documentId,
|
||||
endAt,
|
||||
getDoc,
|
||||
getDocs,
|
||||
getFirestore,
|
||||
limit,
|
||||
orderBy,
|
||||
query,
|
||||
startAfter,
|
||||
startAt,
|
||||
where,
|
||||
} from "firebase/firestore";
|
||||
import {CorporateUser, Group, Type, User} from "@/interfaces/user";
|
||||
import {getGroupsForUser} from "./groups.be";
|
||||
import {uniq, uniqBy} from "lodash";
|
||||
@@ -38,15 +52,16 @@ 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[];
|
||||
export async function getLinkedUsers(userID?: string, userType?: Type, type?: Type, page?: number, size?: number) {
|
||||
if (!userID || userType === "admin" || userType === "developer") {
|
||||
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;
|
||||
return users;
|
||||
}
|
||||
|
||||
const adminGroups = await getGroupsForUser(userID);
|
||||
const groups = await Promise.all(adminGroups.flatMap((x) => x.participants).map(async (x) => await getGroupsForUser(x)));
|
||||
@@ -55,10 +70,24 @@ export async function getLinkedUsers(userID?: string, type?: Type) {
|
||||
const participants = uniq([
|
||||
...adminGroups.flatMap((x) => x.participants),
|
||||
...groups.flat().flatMap((x) => x.participants),
|
||||
...(type === "teacher" ? belongingGroups.flatMap((x) => x.participants) : []),
|
||||
...(userType === "teacher" ? belongingGroups.flatMap((x) => x.participants) : []),
|
||||
]);
|
||||
|
||||
return users.filter((x) => participants.includes(x.id) && x.id !== userID);
|
||||
const q = [
|
||||
where(documentId(), "in", participants),
|
||||
...(!!type ? [where("type", "==", type)] : []),
|
||||
orderBy(documentId()),
|
||||
...(page !== undefined && !!size ? [startAt(page * size)] : []),
|
||||
...(page !== undefined && !!size ? [limit(page + 1 * size)] : []),
|
||||
];
|
||||
|
||||
const snapshot = await getDocs(query(collection(db, "users"), ...q));
|
||||
const users = snapshot.docs.map((doc) => ({
|
||||
id: doc.id,
|
||||
...doc.data(),
|
||||
})) as User[];
|
||||
|
||||
return users;
|
||||
}
|
||||
|
||||
export async function getUserBalance(user: User) {
|
||||
|
||||
Reference in New Issue
Block a user