Corrected a problem with too many participants

This commit is contained in:
Tiago Ribeiro
2024-09-07 13:02:47 +01:00
parent 8803a8c166
commit 8c94bcac52

View File

@@ -57,7 +57,7 @@ export async function getSpecificUsers(ids: string[]) {
export async function getLinkedUsers(userID?: string, userType?: Type, type?: Type, firstID?: string, lastID?: string, size?: number) { export async function getLinkedUsers(userID?: string, userType?: Type, type?: Type, firstID?: string, lastID?: string, size?: number) {
const q = [ const q = [
...(!!type ? [where("type", "==", type)] : []), ...(!!type ? [where("type", "==", type)] : []),
orderBy(documentId()), orderBy("registrationDate"),
...(!!firstID && !lastID ? [endBefore(firstID)] : []), ...(!!firstID && !lastID ? [endBefore(firstID)] : []),
...(!!lastID && !firstID ? [startAfter(lastID)] : []), ...(!!lastID && !firstID ? [startAfter(lastID)] : []),
...(!!size ? [limit(size)] : []), ...(!!size ? [limit(size)] : []),
@@ -87,8 +87,9 @@ export async function getLinkedUsers(userID?: string, userType?: Type, type?: Ty
]); ]);
// [FirebaseError: Invalid Query. A non-empty array is required for 'in' filters.] { // [FirebaseError: Invalid Query. A non-empty array is required for 'in' filters.] {
if(participants.length === 0) return {users: [], total: 0}; 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 snapshot = await getDocs(query(collection(db, "users"), ...[where(documentId(), "in", participants), ...q]));
const users = snapshot.docs.map((doc) => ({ const users = snapshot.docs.map((doc) => ({
id: doc.id, id: doc.id,
@@ -101,6 +102,20 @@ export async function getLinkedUsers(userID?: string, userType?: Type, type?: Ty
users, users,
total: total.data().count, 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,
};
} }
export async function getUserBalance(user: User) { export async function getUserBalance(user: User) {