Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Delete my profile implemented #54

Merged
merged 14 commits into from
Aug 22, 2023
25 changes: 23 additions & 2 deletions src/controllers/profile.controller.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import type { Request, Response } from 'express'
import { updateProfile } from '../services/profile.service'
import { updateProfile, deleteProfile } from '../services/profile.service'
import type Profile from '../entities/profile.entity'

export const getProfileHandler = async (
req: Request,
res: Response
Expand Down Expand Up @@ -46,3 +45,25 @@ export const updateProfileHandler = async (
}
}
}

export const deleteProfileHandler = async (
req: Request,
res: Response
): Promise<void> => {
try {
const user = req.user as Profile
if (!user) {
res.status(404).json({ message: 'Profile not found' })
} else {
await deleteProfile(user.uuid)
res.status(200).json({ message: 'Profile deleted' })
}
} catch (err) {
if (err instanceof Error) {
console.error('Error executing query', err)
res
.status(500)
.json({ error: 'Internal server errorrrr', message: err.message })
}
}
}
10 changes: 10 additions & 0 deletions src/routes/profile/profile.route.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,16 @@ 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)
})
})

describe('Delete profile route', () => {
it('should delete the user profile and return a 200', async () => {
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`).send({}).expect(401)
})

afterAll(async () => {
await dataSource.destroy()
Expand Down
2 changes: 2 additions & 0 deletions src/routes/profile/profile.route.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import express from 'express'
import {
deleteProfileHandler,
getProfileHandler,
updateProfileHandler
} from '../../controllers/profile.controller'
Expand All @@ -9,5 +10,6 @@ const profileRouter = express.Router()

profileRouter.get('/profile', requireAuth, getProfileHandler)
profileRouter.put('/profile', requireAuth, updateProfileHandler)
profileRouter.delete('/profile', requireAuth, deleteProfileHandler)

export default profileRouter
11 changes: 11 additions & 0 deletions src/services/profile.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,3 +45,14 @@ export const updateProfile = async (
return { statusCode: 500, message: 'Internal server error' }
}
}

export const deleteProfile = async (userId: string): Promise<void> => {
const profileRepository = dataSource.getRepository(Profile)

await profileRepository
.createQueryBuilder()
.delete()
.from(Profile)
.where('uuid = :uuid', { uuid: userId })
.execute()
}