diff --git a/src/__tests__/app.test.ts b/src/__tests__/app.test.ts index 60231e2..31ab48f 100644 --- a/src/__tests__/app.test.ts +++ b/src/__tests__/app.test.ts @@ -149,6 +149,34 @@ describe('GET /v1/category', () => { }) }) +describe('DELETE /v1/category', () => { + it('returns status code 200 and a success message', async () => { + const res = await request(app) + .delete('/v1/category') + .set('Content-Type', 'application/json') + .set('Cookie', jwtCookie as string) + .send(JSON.stringify({ + name: 'VueJS Composition API' + })) + + expect(res.status).toEqual(200) + expect(res.body).toHaveProperty('message') + }) + + it('returns status code 400 and an error message', async () => { + const res = await request(app) + .delete('/v1/category') + .set('Content-Type', 'application/json') + .set('Cookie', jwtCookie as string) + .send(JSON.stringify({ + blablabla: 'pouet' + })) + + expect(res.status).toEqual(400) + expect(res.body).toHaveProperty('message') + }) +}) + describe('PUT /v1/user', () => { it('returns status code 200 and success message', async () => { const res = await request(app) diff --git a/src/routers/category.ts b/src/routers/category.ts index 8338555..09fec23 100644 --- a/src/routers/category.ts +++ b/src/routers/category.ts @@ -1,7 +1,7 @@ -import { Category, PrismaClient } from "@prisma/client" +import { Category, Prisma, PrismaClient } from "@prisma/client" import express, { RequestHandler } from "express" import { z } from "zod" -import { parseJwtUserId } from "./user.js" +import { userIdMiddleware } from "./user.js" const categoryRouter = express.Router() const prisma = new PrismaClient() @@ -11,16 +11,10 @@ const categoryData = z.object({ }).required() const categoryGet: RequestHandler = async (req, res) => { - const userId = parseJwtUserId(req.cookies.jwt) - if (userId === undefined) { - res.status(400).json({ message: 'Incorrect JWT payload.' }) - return; - } - let categories: Category[] | null = null try { - categories = await prisma.category.findMany({ where: { user_id: userId } }) + categories = await prisma.category.findMany({ where: { user_id: req.body.userId } }) } catch (error: any) { res.status(400).json({ message: (error.issues ?? error) }) return; @@ -30,18 +24,12 @@ const categoryGet: RequestHandler = async (req, res) => { } const categoryPost: RequestHandler = async (req, res) => { - const userId = parseJwtUserId(req.cookies.jwt) - if (userId === undefined) { - res.status(400).json({ message: 'Incorrect JWT payload.' }) - return; - } - try { const newCategory = categoryData.parse(req.body) await prisma.category.create({ data: { name: newCategory.name, - user_id: userId + user_id: req.body.userId } }) } catch (error: any) { @@ -52,7 +40,27 @@ const categoryPost: RequestHandler = async (req, res) => { res.json({ message: 'Category successfully added.' }) } -categoryRouter.get('/', categoryGet) -categoryRouter.post('/', categoryPost) +const categoryDelete: RequestHandler = async (req, res) => { + let deleted: Prisma.BatchPayload + + try { + const categoryToDel = categoryData.parse(req.body) + deleted = await prisma.category.deleteMany({ + where: { + name: categoryToDel.name, + user_id: req.body.userId + } + }) + } catch (error: any) { + res.status(400).json({ message: (error.issues ?? error) }) + return; + } + + res.json({ message: `${deleted.count} category / categories successfully deleted.` }) +} + +categoryRouter.get('/', userIdMiddleware, categoryGet) +categoryRouter.post('/', userIdMiddleware, categoryPost) +categoryRouter.delete('/', userIdMiddleware, categoryDelete) export default categoryRouter diff --git a/src/routers/user.ts b/src/routers/user.ts index 980267c..5fe1bf3 100644 --- a/src/routers/user.ts +++ b/src/routers/user.ts @@ -21,6 +21,16 @@ export function parseJwtUserId(jwtoken: string): number | undefined { return payload.userId } +export const userIdMiddleware: RequestHandler = (req, res, next) => { + const userId = parseJwtUserId(req.cookies.jwt) + if (userId === undefined) { + res.status(400).json({ message: 'Incorrect JWT payload.' }) + return; + } + req.body.userId = userId + next() +} + const userRouterPut: RequestHandler = async (req, res) => { const userId = parseJwtUserId(req.cookies.jwt) if (userId === undefined) {