33 lines
1.2 KiB
TypeScript
33 lines
1.2 KiB
TypeScript
// 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, WithEntities, WithEntity, WithLabeledEntities } from "@/interfaces/entity";
|
|
import { v4 } from "uuid";
|
|
import { mapBy } from "@/utils";
|
|
import { getEntitiesUsers, getUsers } from "@/utils/users.be";
|
|
import { Group, User } from "@/interfaces/user";
|
|
import { getGroups, getGroupsByEntities } from "@/utils/groups.be";
|
|
|
|
export default withIronSessionApiRoute(handler, sessionOptions);
|
|
|
|
async function handler(req: NextApiRequest, res: NextApiResponse) {
|
|
if (req.method === "GET") return await get(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 groups: WithEntity<Group>[] = ["admin", "developer"].includes(user.type)
|
|
? await getGroups()
|
|
: await getGroupsByEntities(mapBy(user.entities || [], 'id'))
|
|
|
|
res.status(200).json(groups);
|
|
}
|