Continued improving this

This commit is contained in:
Tiago Ribeiro
2024-09-06 17:06:49 +01:00
parent 4530e4079f
commit cfe297cc38
7 changed files with 76 additions and 19 deletions

View File

@@ -5,6 +5,8 @@ import {
doc,
documentId,
endAt,
endBefore,
getCountFromServer,
getDoc,
getDocs,
getFirestore,
@@ -17,7 +19,7 @@ import {
} from "firebase/firestore";
import {CorporateUser, Group, Type, User} from "@/interfaces/user";
import {getGroupsForUser} from "./groups.be";
import {uniq, uniqBy} from "lodash";
import {last, uniq, uniqBy} from "lodash";
import {getUserCodes} from "./codes.be";
import moment from "moment";
const db = getFirestore(app);
@@ -52,14 +54,17 @@ export async function getSpecificUsers(ids: string[]) {
return groups;
}
export async function getLinkedUsers(userID?: string, userType?: Type, type?: Type, page?: number, size?: number) {
export async function getLinkedUsers(userID?: string, userType?: Type, type?: Type, firstID?: string, lastID?: string, size?: number) {
const q = [
...(!!type ? [where("type", "==", type)] : []),
orderBy(documentId()),
...(page !== undefined && !!size ? [startAt(page * size)] : []),
...(page !== undefined && !!size ? [limit(page + 1 * size)] : []),
...(!!firstID && !lastID ? [endBefore(firstID)] : []),
...(!!lastID && !firstID ? [startAfter(lastID)] : []),
...(!!size ? [limit(size)] : []),
];
const totalQ = [...(!!type ? [where("type", "==", type)] : []), orderBy(documentId())];
if (!userID || userType === "admin" || userType === "developer") {
const snapshot = await getDocs(query(collection(db, "users"), ...q));
const users = snapshot.docs.map((doc) => ({
@@ -67,7 +72,8 @@ export async function getLinkedUsers(userID?: string, userType?: Type, type?: Ty
...doc.data(),
})) as User[];
return users;
const total = await getCountFromServer(query(collection(db, "users"), ...totalQ));
return {users, total: total.data().count};
}
const adminGroups = await getGroupsForUser(userID);
@@ -86,7 +92,12 @@ export async function getLinkedUsers(userID?: string, userType?: Type, type?: Ty
...doc.data(),
})) as User[];
return users;
const total = await getCountFromServer(query(collection(db, "users"), ...[where(documentId(), "in", participants), ...totalQ]));
return {
users,
total: total.data().count,
};
}
export async function getUserBalance(user: User) {