feat: tests all codepaths of event-type/[id]

This commit is contained in:
Agusti Fernandez Pardo committed 2022-03-24 23:16:48 +01:00
1 parent dfa227f1b6
commit 84ab74b46a
2 files changed
+38 -14

No files matched your search

+7 -13
View File
@@ -7,6 +7,8 @@ const prisma = new PrismaClient();
const schema = z
.object({
// since nextjs parses query params as strings,
// we need to cast them to numbers using z.transform() and parseInt()
id: z
.string()
.regex(/^\d+$/)
@@ -28,20 +30,12 @@ type ResponseData = {
export async function eventType(req: NextApiRequest, res: NextApiResponse<ResponseData>) {
const { query, method } = req;
if (method === "GET") {
try {
const safe = await schema.safeParse(query);
// if (!safe.success) {
// res.status(500).json({ error: safe.error.message });
// }
if (safe.success) {
const event = await prisma.eventType.findUnique({ where: { id: safe.data.id } });
const safe = await schema.safeParse(query);
if (safe.success) {
const event = await prisma.eventType.findUnique({ where: { id: safe.data.id } });
if (event) res.status(200).json({ data: event });
if (!event) res.status(404).json({ error: "Event type not found" });
}
} catch (error) {
console.log("catched", error);
res.status(500).json({ error: error });
if (event) res.status(200).json({ data: event });
if (!event) res.status(404).json({ error: "Event type not found" });
}
} else {
// Reject any other HTTP method than POST