From d5f85bbe9d0383e9df1914d77cee7af19ce34651 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kau=C3=AA=20Fraga=20Rodrigues?= <88486000+kauefraga@users.noreply.github.com> Date: Sat, 9 Nov 2024 15:38:23 -0300 Subject: [PATCH] Create user delete route --- apps/backend/src/user/user.controller.ts | 28 +++++++++++++++++++++++- 1 file changed, 27 insertions(+), 1 deletion(-) diff --git a/apps/backend/src/user/user.controller.ts b/apps/backend/src/user/user.controller.ts index a235841..0f319ff 100644 --- a/apps/backend/src/user/user.controller.ts +++ b/apps/backend/src/user/user.controller.ts @@ -19,6 +19,10 @@ const AuthUserSchema = z.object({ password: z.string(), }); +const DeleteUserSchema = z.object({ + id: z.string().uuid(), +}); + export const UserController = defineController(http => { http.post('/v1/user/create', async (request, reply) => { const { name, email, password } = CreateUserSchema.parse(request.body); @@ -61,7 +65,7 @@ export const UserController = defineController(http => { .limit(1); if (!existingUser) { - return reply.status(400).send({ + return reply.status(409).send({ message: 'The user does not exist.', }); } @@ -80,4 +84,26 @@ export const UserController = defineController(http => { token, }); }); + + http.delete('/v1/user/delete', async (request, reply) => { + const { id } = DeleteUserSchema.parse(request.body); + + const [existingUser] = await db.select().from(user).where(eq(user.id, id)).limit(1); + + if (!existingUser) { + return reply.status(409).send({ + message: 'The user does not exist.', + }); + } + + const { rowCount } = await db.delete(user).where(eq(user.id, id)); + + if (rowCount === 0) { + return reply.send(500).send({ + message: 'Failed to delete user.', + }); + } + + return reply.status(204).send(); + }); });