Updated the groups and users

This commit is contained in:
Tiago Ribeiro
2024-09-07 15:13:13 +01:00
parent c0b814081e
commit e33fa00fa3
4 changed files with 72 additions and 137 deletions

View File

@@ -18,67 +18,46 @@ import {
where,
} from "firebase/firestore";
import {CorporateUser, Group, Type, User} from "@/interfaces/user";
import {getGroupsForUser} from "./groups.be";
import {getGroupsForUser, getParticipantGroups, getUserGroups, getUsersGroups} from "./groups.be";
import {last, uniq, uniqBy} from "lodash";
import {getUserCodes} from "./codes.be";
import moment from "moment";
const db = getFirestore(app);
import client from "@/lib/mongodb";
const db = client.db(process.env.MONGODB_DB);
export async function getUsers() {
const snapshot = await getDocs(collection(db, "users"));
return snapshot.docs.map((doc) => ({
...doc.data(),
id: doc.id,
registrationDate: moment(doc.data().registrationDate).toISOString(),
})) as unknown as User[];
return await db.collection("users").find<User>({}).toArray();
}
export async function getUser(id: string) {
const userDoc = await getDoc(doc(db, "users", id));
return {...userDoc.data(), id, registrationDate: moment(userDoc.data()?.registrationDate).toISOString()} as unknown as User;
export async function getUser(id: string): Promise<User | undefined> {
const user = await db.collection("users").findOne<User>({id});
return !!user ? user : undefined;
}
export async function getSpecificUsers(ids: string[]) {
if (ids.length === 0) return [];
const snapshot = await getDocs(query(collection(db, "users"), where("id", "in", ids)));
const groups = snapshot.docs.map((doc) => ({
...doc.data(),
id: doc.id,
registrationDate: moment(doc.data().registrationDate).toISOString(),
})) as unknown as User[];
return groups;
return await db
.collection("users")
.find<User>({id: {$in: ids}})
.toArray();
}
export async function getLinkedUsers(userID?: string, userType?: Type, type?: Type, firstID?: string, lastID?: string, size?: number) {
const q = [
...(!!type ? [where("type", "==", type)] : []),
orderBy("registrationDate"),
...(!!firstID && !lastID ? [endBefore(firstID)] : []),
...(!!lastID && !firstID ? [startAfter(lastID)] : []),
...(!!size ? [limit(size)] : []),
];
const totalQ = [...(!!type ? [where("type", "==", type)] : []), orderBy(documentId())];
const filters = {
...(!!type ? {type} : {}),
};
if (!userID || userType === "admin" || userType === "developer") {
const snapshot = await getDocs(query(collection(db, "users"), ...q));
const users = snapshot.docs.map((doc) => ({
id: doc.id,
...doc.data(),
})) as User[];
const total = await getCountFromServer(query(collection(db, "users"), ...totalQ));
return {users, total: total.data().count};
const users = await db.collection("users").find<User>(filters).toArray();
const total = await db.collection("users").countDocuments(filters);
return {users, total};
}
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 adminGroups = await getUserGroups(userID);
const groups = await getUsersGroups(adminGroups.flatMap((x) => x.participants));
const belongingGroups = await getParticipantGroups(userID);
const participants = uniq([
...adminGroups.flatMap((x) => x.participants),
@@ -89,33 +68,13 @@ export async function getLinkedUsers(userID?: string, userType?: Type, type?: Ty
// [FirebaseError: Invalid Query. A non-empty array is required for 'in' filters.] {
if (participants.length === 0) return {users: [], total: 0};
if (participants.length < 30) {
const snapshot = await getDocs(query(collection(db, "users"), ...[where(documentId(), "in", participants), ...q]));
const users = snapshot.docs.map((doc) => ({
id: doc.id,
...doc.data(),
})) as User[];
const users = await db
.collection("users")
.find<User>({...filters, id: {$in: participants}})
.toArray();
const total = await db.collection("users").countDocuments({...filters, id: {$in: participants}});
const total = await getCountFromServer(query(collection(db, "users"), ...[where(documentId(), "in", participants), ...totalQ]));
return {
users,
total: total.data().count,
};
}
const snapshot = await getDocs(query(collection(db, "users"), ...q));
const users = snapshot.docs.map((doc) => ({
id: doc.id,
...doc.data(),
})) as User[];
const participantUsers = users.filter((x) => participants.includes(x.id));
return {
users: participantUsers,
total: participantUsers.length,
};
return {users, total};
}
export async function getUserBalance(user: User) {
@@ -128,7 +87,7 @@ export async function getUserBalance(user: User) {
if (user.type === "corporate") return participants.length + codes.filter((x) => !participants.includes(x.userId || "")).length;
const participantUsers = await Promise.all(participants.map(getUser));
const corporateUsers = participantUsers.filter((x) => x.type === "corporate") as CorporateUser[];
const corporateUsers = participantUsers.filter((x) => x?.type === "corporate") as CorporateUser[];
return (
corporateUsers.reduce((acc, curr) => acc + curr.corporateInformation?.companyInformation?.userAmount || 0, 0) +