Skip to content

Commit

Permalink
refactor(users): write user check helper
Browse files Browse the repository at this point in the history
- Write a helper to check existence of a user
- Helper to return an existing user, having passed existent check
- Updates the test

[Finishes #166138182]
  • Loading branch information
chidimo committed May 20, 2019
1 parent 2b5ee7d commit 4f765a4
Show file tree
Hide file tree
Showing 6 changed files with 68 additions and 51 deletions.
6 changes: 3 additions & 3 deletions controllers/AuthController.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import Model from '../models/Model';
import
{
check_user_exists,
get_user_clause,
get_existing_user,
add_user_to_db,
check_password
} from './helpers/AuthController';
Expand All @@ -25,7 +25,7 @@ const AuthController = {
const [ { id }, ] = rows;
const clause = `WHERE id=${id}`;
const err_msg = `User with id ${id} does not exist.`;
const user = await get_user_clause(users_model, res, clause, err_msg);
const user = await get_existing_user(users_model, res, clause, err_msg);
return res.status(201).json({ data: { ...user, token: req.token } });
},

Expand All @@ -36,7 +36,7 @@ const AuthController = {
// check user exists
const match = await check_password(users_model, email, password, res);
if (match) {
const user = await get_user_clause(
const user = await get_existing_user(
users_model, res, clause, err_msg);
return res
.status(200).json({ data: { ...user, token: req.token } });
Expand Down
52 changes: 27 additions & 25 deletions controllers/UsersController.js
Original file line number Diff line number Diff line change
@@ -1,33 +1,36 @@
import Model from '../models/Model';
import { InternalServerError } from '../utils/errorHandlers';
import { get_user_clause } from './helpers/AuthController';
import {
aws_signed_url,
update_user_photo_url
} from './helpers/UsersController';
get_existing_user, check_user_exists
} from './helpers/AuthController';
import { aws_signed_url, } from './helpers/UsersController';

const users_model = new Model('users');

const UsersController = {
get_user: async (req, res) => {
const { id } = req.params;
const clause = `WHERE id=${id}`;
const err_msg = `User with id ${id} not found`;
const user = await get_user_clause(users_model, res, clause, err_msg);
if (user.id) {
const exists = await check_user_exists(users_model, clause, res);
if (exists) {
const user = await get_existing_user(users_model, res, clause);
return res.status(200).json({ data: user });
}
return res.status(404)
.json({ error: `User with id ${id} not found` });
},

verify_user: async (req, res) => {
const { id } = req.params;
const clause = `WHERE id=${id}`;
const err_msg = `User with id ${id} not found`;
await users_model.update('status=\'verified\'', clause);
const user = await get_user_clause(users_model, res, clause, err_msg);
if (user.id) {
const exists = await check_user_exists(users_model, clause, res);
if (exists) {
await users_model.update('status=\'verified\'', clause);
const user = await get_existing_user(users_model, res, clause);
return res.status(200).json({ data: user });
}
return res.status(404)
.json({ error: `User with id ${id} not found` });
},

get_users: async (req, res) => {
Expand All @@ -54,21 +57,19 @@ const UsersController = {
const { id } = req.params;
const { firstname, lastname, phone, home, office } = req.body;
const clause = `WHERE id=${id}`;
const err_msg = `User with id ${id} not found`;
try {
const exists = await check_user_exists(users_model, clause, res);
if (exists) {
await users_model.update(
`firstname='${firstname}', lastname='${lastname}',
phone='${phone}',
address='{"home": "${home}", "office": "${office}"}'`,
clause
);
const user = await get_user_clause(
users_model, res, clause, err_msg);
const user = await get_existing_user(users_model, res, clause);
return res.status(200).json({ data: user });
}
catch (e) {
return InternalServerError(res, e);
}
return res.status(404)
.json({ error: `User with id ${id} not found` });
},

get_aws_signed_url: (req, res) => {
Expand All @@ -81,15 +82,16 @@ const UsersController = {
update_photo_url: async (req, res) => {
const { id } = req.params;
const { photo_url } = req.body;
try {
await update_user_photo_url(users_model, id, photo_url, res);
const clause = `WHERE id=${id}`;
const err_msg = `User with id ${id} does not exist.`;
const user = await get_user_clause(
users_model, res, clause, err_msg);
const clause = `WHERE id=${id}`;
const exists = await check_user_exists(users_model, clause, res);
if (exists) {
await users_model.update(`photo='${photo_url}'`, clause);
const user = await get_existing_user(users_model, res, clause);
return res.status(200).json({ data: user });
}
catch (e) { return InternalServerError(res, e); }
return res.status(404)
.json({ error: `User with id ${id} not found` });

}
};

Expand Down
5 changes: 1 addition & 4 deletions controllers/helpers/AuthController.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,15 +38,12 @@ export const add_user_to_db = async (model_instance, req, res) => {
catch (e) { return InternalServerError(res, e);}
};

export const get_user_clause = async (model_instance, res, clause, err_msg) => {
export const get_existing_user = async (model_instance, res, clause) => {
try {
const { rows } = await model_instance.select(
'id, email, firstname, photo, lastname, phone, status, address',
clause
);
if (rows.length === 0) return res.status(404).json({
error: err_msg
});
return rows[0];
}
catch (e) { return InternalServerError(res, e); }
Expand Down
9 changes: 0 additions & 9 deletions controllers/helpers/UsersController.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import AWS from 'aws-sdk';
import settings from '../../settings';
import { dev_logger } from '../../utils/loggers';
import { InternalServerError } from '../../utils/errorHandlers';

const s3 = new AWS.S3({
accessKeyId: settings.AWS_settings.accessKeyId,
Expand All @@ -22,11 +21,3 @@ export const aws_signed_url = (id, filetype) => {
const url = s3.getSignedUrl('putObject', params);
return url;
};

export const update_user_photo_url = async (model_instance, id, url, res) => {
try {
await model_instance.update(
`photo='${url}'`, `WHERE id=${id}`);
}
catch (e) { return InternalServerError(res, e); }
};
36 changes: 26 additions & 10 deletions notes.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,16 +42,16 @@ CREATE TABLE IF NOT EXISTS users (
);

INSERT INTO users(email, password, firstname, lastname, phone, address)
VALUES ('a@b.com', 'password', 'first', 'men', '080121515', '{"home": "iyaba", "office": "ring road"}'),
('c@d.go', 'password', 'name', 'cat', '08151584151', '{"home": "london", "office": "NYC"}'),
('me@yahoo.com', 'password', 'tayo', 'dele', '08012345678', '{"home": "ijebu","office": "ijegun"}'),
('abc@gmail.com', 'password', 'what', 'is', '08012345678','{"home": "must","office": "not"}'),
('name@chat.co', 'password', 'niger', 'tornadoes', '08012345678', '{"home": "niger","office": "niger"}'),
('bcc@gmail.com', 'password', 'bcc', 'lions', '08012345678', '{"home": "gboko","office": "gboko"}'),
('bbc@bbc.uk', 'password', 'bbc', 'broadcast', '08012345678', '{"home": "london","office": "uk"}'),
('c@g.move', 'password', 'abc', 'def', '08012345678', '{"home": "shop","office": "home"}'),
('an@dela.ng', 'password', 'and', 'ela', '08012345678', '{"home": "ikorodu","office": "lagos"}'),
('soft@ware.eng', 'password', 'soft', 'eng', '08012345678', '{"home": "remote","office": "on-site"}');
VALUES ('a@b.com', '$2b$08$PyyTo.r0nPso8DHA0HfTs.lZSaGNA6J23V4eiw06rN8iWJin24f3O', 'first', 'men', '080121515', '{"home": "iyaba", "office": "ring road"}'),
('c@d.go', '$2b$08$PyyTo.r0nPso8DHA0HfTs.lZSaGNA6J23V4eiw06rN8iWJin24f3O', 'name', 'cat', '08151584151', '{"home": "london", "office": "NYC"}'),
('me@yahoo.com', '$2b$08$PyyTo.r0nPso8DHA0HfTs.lZSaGNA6J23V4eiw06rN8iWJin24f3O', 'tayo', 'dele', '08012345678', '{"home": "ijebu","office": "ijegun"}'),
('abc@gmail.com', '$2b$08$PyyTo.r0nPso8DHA0HfTs.lZSaGNA6J23V4eiw06rN8iWJin24f3O', 'what', 'is', '08012345678','{"home": "must","office": "not"}'),
('name@chat.co', '$2b$08$PyyTo.r0nPso8DHA0HfTs.lZSaGNA6J23V4eiw06rN8iWJin24f3O', 'niger', 'tornadoes', '08012345678', '{"home": "niger","office": "niger"}'),
('bcc@gmail.com', '$2b$08$PyyTo.r0nPso8DHA0HfTs.lZSaGNA6J23V4eiw06rN8iWJin24f3O', 'bcc', 'lions', '08012345678', '{"home": "gboko","office": "gboko"}'),
('bbc@bbc.uk', '$2b$08$PyyTo.r0nPso8DHA0HfTs.lZSaGNA6J23V4eiw06rN8iWJin24f3O', 'bbc', 'broadcast', '08012345678', '{"home": "london","office": "uk"}'),
('c@g.move', '$2b$08$PyyTo.r0nPso8DHA0HfTs.lZSaGNA6J23V4eiw06rN8iWJin24f3O', 'abc', 'def', '08012345678', '{"home": "shop","office": "home"}'),
('an@dela.ng', '$2b$08$PyyTo.r0nPso8DHA0HfTs.lZSaGNA6J23V4eiw06rN8iWJin24f3O', 'and', 'ela', '08012345678', '{"home": "ikorodu","office": "lagos"}'),
('soft@ware.eng', '$2b$08$PyyTo.r0nPso8DHA0HfTs.lZSaGNA6J23V4eiw06rN8iWJin24f3O', 'soft', 'eng', '08012345678', '{"home": "remote","office": "on-site"}');

CREATE TABLE IF NOT EXISTS loans (
id SERIAL PRIMARY KEY,
Expand Down Expand Up @@ -182,4 +182,20 @@ Warm tones
```
router.get('/repayments', LoansController.get_all_repayments
);
```

## Connecting to heroku

<https://stackoverflow.com/questions/20775490/how-to-create-or-manage-heroku-postgres-database-instance#23333798>

postgres://fgpyagsuzwxcnb:e08fb62b310448400dc8d4b61366191a58876dd4a81861b5125344852492c872@ec2-54-83-36-37.compute-1.amazonaws.com:5432/dfjtmn74eqgvn7
postgres://username:password@localhost/myrailsdb

```cmd
heroku pg:credentials:url DATABASE
psql -h ec2-54-83-36-37.compute-1.amazonaws.com -U fgpyagsuzwxcnb -d dfjtmn74eqgvn7
copy and past password, then enter
```
11 changes: 11 additions & 0 deletions test/users-spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,17 @@ describe('/users', () => {
done();
});
});
it('should not verify non-existent user', done => {
const id = 100;
server
.patch(`/users/${id}/verify`)
.expect(200)
.end((err, res) => {
res.status.should.equal(404);
res.body.error.should.equal(`User with id ${id} not found`);
done();
});
});
});

describe('GET /users?status=', () => {
Expand Down

0 comments on commit 4f765a4

Please sign in to comment.