Skip to content

Commit

Permalink
Merge d0e7cd6 into eaf83fd
Browse files Browse the repository at this point in the history
  • Loading branch information
chidimo committed May 15, 2019
2 parents eaf83fd + d0e7cd6 commit 55bb462
Show file tree
Hide file tree
Showing 37 changed files with 990 additions and 1,652 deletions.
1 change: 1 addition & 0 deletions .babelrc
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
{
"presets": ["@babel/preset-env"],
"plugins": ["@babel/transform-runtime"]
}
43 changes: 43 additions & 0 deletions .codeclimate.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
version: "2" # required to adjust maintainability checks

plugins:
duplication:
enabled: true
eslint:
enabled: true
checks:
argument-count:
config:
threshold: 4
complex-logic:
config:
threshold: 4
file-lines:
config:
threshold: 250
method-complexity:
config:
threshold: 5
method-count:
config:
threshold: 20
method-lines:
config:
threshold: 25
nested-control-flow:
config:
threshold: 4
return-statements:
config:
threshold: 4
similar-code:
config:
threshold: # language-specific defaults. an override will affect all languages.
identical-code:
config:
threshold: # language-specific defaults. an override will affect all languages.
exclude_patterns:
- node_modules/
- test/
- public/
- dist/
24 changes: 23 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
@@ -1,8 +1,30 @@
language: node_js
env:
global:
- CC_TEST_REPORTER_ID=03990f10d1bc434a08af1af70b30627f5333955eb21bf21c3b45eade086b92b5
- NODE_ENV=test
- PGPASSWORD=
- PGPORT=5433
- PGUSER=postgres
- PGHOST=localhost
services:
- postgresql
addons:
postgresql: "10"
apt:
packages:
- postgresql-10
- postgresql-client-10
before_script:
- psql -c 'create database testdb;' -U postgres
before_install:
- sudo sed -i -e '/local.*peer/s/postgres/all/' -e 's/peer\|md5/trust/g' /etc/postgresql/*/main/pg_hba.conf
- sudo service postgresql restart
- sleep 1
matrix:
include:
- node_js: '10'
- node_js: '12'
# - node_js: '12'
cache:
directories: node_modules
deploy:
Expand Down
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

[![Build Status](https://travis-ci.com/chidimo/Quick-Credit.svg?branch=develop)](https://travis-ci.com/chidimo/Quick-Credit)
[![Coverage Status](https://coveralls.io/repos/github/chidimo/Quick-Credit/badge.svg?branch=develop)](https://coveralls.io/github/chidimo/Quick-Credit?branch=develop)
[![Maintainability](https://api.codeclimate.com/v1/badges/38d7483bb05e2777f391/maintainability)](https://codeclimate.com/github/chidimo/Quick-Credit/maintainability)

## Description

Expand All @@ -21,15 +22,15 @@ income earners.
## Installation

1. Install yarn in your system. Check out the yarn installation [docs](https://yarnpkg.com/lang/en/docs/install)
1. Clone the repo using the github link <https://github.com/chidimo/Quick-Credit.git>
1. Clone the repo using the GitHub link <https://github.com/chidimo/Quick-Credit.git>
1. Open a `cmd` shell and `cd` into the root of the project.
1. Install project dependencies with `yarn install`.
1. Optionally install the development dependencies with `yarn install --dev`
1. After installation of all dependencies, start the server with `yarn start` or `yarn devstart`.

## Testing

1. Clone the repo using the github link <https://github.com/chidimo/Quick-Credit.git>
1. Clone the repo using the GitHub link <https://github.com/chidimo/Quick-Credit.git>
1. Open a `cmd` shell and `cd` into the root of the project.
1. Run command `yarn test`
1. Alternative you can start the server with `yarn devstart` and use postman to make API calls to the endpoints defined above. You man use this [postman collection](https://www.getpostman.com/collections/ef92586429ff3a723ee2)
Expand Down
5 changes: 2 additions & 3 deletions app.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,19 @@ import path from 'path';
import cookieParser from 'cookie-parser';
import logger from 'morgan';
import dotenv from 'dotenv';
import validator from 'express-validator';

import indexRouter from './routes/index';

dotenv.config();
const app = express();

// view engine setup
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'ejs');
app.use(logger('dev'));
app.use(express.json());
app.use(express.urlencoded({ extended: true }));
app.use(cookieParser());
app.use(express.static(path.join(__dirname, 'public')));
app.use(validator());

app.use('/', indexRouter);

Expand Down
12 changes: 0 additions & 12 deletions controllers/AppController.js

This file was deleted.

119 changes: 84 additions & 35 deletions controllers/AuthController.js
Original file line number Diff line number Diff line change
@@ -1,46 +1,95 @@
import users from '../utils/sample.users';
import bcrypt from 'bcrypt';

import Model from '../models/Model';
import { InternalServerError, CriticalError } from '../utils/errorHandlers';
import { dev_logger } from '../utils/loggers';

// import { dev_logger, test_logger } from '../utils/loggers';

const users_model = new Model('users');

const AuthController = {
signup: (req, res) => {

if (req.method === 'GET') {
res.render('authentication', { title: 'Sign Up' });
}
else {
const { email, password } = req.body;

res.status(200).json({
status: 201,
data: {
id: 2,
email,
password,
first_name: '',
last_name: '',
address: {
home: '',
office: '',
},
status: 'unverified',
isAdmin: false,
token: req.token,
const { email, password } = req.body;
const hashedPassword = bcrypt.hashSync(password, 8);
dev_logger(`token: ${req.token}`);

// check if user exists
const check_user_existence = async (email) => {
try {
const { rows } = await users_model.select(
'id, email', `email='${email}'`);
const [ user, ] = rows;
if (user) return user;
}
catch (e) { return InternalServerError(req, res, e);}
};

// add user to db
const create_user = async (email, password) => {
try {
await users_model.insert('(email, password)',
`'${email}', '${password}'`);
}
catch (e) { return InternalServerError(req, res, e);}
};

const get_new_user = async (email) => {
try {
const { rows } = await users_model.select(
`id, email, password, firstname,
lastname, phone, status, address`,
`email='${email}'`);

if (rows.length === 0) {
// user was not found.
// This is highly unlikely but should be handled
return CriticalError(
res, 'A supposedly created user is missing');
}
});
}
return res
.status(201)
.json({ data: { ...rows[0], token: req.token } });
}
catch (e) { return InternalServerError(req, res, e);}
};

(async () => {
const user = await check_user_existence(email);
dev_logger(`users******** ${user}`);
if (user) {
return res
.status(404)
.json({ error: `User with email ${email} already exists` });
}
await create_user(email, hashedPassword);
return get_new_user(email);
})();
},

signin: (req, res) => {
if (req.method === 'GET') {
res.render( 'authentication', { title: 'Sign In' });
}
else {
const { email } = req.body;
const user = users.find(user => user.email === email);
const data = { ... user, token: req.token };
res.status(200).json({ data });
}
const { email } = req.body;
(async () => {
try {
const { rows } = await users_model.select(
`id, email, password, firstname,
lastname, phone, status, address`,
`email='${email}'`
);
const [ user, ] = rows;
if (!user) {
return res
.status(404)
.json({ error: `User with email ${email} not found` });
}
// token verification takes care of identifying the user
return res
.status(200)
.json({ data: { ...rows[0], token: req.token } });
}
catch (e) { return InternalServerError(req, res, e); }
})();
},
};

export default AuthController;

Loading

0 comments on commit 55bb462

Please sign in to comment.