Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

Implement Update mentor categories endpoint (Admin) #42 #70

Merged
merged 9 commits into from
Oct 9, 2023
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')
}
}