Skip to content

Commit

Permalink
make saveJobDetails query, update handlebars
Browse files Browse the repository at this point in the history
Closes #93
  • Loading branch information
tdoran committed May 14, 2018
1 parent 882abf6 commit 3d31398
Show file tree
Hide file tree
Showing 12 changed files with 75 additions and 36 deletions.
11 changes: 10 additions & 1 deletion src/controllers/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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(
Expand Down
4 changes: 2 additions & 2 deletions src/controllers/middleware.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -22,4 +22,4 @@ exports.addUserStatus = (req) => {
};
}
return userInfo;
};
};
10 changes: 8 additions & 2 deletions src/controllers/profile.js
Original file line number Diff line number Diff line change
@@ -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 });
});
};
2 changes: 1 addition & 1 deletion src/model/database/db_build.sql
Original file line number Diff line number Diff line change
Expand Up @@ -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)
);

Expand Down
7 changes: 4 additions & 3 deletions src/model/database/db_build_test.sql
Original file line number Diff line number Diff line change
Expand Up @@ -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)
);

Expand Down Expand Up @@ -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)
Expand Down
22 changes: 22 additions & 0 deletions src/model/queries/saveJobDetails.js
Original file line number Diff line number Diff line change
@@ -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;
24 changes: 12 additions & 12 deletions src/test/app.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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);
Expand All @@ -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();
});
});
Expand Down
8 changes: 6 additions & 2 deletions src/views/helpers/jobPrefIsPublic.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
module.exports = (jobViewPref) => {
return jobViewPref === 'public';
}
if (jobViewPref === 'public') {
return 'eye icon';
}
return 'lock icon';
};

6 changes: 2 additions & 4 deletions src/views/helpers/jobStatusText.js
Original file line number Diff line number Diff line change
Expand Up @@ -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';
}
}
};
8 changes: 4 additions & 4 deletions src/views/partials/profile_jobDetails.hbs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<h1>Job Status</h1>
<form method="POST" action="/saveDetails" novalidate>
<form method="POST" action="/saveJobDetails" novalidate>
<span>* Required</span>
<label for="job_view_pref">Preferred
<input type="radio" id="job_pref_public" name="job_view_pref" value="public">
Expand All @@ -17,9 +17,9 @@
</label>
<label for="years_experience">Years experience
<select id="years_experience" name="years_experience">
<option value="{min_years_exp: 0, max_years_exp: 2}">Less than two years</option>
<option value="{min_years_exp: 2, max_years_exp: 5}">Two to five years</option>
<option value="{min_years_exp: 5, max_years_exp: 100}">More than five years</option>
<option value="{'min_years_exp': 0, 'max_years_exp': 2}">Less than two years</option>
<option value="{'min_years_exp': 2, 'max_years_exp': 5}">Two to five years</option>
<option value="{'min_years_exp': 5, 'max_years_exp': 100}">More than five years</option>
</select>
</label>
<label for="github_cv_url">GitHub CV URL
Expand Down
2 changes: 1 addition & 1 deletion src/views/partials/profile_myDetails.hbs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<h1>My Details</h1>
<form method="POST" action="/saveDetails" novalidate>
<form method="POST" action="/savePersonalDetails" novalidate>
<span>* Required</span>
<label for="full_name">Full Name *
<input id="full_name" name="full_name" type="text" {{#if user.full_name}} value="{{user.full_name}}" {{/if}}/>
Expand Down
7 changes: 3 additions & 4 deletions src/views/partials/profile_viewMode.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -51,10 +51,9 @@
<h1>
Job Search
</h1>
{{#if user.job_view_pref}} {{#if jobPrefIsPublic user.job_view_pref}}
<i class="icon icon-visible">eye icon</i>
{{/if}} {{else}}
<i class="icon icon-private">lock</i>{{/if}}
{{#if user.job_view_pref}}
<i class="icon icon-visible">{{jobPrefIsPublic user.job_view_pref}}</i>
{{/if}}
</header>
<button id="open-modal-job">Edit</button>
<h2>Status</h2>
Expand Down

0 comments on commit 3d31398

Please sign in to comment.