ENCOA-314 :
- Implemented Async Select - Changed Stats Page User fetching to use Async Select and only fetch the User data when it needs - Changed Record Filter to use Async Select - Changed useTicketListener to only fetch needed data - Added Sort/Projection to remove unnecessary data processing. - Removed some unnecessary data processing.
This commit is contained in:
@@ -11,14 +11,64 @@ import { mapBy } from ".";
|
||||
|
||||
const db = client.db(process.env.MONGODB_DB);
|
||||
|
||||
export async function getUsers(filter?: object, limit = 0, sort = {}) {
|
||||
export async function getUsers(filter?: object, limit = 0, sort = {}, projection = {}) {
|
||||
return await db
|
||||
.collection("users")
|
||||
.find<User>(filter || {}, { projection: { _id: 0 } })
|
||||
.find<User>(filter || {}, { projection: { _id: 0, ...projection } })
|
||||
.limit(limit)
|
||||
.sort(sort)
|
||||
.toArray();
|
||||
}
|
||||
|
||||
export async function searchUsers(searchInput?: string, limit = 50, page = 0, sort: object = { "name": 1 }, projection = {}, filter?: object) {
|
||||
const compoundFilter = {
|
||||
"compound": {
|
||||
"should": [...(searchInput != "" ? [{
|
||||
"autocomplete": {
|
||||
"query": searchInput,
|
||||
"path": "name",
|
||||
}
|
||||
}, {
|
||||
"autocomplete": {
|
||||
"query": searchInput,
|
||||
"path": "email",
|
||||
}
|
||||
}] : [{ exists: { path: "name" } }])],
|
||||
"minimumShouldMatch": 1,
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
const [{ users, totalUsers }] = await db
|
||||
.collection("users").aggregate([
|
||||
{
|
||||
"$search": {
|
||||
"index": "UsersByNameEmail",
|
||||
...compoundFilter
|
||||
}
|
||||
},
|
||||
...(filter ? [{
|
||||
"$match": Object.entries(filter).reduce((accm, [key, value]) => {
|
||||
if (value.length === 0) return accm
|
||||
accm[key] = Array.isArray(value) ? { $in: value } : value
|
||||
return accm;
|
||||
}, {} as any)
|
||||
}] : []),
|
||||
{
|
||||
$facet: {
|
||||
totalUsers: [{ $count: "count" }],
|
||||
users: [
|
||||
{ $sort: sort },
|
||||
{ $skip: limit * page },
|
||||
{ $limit: limit },
|
||||
{ "$project": { _id: 0, label: { $concat: ["$name", " - ", "$email"] }, value: "$id", ...projection } }
|
||||
]
|
||||
}
|
||||
}
|
||||
]).toArray();
|
||||
return { users, total: totalUsers[0]?.count || 0 };
|
||||
}
|
||||
|
||||
export async function countUsers(filter?: object) {
|
||||
return await db
|
||||
.collection("users")
|
||||
@@ -67,10 +117,10 @@ export async function countEntityUsers(id: string, filter?: object) {
|
||||
return await db.collection("users").countDocuments({ "entities.id": id, ...(filter || {}) });
|
||||
}
|
||||
|
||||
export async function getEntitiesUsers(ids: string[], filter?: object, limit?: number) {
|
||||
export async function getEntitiesUsers(ids: string[], filter?: object, limit?: number, projection = {}) {
|
||||
return await db
|
||||
.collection("users")
|
||||
.find<User>({ "entities.id": { $in: ids }, ...(filter || {}) })
|
||||
.find<User>({ "entities.id": { $in: ids }, ...(filter || {}) }, projection)
|
||||
.limit(limit || 0)
|
||||
.toArray();
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user