Skip to content

Commit

Permalink
Implement Update mentor categories endpoint (Admin) #42 (#70)
Browse files Browse the repository at this point in the history
Co-authored-by: Krishnadeva <[email protected]>
  • Loading branch information
sathudeva7 and Krishnadeva authored Oct 9, 2023
1 parent e6d2520 commit 50d58de
Show file tree
Hide file tree
Showing 4 changed files with 101 additions and 2 deletions.
30 changes: 29 additions & 1 deletion src/controllers/admin/category.controller.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
import type { Request, Response } from 'express'
import type Profile from '../../entities/profile.entity'
import { ProfileTypes } from '../../enums'
import { createCategory } from '../../services/admin/category.service'
import {
changeCategory,
createCategory
} from '../../services/admin/category.service'

export const addCategory = async (
req: Request,
Expand All @@ -25,3 +28,28 @@ export const addCategory = async (
res.status(500).json({ error: err })
}
}

export const updateCategory = async (
req: Request,
res: Response
): Promise<void> => {
try {
const user = req.user as Profile
const { categoryName } = req.body
const { categoryId } = req.params

if (user.type !== ProfileTypes.ADMIN) {
res.status(403).json({ message: 'Only Admins are allowed' })
} else {
const { category, statusCode, message } = await changeCategory(
categoryId,
categoryName
)

res.status(statusCode).json({ category, message })
}
} catch (err) {
console.error('Error executing query', err)
res.status(500).json({ error: err })
}
}
29 changes: 29 additions & 0 deletions src/routes/admin/category/category.route.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,14 @@ import { ProfileTypes } from '../../../enums'
import { dataSource } from '../../../configs/dbConfig'
import bcrypt from 'bcrypt'
import { mockUser, mockAdmin } from '../../../../mocks'
import Category from '../../../entities/category.entity'

const port = Math.floor(Math.random() * (9999 - 3000 + 1)) + 3000

let server: Express
let agent: supertest.SuperAgentTest
let adminAgent: supertest.SuperAgentTest
let savedCategory: Category

describe('Admin category routes', () => {
beforeAll(async () => {
Expand Down Expand Up @@ -42,6 +44,10 @@ describe('Admin category routes', () => {
await profileRepository.save(newProfile)

await adminAgent.post('/api/auth/login').send(mockAdmin).expect(200)

const categoryRepository = dataSource.getRepository(Category)
const newCategory = new Category('Random Category', [])
savedCategory = await categoryRepository.save(newCategory)
}, 5000)

it('should add a category', async () => {
Expand All @@ -57,4 +63,27 @@ describe('Admin category routes', () => {
.send({ categoryName: 'Computer Science' })
.expect(403)
})

it('should update a category', async () => {
const response = await adminAgent
.put(`/api/admin/categories/${savedCategory.uuid}`)
.send({ categoryName: 'Science' })
.expect(201)

expect(response.body).toHaveProperty('category')
})

it('should return 404 when an invalid category id was provided', async () => {
await adminAgent
.put('/api/admin/categories/0058ab92-1c82-4af1-9f84-c60a3e922244')
.send({ categoryName: 'Computer Science' })
.expect(404)
})

it('should only allow admins to update a category', async () => {
await agent
.put(`/api/admin/categories/${savedCategory.uuid}`)
.send({ categoryName: 'Science' })
.expect(403)
})
})
6 changes: 5 additions & 1 deletion src/routes/admin/category/category.route.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
import express from 'express'
import { requireAuth } from '../../../controllers/auth.controller'
import { addCategory } from '../../../controllers/admin/category.controller'
import {
addCategory,
updateCategory
} from '../../../controllers/admin/category.controller'

const categoryRouter = express.Router()

categoryRouter.post('/', requireAuth, addCategory)
categoryRouter.put('/:categoryId', requireAuth, updateCategory)

export default categoryRouter
38 changes: 38 additions & 0 deletions src/services/admin/category.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,41 @@ export const createCategory = async (
throw new Error('Error creating category')
}
}

export const changeCategory = async (
categoryId: string,
categoryName: string
): Promise<{
statusCode: number
category?: Category | null
message: string
}> => {
try {
const categoryRepository = dataSource.getRepository(Category)

const category = await categoryRepository.findOne({
where: { uuid: categoryId }
})

if (!category) {
return {
statusCode: 404,
message: 'Category not found'
}
}

await categoryRepository.update(
{ uuid: categoryId },
{ category: categoryName }
)

return {
statusCode: 201,
category,
message: 'Category updated successfully'
}
} catch (err) {
console.error('Error updating category', err)
throw new Error('Error updating category')
}
}

0 comments on commit 50d58de

Please sign in to comment.