Solved some issues with the redirect as well as adding a way to create entities
This commit is contained in:
@@ -1,17 +1,10 @@
|
||||
import {Entity, EntityWithRoles, Role} from "@/interfaces/entity";
|
||||
import client from "@/lib/mongodb";
|
||||
import { RolePermission } from "@/resources/entityPermissions";
|
||||
import { ADMIN_PERMISSIONS, DEFAULT_PERMISSIONS, RolePermission } from "@/resources/entityPermissions";
|
||||
import { v4 } from "uuid";
|
||||
import {getRolesByEntities, getRolesByEntity} from "./roles.be";
|
||||
|
||||
const db = client.db(process.env.MONGODB_DB);
|
||||
const DEFAULT_PERMISSIONS: RolePermission[] = [
|
||||
"view_students",
|
||||
"view_teachers",
|
||||
"view_assignments",
|
||||
"view_classrooms",
|
||||
"view_entity_roles"
|
||||
]
|
||||
|
||||
export const getEntityWithRoles = async (id: string): Promise<EntityWithRoles | undefined> => {
|
||||
const entity = await getEntity(id);
|
||||
@@ -45,14 +38,50 @@ export const getEntities = async (ids?: string[]) => {
|
||||
|
||||
export const createEntity = async (entity: Entity) => {
|
||||
await db.collection("entities").insertOne(entity)
|
||||
await db.collection("roles").insertOne({
|
||||
|
||||
const defaultRole = {
|
||||
id: v4(),
|
||||
label: "Default",
|
||||
permissions: DEFAULT_PERMISSIONS,
|
||||
isDefault: true,
|
||||
entityID: entity.id
|
||||
})
|
||||
}
|
||||
|
||||
const adminRole = {
|
||||
id: v4(),
|
||||
label: "Admin",
|
||||
permissions: ADMIN_PERMISSIONS,
|
||||
entityID: entity.id
|
||||
}
|
||||
|
||||
await db.collection("roles").insertOne(defaultRole)
|
||||
await db.collection("roles").insertOne(adminRole)
|
||||
|
||||
return {default: defaultRole, admin: adminRole}
|
||||
}
|
||||
|
||||
export const addUserToEntity = async (user: string, entity: string, role: string) =>
|
||||
await db.collection("users").updateOne(
|
||||
{id: user},
|
||||
{
|
||||
// @ts-expect-error
|
||||
$push: {
|
||||
entities: {id: entity, role},
|
||||
},
|
||||
},
|
||||
);
|
||||
|
||||
export const addUsersToEntity = async (users: string[], entity: string, role: string) =>
|
||||
await db.collection("users").updateMany(
|
||||
{id: {$in: users}},
|
||||
{
|
||||
// @ts-expect-error
|
||||
$push: {
|
||||
entities: {id: entity, role},
|
||||
},
|
||||
},
|
||||
);
|
||||
|
||||
export const deleteEntity = async (entity: Entity) => {
|
||||
await db.collection("entities").deleteOne({id: entity.id})
|
||||
await db.collection("roles").deleteMany({entityID: entity.id})
|
||||
|
||||
Reference in New Issue
Block a user