From 6c50a67d40979db283b4f2ad72732e4ad82187d8 Mon Sep 17 00:00:00 2001 From: Danishkar Date: Wed, 9 Aug 2023 19:55:42 +0530 Subject: [PATCH 01/11] delete my profile implemented --- src/controllers/profile.controller.ts | 21 ++++++++++++++++++++- src/routes/profile/profile.route.ts | 2 ++ src/services/profile.service.ts | 11 +++++++++++ 3 files changed, 33 insertions(+), 1 deletion(-) diff --git a/src/controllers/profile.controller.ts b/src/controllers/profile.controller.ts index c77b69d6..cd0fc2cd 100644 --- a/src/controllers/profile.controller.ts +++ b/src/controllers/profile.controller.ts @@ -1,5 +1,5 @@ import type { Request, Response } from 'express' -import { getProfile, updateProfile } from '../services/profile.service' +import { getProfile, updateProfile, deleteProfile } from '../services/profile.service' export const getProfileHandler = async ( req: Request, @@ -40,3 +40,22 @@ export const updateProfileHandler = async ( res.status(500).json({ error: err }) } } + +export const deleteProfileHandler = async ( + req: Request, + res: Response +): Promise => { + try { + const userId = req.params.uuid + const user = await getProfile(req) + if (!user || user.uuid !== userId) { + res.status(404).json({ message: 'Profile not found' }) + } else { + await deleteProfile(user) + res.status(200).json({ message: 'Profile deleted' }) + } + }catch (err) { + console.error('Error executing query', err) + res.status(500).json({ error: err }) + } +} diff --git a/src/routes/profile/profile.route.ts b/src/routes/profile/profile.route.ts index f2742802..413f56a9 100644 --- a/src/routes/profile/profile.route.ts +++ b/src/routes/profile/profile.route.ts @@ -1,5 +1,6 @@ import express from 'express' import { + deleteProfileHandler, getProfileHandler, updateProfileHandler } from '../../controllers/profile.controller' @@ -9,5 +10,6 @@ const profileRouter = express.Router() profileRouter.get('/profile', requireAuth, getProfileHandler) profileRouter.put('/profile', requireAuth, updateProfileHandler) +profileRouter.delete('/profile/:uuid', requireAuth, deleteProfileHandler) export default profileRouter diff --git a/src/services/profile.service.ts b/src/services/profile.service.ts index aa442df6..6c5d4138 100644 --- a/src/services/profile.service.ts +++ b/src/services/profile.service.ts @@ -50,3 +50,14 @@ export const updateProfile = async ( return updatedProfile.raw.length === 0 ? undefined : updatedProfile.raw[0] } + +export const deleteProfile = async (user: Profile): Promise => { + const profileRepository = dataSource.getRepository(Profile) + + await profileRepository + .createQueryBuilder() + .delete() + .from(Profile) + .where('uuid = :uuid', { uuid: user?.uuid }) + .execute() +} From d20afffc647ade6499b426da1a64c01a0546a136 Mon Sep 17 00:00:00 2001 From: Danishkar Date: Wed, 16 Aug 2023 14:03:54 +0530 Subject: [PATCH 02/11] changes the argument of delete profile has userId --- src/controllers/profile.controller.ts | 2 +- src/services/profile.service.ts | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/controllers/profile.controller.ts b/src/controllers/profile.controller.ts index 2ccbc78c..86ede288 100644 --- a/src/controllers/profile.controller.ts +++ b/src/controllers/profile.controller.ts @@ -59,7 +59,7 @@ export const deleteProfileHandler = async ( if (!user || user.uuid !== userId) { res.status(404).json({ message: 'Profile not found' }) } else { - await deleteProfile(user) + await deleteProfile(user.uuid) res.status(200).json({ message: 'Profile deleted' }) } }catch (err) { diff --git a/src/services/profile.service.ts b/src/services/profile.service.ts index 53f9c8d8..4aeaf864 100644 --- a/src/services/profile.service.ts +++ b/src/services/profile.service.ts @@ -40,13 +40,13 @@ export const updateProfile = async ( return savedProfile } -export const deleteProfile = async (user: Profile): Promise => { +export const deleteProfile = async (userId: string): Promise => { const profileRepository = dataSource.getRepository(Profile) await profileRepository .createQueryBuilder() .delete() .from(Profile) - .where('uuid = :uuid', { uuid: user?.uuid }) + .where('uuid = :uuid', { uuid: userId }) .execute() } From 9092a8cd67436b7f4372d7055d7f91481e069373 Mon Sep 17 00:00:00 2001 From: Danishkar Date: Wed, 16 Aug 2023 14:22:35 +0530 Subject: [PATCH 03/11] changes the getProfile method to req.user --- src/controllers/profile.controller.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/controllers/profile.controller.ts b/src/controllers/profile.controller.ts index d72b9798..df9a670a 100644 --- a/src/controllers/profile.controller.ts +++ b/src/controllers/profile.controller.ts @@ -52,7 +52,7 @@ export const deleteProfileHandler = async ( ): Promise => { try { const userId = req.params.uuid - const user = await getProfile(req) + const user = req.user as Profile if (!user || user.uuid !== userId) { res.status(404).json({ message: 'Profile not found' }) } else { From ddf59e4fa1eed8ca2d28607b9014489b88ff7ef8 Mon Sep 17 00:00:00 2001 From: Danishkar Date: Wed, 16 Aug 2023 18:49:17 +0530 Subject: [PATCH 04/11] lint fix --- src/controllers/profile.controller.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/controllers/profile.controller.ts b/src/controllers/profile.controller.ts index df9a670a..e8f5dce4 100644 --- a/src/controllers/profile.controller.ts +++ b/src/controllers/profile.controller.ts @@ -59,7 +59,7 @@ export const deleteProfileHandler = async ( await deleteProfile(user.uuid) res.status(200).json({ message: 'Profile deleted' }) } - }catch (err) { + } catch (err) { console.error('Error executing query', err) res.status(500).json({ error: err }) } From 3138113a2f5bf25cdfe8aaabd7de316b1d0c0211 Mon Sep 17 00:00:00 2001 From: Danishkar Date: Thu, 17 Aug 2023 14:15:13 +0530 Subject: [PATCH 05/11] delete profile testing --- src/routes/profile/profile.route.test.ts | 20 +++++++++++++++++++- 1 file changed, 19 insertions(+), 1 deletion(-) diff --git a/src/routes/profile/profile.route.test.ts b/src/routes/profile/profile.route.test.ts index 61b68969..ac117388 100644 --- a/src/routes/profile/profile.route.test.ts +++ b/src/routes/profile/profile.route.test.ts @@ -57,7 +57,6 @@ describe('profile', () => { await agent.put('/api/me/profile').send(updatedProfile).expect(200) }) - it('should return a 401 when a valid access token is not provided', async () => { await supertest(server).put('/api/me/profile').send({}).expect(401) }) @@ -66,4 +65,23 @@ describe('profile', () => { await dataSource.destroy() }) }) + describe('Delete profile route', () => { + let profileUuid: string; + beforeAll(async () => { + const response = await agent.get('/api/me/profile'); + profileUuid = response.body.uuid; + }); + + it('should delete the user profile and return a 200', async () => { + await agent.delete(`/api/me/profile/${profileUuid}`).expect(200); + }); + + it('should return a 401 when a valid access token is not provided', async () => { + await supertest(server).delete(`/api/me/profile/${profileUuid}`).send({}).expect(401); + }); + + afterAll(async () => { + await dataSource.destroy(); + }); + }); }) From 6a44ef18ffaa5d613268e5f21b3909fc560d26d5 Mon Sep 17 00:00:00 2001 From: Danishkar Date: Thu, 17 Aug 2023 20:59:15 +0530 Subject: [PATCH 06/11] lint fix --- src/routes/profile/profile.route.test.ts | 33 +++++++++++++----------- 1 file changed, 18 insertions(+), 15 deletions(-) diff --git a/src/routes/profile/profile.route.test.ts b/src/routes/profile/profile.route.test.ts index ac117388..3718ecf6 100644 --- a/src/routes/profile/profile.route.test.ts +++ b/src/routes/profile/profile.route.test.ts @@ -66,22 +66,25 @@ describe('profile', () => { }) }) describe('Delete profile route', () => { - let profileUuid: string; + let profileUuid: string beforeAll(async () => { - const response = await agent.get('/api/me/profile'); - profileUuid = response.body.uuid; - }); - - it('should delete the user profile and return a 200', async () => { - await agent.delete(`/api/me/profile/${profileUuid}`).expect(200); - }); - + const response = await agent.get('/api/me/profile') + profileUuid = response.body.uuid + }) + + it('should delete the user profile and return a 200', async () => { + await agent.delete(`/api/me/profile/${profileUuid}`).expect(200) + }) + it('should return a 401 when a valid access token is not provided', async () => { - await supertest(server).delete(`/api/me/profile/${profileUuid}`).send({}).expect(401); - }); - + await supertest(server) + .delete(`/api/me/profile/${profileUuid}`) + .send({}) + .expect(401) + }) + afterAll(async () => { - await dataSource.destroy(); - }); - }); + await dataSource.destroy() + }) + }) }) From 79b01a9dcf588d4324d566f0b7e715c848a091ce Mon Sep 17 00:00:00 2001 From: Danishkar Date: Sat, 19 Aug 2023 21:46:53 +0530 Subject: [PATCH 07/11] requested changes --- src/controllers/profile.controller.ts | 11 +++++++---- src/routes/profile/profile.route.ts | 2 +- 2 files changed, 8 insertions(+), 5 deletions(-) diff --git a/src/controllers/profile.controller.ts b/src/controllers/profile.controller.ts index e8f5dce4..09c0a8d6 100644 --- a/src/controllers/profile.controller.ts +++ b/src/controllers/profile.controller.ts @@ -51,16 +51,19 @@ export const deleteProfileHandler = async ( res: Response ): Promise => { try { - const userId = req.params.uuid const user = req.user as Profile - if (!user || user.uuid !== userId) { + if (!user) { res.status(404).json({ message: 'Profile not found' }) } else { await deleteProfile(user.uuid) res.status(200).json({ message: 'Profile deleted' }) } } catch (err) { - console.error('Error executing query', err) - res.status(500).json({ error: err }) + if (err instanceof Error) { + console.error('Error executing query', err) + res + .status(500) + .json({ error: 'Internal server error', message: err.message }) + } } } diff --git a/src/routes/profile/profile.route.ts b/src/routes/profile/profile.route.ts index 413f56a9..b30ac759 100644 --- a/src/routes/profile/profile.route.ts +++ b/src/routes/profile/profile.route.ts @@ -10,6 +10,6 @@ const profileRouter = express.Router() profileRouter.get('/profile', requireAuth, getProfileHandler) profileRouter.put('/profile', requireAuth, updateProfileHandler) -profileRouter.delete('/profile/:uuid', requireAuth, deleteProfileHandler) +profileRouter.delete('/profile', requireAuth, deleteProfileHandler) export default profileRouter From 1d761f673cf352f7418f753ad730ad4dca99168f Mon Sep 17 00:00:00 2001 From: Danishkar Date: Tue, 22 Aug 2023 11:39:28 +0530 Subject: [PATCH 08/11] test cases for the delete profile --- src/controllers/profile.controller.ts | 2 +- src/routes/profile/profile.route.test.ts | 17 ++--------------- 2 files changed, 3 insertions(+), 16 deletions(-) diff --git a/src/controllers/profile.controller.ts b/src/controllers/profile.controller.ts index 09c0a8d6..1e7c10f7 100644 --- a/src/controllers/profile.controller.ts +++ b/src/controllers/profile.controller.ts @@ -63,7 +63,7 @@ export const deleteProfileHandler = async ( console.error('Error executing query', err) res .status(500) - .json({ error: 'Internal server error', message: err.message }) + .json({ error: 'Internal server errorrrr', message: err.message }) } } } diff --git a/src/routes/profile/profile.route.test.ts b/src/routes/profile/profile.route.test.ts index 3718ecf6..c88e4acd 100644 --- a/src/routes/profile/profile.route.test.ts +++ b/src/routes/profile/profile.route.test.ts @@ -60,27 +60,14 @@ describe('profile', () => { it('should return a 401 when a valid access token is not provided', async () => { await supertest(server).put('/api/me/profile').send({}).expect(401) }) - - afterAll(async () => { - await dataSource.destroy() - }) }) describe('Delete profile route', () => { - let profileUuid: string - beforeAll(async () => { - const response = await agent.get('/api/me/profile') - profileUuid = response.body.uuid - }) - it('should delete the user profile and return a 200', async () => { - await agent.delete(`/api/me/profile/${profileUuid}`).expect(200) + await agent.delete('/api/me/profile').expect(200) }) it('should return a 401 when a valid access token is not provided', async () => { - await supertest(server) - .delete(`/api/me/profile/${profileUuid}`) - .send({}) - .expect(401) + await supertest(server).delete(`/api/me/profile`).send({}).expect(401) }) afterAll(async () => { From 735ac0d903fefb77581585609aec06365aab0e7f Mon Sep 17 00:00:00 2001 From: Anjula Shanaka Date: Tue, 22 Aug 2023 16:12:48 +0530 Subject: [PATCH 09/11] Fix formatting errors --- src/routes/profile/profile.route.test.ts | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/routes/profile/profile.route.test.ts b/src/routes/profile/profile.route.test.ts index c88e4acd..9a9aa603 100644 --- a/src/routes/profile/profile.route.test.ts +++ b/src/routes/profile/profile.route.test.ts @@ -57,10 +57,12 @@ describe('profile', () => { await agent.put('/api/me/profile').send(updatedProfile).expect(200) }) + it('should return a 401 when a valid access token is not provided', async () => { await supertest(server).put('/api/me/profile').send({}).expect(401) }) }) + describe('Delete profile route', () => { it('should delete the user profile and return a 200', async () => { await agent.delete('/api/me/profile').expect(200) From f4f4fc761be0dc5b09e5ba3f0d03cbdabe4c68ef Mon Sep 17 00:00:00 2001 From: Anjula Shanaka Date: Tue, 22 Aug 2023 16:22:54 +0530 Subject: [PATCH 10/11] Apply suggestions from code review --- src/routes/profile/profile.route.test.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/routes/profile/profile.route.test.ts b/src/routes/profile/profile.route.test.ts index 9a9aa603..f0b9b8f6 100644 --- a/src/routes/profile/profile.route.test.ts +++ b/src/routes/profile/profile.route.test.ts @@ -57,12 +57,12 @@ describe('profile', () => { await agent.put('/api/me/profile').send(updatedProfile).expect(200) }) - + it('should return a 401 when a valid access token is not provided', async () => { await supertest(server).put('/api/me/profile').send({}).expect(401) }) }) - + describe('Delete profile route', () => { it('should delete the user profile and return a 200', async () => { await agent.delete('/api/me/profile').expect(200) From c31db79e0aa0d742a4148995ab50be18fc6919b8 Mon Sep 17 00:00:00 2001 From: Danishkar Date: Tue, 22 Aug 2023 16:41:54 +0530 Subject: [PATCH 11/11] lint fixs --- src/routes/profile/profile.route.test.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/routes/profile/profile.route.test.ts b/src/routes/profile/profile.route.test.ts index f0b9b8f6..cd6ec293 100644 --- a/src/routes/profile/profile.route.test.ts +++ b/src/routes/profile/profile.route.test.ts @@ -57,12 +57,12 @@ describe('profile', () => { await agent.put('/api/me/profile').send(updatedProfile).expect(200) }) - + it('should return a 401 when a valid access token is not provided', async () => { await supertest(server).put('/api/me/profile').send({}).expect(401) }) }) - + describe('Delete profile route', () => { it('should delete the user profile and return a 200', async () => { await agent.delete('/api/me/profile').expect(200)