Pagination on UserList

This commit is contained in:
Carlos Mesquita
2024-09-09 01:22:13 +01:00
parent 02564c8426
commit 9177a6b2ac
4 changed files with 111 additions and 108 deletions

View File

@@ -1,7 +1,7 @@
import {CorporateUser, Group, Type, User} from "@/interfaces/user";
import {getGroupsForUser, getParticipantGroups, getUserGroups, getUsersGroups} from "./groups.be";
import {last, uniq, uniqBy} from "lodash";
import {getUserCodes} from "./codes.be";
import { CorporateUser, Group, Type, User } from "@/interfaces/user";
import { getGroupsForUser, getParticipantGroups, getUserGroups, getUsersGroups } from "./groups.be";
import { last, uniq, uniqBy } from "lodash";
import { getUserCodes } from "./codes.be";
import moment from "moment";
import client from "@/lib/mongodb";
@@ -12,7 +12,7 @@ export async function getUsers() {
}
export async function getUser(id: string): Promise<User | undefined> {
const user = await db.collection("users").findOne<User>({id});
const user = await db.collection("users").findOne<User>({ id });
return !!user ? user : undefined;
}
@@ -21,7 +21,7 @@ export async function getSpecificUsers(ids: string[]) {
return await db
.collection("users")
.find<User>({id: {$in: ids}})
.find<User>({ id: { $in: ids } })
.toArray();
}
@@ -33,21 +33,32 @@ export async function getLinkedUsers(
size?: number,
sort?: string,
direction?: "asc" | "desc",
searchTerm?: string | undefined,
) {
const filters = {
...(!!type ? {type} : {}),
};
const filters: any = {};
if (type) {
filters.type = type;
}
if (searchTerm) {
filters.$or = [
{ name: { $regex: searchTerm, $options: 'i' } },
{ email: { $regex: searchTerm, $options: 'i' } },
{ company: { $regex: searchTerm, $options: 'i' } },
];
}
if (!userID || userType === "admin" || userType === "developer") {
const users = await db
.collection("users")
.find<User>(filters)
.sort(sort ? {[sort]: direction === "desc" ? -1 : 1} : {})
.sort(sort ? { [sort]: direction === "desc" ? -1 : 1 } : {})
.skip(page && size ? page * size : 0)
.limit(size || 0)
.toArray();
const total = await db.collection("users").countDocuments(filters);
return {users, total};
return { users, total };
}
const adminGroups = await getUserGroups(userID);
@@ -61,17 +72,17 @@ export async function getLinkedUsers(
]);
// [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 users = await db
.collection("users")
.find<User>({...filters, id: {$in: participants}})
.find<User>({ ...filters, id: { $in: participants } })
.skip(page && size ? page * size : 0)
.limit(size || 0)
.toArray();
const total = await db.collection("users").countDocuments({...filters, id: {$in: participants}});
const total = await db.collection("users").countDocuments({ ...filters, id: { $in: participants } });
return {users, total};
return { users, total };
}
export async function getUserBalance(user: User) {