mirror of
https://github.com/detleph/server.git
synced 2026-09-04 08:36:06 +02:00
Add controller and routes for groups
This commit is contained in:
@@ -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);
|
||||||
|
};
|
||||||
@@ -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;
|
||||||
@@ -5,6 +5,7 @@ import adminAuthRouter from "./Routes/admin_auth.routes";
|
|||||||
import argon2 from "argon2";
|
import argon2 from "argon2";
|
||||||
import adminRouter from "./Routes/admin.routes";
|
import adminRouter from "./Routes/admin.routes";
|
||||||
import organisationRouter from "./Routes/organisation.routes";
|
import organisationRouter from "./Routes/organisation.routes";
|
||||||
|
import groupRouter from "./Routes/group.routes";
|
||||||
|
|
||||||
require("dotenv").config(); // Load dotenv config
|
require("dotenv").config(); // Load dotenv config
|
||||||
|
|
||||||
@@ -49,6 +50,8 @@ async function main() {
|
|||||||
|
|
||||||
app.use("/api/organisations", organisationRouter);
|
app.use("/api/organisations", organisationRouter);
|
||||||
|
|
||||||
|
app.use("/api/groups", groupRouter);
|
||||||
|
|
||||||
app.listen(process.env.PORT, () => {
|
app.listen(process.env.PORT, () => {
|
||||||
console.log(`Listening on Port: ${process.env.PORT}`);
|
console.log(`Listening on Port: ${process.env.PORT}`);
|
||||||
});
|
});
|
||||||
|
|||||||
Reference in New Issue
Block a user