added createParticipant, patched DataType

This commit is contained in:
Laurin
2022-05-29 00:27:04 +02:00
parent 233182c0ea
commit 0431406328
2 changed files with 69 additions and 0 deletions
+1
View File
@@ -23,6 +23,7 @@ export enum DataType {
NUMBER = "number",
INTEGER = "integer",
PERMISSION_LEVEL = "'ELEVATED' | 'STANDARD'",
JOB = "'TEAMLEADER' | 'MEMBER'",
DATETIME = "ISOstring",
UUID = "string",
RESULT_SCHEMA = "result_schema",
+68
View File
@@ -0,0 +1,68 @@
import prisma from "../lib/prisma";
import { z } from "zod";
import { Request, Response } from "express";
import { createInsufficientPermissionsError, DataType, generateError, generateInvalidBodyError } from "./common";
import { Job } from "@prisma/client";
import { PrismaClientKnownRequestError } from "@prisma/client/runtime";
//TODO: add TeamleaderAuthentification
// discuss wether
const ParticipantBody = z.object({
firstName: z.string(),
lastName: z.string(),
groupId: z.string(),
job: z.enum(["TEAMLEADER", "MEMBER"]),
})
const returnedParticipant = {
pid: true,
firstName: true,
lastName: true,
relevance: true,
team: { select: { pid: true, } },
group: { select: { pid: true, } },
} as const;
export const createParticipant = async (req: Request<{ pid: string}>, res: Response) => {
//insert TeamleaderAuth
const result = ParticipantBody.safeParse(req.body);
if(result.success === false){
return res.status(400).json(
generateInvalidBodyError({
firstname: DataType.STRING,
lastName: DataType.STRING,
groupId: DataType.UUID,
job: DataType.JOB,
})
);
}
const body = result.data;
const { pid } = req.params;
try {
const participant = await prisma.participant.create({
data: {
firstName: body.firstName,
lastName: body.lastName,
relevance: body.job,
group: { connect: { pid: body.groupId } },
team: { connect: { pid } },
},
select: returnedParticipant,
});
return res.status(201).json({
type: "success",
payload: participant,
});
} catch (e) {
if (e instanceof PrismaClientKnownRequestError && e.code === "P2025") {
return res.status(404).json(generateError(`Could not link to team with ID '${pid}, or group with ID ${body.groupId}'`));
}
throw e;
}
}