Started implementing the roles permissions
This commit is contained in:
@@ -1,15 +1,16 @@
|
||||
import {Entity, EntityWithRoles, Role} from "@/interfaces/entity";
|
||||
import client from "@/lib/mongodb";
|
||||
import { v4 } from "uuid";
|
||||
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> => {
|
||||
export const getEntityWithRoles = async (id: string): Promise<EntityWithRoles | undefined> => {
|
||||
const entity = await getEntity(id);
|
||||
if (!entity) return undefined;
|
||||
|
||||
const roles = await getRolesByEntity(id);
|
||||
return {entity, roles};
|
||||
return {...entity, roles};
|
||||
};
|
||||
|
||||
export const getEntity = async (id: string) => {
|
||||
@@ -33,3 +34,28 @@ export const getEntities = async (ids?: string[]) => {
|
||||
.find<Entity>(ids ? {id: {$in: ids}} : {})
|
||||
.toArray();
|
||||
};
|
||||
|
||||
export const createEntity = async (entity: Entity) => {
|
||||
await db.collection("entities").insertOne(entity)
|
||||
await db.collection("roles").insertOne({
|
||||
id: v4(),
|
||||
label: "Default",
|
||||
permissions: [],
|
||||
entityID: entity.id
|
||||
})
|
||||
}
|
||||
|
||||
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("users").updateMany(
|
||||
{"entities.id": entity.id},
|
||||
{
|
||||
// @ts-expect-error
|
||||
$pull: {
|
||||
entities: {id: entity.id},
|
||||
},
|
||||
},
|
||||
);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user