From 265b76083ad8e7fc9f686e0cfcbc2f59d568bd71 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Omar=20L=C3=B3pez?= Date: Thu, 28 Oct 2021 15:30:42 -0600 Subject: [PATCH] Fixes integrations api endpoint (#1066) --- pages/api/integrations.ts | 46 +++++++++++++++++++++------------------ 1 file changed, 25 insertions(+), 21 deletions(-) diff --git a/pages/api/integrations.ts b/pages/api/integrations.ts index 463ecd9b..76b0b296 100644 --- a/pages/api/integrations.ts +++ b/pages/api/integrations.ts @@ -1,20 +1,25 @@ +import type { NextApiRequest, NextApiResponse } from "next"; + import { getSession } from "@lib/auth"; +import prisma from "@lib/prisma"; -import prisma from "../../lib/prisma"; +export default async function handler(req: NextApiRequest, res: NextApiResponse) { + if (!["GET", "DELETE"].includes(req.method!)) { + return res.status(405).end(); + } + + // Check that user is authenticated + const session = await getSession({ req }); + + if (!session) { + res.status(401).json({ message: "You must be logged in to do this" }); + return; + } -export default async function handler(req, res) { if (req.method === "GET") { - // Check that user is authenticated - const session = await getSession({ req: req }); - - if (!session) { - res.status(401).json({ message: "You must be logged in to do this" }); - return; - } - const credentials = await prisma.credential.findMany({ where: { - userId: session.user.id, + userId: session.user?.id, }, select: { type: true, @@ -25,19 +30,18 @@ export default async function handler(req, res) { } if (req.method == "DELETE") { - const session = await getSession({ req: req }); - - if (!session) { - res.status(401).json({ message: "You must be logged in to do this" }); - return; - } - const id = req.body.id; - await prisma.credential.delete({ + await prisma.user.update({ where: { - id: id, - userId: session.user.id, + id: session?.user?.id, + }, + data: { + credentials: { + delete: { + id, + }, + }, }, });