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);
|
||||
|
||||
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 === "DELETE") return await del(req, res);
|
||||
}
|
||||
|
||||
async function get(req: NextApiRequest, res: NextApiResponse) {
|
||||
@@ -35,9 +36,10 @@ async function del(req: NextApiRequest, res: NextApiResponse) {
|
||||
const { id } = req.query as { id: string };
|
||||
|
||||
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)
|
||||
return res.status(200).json({ok: true});
|
||||
|
||||
@@ -2,7 +2,7 @@
|
||||
import type {NextApiRequest, NextApiResponse} from "next";
|
||||
import {withIronSessionApiRoute} from "iron-session/next";
|
||||
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 {v4} from "uuid";
|
||||
import { requestUser } from "@/utils/api";
|
||||
@@ -39,6 +39,14 @@ async function post(req: NextApiRequest, res: NextApiResponse) {
|
||||
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);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user