Add controller and routes for groups

This commit is contained in:
Stephan
2022-04-26 16:04:26 +02:00
parent 8aa4e5ef16
commit 0c65aec96c
3 changed files with 107 additions and 0 deletions
+92
View File
@@ -0,0 +1,92 @@
import { PrismaClientUnknownRequestError } from "@prisma/client/runtime";
import { Request, Response } from "express";
import prisma from "../lib/prisma";
import { generateError, genericError } from "./common";
const basicGroup = {
pid: true,
name: true,
oragnisation: { select: { pid: true, name: true } },
} as const;
export const _getAllGroups = async (res: Response, organisationId: string | undefined) => {
const groups = await prisma.group.findMany({
where: { oragnisation: { pid: organisationId } },
select: basicGroup,
});
return res.status(200).json({
type: "success",
payload: {
groups,
},
});
};
interface GetAllGroupsSearchParams {
organisationPid?: string;
}
export const getAllGroups = async (req: Request<{}, {}, {}, GetAllGroupsSearchParams>, res: Response) => {
return _getAllGroups(res, req.query.organisationPid);
};
interface getAllGroupsWithParamQueryParams {
organisationPid: string;
}
export const getAllGroupsWithParam = async (req: Request<getAllGroupsWithParamQueryParams>, res: Response) => {
return _getAllGroups(res, req.params.organisationPid);
};
interface GetGroupQueryParams {
pid: string;
}
export const getGroup = async (req: Request<GetGroupQueryParams>, res: Response) => {
const { pid } = req.params;
try {
const group = await prisma.group.findUnique({
where: { pid },
select: {
pid: true,
name: true,
oragnisation: { select: { pid: true, name: true } },
admins: { select: { pid: true, name: true } },
participants: { select: { pid: true } },
},
});
if (!group) {
return res.status(404).json(generateError(`The group with ID '${pid}' could not be found`));
}
return res.status(200).json({
type: "success",
payload: {
group: {
...group,
organisation: {
...group.oragnisation,
_links: [{ rel: "self", type: "GET", href: `/api/organisation/${group.oragnisation.pid}` }],
},
admins: group.admins.map((admin) => ({
...admin,
_links: [{ rel: "self", type: "GET", href: `/api/admins/${admin.pid}` }],
})),
participants: group.participants.map((participant) => ({
...participant,
_links: [{ rel: "self", type: "GET", href: `/api/participant/${participant.pid}` }],
})),
},
},
});
} catch (e) {
if (e instanceof PrismaClientUnknownRequestError) {
return res.status(400).json(generateError("Unknown error occured. This could be due to malformed IDs"));
}
}
return res.status(500).json(genericError);
};
+12
View File
@@ -0,0 +1,12 @@
import express from "express";
import { getAllGroups, getAllGroupsWithParam, getGroup } from "../Controllers/group.controllers";
import organisationRouter from "./organisation.routes";
const router = express.Router();
router.get("/", getAllGroups);
router.get("/:pid", getGroup);
organisationRouter.get("/:organisationPid/groups", getAllGroupsWithParam);
export default router;
+3
View File
@@ -5,6 +5,7 @@ import adminAuthRouter from "./Routes/admin_auth.routes";
import argon2 from "argon2";
import adminRouter from "./Routes/admin.routes";
import organisationRouter from "./Routes/organisation.routes";
import groupRouter from "./Routes/group.routes";
require("dotenv").config(); // Load dotenv config
@@ -49,6 +50,8 @@ async function main() {
app.use("/api/organisations", organisationRouter);
app.use("/api/groups", groupRouter);
app.listen(process.env.PORT, () => {
console.log(`Listening on Port: ${process.env.PORT}`);
});