Solved some issues with the redirect as well as adding a way to create entities
This commit is contained in:
@@ -14,8 +14,9 @@ const db = client.db(process.env.MONGODB_DB);
|
|||||||
export default withIronSessionApiRoute(handler, sessionOptions);
|
export default withIronSessionApiRoute(handler, sessionOptions);
|
||||||
|
|
||||||
async function handler(req: NextApiRequest, res: NextApiResponse) {
|
async function handler(req: NextApiRequest, res: NextApiResponse) {
|
||||||
if (req.method === "get") return await get(req, res);
|
if (req.method === "GET") return await get(req, res);
|
||||||
if (req.method === "PATCH") return await patch(req, res);
|
if (req.method === "PATCH") return await patch(req, res);
|
||||||
|
if (req.method === "DELETE") return await del(req, res);
|
||||||
}
|
}
|
||||||
|
|
||||||
async function get(req: NextApiRequest, res: NextApiResponse) {
|
async function get(req: NextApiRequest, res: NextApiResponse) {
|
||||||
@@ -37,7 +38,8 @@ async function del(req: NextApiRequest, res: NextApiResponse) {
|
|||||||
const entity = await getEntityWithRoles(id)
|
const entity = await getEntityWithRoles(id)
|
||||||
if (!entity) return res.status(404).json({ ok: false })
|
if (!entity) return res.status(404).json({ ok: false })
|
||||||
|
|
||||||
if (!doesEntityAllow(user, entity, "delete_entity_role")) return res.status(403).json({ok: false})
|
if (!doesEntityAllow(user, entity, "delete_entity") && !["admin", "developer"].includes(user.type))
|
||||||
|
return res.status(403).json({ok: false})
|
||||||
|
|
||||||
await deleteEntity(entity)
|
await deleteEntity(entity)
|
||||||
return res.status(200).json({ok: true});
|
return res.status(200).json({ok: true});
|
||||||
|
|||||||
@@ -2,7 +2,7 @@
|
|||||||
import type {NextApiRequest, NextApiResponse} from "next";
|
import type {NextApiRequest, NextApiResponse} from "next";
|
||||||
import {withIronSessionApiRoute} from "iron-session/next";
|
import {withIronSessionApiRoute} from "iron-session/next";
|
||||||
import {sessionOptions} from "@/lib/session";
|
import {sessionOptions} from "@/lib/session";
|
||||||
import {createEntity, getEntities, getEntitiesWithRoles} from "@/utils/entities.be";
|
import {addUsersToEntity, addUserToEntity, createEntity, getEntities, getEntitiesWithRoles} from "@/utils/entities.be";
|
||||||
import {Entity} from "@/interfaces/entity";
|
import {Entity} from "@/interfaces/entity";
|
||||||
import {v4} from "uuid";
|
import {v4} from "uuid";
|
||||||
import { requestUser } from "@/utils/api";
|
import { requestUser } from "@/utils/api";
|
||||||
@@ -39,6 +39,14 @@ async function post(req: NextApiRequest, res: NextApiResponse) {
|
|||||||
label: req.body.label,
|
label: req.body.label,
|
||||||
};
|
};
|
||||||
|
|
||||||
await createEntity(entity)
|
const members = req.body.members as string[] | undefined || []
|
||||||
|
console.log(members)
|
||||||
|
|
||||||
|
const roles = await createEntity(entity)
|
||||||
|
console.log(roles)
|
||||||
|
|
||||||
|
await addUserToEntity(user.id, entity.id, roles.admin.id)
|
||||||
|
if (members.length > 0) await addUsersToEntity(members, entity.id, roles.default.id)
|
||||||
|
|
||||||
return res.status(200).json(entity);
|
return res.status(200).json(entity);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -137,8 +137,19 @@ export default function Dashboard({ user, users, entities, assignments, stats, g
|
|||||||
value={masterCorporates.length}
|
value={masterCorporates.length}
|
||||||
color="purple"
|
color="purple"
|
||||||
/>
|
/>
|
||||||
<IconCard Icon={BsPeople} label="Classrooms" value={groups.length} color="purple" />
|
<IconCard
|
||||||
<IconCard Icon={BsPeopleFill} label="Entities" value={entities.length} color="purple" />
|
Icon={BsPeople}
|
||||||
|
onClick={() => router.push("/classrooms")}
|
||||||
|
label="Classrooms"
|
||||||
|
value={groups.length}
|
||||||
|
color="purple"
|
||||||
|
/>
|
||||||
|
<IconCard Icon={BsPeopleFill}
|
||||||
|
onClick={() => router.push("/entities")}
|
||||||
|
label="Entities"
|
||||||
|
value={entities.length}
|
||||||
|
color="purple"
|
||||||
|
/>
|
||||||
<IconCard Icon={BsClipboard2Data} label="Exams Performed" value={uniqBy(stats, "exam").length} color="purple" />
|
<IconCard Icon={BsClipboard2Data} label="Exams Performed" value={uniqBy(stats, "exam").length} color="purple" />
|
||||||
<IconCard Icon={BsPaperclip} label="Average Level" value={averageLevelCalculator(stats).toFixed(1)} color="purple" />
|
<IconCard Icon={BsPaperclip} label="Average Level" value={averageLevelCalculator(stats).toFixed(1)} color="purple" />
|
||||||
<IconCard Icon={BsPersonFillGear}
|
<IconCard Icon={BsPersonFillGear}
|
||||||
|
|||||||
@@ -144,7 +144,12 @@ export default function Dashboard({ user, users, entities, assignments, stats, g
|
|||||||
value={groups.length}
|
value={groups.length}
|
||||||
color="purple"
|
color="purple"
|
||||||
/>
|
/>
|
||||||
<IconCard Icon={BsPeopleFill} label="Entities" value={entities.length} color="purple" />
|
<IconCard Icon={BsPeopleFill}
|
||||||
|
onClick={() => router.push("/entities")}
|
||||||
|
label="Entities"
|
||||||
|
value={entities.length}
|
||||||
|
color="purple"
|
||||||
|
/>
|
||||||
<IconCard Icon={BsClipboard2Data} label="Exams Performed" value={uniqBy(stats, "exam").length} color="purple" />
|
<IconCard Icon={BsClipboard2Data} label="Exams Performed" value={uniqBy(stats, "exam").length} color="purple" />
|
||||||
<IconCard Icon={BsPaperclip} label="Average Level" value={averageLevelCalculator(stats).toFixed(1)} color="purple" />
|
<IconCard Icon={BsPaperclip} label="Average Level" value={averageLevelCalculator(stats).toFixed(1)} color="purple" />
|
||||||
<IconCard Icon={BsPersonFillGear}
|
<IconCard Icon={BsPersonFillGear}
|
||||||
|
|||||||
@@ -139,12 +139,17 @@ export default function Dashboard({ user, users, entities, assignments, stats, g
|
|||||||
/>
|
/>
|
||||||
<IconCard
|
<IconCard
|
||||||
Icon={BsPeople}
|
Icon={BsPeople}
|
||||||
onClick={() => router.push("/entities")}
|
onClick={() => router.push("/classrooms")}
|
||||||
label="Classrooms"
|
label="Classrooms"
|
||||||
value={groups.length}
|
value={groups.length}
|
||||||
color="purple"
|
color="purple"
|
||||||
/>
|
/>
|
||||||
<IconCard Icon={BsPeopleFill} label="Entities" value={entities.length} color="purple" />
|
<IconCard Icon={BsPeopleFill}
|
||||||
|
onClick={() => router.push("/entities")}
|
||||||
|
label="Entities"
|
||||||
|
value={entities.length}
|
||||||
|
color="purple"
|
||||||
|
/>
|
||||||
<IconCard Icon={BsClipboard2Data} label="Exams Performed" value={uniqBy(stats, "exam").length} color="purple" />
|
<IconCard Icon={BsClipboard2Data} label="Exams Performed" value={uniqBy(stats, "exam").length} color="purple" />
|
||||||
<IconCard Icon={BsPaperclip} label="Average Level" value={averageLevelCalculator(stats).toFixed(1)} color="purple" />
|
<IconCard Icon={BsPaperclip} label="Average Level" value={averageLevelCalculator(stats).toFixed(1)} color="purple" />
|
||||||
<IconCard Icon={BsPersonFillGear}
|
<IconCard Icon={BsPersonFillGear}
|
||||||
|
|||||||
@@ -1,8 +1,8 @@
|
|||||||
import {User} from "@/interfaces/user";
|
import {User} from "@/interfaces/user";
|
||||||
import {sessionOptions} from "@/lib/session";
|
import {sessionOptions} from "@/lib/session";
|
||||||
|
import { redirect } from "@/utils";
|
||||||
import { requestUser } from "@/utils/api";
|
import { requestUser } from "@/utils/api";
|
||||||
import {withIronSessionSsr} from "iron-session/next";
|
import {withIronSessionSsr} from "iron-session/next";
|
||||||
import { redirect } from "next/navigation";
|
|
||||||
|
|
||||||
export const getServerSideProps = withIronSessionSsr(async ({req, res}) => {
|
export const getServerSideProps = withIronSessionSsr(async ({req, res}) => {
|
||||||
const user = await requestUser(req, res)
|
const user = await requestUser(req, res)
|
||||||
|
|||||||
@@ -137,8 +137,19 @@ export default function Dashboard({ user, users, entities, assignments, stats, g
|
|||||||
/>
|
/>
|
||||||
<IconCard
|
<IconCard
|
||||||
onClick={() => router.push("/users?type=corporate")} Icon={BsBank} label="Corporate Accounts" value={corporates.length} color="purple" />
|
onClick={() => router.push("/users?type=corporate")} Icon={BsBank} label="Corporate Accounts" value={corporates.length} color="purple" />
|
||||||
<IconCard onClick={() => router.push("/classrooms")} Icon={BsPeople} label="Classrooms" value={groups.length} color="purple" />
|
<IconCard
|
||||||
<IconCard Icon={BsPeopleFill} label="Entities" value={entities.length} color="purple" />
|
Icon={BsPeople}
|
||||||
|
onClick={() => router.push("/classrooms")}
|
||||||
|
label="Classrooms"
|
||||||
|
value={groups.length}
|
||||||
|
color="purple"
|
||||||
|
/>
|
||||||
|
<IconCard Icon={BsPeopleFill}
|
||||||
|
onClick={() => router.push("/entities")}
|
||||||
|
label="Entities"
|
||||||
|
value={entities.length}
|
||||||
|
color="purple"
|
||||||
|
/>
|
||||||
<IconCard Icon={BsClipboard2Data} label="Exams Performed" value={uniqBy(stats, "exam").length} color="purple" />
|
<IconCard Icon={BsClipboard2Data} label="Exams Performed" value={uniqBy(stats, "exam").length} color="purple" />
|
||||||
<IconCard Icon={BsPaperclip} label="Average Level" value={averageLevelCalculator(stats).toFixed(1)} color="purple" />
|
<IconCard Icon={BsPaperclip} label="Average Level" value={averageLevelCalculator(stats).toFixed(1)} color="purple" />
|
||||||
<IconCard Icon={BsPersonFillGear}
|
<IconCard Icon={BsPersonFillGear}
|
||||||
|
|||||||
176
src/pages/entities/create.tsx
Normal file
176
src/pages/entities/create.tsx
Normal file
@@ -0,0 +1,176 @@
|
|||||||
|
/* eslint-disable @next/next/no-img-element */
|
||||||
|
import Layout from "@/components/High/Layout";
|
||||||
|
import Input from "@/components/Low/Input";
|
||||||
|
import Select from "@/components/Low/Select";
|
||||||
|
import Tooltip from "@/components/Low/Tooltip";
|
||||||
|
import {useListSearch} from "@/hooks/useListSearch";
|
||||||
|
import usePagination from "@/hooks/usePagination";
|
||||||
|
import {Entity, EntityWithRoles} from "@/interfaces/entity";
|
||||||
|
import {User} from "@/interfaces/user";
|
||||||
|
import {sessionOptions} from "@/lib/session";
|
||||||
|
import {USER_TYPE_LABELS} from "@/resources/user";
|
||||||
|
import {mapBy, redirect, serialize} from "@/utils";
|
||||||
|
import {getEntities, getEntitiesWithRoles} from "@/utils/entities.be";
|
||||||
|
import {shouldRedirectHome} from "@/utils/navigation.disabled";
|
||||||
|
import {getUserName} from "@/utils/users";
|
||||||
|
import {getLinkedUsers, getUsers} from "@/utils/users.be";
|
||||||
|
import axios from "axios";
|
||||||
|
import clsx from "clsx";
|
||||||
|
import {withIronSessionSsr} from "iron-session/next";
|
||||||
|
import moment from "moment";
|
||||||
|
import Head from "next/head";
|
||||||
|
import Link from "next/link";
|
||||||
|
import {useRouter} from "next/router";
|
||||||
|
import {Divider} from "primereact/divider";
|
||||||
|
import {useState} from "react";
|
||||||
|
import {BsCheck, BsChevronLeft, BsClockFill, BsEnvelopeFill, BsStopwatchFill} from "react-icons/bs";
|
||||||
|
import {toast, ToastContainer} from "react-toastify";
|
||||||
|
import { requestUser } from "@/utils/api";
|
||||||
|
import { findAllowedEntities } from "@/utils/permissions";
|
||||||
|
|
||||||
|
export const getServerSideProps = withIronSessionSsr(async ({req, res}) => {
|
||||||
|
const user = await requestUser(req, res)
|
||||||
|
if (!user) return redirect("/login")
|
||||||
|
|
||||||
|
if (shouldRedirectHome(user)) return redirect("/")
|
||||||
|
if (!["admin", "developer"].includes(user.type)) return redirect("/entities")
|
||||||
|
|
||||||
|
const users = await getUsers()
|
||||||
|
|
||||||
|
return {
|
||||||
|
props: serialize({user, users: users.filter((x) => x.id !== user.id)}),
|
||||||
|
};
|
||||||
|
}, sessionOptions);
|
||||||
|
|
||||||
|
interface Props {
|
||||||
|
user: User;
|
||||||
|
users: User[];
|
||||||
|
}
|
||||||
|
|
||||||
|
export default function Home({user, users}: Props) {
|
||||||
|
const [isLoading, setIsLoading] = useState(false);
|
||||||
|
const [selectedUsers, setSelectedUsers] = useState<string[]>([]);
|
||||||
|
const [label, setLabel] = useState("");
|
||||||
|
|
||||||
|
const {rows, renderSearch} = useListSearch<User>([["name"], ["corporateInformation", "companyInformation", "name"]], users);
|
||||||
|
const {items, renderMinimal} = usePagination<User>(rows, 16);
|
||||||
|
|
||||||
|
const router = useRouter();
|
||||||
|
|
||||||
|
const createGroup = () => {
|
||||||
|
if (!label.trim()) return;
|
||||||
|
if (!confirm(`Are you sure you want to create this entity with ${selectedUsers.length} members?`)) return;
|
||||||
|
|
||||||
|
setIsLoading(true);
|
||||||
|
|
||||||
|
axios
|
||||||
|
.post<Entity>(`/api/entities`, {label, members: selectedUsers})
|
||||||
|
.then((result) => {
|
||||||
|
toast.success("Your entity has been created successfully!");
|
||||||
|
router.replace(`/entities/${result.data.id}`);
|
||||||
|
})
|
||||||
|
.catch((e) => {
|
||||||
|
console.error(e);
|
||||||
|
toast.error("Something went wrong!");
|
||||||
|
})
|
||||||
|
.finally(() => setIsLoading(false));
|
||||||
|
};
|
||||||
|
|
||||||
|
const toggleUser = (u: User) => setSelectedUsers((prev) => (prev.includes(u.id) ? prev.filter((p) => p !== u.id) : [...prev, u.id]));
|
||||||
|
|
||||||
|
return (
|
||||||
|
<>
|
||||||
|
<Head>
|
||||||
|
<title>Create Entity | EnCoach</title>
|
||||||
|
<meta
|
||||||
|
name="description"
|
||||||
|
content="A training platform for the IELTS exam provided by the Muscat Training Institute and developed by eCrop."
|
||||||
|
/>
|
||||||
|
<meta name="viewport" content="width=device-width, initial-scale=1" />
|
||||||
|
<link rel="icon" href="/favicon.ico" />
|
||||||
|
</Head>
|
||||||
|
<ToastContainer />
|
||||||
|
<Layout user={user}>
|
||||||
|
<section className="flex flex-col gap-0">
|
||||||
|
<div className="flex gap-3 justify-between">
|
||||||
|
<div className="flex items-center gap-2">
|
||||||
|
<Link
|
||||||
|
href="/classrooms"
|
||||||
|
className="text-mti-purple hover:text-mti-purple-dark transition ease-in-out duration-300 text-xl">
|
||||||
|
<BsChevronLeft />
|
||||||
|
</Link>
|
||||||
|
<h2 className="font-bold text-2xl">Create Entity</h2>
|
||||||
|
</div>
|
||||||
|
<div className="flex items-center gap-4">
|
||||||
|
<button
|
||||||
|
onClick={createGroup}
|
||||||
|
disabled={!label.trim() || isLoading}
|
||||||
|
className="flex items-center gap-1 px-2 py-2 border rounded-full border-mti-green bg-mti-green-light text-white hover:bg-mti-green-dark disabled:hover:bg-mti-green-light disabled:opacity-50 disabled:cursor-not-allowed cursor-pointer transition ease-in-out duration-300">
|
||||||
|
<BsCheck />
|
||||||
|
<span className="text-xs">Create Entity</span>
|
||||||
|
</button>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
<Divider />
|
||||||
|
<div className="flex flex-col gap-4 w-full">
|
||||||
|
<span className="font-semibold text-xl">Entity Label:</span>
|
||||||
|
<Input name="name" onChange={setLabel} type="text" placeholder="Entity A" />
|
||||||
|
</div>
|
||||||
|
<Divider />
|
||||||
|
<div className="flex items-center justify-between mb-4">
|
||||||
|
<span className="font-semibold text-xl">Members ({selectedUsers.length} selected):</span>
|
||||||
|
</div>
|
||||||
|
<div className="w-full flex items-center gap-4">
|
||||||
|
{renderSearch()}
|
||||||
|
{renderMinimal()}
|
||||||
|
</div>
|
||||||
|
</section>
|
||||||
|
|
||||||
|
<section className="w-full h-full grid grid-cols-1 md:grid-cols-2 lg:grid-cols-4 gap-4">
|
||||||
|
{items.map((u) => (
|
||||||
|
<button
|
||||||
|
onClick={() => toggleUser(u)}
|
||||||
|
disabled={isLoading}
|
||||||
|
key={u.id}
|
||||||
|
className={clsx(
|
||||||
|
"p-4 pr-6 h-48 relative border rounded-xl flex flex-col gap-3 justify-between text-left cursor-pointer",
|
||||||
|
"hover:border-mti-purple transition ease-in-out duration-300",
|
||||||
|
selectedUsers.includes(u.id) && "border-mti-purple",
|
||||||
|
)}>
|
||||||
|
<div className="flex items-center gap-2">
|
||||||
|
<div className="min-w-[3rem] min-h-[3rem] w-12 h-12 border flex items-center justify-center overflow-hidden rounded-full">
|
||||||
|
<img src={u.profilePicture} alt={u.name} />
|
||||||
|
</div>
|
||||||
|
<div className="flex flex-col">
|
||||||
|
<span className="font-semibold">{getUserName(u)}</span>
|
||||||
|
<span className="opacity-80 text-sm">{USER_TYPE_LABELS[u.type]}</span>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div className="flex flex-col gap-1">
|
||||||
|
<span className="flex items-center gap-2">
|
||||||
|
<Tooltip tooltip="E-mail address">
|
||||||
|
<BsEnvelopeFill />
|
||||||
|
</Tooltip>
|
||||||
|
{u.email}
|
||||||
|
</span>
|
||||||
|
<span className="flex items-center gap-2">
|
||||||
|
<Tooltip tooltip="Expiration Date">
|
||||||
|
<BsStopwatchFill />
|
||||||
|
</Tooltip>
|
||||||
|
{u.subscriptionExpirationDate ? moment(u.subscriptionExpirationDate).format("DD/MM/YYYY") : "Unlimited"}
|
||||||
|
</span>
|
||||||
|
<span className="flex items-center gap-2">
|
||||||
|
<Tooltip tooltip="Last Login">
|
||||||
|
<BsClockFill />
|
||||||
|
</Tooltip>
|
||||||
|
{u.lastLogin ? moment(u.lastLogin).format("DD/MM/YYYY - HH:mm") : "N/A"}
|
||||||
|
</span>
|
||||||
|
</div>
|
||||||
|
</button>
|
||||||
|
))}
|
||||||
|
</section>
|
||||||
|
</Layout>
|
||||||
|
</>
|
||||||
|
);
|
||||||
|
}
|
||||||
@@ -43,3 +43,59 @@ export type RolePermission =
|
|||||||
"delete_assignment" |
|
"delete_assignment" |
|
||||||
"start_assignment" |
|
"start_assignment" |
|
||||||
"archive_assignment";
|
"archive_assignment";
|
||||||
|
|
||||||
|
export const DEFAULT_PERMISSIONS: RolePermission[] = [
|
||||||
|
"view_students",
|
||||||
|
"view_teachers",
|
||||||
|
"view_assignments",
|
||||||
|
"view_classrooms",
|
||||||
|
"view_entity_roles"
|
||||||
|
]
|
||||||
|
|
||||||
|
export const ADMIN_PERMISSIONS: RolePermission[] = [
|
||||||
|
"view_students",
|
||||||
|
"view_teachers",
|
||||||
|
"view_corporates",
|
||||||
|
"view_mastercorporates",
|
||||||
|
"edit_students",
|
||||||
|
"edit_teachers",
|
||||||
|
"edit_corporates",
|
||||||
|
"edit_mastercorporates",
|
||||||
|
"delete_students",
|
||||||
|
"delete_teachers",
|
||||||
|
"delete_corporates",
|
||||||
|
"delete_mastercorporates",
|
||||||
|
"generate_reading",
|
||||||
|
"delete_reading",
|
||||||
|
"generate_listening",
|
||||||
|
"delete_listening",
|
||||||
|
"generate_writing",
|
||||||
|
"delete_writing",
|
||||||
|
"generate_speaking",
|
||||||
|
"delete_speaking",
|
||||||
|
"generate_level",
|
||||||
|
"delete_level",
|
||||||
|
"view_classrooms",
|
||||||
|
"create_classroom",
|
||||||
|
"rename_classrooms",
|
||||||
|
"add_to_classroom",
|
||||||
|
"remove_from_classroom",
|
||||||
|
"delete_classroom",
|
||||||
|
"view_entities",
|
||||||
|
"rename_entity",
|
||||||
|
"add_to_entity",
|
||||||
|
"remove_from_entity",
|
||||||
|
"delete_entity",
|
||||||
|
"view_entity_roles",
|
||||||
|
"create_entity_role",
|
||||||
|
"rename_entity_role",
|
||||||
|
"edit_role_permissions",
|
||||||
|
"assign_to_role",
|
||||||
|
"delete_entity_role",
|
||||||
|
"view_assignments",
|
||||||
|
"create_assignment",
|
||||||
|
"edit_assignment",
|
||||||
|
"delete_assignment",
|
||||||
|
"start_assignment",
|
||||||
|
"archive_assignment"
|
||||||
|
]
|
||||||
|
|||||||
@@ -1,17 +1,10 @@
|
|||||||
import {Entity, EntityWithRoles, Role} from "@/interfaces/entity";
|
import {Entity, EntityWithRoles, Role} from "@/interfaces/entity";
|
||||||
import client from "@/lib/mongodb";
|
import client from "@/lib/mongodb";
|
||||||
import { RolePermission } from "@/resources/entityPermissions";
|
import { ADMIN_PERMISSIONS, DEFAULT_PERMISSIONS, RolePermission } from "@/resources/entityPermissions";
|
||||||
import { v4 } from "uuid";
|
import { v4 } from "uuid";
|
||||||
import {getRolesByEntities, getRolesByEntity} from "./roles.be";
|
import {getRolesByEntities, getRolesByEntity} from "./roles.be";
|
||||||
|
|
||||||
const db = client.db(process.env.MONGODB_DB);
|
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> => {
|
export const getEntityWithRoles = async (id: string): Promise<EntityWithRoles | undefined> => {
|
||||||
const entity = await getEntity(id);
|
const entity = await getEntity(id);
|
||||||
@@ -45,14 +38,50 @@ export const getEntities = async (ids?: string[]) => {
|
|||||||
|
|
||||||
export const createEntity = async (entity: Entity) => {
|
export const createEntity = async (entity: Entity) => {
|
||||||
await db.collection("entities").insertOne(entity)
|
await db.collection("entities").insertOne(entity)
|
||||||
await db.collection("roles").insertOne({
|
|
||||||
|
const defaultRole = {
|
||||||
id: v4(),
|
id: v4(),
|
||||||
label: "Default",
|
label: "Default",
|
||||||
permissions: DEFAULT_PERMISSIONS,
|
permissions: DEFAULT_PERMISSIONS,
|
||||||
|
isDefault: true,
|
||||||
entityID: entity.id
|
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) => {
|
export const deleteEntity = async (entity: Entity) => {
|
||||||
await db.collection("entities").deleteOne({id: entity.id})
|
await db.collection("entities").deleteOne({id: entity.id})
|
||||||
await db.collection("roles").deleteMany({entityID: entity.id})
|
await db.collection("roles").deleteMany({entityID: entity.id})
|
||||||
|
|||||||
Reference in New Issue
Block a user