diff --git a/package.json b/package.json index 14ce6ad2..b9e7e770 100644 --- a/package.json +++ b/package.json @@ -11,7 +11,7 @@ "dev": "NODE_ENV=development nodemon server/index.js", "project-setup": "npm install && cd client && npm install", "heroku-postbuild": "cd client && npm install && npm run build", - "test": "NODE_ENV=test jest --runInBand --coverage", + "test": "NODE_ENV=test jest --no-cache --runInBand --coverage", "kill": "fuser -k 5000/tcp", "lint": "eslint server/", "lint-fix": "eslint server/ --fix", diff --git a/server/controllers/routes/user/index.js b/server/controllers/routes/user/index.js index e4e39302..9b0b393c 100644 --- a/server/controllers/routes/user/index.js +++ b/server/controllers/routes/user/index.js @@ -1,12 +1,13 @@ const router = require('express').Router(); const { getAlumni } = require('./student'); +const { getProjectData, getProjectsData } = require('./project'); const { getCohortsData, getSpecificCohort } = require('./cohort'); -const { getProjectData } = require('./project'); router.get('/alumni', getAlumni); -router.get('/cohorts/:cohortId', getSpecificCohort); +router.get('/projects', getProjectsData); router.get('/cohorts', getCohortsData); +router.get('/cohorts/:cohortId', getSpecificCohort); router.get('/projects/:projectId', getProjectData); module.exports = router; diff --git a/server/controllers/routes/user/project/getProjectsByType.js b/server/controllers/routes/user/project/getProjectsByType.js new file mode 100644 index 00000000..7d074185 --- /dev/null +++ b/server/controllers/routes/user/project/getProjectsByType.js @@ -0,0 +1,33 @@ +const { getProjects } = require('../../../../database/queries'); + +const getProjectsData = async (req, res, next) => { + try { + const { type } = req.query; + if ( + type.toLowerCase() === 'internal' || + type.toLowerCase() === 'remotely' + ) { + const { rows } = await getProjects(type); + if (rows.length > 0) { + res.json({ + statusCode: 200, + data: rows, + }); + } else { + res.json({ + statusCode: 200, + message: `There is no ${type} projects`, + }); + } + } else { + res.status(404).json({ + statusCode: 404, + message: 'Please enter valid type', + }); + } + } catch (err) { + next(err); + } +}; + +module.exports = getProjectsData; diff --git a/server/controllers/routes/user/project/index.js b/server/controllers/routes/user/project/index.js index a4f9fc5c..2c76c565 100644 --- a/server/controllers/routes/user/project/index.js +++ b/server/controllers/routes/user/project/index.js @@ -1,5 +1,7 @@ +const getProjectsData = require('./getProjectsByType'); const getProjectData = require('./getProjectById'); module.exports = { + getProjectsData, getProjectData, }; diff --git a/server/database/queries/index.js b/server/database/queries/index.js index 57f7b1b8..ed715406 100644 --- a/server/database/queries/index.js +++ b/server/database/queries/index.js @@ -13,6 +13,7 @@ const { getProjectById, deleteProject, getCohortProjectsQuery, + getProjects, } = require('./project'); module.exports = { @@ -29,4 +30,5 @@ module.exports = { putStudent, getProjectById, deleteProject, + getProjects, }; diff --git a/server/database/queries/project/getProjectsByType.js b/server/database/queries/project/getProjectsByType.js new file mode 100644 index 00000000..000e95ec --- /dev/null +++ b/server/database/queries/project/getProjectsByType.js @@ -0,0 +1,9 @@ +const connection = require('../../config/connection'); + +const getProjects = (ProjectType) => + connection.query( + 'SELECT * FROM project WHERE project_type ilike $1 ORDER BY name', + [ProjectType], + ); + +module.exports = getProjects; diff --git a/server/database/queries/project/index.js b/server/database/queries/project/index.js index f87efe44..9d9ba6cd 100644 --- a/server/database/queries/project/index.js +++ b/server/database/queries/project/index.js @@ -1,3 +1,4 @@ +const getProjects = require('./getProjectsByType'); const addProjectQuery = require('./addProject'); const getCohortProjectsQuery = require('./getCohortProjects'); const editProjectQuery = require('./editProject'); @@ -5,6 +6,7 @@ const getProjectById = require('./getProjectById'); const deleteProject = require('./deleteProject'); module.exports = { + getProjects, addProjectQuery, getCohortProjectsQuery, editProjectQuery, diff --git a/test/index.test.js b/test/index.test.js index 75034c57..64805f62 100644 --- a/test/index.test.js +++ b/test/index.test.js @@ -209,6 +209,34 @@ describe('Delete specific student by ID', () => { }); }); +describe('Get project by type', () => { + test('Route /projects?type=internal status 200, json header, rows[0].name = Applicants System ', (done) => { + return request(app) + .get('/api/v1/projects?type=internal') + .expect(200) + .expect('Content-Type', /json/) + .end((err, res) => { + if (err) return done(err); + const { name } = res.body.data[0]; + expect(name).toBe('Applicants System'); + done(); + }); + }); + + test('Route /projects?type=g58g status 404, json header ', (done) => { + return request(app) + .get('/api/v1/projects?type=gg') + .expect(404) + .expect('Content-Type', /json/) + .end((err, res) => { + if (err) return done(err); + const { message } = res.body; + expect(message).toBe('Please enter valid type'); + done(); + }); + }); +}); + describe('Admin, Put project', () => { const validData = { name: 'Rehab',