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

35
src/utils/entities.be.ts Normal file
View File

@@ -0,0 +1,35 @@
import {Entity, EntityWithRoles, Role} from "@/interfaces/entity";
import client from "@/lib/mongodb";
import {getRolesByEntities, getRolesByEntity} from "./roles.be";
const db = client.db(process.env.MONGODB_DB);
export const getEntityWithRoles = async (id: string): Promise<{entity: Entity; roles: Role[]} | undefined> => {
const entity = await getEntity(id);
if (!entity) return undefined;
const roles = await getRolesByEntity(id);
return {entity, roles};
};
export const getEntity = async (id: string) => {
return (await db.collection("entities").findOne<Entity>({id})) ?? undefined;
};
export const getEntitiesWithRoles = async (ids?: string[]): Promise<EntityWithRoles[]> => {
const entities = await db
.collection("entities")
.find<Entity>(ids ? {id: {$in: ids}} : {})
.toArray();
const roles = await getRolesByEntities(entities.map((x) => x.id));
return entities.map((x) => ({...x, roles: roles.filter((y) => y.entityID === x.id) || []}));
};
export const getEntities = async (ids?: string[]) => {
return await db
.collection("entities")
.find<Entity>(ids ? {id: {$in: ids}} : {})
.toArray();
};