diff --git a/src/controllers/index.js b/src/controllers/index.js index c45336c..2b1ad72 100644 --- a/src/controllers/index.js +++ b/src/controllers/index.js @@ -6,6 +6,7 @@ const home = require('./home'); const profile = require('./profile'); const error = require('./error'); const { ensureAuthenticated } = require('./middleware'); +const saveJobDetails = require('../model/queries/saveJobDetails'); // UNPROTECTED ROUTES // router.get('/', home.get); @@ -15,11 +16,19 @@ router.get('/notmember', (req, res) => { // PROTECTED ROUTES // router.get('/myprofile/:github_id', ensureAuthenticated, profile.get); -router.post('/saveDetails', ensureAuthenticated, (req, res) => { +router.post('/savePersonalDetails', ensureAuthenticated, (req, res) => { // post user data (req.body) to database console.log('form data: ', req.body); res.redirect('/myprofile/:github_id'); }); +router.post('/saveJobDetails', ensureAuthenticated, (req, res) => { + console.log('form data: ', req.body); + + return saveJobDetails(req.body, req.user.github_id).then(() => { + res.redirect('/myprofile/:github_id'); + }) + .catch(err => console.log('Error saving job details: ', err)); +}); // AUTHENTICATION ROUTES // router.get( diff --git a/src/controllers/middleware.js b/src/controllers/middleware.js index f059cc3..4ab537d 100644 --- a/src/controllers/middleware.js +++ b/src/controllers/middleware.js @@ -9,7 +9,7 @@ exports.ensureAuthenticated = (req, res, next) => { // Updates user object to include user session exports.addUserStatus = (req) => { // Deep copy of req.user - let userInfo = JSON.parse(JSON.stringify((req.user))); + const userInfo = JSON.parse(JSON.stringify((req.user))); if (req.session.registeredProfile) { userInfo.status = { login: true, @@ -22,4 +22,4 @@ exports.addUserStatus = (req) => { }; } return userInfo; -}; \ No newline at end of file +}; diff --git a/src/controllers/profile.js b/src/controllers/profile.js index 731c625..6b524f3 100644 --- a/src/controllers/profile.js +++ b/src/controllers/profile.js @@ -1,5 +1,11 @@ const { addUserStatus } = require('./middleware'); +const getMemberData = require('../model/queries/getMemberData'); + exports.get = (req, res) => { - let user = addUserStatus(req); - res.render('profile', { activePage: { profile: true }, user }); + // const user = addUserStatus(req); + // console.log('user: ', user); + getMemberData(req.user.github_id).then((user) => { + console.log(user); + res.render('profile', { activePage: { profile: true }, user }); + }); }; diff --git a/src/model/database/db_build.sql b/src/model/database/db_build.sql index 3ff7fd9..096a0cf 100644 --- a/src/model/database/db_build.sql +++ b/src/model/database/db_build.sql @@ -23,12 +23,12 @@ CREATE TABLE members ( linkedin_url VARCHAR(4000), twitter_handle VARCHAR(255), member_type VARCHAR(255), + job_view_pref VARCHAR(255), job_search_status VARCHAR(255), min_years_exp INTEGER, max_years_exp INTEGER, github_cv_url VARCHAR(4000), cv_url VARCHAR(4000), - job_view_pref VARCHAR(255), FOREIGN KEY (fac_code_id) REFERENCES fac_code(id) ); diff --git a/src/model/database/db_build_test.sql b/src/model/database/db_build_test.sql index 4f70c6c..2450812 100644 --- a/src/model/database/db_build_test.sql +++ b/src/model/database/db_build_test.sql @@ -23,12 +23,13 @@ CREATE TABLE members ( linkedin_url VARCHAR(4000), twitter_handle VARCHAR(255), member_type VARCHAR(255), + job_view_pref VARCHAR(255), job_search_status VARCHAR(255), + years_experience VARCHAR(255), min_years_exp INTEGER, max_years_exp INTEGER, github_cv_url VARCHAR(4000), cv_url VARCHAR(4000), - job_view_pref VARCHAR(255), FOREIGN KEY (fac_code_id) REFERENCES fac_code(id) ); @@ -56,8 +57,8 @@ VALUES INSERT INTO members (github_id, full_name, github_handle, github_avatar_url, fac_campus, fac_code_id, linkedin_url, twitter_handle, member_type, job_search_status, min_years_exp, max_years_exp, github_cv_url, cv_url, job_view_pref) VALUES - (1, 'Helen', 'helenzhou6', 'https://uk.linkedin.com/dbsmith', 'london', 1, 'https://uk.linkedin.com/', 'hel_zhou', 'admin', 'red', 0, 1, 'https://github.com/helenzhou6/CV', 'https://github.com/helenzhou6/CV', 'private'), - (2, 'Deborah', 'dsmith', 'https://uk.linkedin.com/dbsmith', 'gaza', 2, 'https://uk.linkedin.com/dbsmith', 'dbsmith', 'member', 'yellow', 2, 5, NULL, NULL, 'public'); + (1, 'Helen', 'helenzhou6', 'https://uk.linkedin.com/dbsmith', 'london', 1, 'https://uk.linkedin.com/', 'hel_zhou', 'admin', 'private', 'red', 0, 1, 'https://github.com/helenzhou6/CV', 'https://github.com/helenzhou6/CV'), + (2, 'Deborah', 'dsmith', 'https://uk.linkedin.com/dbsmith', 'gaza', 2, 'https://uk.linkedin.com/dbsmith', 'dbsmith', 'member', 'public', 'yellow', 2, 5, NULL, NULL); INSERT INTO member_tech_stack (member_id, stack_id, order_num) diff --git a/src/model/queries/saveJobDetails.js b/src/model/queries/saveJobDetails.js new file mode 100644 index 0000000..f6638b3 --- /dev/null +++ b/src/model/queries/saveJobDetails.js @@ -0,0 +1,22 @@ +const db = require('../database/db_connection'); + +const saveJobDetails = (formData, githubId) => { + const parsedYears = JSON.parse(formData.years_experience.replace(/'/g, '"')); + + const objCopy = JSON.parse(JSON.stringify(formData)); + objCopy.min_years_exp = parsedYears.min_years_exp; + objCopy.max_years_exp = parsedYears.max_years_exp; + delete objCopy.years_experience; + const values = Object.values(objCopy); + values.push(githubId); + + return db.query( + 'UPDATE members SET job_view_pref = $1, job_search_status = $2, github_cv_url = $3, cv_url = $4, min_years_exp = $5, max_years_exp = $6 WHERE github_id = $7', + values, + ).then(() => console.log(`${githubId} member job info posted to DB`)) + .catch((error) => { + throw new Error(error.message); + }); +}; + +module.exports = saveJobDetails; diff --git a/src/test/app.test.js b/src/test/app.test.js index 7c49b98..5c2143e 100644 --- a/src/test/app.test.js +++ b/src/test/app.test.js @@ -9,15 +9,15 @@ test('APP.JS & CONTROLLER TESTS', (t) => { test(`SERVER: Test if Express app is running on http://${process.env.HOST}:${ process.env.PORT - } or http://localhost:3000/`, (t) => { - request(app) - .get('/') - .end((err, res) => { - t.ok(res, `response received with status code: ${res.status}`); - t.error(err, 'no server error'); - t.end(); - }); - }); +} or http://localhost:3000/`, (t) => { + request(app) + .get('/') + .end((err, res) => { + t.ok(res, `response received with status code: ${res.status}`); + t.error(err, 'no server error'); + t.end(); + }); +}); test('SERVER: Test if home route gets status code 200 and returns html content', (t) => { request(app) @@ -79,9 +79,9 @@ test('Test for /myprofile/1 - unauthorised', (t) => { }); }); -test('Test for /saveDetails (post request) - unauthorised', (t) => { +test('Test for /savePersonalDetails (post request) - unauthorised', (t) => { request(app) - .post('/saveDetails') + .post('/savePersonalDetails') .expect(302) .end((err, res) => { if (err) console.log('ERROR', err.message); @@ -99,7 +99,7 @@ test('ERRORS: Test if server returns 404 on invalid route', (t) => { .expect(404) .end((err, res) => { t.equal(res.statusCode, 404, 'should return 404'); - t.error(err, `no server error`); + t.error(err, 'no server error'); t.end(); }); }); diff --git a/src/views/helpers/jobPrefIsPublic.js b/src/views/helpers/jobPrefIsPublic.js index 1724115..4745040 100644 --- a/src/views/helpers/jobPrefIsPublic.js +++ b/src/views/helpers/jobPrefIsPublic.js @@ -1,3 +1,7 @@ module.exports = (jobViewPref) => { - return jobViewPref === 'public'; -} \ No newline at end of file + if (jobViewPref === 'public') { + return 'eye icon'; + } + return 'lock icon'; +}; + diff --git a/src/views/helpers/jobStatusText.js b/src/views/helpers/jobStatusText.js index 0492f27..f23ece2 100644 --- a/src/views/helpers/jobStatusText.js +++ b/src/views/helpers/jobStatusText.js @@ -2,11 +2,9 @@ module.exports = (jobStatus) => { switch (jobStatus) { case 'green': return 'Currently Looking'; - break; - case 'orange': + case 'yellow': return 'Open to opportunities'; - break; default: return 'Not Looking'; } -} +}; diff --git a/src/views/partials/profile_jobDetails.hbs b/src/views/partials/profile_jobDetails.hbs index 694f9aa..0937f2c 100644 --- a/src/views/partials/profile_jobDetails.hbs +++ b/src/views/partials/profile_jobDetails.hbs @@ -1,5 +1,5 @@