Improved part of the performance of the dashboards

This commit is contained in:
Tiago Ribeiro
2024-12-24 10:31:52 +00:00
parent f8e9cfbeff
commit 770056e0c4
9 changed files with 110 additions and 85 deletions

View File

@@ -1,8 +1,8 @@
import {Entity, EntityWithRoles, Role} from "@/interfaces/entity";
import { Entity, EntityWithRoles, Role } from "@/interfaces/entity";
import client from "@/lib/mongodb";
import { ADMIN_PERMISSIONS, DEFAULT_PERMISSIONS, RolePermission } from "@/resources/entityPermissions";
import { v4 } from "uuid";
import {getRolesByEntities, getRolesByEntity} from "./roles.be";
import { getRolesByEntities, getRolesByEntity } from "./roles.be";
const db = client.db(process.env.MONGODB_DB);
@@ -11,28 +11,28 @@ export const getEntityWithRoles = async (id: string): Promise<EntityWithRoles |
if (!entity) return undefined;
const roles = await getRolesByEntity(id);
return {...entity, roles};
return { ...entity, roles };
};
export const getEntity = async (id: string) => {
return (await db.collection("entities").findOne<Entity>({id})) ?? undefined;
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}} : {})
.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) || []}));
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}} : {})
.find<Entity>(ids ? { id: { $in: ids } } : {})
.toArray();
};
@@ -57,41 +57,52 @@ export const createEntity = async (entity: Entity) => {
await db.collection("roles").insertOne(defaultRole)
await db.collection("roles").insertOne(adminRole)
return {default: defaultRole, admin: adminRole}
return { default: defaultRole, admin: adminRole }
}
export const addUserToEntity = async (user: string, entity: string, role: string) =>
await db.collection("users").updateOne(
{id: user},
{ id: user },
{
// @ts-expect-error
$push: {
entities: {id: entity, role},
entities: { id: entity, role },
},
},
);
export const addUsersToEntity = async (users: string[], entity: string, role: string) =>
await db.collection("users").updateMany(
{id: {$in: users}},
{ id: { $in: users } },
{
// @ts-expect-error
$push: {
entities: {id: entity, role},
entities: { id: entity, role },
},
},
);
export const removeUsersFromEntity = async (users: string[], entity: string) =>
await db.collection("users").updateMany(
{ id: { $in: users } },
{
// @ts-expect-error
$pull: {
entities: { id: entity },
},
},
);
export const deleteEntity = async (entity: Entity) => {
await db.collection("entities").deleteOne({id: entity.id})
await db.collection("roles").deleteMany({entityID: entity.id})
await db.collection("entities").deleteOne({ id: entity.id })
await db.collection("roles").deleteMany({ entityID: entity.id })
await db.collection("users").updateMany(
{"entities.id": entity.id},
{ "entities.id": entity.id },
{
// @ts-expect-error
$pull: {
entities: {id: entity.id},
entities: { id: entity.id },
},
},
);

View File

@@ -1,4 +1,4 @@
import {Role} from "@/interfaces/entity";
import { Role } from "@/interfaces/entity";
import client from "@/lib/mongodb";
const db = client.db(process.env.MONGODB_DB);
@@ -6,16 +6,18 @@ const db = client.db(process.env.MONGODB_DB);
export const getRolesByEntities = async (entityIDs: string[]) =>
await db
.collection("roles")
.find<Role>({entityID: {$in: entityIDs}})
.find<Role>({ entityID: { $in: entityIDs } })
.toArray();
export const getRolesByEntity = async (entityID: string) => await db.collection("roles").find<Role>({entityID}).toArray();
export const getRolesByEntity = async (entityID: string) => await db.collection("roles").find<Role>({ entityID }).toArray();
export const getRoles = async (ids?: string[]) => await db.collection("roles").find<Role>(!ids ? {} : {id: {$in: ids}}).toArray();
export const getRole = async (id: string) => (await db.collection("roles").findOne<Role>({id})) ?? undefined;
export const getRoles = async (ids?: string[]) => await db.collection("roles").find<Role>(!ids ? {} : { id: { $in: ids } }).toArray();
export const getRole = async (id: string) => (await db.collection("roles").findOne<Role>({ id })) ?? undefined;
export const getDefaultRole = async (entityID: string) => await db.collection("roles").findOne<Role>({ isDefault: true, entityID })
export const createRole = async (role: Role) => await db.collection("roles").insertOne(role)
export const deleteRole = async (id: string) => await db.collection("roles").deleteOne({id})
export const deleteRole = async (id: string) => await db.collection("roles").deleteOne({ id })
export const transferRole = async (previousRole: string, newRole: string) =>
await db.collection("users")