From 08e18db3c3b5c22899ca7141dd7f1ccc59b4bc8f Mon Sep 17 00:00:00 2001 From: Stephan <57194608+stephan418@users.noreply.github.com> Date: Sat, 23 Apr 2022 15:56:55 +0200 Subject: [PATCH] Extract common function to handle selecting organisations --- src/Controllers/organisation.controller.ts | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/src/Controllers/organisation.controller.ts b/src/Controllers/organisation.controller.ts index c5ec5df..559cc8d 100644 --- a/src/Controllers/organisation.controller.ts +++ b/src/Controllers/organisation.controller.ts @@ -3,12 +3,13 @@ import { Request, Response } from "express"; import prisma from "../lib/prisma"; import { AUTH_ERROR, createInsufficientPermissionsError, DataType, generateInvalidBodyError } from "./common"; -export const getAllOrganisations = async (req: Request, res: Response) => { +export const _getAllOrganisations = async (res: Response, eventId: number | undefined = undefined) => { const organisations = await prisma.organisation.findMany({ + where: { eventId }, select: { pid: true, name: true, event: { select: { pid: true, name: true } } }, }); - res.status(200).json({ + return res.status(200).json({ type: "success", payload: { organisations, @@ -16,6 +17,16 @@ export const getAllOrganisations = async (req: Request, res: Response) => { }); }; +interface GetAllOrganisationsSearchParams { + eventId?: string; +} + +export const getAllOrganisations = async (req: Request<{}, {}, {}, GetAllOrganisationsSearchParams>, res: Response) => { + const eventId = parseInt(req.query.eventId || ""); + + return _getAllOrganisations(res, isNaN(eventId) ? undefined : eventId); +}; + interface GetOrganisationQueryParams { pid: string; }