From 8c94bcac526367d52b8b634b77f9f61dd6da295d Mon Sep 17 00:00:00 2001 From: Tiago Ribeiro Date: Sat, 7 Sep 2024 13:02:47 +0100 Subject: [PATCH] Corrected a problem with too many participants --- src/utils/users.be.ts | 27 +++++++++++++++++++++------ 1 file changed, 21 insertions(+), 6 deletions(-) diff --git a/src/utils/users.be.ts b/src/utils/users.be.ts index 97b17bef..ee28955f 100644 --- a/src/utils/users.be.ts +++ b/src/utils/users.be.ts @@ -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) { const q = [ ...(!!type ? [where("type", "==", type)] : []), - orderBy(documentId()), + orderBy("registrationDate"), ...(!!firstID && !lastID ? [endBefore(firstID)] : []), ...(!!lastID && !firstID ? [startAfter(lastID)] : []), ...(!!size ? [limit(size)] : []), @@ -87,19 +87,34 @@ 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 === 0) return {users: [], total: 0}; - const snapshot = await getDocs(query(collection(db, "users"), ...[where(documentId(), "in", participants), ...q])); + 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 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 total = await getCountFromServer(query(collection(db, "users"), ...[where(documentId(), "in", participants), ...totalQ])); + const participantUsers = users.filter((x) => participants.includes(x.id)); return { - users, - total: total.data().count, + users: participantUsers, + total: participantUsers.length, }; }