diff --git a/src/controllers/admin/category.controller.ts b/src/controllers/admin/category.controller.ts index e6ded81b..4b39c71f 100644 --- a/src/controllers/admin/category.controller.ts +++ b/src/controllers/admin/category.controller.ts @@ -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, @@ -25,3 +28,28 @@ export const addCategory = async ( res.status(500).json({ error: err }) } } + +export const updateCategory = async ( + req: Request, + res: Response +): Promise => { + 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 }) + } +} diff --git a/src/routes/admin/category/category.route.test.ts b/src/routes/admin/category/category.route.test.ts index affa4848..96a94893 100644 --- a/src/routes/admin/category/category.route.test.ts +++ b/src/routes/admin/category/category.route.test.ts @@ -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 () => { @@ -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 () => { @@ -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) + }) }) diff --git a/src/routes/admin/category/category.route.ts b/src/routes/admin/category/category.route.ts index 7f21fc0c..3b7b1d01 100644 --- a/src/routes/admin/category/category.route.ts +++ b/src/routes/admin/category/category.route.ts @@ -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 diff --git a/src/services/admin/category.service.ts b/src/services/admin/category.service.ts index 0b1cc3f7..38feafd3 100644 --- a/src/services/admin/category.service.ts +++ b/src/services/admin/category.service.ts @@ -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') + } +}