diff --git a/server/controllers/index.js b/server/controllers/index.js index 24822543..aeffa462 100644 --- a/server/controllers/index.js +++ b/server/controllers/index.js @@ -1,9 +1,9 @@ const router = require('express').Router(); const admin = require('./routes/admin'); -router.get('/', (req, res) => { - res.send('

CA WIKI

'); -}); +const { serverError } = require('./middlewares/errorHandle'); + +router.use(serverError); router.use(admin); diff --git a/server/controllers/middlewares/errorHandle/error.js b/server/controllers/middlewares/errorHandle/error.js new file mode 100644 index 00000000..a041036f --- /dev/null +++ b/server/controllers/middlewares/errorHandle/error.js @@ -0,0 +1,7 @@ +/* eslint-disable no-unused-vars */ + +exports.serverError = (err, req, res, next) => { + res + .status(500) + .json({ StatusCode: '500', title: 'internal server error 500', err }); +}; diff --git a/server/controllers/middlewares/errorHandle/index.js b/server/controllers/middlewares/errorHandle/index.js index e69de29b..1891385e 100644 --- a/server/controllers/middlewares/errorHandle/index.js +++ b/server/controllers/middlewares/errorHandle/index.js @@ -0,0 +1,5 @@ +const { serverError } = require('./error'); + +module.exports = { + serverError, +}; diff --git a/server/controllers/routes/admin/cohort/deleteCohort.js b/server/controllers/routes/admin/cohort/deleteCohort.js index 78a23b91..07709373 100644 --- a/server/controllers/routes/admin/cohort/deleteCohort.js +++ b/server/controllers/routes/admin/cohort/deleteCohort.js @@ -2,16 +2,18 @@ const { deleteCohortQuery } = require('../../../../database/queries'); const deleteCohort = async (req, res, next) => { try { - const check = await deleteCohortQuery(req.params.cohortId); + const { cohortId } = req.params; + const check = await deleteCohortQuery(cohortId); if (check.rowCount !== 0) { res.json({ StatusCode: 200, data: { message: 'Cohort deleted successfully' }, }); } else { - const err = new Error(); - err.message = 'cohort id does not exist'; - next(err); + res.status(404).json({ + StatusCode: 404, + data: { message: 'Cohort does not exist' }, + }); } } catch (err) { next(err); diff --git a/server/controllers/routes/admin/project/addProject.js b/server/controllers/routes/admin/project/addProject.js index 733e9c25..af436fc9 100644 --- a/server/controllers/routes/admin/project/addProject.js +++ b/server/controllers/routes/admin/project/addProject.js @@ -1,16 +1,23 @@ const { addProjectQuery } = require('../../../../database/queries'); -const { addProjectSchema } = require('../../../../validation'); +const { projectSchema } = require('../../../../validation'); const addProject = async (req, res, next) => { try { - await addProjectSchema.validate(req.body); + await projectSchema.validate(req.body, { abortEarly: false }); await addProjectQuery(req.body); - res.json({ - StatusCode: 200, - data: { message: 'Cohort Added successfully' }, + res.status(201).json({ + StatusCode: 201, + data: { message: 'Project Added successfully' }, }); } catch (err) { - next(err); + if (err.errors) { + res.json({ + StatusCode: 400, + data: { message: err.errors }, + }); + } else { + next(err); + } } }; diff --git a/server/controllers/routes/admin/project/editProject.js b/server/controllers/routes/admin/project/editProject.js index 621f1e25..98b6ab88 100644 --- a/server/controllers/routes/admin/project/editProject.js +++ b/server/controllers/routes/admin/project/editProject.js @@ -1,17 +1,26 @@ const { editProjectQuery } = require('../../../../database/queries'); -const { editProjectSchema } = require('../../../../validation'); +const { projectSchema } = require('../../../../validation'); const editProject = async (req, res, next) => { try { - req.body.projectId = req.params.projectId; - await editProjectSchema.validate(req.body); + await projectSchema.validate( + { ...req.body, projectId: req.params.projectId }, + { abortEarly: false }, + ); await editProjectQuery(req.body); res.json({ StatusCode: 200, data: { message: 'project updated successfully' }, }); } catch (err) { - next(err); + if (err.errors) { + res.json({ + StatusCode: 400, + data: { message: err.errors }, + }); + } else { + next(err); + } } }; diff --git a/server/validation/index.js b/server/validation/index.js index b2672d18..672f7cac 100644 --- a/server/validation/index.js +++ b/server/validation/index.js @@ -1,7 +1,5 @@ -const addProjectSchema = require('./validateAddProject'); -const editProjectSchema = require('./validateEditProject'); +const projectSchema = require('./projectSchema'); module.exports = { - addProjectSchema, - editProjectSchema, + projectSchema, }; diff --git a/server/validation/validateAddProject.js b/server/validation/projectSchema.js similarity index 62% rename from server/validation/validateAddProject.js rename to server/validation/projectSchema.js index cd7ca591..37edb611 100644 --- a/server/validation/validateAddProject.js +++ b/server/validation/projectSchema.js @@ -1,13 +1,14 @@ const yup = require('yup'); -const addProjectSchema = yup.object({ +const projectSchema = yup.object({ name: yup.string().required(), description: yup.string().required(), imgUrl: yup.string().url().required(), githubLink: yup.string().url().required(), websiteLink: yup.string().url().required(), - projectType: yup.string().required(), + projectType: yup.mixed().oneOf(['internal', 'remotely']), cohortId: yup.number().integer().positive().required(), + projectId: yup.number().integer().positive(), }); -module.exports = addProjectSchema; +module.exports = projectSchema; diff --git a/server/validation/validateEditProject.js b/server/validation/validateEditProject.js deleted file mode 100644 index 0e792294..00000000 --- a/server/validation/validateEditProject.js +++ /dev/null @@ -1,14 +0,0 @@ -const yup = require('yup'); - -const editProjectSchema = yup.object({ - name: yup.string().required(), - description: yup.string().required(), - imgUrl: yup.string().url().required(), - githubLink: yup.string().url().required(), - websiteLink: yup.string().url().required(), - projectType: yup.string().required(), - cohortId: yup.number().integer().positive().required(), - projectId: yup.number().integer().positive().required(), -}); - -module.exports = editProjectSchema; diff --git a/test/index.test.js b/test/index.test.js index 7b47c017..9e821d41 100644 --- a/test/index.test.js +++ b/test/index.test.js @@ -4,77 +4,80 @@ const dbBuild = require('../server/database/config/build'); const app = require('../server/app'); -beforeEach(() => { - return dbBuild(); -}); +beforeAll(() => dbBuild()); -afterAll(() => { - return connection.end(); -}); +afterAll(() => connection.end()); -test('PUT Route /projects/1 status 200, json header, message:Cohort updated successfully', (done) => { - const testData = { - name: 'Mooooot', - description: 'project test', - imgUrl: 'https://github.com/GSG-G1', - githubLink: 'https://github.com/GSG-G1', - websiteLink: 'https://github.com/GSG-G1', - projectType: 'https://github.com/GSG-G1', - cohortId: '2', - }; - return request(app) - .put('/api/v1/projects/5') - .send(testData) - .expect(200) - .expect('Content-Type', /json/) - .end(async (err, res) => { - if (err) return done(err); - const { message } = res.body.data; - const result = await connection.query( - 'SELECT * from project WHERE id = 5', - ); - expect(message).toBe('project updated successfully'); - expect(result.rows[0].name).toBe('Mooooot'); - done(); - }); +describe('Admin, Project', () => { + test('Route /projects status 200, json header, data.message = Cohort Added successfully ', (done) => { + const reqData = { + name: 'Mohmmedzw851@', + description: 'description', + imgUrl: 'https://avatars3.githubusercontent.com/u/52123464?s=200&v=4', + githubLink: 'https://avatars3.githubusercontent.com/u/52123464?s=200&v=4', + websiteLink: + 'https://avatars3.githubusercontent.com/u/52123464?s=200&v=4', + projectType: 'internal', + cohortId: '1', + }; + return request(app) + .post('/api/v1/projects') + .send(reqData) + .expect(201) + .expect('Content-Type', /json/) + .end(async (err, res) => { + const { message } = res.body.data; + if (err) return done(err); + const { rows } = await connection.query( + 'SELECT * from project WHERE id = 6', + ); + expect(rows[0].name).toBe('Mohmmedzw851@'); + expect(message).toBe('Project Added successfully'); + done(); + }); + }); }); -test('Route /projects status 200, json header, data.message = Cohort Added successfully ', (done) => { - const reqData = { - name: 'Mohmmedzw851@', - description: 'description', - imgUrl: 'https://avatars3.githubusercontent.com/u/52123464?s=200&v=4', - githubLink: 'https://avatars3.githubusercontent.com/u/52123464?s=200&v=4', - websiteLink: 'https://avatars3.githubusercontent.com/u/52123464?s=200&v=4', - projectType: 'internal', - cohortId: '1', - }; - return request(app) - .post('/api/v1/projects') - .send(reqData) - .expect(200) - .expect('Content-Type', /json/) - .end((err, res) => { - const { message } = res.body.data; - if (err) return done(err); - expect(message).toBe('Cohort Added successfully'); - done(); - }); +describe('Admin, (/cohorts/:cohortId)', () => { + test('Route /cohorts/1 status 200, data.message = Cohort deleted successfully ', (done) => { + return request(app) + .delete('/api/v1/cohorts/1') + .expect(200) + .expect('Content-Type', /json/) + .end(async (err, res) => { + const { message } = res.body.data; + if (err) return done(err); + const { rows } = await connection.query( + 'SELECT * from cohort WHERE id = 1', + ); + expect(rows).toHaveLength(0); + expect(message).toBe('Cohort deleted successfully'); + done(); + }); + }); }); -test('Route /cohorts/1 status 200, data.message = Cohort deleted successfully ', (done) => { - return request(app) - .delete('/api/v1/cohorts/1') - .expect(200) - .expect('Content-Type', /json/) - .end(async (err, res) => { - const { message } = res.body.data; - if (err) return done(err); - const { rows } = await connection.query( - 'SELECT * from cohort WHERE id = 1', - ); - expect(rows).toHaveLength(0); - expect(message).toBe('Cohort deleted successfully'); - done(); - }); +describe('Admin, (/projects/:projectId)', () => { + test('PUT Route /projects/1 status 200, json header, message:Cohort updated successfully', (done) => { + const testData = { + name: 'Mooooot', + description: 'project test', + imgUrl: 'https://github.com/GSG-G1', + githubLink: 'https://github.com/GSG-G1', + websiteLink: 'https://github.com/GSG-G1', + projectType: 'remotely', + cohortId: '2', + }; + return request(app) + .put('/api/v1/projects/5') + .send(testData) + .expect(200) + .expect('Content-Type', /json/) + .end(async (err, res) => { + if (err) return done(err); + const { message } = res.body.data; + expect(message).toBe('project updated successfully'); + done(); + }); + }); });