From dd0064f68d824a826bc1f1588a44a4647c138efd Mon Sep 17 00:00:00 2001 From: mu7ammadabed Date: Mon, 30 Mar 2020 01:13:12 +0300 Subject: [PATCH 01/19] Client and server errors handling Add client and server errors handling as a middleware at error.js and require them at app.js Relates #42 --- server/controllers/index.js | 7 ++++--- server/controllers/middlewares/errorHandle/error.js | 7 +++++++ server/controllers/middlewares/errorHandle/index.js | 6 ++++++ 3 files changed, 17 insertions(+), 3 deletions(-) create mode 100644 server/controllers/middlewares/errorHandle/error.js diff --git a/server/controllers/index.js b/server/controllers/index.js index 403948e7..a46c3ead 100644 --- a/server/controllers/index.js +++ b/server/controllers/index.js @@ -1,7 +1,8 @@ const router = require('express').Router(); -router.get('/', (req, res) => { - res.send('

CA WIKI

'); -}); +const { client, server } = require('./middlewares/errorHandle'); + +router.all('*', client); +router.use(server); module.exports = router; diff --git a/server/controllers/middlewares/errorHandle/error.js b/server/controllers/middlewares/errorHandle/error.js new file mode 100644 index 00000000..21cfdb33 --- /dev/null +++ b/server/controllers/middlewares/errorHandle/error.js @@ -0,0 +1,7 @@ +exports.client = (req, res) => { + res.status(404).json('404', { title: 'page not found 404' }); +}; + +exports.server = (err, req, res, next) => { + res.status(500).json('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..2a9fe351 100644 --- a/server/controllers/middlewares/errorHandle/index.js +++ b/server/controllers/middlewares/errorHandle/index.js @@ -0,0 +1,6 @@ +const {client, server } = require('./error'); + +module.exports = { + client, + server +} \ No newline at end of file From 5f4911f6cc1ea74125fc12aa5e1f6ba2d4d0ea84 Mon Sep 17 00:00:00 2001 From: mu7ammadabed Date: Mon, 30 Mar 2020 01:27:44 +0300 Subject: [PATCH 02/19] eslint fixes --- server/controllers/middlewares/errorHandle/error.js | 1 + server/controllers/middlewares/errorHandle/index.js | 8 ++++---- 2 files changed, 5 insertions(+), 4 deletions(-) diff --git a/server/controllers/middlewares/errorHandle/error.js b/server/controllers/middlewares/errorHandle/error.js index 21cfdb33..8d73a750 100644 --- a/server/controllers/middlewares/errorHandle/error.js +++ b/server/controllers/middlewares/errorHandle/error.js @@ -1,3 +1,4 @@ +/* eslint-disable no-unused-vars */ exports.client = (req, res) => { res.status(404).json('404', { title: 'page not found 404' }); }; diff --git a/server/controllers/middlewares/errorHandle/index.js b/server/controllers/middlewares/errorHandle/index.js index 2a9fe351..9c3cf442 100644 --- a/server/controllers/middlewares/errorHandle/index.js +++ b/server/controllers/middlewares/errorHandle/index.js @@ -1,6 +1,6 @@ -const {client, server } = require('./error'); +const { client, server } = require('./error'); module.exports = { - client, - server -} \ No newline at end of file + client, + server, +}; From e5a6e7a751578089680be64c1adfe71926f4bc5b Mon Sep 17 00:00:00 2001 From: mu7ammadabed Date: Mon, 30 Mar 2020 19:16:39 +0300 Subject: [PATCH 03/19] Change error name into more descriptive variable name --- server/controllers/index.js | 6 +++--- server/controllers/middlewares/errorHandle/error.js | 10 ++++++---- server/controllers/middlewares/errorHandle/index.js | 6 +++--- 3 files changed, 12 insertions(+), 10 deletions(-) diff --git a/server/controllers/index.js b/server/controllers/index.js index a46c3ead..5fb5b86a 100644 --- a/server/controllers/index.js +++ b/server/controllers/index.js @@ -1,8 +1,8 @@ const router = require('express').Router(); -const { client, server } = require('./middlewares/errorHandle'); +const { clientError, serverError } = require('./middlewares/errorHandle'); -router.all('*', client); -router.use(server); +router.use(clientError); +router.use(serverError); module.exports = router; diff --git a/server/controllers/middlewares/errorHandle/error.js b/server/controllers/middlewares/errorHandle/error.js index 8d73a750..873015ec 100644 --- a/server/controllers/middlewares/errorHandle/error.js +++ b/server/controllers/middlewares/errorHandle/error.js @@ -1,8 +1,10 @@ /* eslint-disable no-unused-vars */ -exports.client = (req, res) => { - res.status(404).json('404', { title: 'page not found 404' }); +exports.clientError = (req, res) => { + res.status(404).json({ StatusCode: '404', title: 'page not found 404' }); }; -exports.server = (err, req, res, next) => { - res.status(500).json('500', { title: 'internal server error 500', err }); +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 9c3cf442..8f3e38f3 100644 --- a/server/controllers/middlewares/errorHandle/index.js +++ b/server/controllers/middlewares/errorHandle/index.js @@ -1,6 +1,6 @@ -const { client, server } = require('./error'); +const { clientError, serverError } = require('./error'); module.exports = { - client, - server, + clientError, + serverError, }; From c613d5b35cd639169d987985138e4ea30a3b38f2 Mon Sep 17 00:00:00 2001 From: MohammedAlghazali Date: Mon, 30 Mar 2020 20:37:37 +0300 Subject: [PATCH 04/19] check if cohort id exist or not relates #29 --- .../routes/admin/cohort/deleteCohort.js | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) diff --git a/server/controllers/routes/admin/cohort/deleteCohort.js b/server/controllers/routes/admin/cohort/deleteCohort.js index c65377fb..8d904bb2 100644 --- a/server/controllers/routes/admin/cohort/deleteCohort.js +++ b/server/controllers/routes/admin/cohort/deleteCohort.js @@ -2,11 +2,17 @@ const deleteCohort = require('../../../../database/queries/cohort/deleteCohort') const cohortDelete = async (req, res, next) => { try { - await deleteCohort(req.params.cohortId); - res.json({ - StatusCode: 200, - data: { message: 'Cohort deleted successfully' }, - }); + const check = await deleteCohort(req.params.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); + } } catch (err) { next(err); } From 01cc88be9b15adeea53b4a86b00bd3a9d545c491 Mon Sep 17 00:00:00 2001 From: MohammedAlghazali Date: Mon, 30 Mar 2020 20:41:56 +0300 Subject: [PATCH 05/19] explanation comment for route.all using relates #29 --- server/controllers/routes/admin/index.js | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/server/controllers/routes/admin/index.js b/server/controllers/routes/admin/index.js index 097affbd..ca4ceaa7 100644 --- a/server/controllers/routes/admin/index.js +++ b/server/controllers/routes/admin/index.js @@ -4,8 +4,7 @@ const { cohortDelete } = require('./cohort'); router .route('/cohorts/:cohortId') .all((req, res, next) => { - // runs for all HTTP verbs first - // think of it as route specific middleware! + // ToDo: make middleware to check authentication next(); }) .get((req, res, next) => { From df31b0aac51d2816cc825ef666a0c969dd29d2a8 Mon Sep 17 00:00:00 2001 From: MohammedAlghazali Date: Mon, 30 Mar 2020 20:54:46 +0300 Subject: [PATCH 06/19] Modify some default export and link the file with index in database/query relates #29 --- server/controllers/routes/admin/cohort/deleteCohort.js | 6 ++---- server/controllers/routes/admin/cohort/index.js | 2 +- server/database/queries/cohort/index.js | 5 +++++ server/database/queries/index.js | 5 +++++ 4 files changed, 13 insertions(+), 5 deletions(-) diff --git a/server/controllers/routes/admin/cohort/deleteCohort.js b/server/controllers/routes/admin/cohort/deleteCohort.js index 8d904bb2..a70452a2 100644 --- a/server/controllers/routes/admin/cohort/deleteCohort.js +++ b/server/controllers/routes/admin/cohort/deleteCohort.js @@ -1,4 +1,4 @@ -const deleteCohort = require('../../../../database/queries/cohort/deleteCohort'); +const { deleteCohort } = require('../../../../database/queries'); const cohortDelete = async (req, res, next) => { try { @@ -18,6 +18,4 @@ const cohortDelete = async (req, res, next) => { } }; -module.exports = { - cohortDelete, -}; +module.exports = cohortDelete; diff --git a/server/controllers/routes/admin/cohort/index.js b/server/controllers/routes/admin/cohort/index.js index 5e658610..036fb583 100644 --- a/server/controllers/routes/admin/cohort/index.js +++ b/server/controllers/routes/admin/cohort/index.js @@ -1,4 +1,4 @@ -const { cohortDelete } = require('./deleteCohort'); +const cohortDelete = require('./deleteCohort'); module.exports = { cohortDelete, diff --git a/server/database/queries/cohort/index.js b/server/database/queries/cohort/index.js index e69de29b..f3feddab 100644 --- a/server/database/queries/cohort/index.js +++ b/server/database/queries/cohort/index.js @@ -0,0 +1,5 @@ +const deleteCohort = require('./deleteCohort'); + +module.exports = { + deleteCohort, +}; diff --git a/server/database/queries/index.js b/server/database/queries/index.js index e69de29b..15658d72 100644 --- a/server/database/queries/index.js +++ b/server/database/queries/index.js @@ -0,0 +1,5 @@ +const { deleteCohort } = require('./cohort'); + +module.exports = { + deleteCohort, +}; From d2afa98a9a6a604943f9ad12c2cb07bd0e29494b Mon Sep 17 00:00:00 2001 From: MohammedAlghazali Date: Mon, 30 Mar 2020 21:29:54 +0300 Subject: [PATCH 07/19] add query test fot /cohorts/:cohortId route to be sure the delete done relates #29 --- test/index.test.js | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/test/index.test.js b/test/index.test.js index 1bf35312..89f1040b 100644 --- a/test/index.test.js +++ b/test/index.test.js @@ -13,14 +13,18 @@ afterAll(() => { }); describe('Admin, (/cohorts/:cohortId)', () => { - test('Route /cohorts/1 status 200, json header, data.message = 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((err, res) => { + .end(async (err, res) => { const { data } = res.body; if (err) return done(err); + const result = await connection.query( + 'SELECT * from cohort WHERE id = 1', + ); + expect(result.rows).toHaveLength(0); expect(data.message).toBe('Cohort deleted successfully'); done(); }); From acf5b2bcbe5c9382c5759fb49f0ef0a7f2d1f3ee Mon Sep 17 00:00:00 2001 From: MohammedAlghazali Date: Tue, 31 Mar 2020 14:54:34 +0300 Subject: [PATCH 08/19] change query function name and route function relates #29 --- server/controllers/routes/admin/cohort/deleteCohort.js | 8 ++++---- server/controllers/routes/admin/cohort/index.js | 4 ++-- server/controllers/routes/admin/index.js | 4 ++-- server/database/queries/cohort/deleteCohort.js | 4 ++-- server/database/queries/cohort/index.js | 4 ++-- server/database/queries/index.js | 4 ++-- 6 files changed, 14 insertions(+), 14 deletions(-) diff --git a/server/controllers/routes/admin/cohort/deleteCohort.js b/server/controllers/routes/admin/cohort/deleteCohort.js index a70452a2..78a23b91 100644 --- a/server/controllers/routes/admin/cohort/deleteCohort.js +++ b/server/controllers/routes/admin/cohort/deleteCohort.js @@ -1,8 +1,8 @@ -const { deleteCohort } = require('../../../../database/queries'); +const { deleteCohortQuery } = require('../../../../database/queries'); -const cohortDelete = async (req, res, next) => { +const deleteCohort = async (req, res, next) => { try { - const check = await deleteCohort(req.params.cohortId); + const check = await deleteCohortQuery(req.params.cohortId); if (check.rowCount !== 0) { res.json({ StatusCode: 200, @@ -18,4 +18,4 @@ const cohortDelete = async (req, res, next) => { } }; -module.exports = cohortDelete; +module.exports = deleteCohort; diff --git a/server/controllers/routes/admin/cohort/index.js b/server/controllers/routes/admin/cohort/index.js index 036fb583..f3feddab 100644 --- a/server/controllers/routes/admin/cohort/index.js +++ b/server/controllers/routes/admin/cohort/index.js @@ -1,5 +1,5 @@ -const cohortDelete = require('./deleteCohort'); +const deleteCohort = require('./deleteCohort'); module.exports = { - cohortDelete, + deleteCohort, }; diff --git a/server/controllers/routes/admin/index.js b/server/controllers/routes/admin/index.js index ca4ceaa7..465de719 100644 --- a/server/controllers/routes/admin/index.js +++ b/server/controllers/routes/admin/index.js @@ -1,5 +1,5 @@ const router = require('express').Router(); -const { cohortDelete } = require('./cohort'); +const { deleteCohort } = require('./cohort'); router .route('/cohorts/:cohortId') @@ -16,6 +16,6 @@ router .post((req, res, next) => { next(new Error('not implemented')); }) - .delete(cohortDelete); + .delete(deleteCohort); module.exports = router; diff --git a/server/database/queries/cohort/deleteCohort.js b/server/database/queries/cohort/deleteCohort.js index f9da6f74..a349750b 100644 --- a/server/database/queries/cohort/deleteCohort.js +++ b/server/database/queries/cohort/deleteCohort.js @@ -1,6 +1,6 @@ const connection = require('../../config/connection'); -const deleteCohort = (cohortId) => +const deleteCohortQuery = (cohortId) => connection.query('DELETE FROM cohort WHERE id = $1', [cohortId]); -module.exports = deleteCohort; +module.exports = deleteCohortQuery; diff --git a/server/database/queries/cohort/index.js b/server/database/queries/cohort/index.js index f3feddab..9044c80d 100644 --- a/server/database/queries/cohort/index.js +++ b/server/database/queries/cohort/index.js @@ -1,5 +1,5 @@ -const deleteCohort = require('./deleteCohort'); +const deleteCohortQuery = require('./deleteCohort'); module.exports = { - deleteCohort, + deleteCohortQuery, }; diff --git a/server/database/queries/index.js b/server/database/queries/index.js index 15658d72..c0d36a24 100644 --- a/server/database/queries/index.js +++ b/server/database/queries/index.js @@ -1,5 +1,5 @@ -const { deleteCohort } = require('./cohort'); +const { deleteCohortQuery } = require('./cohort'); module.exports = { - deleteCohort, + deleteCohortQuery, }; From a4b2af5880cbe57080e335c12e173bd98e93fbbf Mon Sep 17 00:00:00 2001 From: MohammedAlghazali Date: Tue, 31 Mar 2020 20:47:56 +0300 Subject: [PATCH 09/19] handle error and make some destructure reelates #29 --- .../routes/admin/cohort/deleteCohort.js | 10 ++++++---- test/index.test.js | 16 ++++++---------- 2 files changed, 12 insertions(+), 14 deletions(-) diff --git a/server/controllers/routes/admin/cohort/deleteCohort.js b/server/controllers/routes/admin/cohort/deleteCohort.js index 78a23b91..bbf52d0b 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(400).json({ + StatusCode: 400, + data: { message: 'Cohort does not exist' }, + }); } } catch (err) { next(err); diff --git a/test/index.test.js b/test/index.test.js index 89f1040b..eb16632b 100644 --- a/test/index.test.js +++ b/test/index.test.js @@ -4,13 +4,9 @@ const dbBuild = require('../server/database/config/build'); const app = require('../server/app'); -beforeAll(() => { - return dbBuild(); -}); +beforeAll(() => dbBuild()); -afterAll(() => { - return connection.end(); -}); +afterAll(() => connection.end()); describe('Admin, (/cohorts/:cohortId)', () => { test('Route /cohorts/1 status 200, data.message = Cohort deleted successfully ', (done) => { @@ -19,13 +15,13 @@ describe('Admin, (/cohorts/:cohortId)', () => { .expect(200) .expect('Content-Type', /json/) .end(async (err, res) => { - const { data } = res.body; + const { message } = res.body.data; if (err) return done(err); - const result = await connection.query( + const { rows } = await connection.query( 'SELECT * from cohort WHERE id = 1', ); - expect(result.rows).toHaveLength(0); - expect(data.message).toBe('Cohort deleted successfully'); + expect(rows).toHaveLength(0); + expect(message).toBe('Cohort deleted successfully'); done(); }); }); From 03eb509402a9c876ffb9bf25caf1ec3784558b6a Mon Sep 17 00:00:00 2001 From: MohammedAlghazali Date: Tue, 31 Mar 2020 21:06:13 +0300 Subject: [PATCH 10/19] handle error and change some validation rules relates #24 --- server/controllers/routes/admin/project/addProject.js | 11 ++++++++--- server/validation/index.js | 4 ++-- .../{validateAddProject.js => projectSchema.js} | 6 +++--- test/index.test.js | 2 +- 4 files changed, 14 insertions(+), 9 deletions(-) rename server/validation/{validateAddProject.js => projectSchema.js} (70%) diff --git a/server/controllers/routes/admin/project/addProject.js b/server/controllers/routes/admin/project/addProject.js index 733e9c25..6e1273c5 100644 --- a/server/controllers/routes/admin/project/addProject.js +++ b/server/controllers/routes/admin/project/addProject.js @@ -1,13 +1,18 @@ 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).catch((err) => + res.json({ + StatusCode: 400, + data: { message: err.errors }, + }), + ); await addProjectQuery(req.body); res.json({ StatusCode: 200, - data: { message: 'Cohort Added successfully' }, + data: { message: 'Project Added successfully' }, }); } catch (err) { next(err); diff --git a/server/validation/index.js b/server/validation/index.js index bfe6cb81..672f7cac 100644 --- a/server/validation/index.js +++ b/server/validation/index.js @@ -1,5 +1,5 @@ -const addProjectSchema = require('./validateAddProject'); +const projectSchema = require('./projectSchema'); module.exports = { - addProjectSchema, + projectSchema, }; diff --git a/server/validation/validateAddProject.js b/server/validation/projectSchema.js similarity index 70% rename from server/validation/validateAddProject.js rename to server/validation/projectSchema.js index cd7ca591..a837873a 100644 --- a/server/validation/validateAddProject.js +++ b/server/validation/projectSchema.js @@ -1,13 +1,13 @@ 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(), }); -module.exports = addProjectSchema; +module.exports = projectSchema; diff --git a/test/index.test.js b/test/index.test.js index ea0b89e1..923e8210 100644 --- a/test/index.test.js +++ b/test/index.test.js @@ -36,7 +36,7 @@ describe('Admin, Project', () => { 'SELECT * from project WHERE id = 3', ); expect(rows[0].name).toBe('Mohmmedzw851@'); - expect(message).toBe('Cohort Added successfully'); + expect(message).toBe('Project Added successfully'); done(); }); }); From d131a1efe37948d1949c9c2c0cd854a79fe631db Mon Sep 17 00:00:00 2001 From: MohammedAlghazali Date: Tue, 31 Mar 2020 21:10:25 +0300 Subject: [PATCH 11/19] modify status code for error relates #29 --- server/controllers/routes/admin/cohort/deleteCohort.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/server/controllers/routes/admin/cohort/deleteCohort.js b/server/controllers/routes/admin/cohort/deleteCohort.js index bbf52d0b..07709373 100644 --- a/server/controllers/routes/admin/cohort/deleteCohort.js +++ b/server/controllers/routes/admin/cohort/deleteCohort.js @@ -10,8 +10,8 @@ const deleteCohort = async (req, res, next) => { data: { message: 'Cohort deleted successfully' }, }); } else { - res.status(400).json({ - StatusCode: 400, + res.status(404).json({ + StatusCode: 404, data: { message: 'Cohort does not exist' }, }); } From 45e32b73e4a65909e60b563fe61e01eda89e2dfc Mon Sep 17 00:00:00 2001 From: MohammedAlghazali Date: Tue, 31 Mar 2020 22:28:24 +0300 Subject: [PATCH 12/19] modify handle error position relates #24 --- server/controllers/routes/admin/project/addProject.js | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/server/controllers/routes/admin/project/addProject.js b/server/controllers/routes/admin/project/addProject.js index 6e1273c5..f80d8370 100644 --- a/server/controllers/routes/admin/project/addProject.js +++ b/server/controllers/routes/admin/project/addProject.js @@ -15,7 +15,14 @@ const addProject = async (req, res, next) => { data: { message: 'Project Added successfully' }, }); } catch (err) { - next(err); + if (err.errors) { + res.json({ + StatusCode: 400, + data: { message: err.errors }, + }); + } else { + next(err); + } } }; From 54aeed9b476d0a230aed474615b2166bc6323b79 Mon Sep 17 00:00:00 2001 From: MohammedAlghazali Date: Tue, 31 Mar 2020 22:31:36 +0300 Subject: [PATCH 13/19] remove old handle error relates #24 --- server/controllers/routes/admin/project/addProject.js | 7 +------ 1 file changed, 1 insertion(+), 6 deletions(-) diff --git a/server/controllers/routes/admin/project/addProject.js b/server/controllers/routes/admin/project/addProject.js index f80d8370..a50ffdb7 100644 --- a/server/controllers/routes/admin/project/addProject.js +++ b/server/controllers/routes/admin/project/addProject.js @@ -3,12 +3,7 @@ const { projectSchema } = require('../../../../validation'); const addProject = async (req, res, next) => { try { - await projectSchema.validate(req.body).catch((err) => - res.json({ - StatusCode: 400, - data: { message: err.errors }, - }), - ); + await projectSchema.validate(req.body); await addProjectQuery(req.body); res.json({ StatusCode: 200, From eb7959fa9cb12f86f70e728bf20d1c47765a25e7 Mon Sep 17 00:00:00 2001 From: MohammedAlghazali Date: Tue, 31 Mar 2020 22:41:35 +0300 Subject: [PATCH 14/19] display all error from validation relates #24 --- server/controllers/routes/admin/project/addProject.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/server/controllers/routes/admin/project/addProject.js b/server/controllers/routes/admin/project/addProject.js index a50ffdb7..d2d74d07 100644 --- a/server/controllers/routes/admin/project/addProject.js +++ b/server/controllers/routes/admin/project/addProject.js @@ -3,7 +3,7 @@ const { projectSchema } = require('../../../../validation'); const addProject = async (req, res, next) => { try { - await projectSchema.validate(req.body); + await projectSchema.validate(req.body, { abortEarly: false }); await addProjectQuery(req.body); res.json({ StatusCode: 200, From fc709b3eb4c5e71e1ce0ea8026bd505f9cb482c7 Mon Sep 17 00:00:00 2001 From: MohammedAlghazali Date: Tue, 31 Mar 2020 22:57:09 +0300 Subject: [PATCH 15/19] change status for response relates #24 --- server/controllers/routes/admin/project/addProject.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/server/controllers/routes/admin/project/addProject.js b/server/controllers/routes/admin/project/addProject.js index d2d74d07..e98159c8 100644 --- a/server/controllers/routes/admin/project/addProject.js +++ b/server/controllers/routes/admin/project/addProject.js @@ -6,7 +6,7 @@ const addProject = async (req, res, next) => { await projectSchema.validate(req.body, { abortEarly: false }); await addProjectQuery(req.body); res.json({ - StatusCode: 200, + StatusCode: 201, data: { message: 'Project Added successfully' }, }); } catch (err) { From e3e13474bdf0fb464bc7455c7bc3cb350bd40da2 Mon Sep 17 00:00:00 2001 From: MohammedAlghazali Date: Tue, 31 Mar 2020 23:00:43 +0300 Subject: [PATCH 16/19] change status for response and message relates #24 --- server/controllers/routes/admin/project/addProject.js | 2 +- test/index.test.js | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/server/controllers/routes/admin/project/addProject.js b/server/controllers/routes/admin/project/addProject.js index e98159c8..af436fc9 100644 --- a/server/controllers/routes/admin/project/addProject.js +++ b/server/controllers/routes/admin/project/addProject.js @@ -5,7 +5,7 @@ const addProject = async (req, res, next) => { try { await projectSchema.validate(req.body, { abortEarly: false }); await addProjectQuery(req.body); - res.json({ + res.status(201).json({ StatusCode: 201, data: { message: 'Project Added successfully' }, }); diff --git a/test/index.test.js b/test/index.test.js index 5497d189..7d6f449c 100644 --- a/test/index.test.js +++ b/test/index.test.js @@ -23,7 +23,7 @@ describe('Admin, Project', () => { return request(app) .post('/api/v1/projects') .send(reqData) - .expect(200) + .expect(201) .expect('Content-Type', /json/) .end(async (err, res) => { const { message } = res.body.data; From 8c059abf9bb2bc76beea4cb9c070361f0d8f16f2 Mon Sep 17 00:00:00 2001 From: MohammedAlghazali Date: Wed, 1 Apr 2020 10:46:15 +0300 Subject: [PATCH 17/19] change status code relates #24 --- server/controllers/routes/admin/project/addProject.js | 4 ++-- test/index.test.js | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/server/controllers/routes/admin/project/addProject.js b/server/controllers/routes/admin/project/addProject.js index af436fc9..3583d0d6 100644 --- a/server/controllers/routes/admin/project/addProject.js +++ b/server/controllers/routes/admin/project/addProject.js @@ -5,8 +5,8 @@ const addProject = async (req, res, next) => { try { await projectSchema.validate(req.body, { abortEarly: false }); await addProjectQuery(req.body); - res.status(201).json({ - StatusCode: 201, + res.status(200).json({ + StatusCode: 200, data: { message: 'Project Added successfully' }, }); } catch (err) { diff --git a/test/index.test.js b/test/index.test.js index 7d6f449c..5497d189 100644 --- a/test/index.test.js +++ b/test/index.test.js @@ -23,7 +23,7 @@ describe('Admin, Project', () => { return request(app) .post('/api/v1/projects') .send(reqData) - .expect(201) + .expect(200) .expect('Content-Type', /json/) .end(async (err, res) => { const { message } = res.body.data; From f7bcba44cbd71241a975272815c90ad5ffade3c9 Mon Sep 17 00:00:00 2001 From: MohammedAlghazali Date: Wed, 1 Apr 2020 11:16:51 +0300 Subject: [PATCH 18/19] reomve client error handle relates #24 --- server/controllers/index.js | 3 +-- server/controllers/middlewares/errorHandle/error.js | 3 --- server/controllers/middlewares/errorHandle/index.js | 3 +-- server/controllers/routes/admin/project/addProject.js | 2 +- test/index.test.js | 2 +- 5 files changed, 4 insertions(+), 9 deletions(-) diff --git a/server/controllers/index.js b/server/controllers/index.js index 81eb0c12..aeffa462 100644 --- a/server/controllers/index.js +++ b/server/controllers/index.js @@ -1,9 +1,8 @@ const router = require('express').Router(); const admin = require('./routes/admin'); -const { clientError, serverError } = require('./middlewares/errorHandle'); +const { serverError } = require('./middlewares/errorHandle'); -router.use(clientError); router.use(serverError); router.use(admin); diff --git a/server/controllers/middlewares/errorHandle/error.js b/server/controllers/middlewares/errorHandle/error.js index 873015ec..a041036f 100644 --- a/server/controllers/middlewares/errorHandle/error.js +++ b/server/controllers/middlewares/errorHandle/error.js @@ -1,7 +1,4 @@ /* eslint-disable no-unused-vars */ -exports.clientError = (req, res) => { - res.status(404).json({ StatusCode: '404', title: 'page not found 404' }); -}; exports.serverError = (err, req, res, next) => { res diff --git a/server/controllers/middlewares/errorHandle/index.js b/server/controllers/middlewares/errorHandle/index.js index 8f3e38f3..1891385e 100644 --- a/server/controllers/middlewares/errorHandle/index.js +++ b/server/controllers/middlewares/errorHandle/index.js @@ -1,6 +1,5 @@ -const { clientError, serverError } = require('./error'); +const { serverError } = require('./error'); module.exports = { - clientError, serverError, }; diff --git a/server/controllers/routes/admin/project/addProject.js b/server/controllers/routes/admin/project/addProject.js index 3583d0d6..90cdbdcd 100644 --- a/server/controllers/routes/admin/project/addProject.js +++ b/server/controllers/routes/admin/project/addProject.js @@ -5,7 +5,7 @@ const addProject = async (req, res, next) => { try { await projectSchema.validate(req.body, { abortEarly: false }); await addProjectQuery(req.body); - res.status(200).json({ + res.status(201).json({ StatusCode: 200, data: { message: 'Project Added successfully' }, }); diff --git a/test/index.test.js b/test/index.test.js index 5497d189..7d6f449c 100644 --- a/test/index.test.js +++ b/test/index.test.js @@ -23,7 +23,7 @@ describe('Admin, Project', () => { return request(app) .post('/api/v1/projects') .send(reqData) - .expect(200) + .expect(201) .expect('Content-Type', /json/) .end(async (err, res) => { const { message } = res.body.data; From a762f024f4bae674073d3870dc6e1221aa311055 Mon Sep 17 00:00:00 2001 From: MohammedAlghazali Date: Wed, 1 Apr 2020 11:21:27 +0300 Subject: [PATCH 19/19] add status 201 to message relates #24 --- server/controllers/routes/admin/project/addProject.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/server/controllers/routes/admin/project/addProject.js b/server/controllers/routes/admin/project/addProject.js index 90cdbdcd..af436fc9 100644 --- a/server/controllers/routes/admin/project/addProject.js +++ b/server/controllers/routes/admin/project/addProject.js @@ -6,7 +6,7 @@ const addProject = async (req, res, next) => { await projectSchema.validate(req.body, { abortEarly: false }); await addProjectQuery(req.body); res.status(201).json({ - StatusCode: 200, + StatusCode: 201, data: { message: 'Project Added successfully' }, }); } catch (err) {