Continued creating the entity system
This commit is contained in:
46
src/pages/api/entities/[id]/index.ts
Normal file
46
src/pages/api/entities/[id]/index.ts
Normal file
@@ -0,0 +1,46 @@
|
||||
// Next.js API route support: https://nextjs.org/docs/api-routes/introduction
|
||||
import type {NextApiRequest, NextApiResponse} from "next";
|
||||
import {withIronSessionApiRoute} from "iron-session/next";
|
||||
import {sessionOptions} from "@/lib/session";
|
||||
import {getEntity, getEntityWithRoles} from "@/utils/entities.be";
|
||||
import client from "@/lib/mongodb";
|
||||
import {Entity} from "@/interfaces/entity";
|
||||
|
||||
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 === "PATCH") return await patch(req, res);
|
||||
}
|
||||
|
||||
async function get(req: NextApiRequest, res: NextApiResponse) {
|
||||
if (!req.session.user) {
|
||||
res.status(401).json({ok: false});
|
||||
return;
|
||||
}
|
||||
|
||||
const {id, showRoles} = req.query as {id: string; showRoles: string};
|
||||
|
||||
const entity = await (!!showRoles ? getEntityWithRoles : getEntity)(id);
|
||||
res.status(200).json(entity);
|
||||
}
|
||||
|
||||
async function patch(req: NextApiRequest, res: NextApiResponse) {
|
||||
if (!req.session.user) {
|
||||
res.status(401).json({ok: false});
|
||||
return;
|
||||
}
|
||||
|
||||
const {id} = req.query as {id: string};
|
||||
|
||||
const user = req.session.user;
|
||||
if (!user.entities.map((x) => x.id).includes(id)) {
|
||||
return res.status(403).json({ok: false});
|
||||
}
|
||||
|
||||
const entity = await db.collection<Entity>("entities").updateOne({id}, {$set: {label: req.body.label}});
|
||||
|
||||
return res.status(200).json({ok: entity.acknowledged});
|
||||
}
|
||||
65
src/pages/api/entities/[id]/users.ts
Normal file
65
src/pages/api/entities/[id]/users.ts
Normal file
@@ -0,0 +1,65 @@
|
||||
// Next.js API route support: https://nextjs.org/docs/api-routes/introduction
|
||||
import type {NextApiRequest, NextApiResponse} from "next";
|
||||
import {withIronSessionApiRoute} from "iron-session/next";
|
||||
import {sessionOptions} from "@/lib/session";
|
||||
import {countEntityUsers, getEntityUsers} from "@/utils/users.be";
|
||||
import client from "@/lib/mongodb";
|
||||
|
||||
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 === "PATCH") return await patch(req, res);
|
||||
}
|
||||
|
||||
async function get(req: NextApiRequest, res: NextApiResponse) {
|
||||
if (!req.session.user) {
|
||||
res.status(401).json({ok: false});
|
||||
return;
|
||||
}
|
||||
|
||||
const {id, onlyCount} = req.query as {id: string; onlyCount: string};
|
||||
|
||||
if (onlyCount) return res.status(200).json(await countEntityUsers(id));
|
||||
|
||||
const users = await getEntityUsers(id);
|
||||
res.status(200).json(users);
|
||||
}
|
||||
|
||||
async function patch(req: NextApiRequest, res: NextApiResponse) {
|
||||
if (!req.session.user) {
|
||||
res.status(401).json({ok: false});
|
||||
return;
|
||||
}
|
||||
|
||||
const {id} = req.query as {id: string};
|
||||
const {add, members, role} = req.body as {add: boolean; members: string[]; role?: string};
|
||||
|
||||
if (add) {
|
||||
await db.collection("users").updateMany(
|
||||
{id: {$in: members}},
|
||||
{
|
||||
// @ts-expect-error
|
||||
$push: {
|
||||
entities: {id, role},
|
||||
},
|
||||
},
|
||||
);
|
||||
|
||||
return res.status(204).end();
|
||||
}
|
||||
|
||||
await db.collection("users").updateMany(
|
||||
{id: {$in: members}},
|
||||
{
|
||||
// @ts-expect-error
|
||||
$pull: {
|
||||
entities: {id},
|
||||
},
|
||||
},
|
||||
);
|
||||
|
||||
return res.status(204).end();
|
||||
}
|
||||
48
src/pages/api/entities/index.ts
Normal file
48
src/pages/api/entities/index.ts
Normal file
@@ -0,0 +1,48 @@
|
||||
// Next.js API route support: https://nextjs.org/docs/api-routes/introduction
|
||||
import type {NextApiRequest, NextApiResponse} from "next";
|
||||
import {withIronSessionApiRoute} from "iron-session/next";
|
||||
import {sessionOptions} from "@/lib/session";
|
||||
import {getEntities, getEntitiesWithRoles} from "@/utils/entities.be";
|
||||
import {Entity} from "@/interfaces/entity";
|
||||
import {v4} from "uuid";
|
||||
|
||||
export default withIronSessionApiRoute(handler, sessionOptions);
|
||||
|
||||
async function handler(req: NextApiRequest, res: NextApiResponse) {
|
||||
if (req.method === "GET") return await get(req, res);
|
||||
if (req.method === "POST") return await post(req, res);
|
||||
}
|
||||
|
||||
async function get(req: NextApiRequest, res: NextApiResponse) {
|
||||
if (!req.session.user) {
|
||||
res.status(401).json({ok: false});
|
||||
return;
|
||||
}
|
||||
|
||||
const user = req.session.user;
|
||||
const {showRoles} = req.query as {showRoles: string};
|
||||
|
||||
const getFn = showRoles ? getEntitiesWithRoles : getEntities;
|
||||
|
||||
if (["admin", "developer"].includes(user.type)) return res.status(200).json(await getFn());
|
||||
res.status(200).json(await getFn(user.entities.map((x) => x.id)));
|
||||
}
|
||||
|
||||
async function post(req: NextApiRequest, res: NextApiResponse) {
|
||||
if (!req.session.user) {
|
||||
res.status(401).json({ok: false});
|
||||
return;
|
||||
}
|
||||
|
||||
const user = req.session.user;
|
||||
if (!["admin", "developer"].includes(user.type)) {
|
||||
return res.status(403).json({ok: false});
|
||||
}
|
||||
|
||||
const entity: Entity = {
|
||||
id: v4(),
|
||||
label: req.body.label,
|
||||
};
|
||||
|
||||
return res.status(200).json(entity);
|
||||
}
|
||||
Reference in New Issue
Block a user