|
|
|
|
@@ -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 student = await countEntitiesUsers(mapBy(studentsAllowedEntities, 'id'), { type: "student" })
|
|
|
|
|
const teacher = await countEntitiesUsers(mapBy(teachersAllowedEntities, 'id'), { type: "teacher" })
|
|
|
|
|
const corporate = await countEntitiesUsers(mapBy(corporateAllowedEntities, 'id'), { type: "corporate" })
|
|
|
|
|
const masterCorporate = await countEntitiesUsers(mapBy(masterCorporateAllowedEntities, 'id'), { type: "mastercorporate" })
|
|
|
|
|
|
|
|
|
|
return { student, teacher, corporate, masterCorporate }
|
|
|
|
|
}
|
|
|
|
|
|