Only show details when authenticated

This commit is contained in:
Stephan
2022-04-26 16:04:26 +02:00
parent 0c65aec96c
commit fe6879ea56
+29 -16
View File
@@ -1,3 +1,4 @@
import { Admin } from "@prisma/client";
import { PrismaClientUnknownRequestError } from "@prisma/client/runtime"; import { PrismaClientUnknownRequestError } from "@prisma/client/runtime";
import { Request, Response } from "express"; import { Request, Response } from "express";
import prisma from "../lib/prisma"; import prisma from "../lib/prisma";
@@ -47,15 +48,23 @@ export const getGroup = async (req: Request<GetGroupQueryParams>, res: Response)
const { pid } = req.params; const { pid } = req.params;
try { 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 }, where: { pid },
select: { select: req.auth?.isAuthenticated
pid: true, ? {
name: true, pid: true,
oragnisation: { select: { pid: true, name: true } }, name: true,
admins: { select: { pid: true, name: true } }, oragnisation: { select: { pid: true, name: true } },
participants: { select: { pid: true } }, admins: { select: { pid: true, name: true } },
}, participants: { select: { pid: true } },
}
: basicGroup,
}); });
if (!group) { if (!group) {
@@ -71,14 +80,18 @@ export const getGroup = async (req: Request<GetGroupQueryParams>, res: Response)
...group.oragnisation, ...group.oragnisation,
_links: [{ rel: "self", type: "GET", href: `/api/organisation/${group.oragnisation.pid}` }], _links: [{ rel: "self", type: "GET", href: `/api/organisation/${group.oragnisation.pid}` }],
}, },
admins: group.admins.map((admin) => ({ ...(req.auth?.isAuthenticated
...admin, ? {
_links: [{ rel: "self", type: "GET", href: `/api/admins/${admin.pid}` }], admins: group.admins?.map((admin) => ({
})), ...admin,
participants: group.participants.map((participant) => ({ _links: [{ rel: "self", type: "GET", href: `/api/admins/${admin.pid}` }],
...participant, })),
_links: [{ rel: "self", type: "GET", href: `/api/participant/${participant.pid}` }], participants: group.participants?.map((participant) => ({
})), ...participant,
_links: [{ rel: "self", type: "GET", href: `/api/participant/${participant.pid}` }],
})),
}
: {}),
}, },
}, },
}); });