Improved a bit of the speed of the application

This commit is contained in:
Tiago Ribeiro
2024-12-11 18:32:29 +00:00
parent 76cbf8dc41
commit ce35ba71f4
6 changed files with 148 additions and 163 deletions

View File

@@ -60,6 +60,12 @@ export const getEntitiesAssignments = async (ids: string[]) => {
.toArray();
};
export const countEntitiesAssignments = async (ids: string[], filter?: object) => {
return await db
.collection("assignments")
.countDocuments({ entity: { $in: ids }, ...(filter || {}) })
};
export const getAssignmentsForCorporates = async (userType: Type, idsList: string[], startDate?: Date, endDate?: Date) => {
const assigners = await Promise.all(
idsList.map(async (id) => {

View File

@@ -173,3 +173,9 @@ export const getGroupsByEntities = async (ids: string[]): Promise<WithEntity<Gro
{ $match: { entity: { $in: ids } } },
...addEntityToGroupPipeline
]).toArray()
export const countGroups = async () =>
await db.collection("groups").countDocuments({})
export const countGroupsByEntities = async (ids: string[]) =>
await db.collection("groups").countDocuments({ entity: { $in: ids } })

View File

@@ -11,12 +11,19 @@ import { mapBy } from ".";
const db = client.db(process.env.MONGODB_DB);
export async function getUsers(filter?: object) {
export async function getUsers(filter?: object, limit = 0, sort = {}) {
return await db
.collection("users")
.find<User>(filter || {}, { projection: { _id: 0 } })
.limit(limit)
.sort(sort)
.toArray();
}
export async function countUsers(filter?: object) {
return await db
.collection("users")
.countDocuments(filter || {})
}
export async function getUserWithEntity(id: string): Promise<WithEntities<User> | undefined> {
const user = await db.collection("users").findOne<User>({ id: id }, { projection: { _id: 0 } });
@@ -68,8 +75,8 @@ export async function getEntitiesUsers(ids: string[], filter?: object, limit?: n
.toArray();
}
export async function countEntitiesUsers(ids: string[]) {
return await db.collection("users").countDocuments({ "entities.id": { $in: ids } });
export async function countEntitiesUsers(ids: string[], filter?: object) {
return await db.collection("users").countDocuments({ "entities.id": { $in: ids }, ...(filter || {}) });
}
export async function getLinkedUsers(
@@ -154,3 +161,17 @@ export const filterAllowedUsers = async (user: User, entities: EntityWithRoles[]
return [...students, ...teachers, ...corporates, ...masterCorporates]
}
export const countAllowedUsers = async (user: User, entities: EntityWithRoles[]) => {
const studentsAllowedEntities = findAllowedEntities(user, entities, 'view_students')
const teachersAllowedEntities = findAllowedEntities(user, entities, 'view_teachers')
const corporateAllowedEntities = findAllowedEntities(user, entities, 'view_corporates')
const masterCorporateAllowedEntities = findAllowedEntities(user, entities, 'view_mastercorporates')
const students = await countEntitiesUsers(mapBy(studentsAllowedEntities, 'id'), { type: "student" })
const teachers = await countEntitiesUsers(mapBy(teachersAllowedEntities, 'id'), { type: "teacher" })
const corporates = await countEntitiesUsers(mapBy(corporateAllowedEntities, 'id'), { type: "corporate" })
const masterCorporates = await countEntitiesUsers(mapBy(masterCorporateAllowedEntities, 'id'), { type: "mastercorporate" })
return { students, teachers, corporates, masterCorporates }
}