From fe6879ea56671129fd6dd7ae948a3cb2a765f79f Mon Sep 17 00:00:00 2001 From: Stephan <57194608+stephan418@users.noreply.github.com> Date: Sat, 23 Apr 2022 21:50:52 +0200 Subject: [PATCH] Only show details when authenticated --- src/Controllers/group.controllers.ts | 45 ++++++++++++++++++---------- 1 file changed, 29 insertions(+), 16 deletions(-) diff --git a/src/Controllers/group.controllers.ts b/src/Controllers/group.controllers.ts index b9ba189..ec95ada 100644 --- a/src/Controllers/group.controllers.ts +++ b/src/Controllers/group.controllers.ts @@ -1,3 +1,4 @@ +import { Admin } from "@prisma/client"; import { PrismaClientUnknownRequestError } from "@prisma/client/runtime"; import { Request, Response } from "express"; import prisma from "../lib/prisma"; @@ -47,15 +48,23 @@ export const getGroup = async (req: Request, res: Response) const { pid } = req.params; try { - const group = await prisma.group.findUnique({ + const group: { + pid: string; + name: string; + oragnisation: { pid: string; name: string }; + admins?: { pid: string; name: string }[]; + participants?: { pid: string }[]; + } | null = 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 } }, - }, + select: req.auth?.isAuthenticated + ? { + pid: true, + name: true, + oragnisation: { select: { pid: true, name: true } }, + admins: { select: { pid: true, name: true } }, + participants: { select: { pid: true } }, + } + : basicGroup, }); if (!group) { @@ -71,14 +80,18 @@ export const getGroup = async (req: Request, res: Response) ...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}` }], - })), + ...(req.auth?.isAuthenticated + ? { + 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}` }], + })), + } + : {}), }, }, });