Continued creating the entity system

This commit is contained in:
Tiago Ribeiro
2024-10-01 17:39:43 +01:00
parent bae02e5192
commit 564e6438cb
37 changed files with 2522 additions and 130 deletions

View File

@@ -1,9 +1,11 @@
import {CorporateUser, Group, Type, User} from "@/interfaces/user";
import {CorporateUser, Type, User} from "@/interfaces/user";
import {getGroupsForUser, getParticipantGroups, getUserGroups, getUsersGroups} from "./groups.be";
import {last, uniq, uniqBy} from "lodash";
import {uniq} from "lodash";
import {getUserCodes} from "./codes.be";
import moment from "moment";
import client from "@/lib/mongodb";
import {WithEntity} from "@/interfaces/entity";
import {getEntity} from "./entities.be";
import {getRole} from "./roles.be";
const db = client.db(process.env.MONGODB_DB);
@@ -14,6 +16,22 @@ export async function getUsers() {
.toArray();
}
export async function getUserWithEntity(id: string): Promise<WithEntity<User> | undefined> {
const user = await db.collection("users").findOne<User>({id: id}, {projection: {_id: 0}});
if (!user) return undefined;
const entities = await Promise.all(
user.entities.map(async (e) => {
const entity = await getEntity(e.id);
const role = await getRole(e.role);
return {entity, role};
}),
);
return {...user, entities};
}
export async function getUser(id: string): Promise<User | undefined> {
const user = await db.collection("users").findOne<User>({id: id}, {projection: {_id: 0}});
return !!user ? user : undefined;
@@ -28,6 +46,30 @@ export async function getSpecificUsers(ids: string[]) {
.toArray();
}
export async function getEntityUsers(id: string, limit?: number) {
return await db
.collection("users")
.find<User>({"entities.id": id})
.limit(limit || 0)
.toArray();
}
export async function countEntityUsers(id: string) {
return await db.collection("users").countDocuments({"entities.id": id});
}
export async function getEntitiesUsers(ids: string[], limit?: number) {
return await db
.collection("users")
.find<User>({"entities.id": {$in: ids}})
.limit(limit || 0)
.toArray();
}
export async function countEntitiesUsers(ids: string[]) {
return await db.collection("users").countDocuments({"entities.id": {$in: ids}});
}
export async function getLinkedUsers(
userID?: string,
userType?: Type,