Skip to content

Commit

Permalink
Merge pull request #25 from chidimo/ch-update-to-es6-165849435
Browse files Browse the repository at this point in the history
#165849435 Update all code to es6
  • Loading branch information
chidimo committed May 7, 2019
2 parents 7509e60 + 8b1d6dc commit 50dffc1
Show file tree
Hide file tree
Showing 22 changed files with 1,235 additions and 351 deletions.
3 changes: 3 additions & 0 deletions .babelrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"presets": ["@babel/preset-env"],
}
34 changes: 17 additions & 17 deletions .eslintrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,35 +19,35 @@
"prefer-const": "warn",
"no-const-assign": "warn",
"no-var": "warn",
"no-new-object": "error",
"no-new-func": "error",
"no-new-object": "warn",
"no-new-func": "warn",
"object-shorthand": "warn",
"quote-props": ["warn", "as-needed"],
"array-callback-return": "warn",
"prefer-destructuring": "warn",
"quotes": ["warn", "single", { "avoidEscape": true}],
"prefer-template": "warn",
"func-style": ["error", "expression", {"allowArrowFunctions": true}],
"func-style": ["warn", "expression", {"allowArrowFunctions": true}],
"no-param-reassign": "warn",
"prefer-arrow-callback": "warn",
"arrow-spacing": ["warn", {"before": true, "after": true}],
"dot-notation": "warn",
"one-var": "off",
"no-undef": "warn",
"no-multi-assign": "error",
"no-plusplus": "error",
"max-len": ["error", { "code": 80 }],
"no-multi-assign": "warn",
"no-plusplus": "warn",
"max-len": ["warn", { "code": 80 }],
"no-unused-vars": "warn",
"eqeqeq": "error",
"nonblock-statement-body-position": ["error", "beside"],
"no-else-return": "error",
"spaced-comment": ["error", "always"],
"indent": ["error", 4],
"space-before-blocks": "error",
"keyword-spacing": ["error", { "before": true }],
"space-infix-ops": "error",
"eol-last": ["error", "always"],
"object-curly-spacing": ["error", "always"],
"array-bracket-spacing": ["error", "always"]
"eqeqeq": "warn",
"nonblock-statement-body-position": ["warn", "beside"],
"no-else-return": "warn",
"spaced-comment": ["warn", "always"],
"indent": ["warn", 4],
"space-before-blocks": "warn",
"keyword-spacing": ["warn", { "before": true }],
"space-infix-ops": "warn",
"eol-last": ["warn", "always"],
"object-curly-spacing": ["warn", "always"],
"array-bracket-spacing": ["warn", "always"]
}
}
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@ node_modules/
yarn-error.log
coverage/
.nyc_output/
dist/
14 changes: 7 additions & 7 deletions app.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
const createError = require('http-errors');
const express = require('express');
const path = require('path');
const cookieParser = require('cookie-parser');
const logger = require('morgan');
import express from 'express';
import createError from 'http-errors';
import path from 'path';
import cookieParser from 'cookie-parser';
import logger from 'morgan';

const indexRouter = require('./routes/index');
import indexRouter from './routes/index';

const app = express();

Expand Down Expand Up @@ -35,4 +35,4 @@ app.use((err, req, res, next) => {
res.render('error');
});

module.exports = app;
export default app;
8 changes: 5 additions & 3 deletions bin/www → bin/www.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,11 @@
* Module dependencies.
*/

const app = require('../app');
const debug = require('debug')('quick-credit:server');
const http = require('http');
// const debug = require('debug')('quick-credit:server');

import debug from 'debug';
import app from '../app';
import http from 'http';

/**
* Normalize a port into a number, string, or false.
Expand Down
68 changes: 68 additions & 0 deletions controllers/AuthController.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
import { _ } from 'underscore';
import utils from '../utils/helpers';
import users from '../utils/sample.users';

const AuthController = {

index: (req, res) => {
res.render('index', { title: 'Welcome' });
},

about: (req, res) => {
res.render('about', { title: 'About' });
},

signup: (req, res) => {
if (req.method === 'GET') {
res.render('authentication', { title: 'Sign Up' });
}
else {
const { email, password, confirm_password } = req.body;
const id = utils.get_random_id();
if (password !== confirm_password) {
res.send({ status: 404, error: 'Passwords do not match' });
}
else {
res.send({
status: 201,
data: {
id,
email,
password,
first_name: '',
last_name: '',
address: {
home: '',
office: '',
},
status: 'unverified',
isAdmin: false,
token: '45erkjherht45495783',
}
});
}
}
},

signin: (req, res) => {
if (req.method === 'GET') {
res.render( 'authentication', { title: 'Sign In' });
}
else {
const { email, password } = req.body;
const [ user, ] = _.filter(users, user => (user.email === email));
if (user) {
if (user.password === password) res.send({
status: 200, data: user
});
else res.send({
status: 404, error: 'Email and password do not match'
});
}
else res.send({ status: 404, error: 'User not found' });
}
},
};

export default AuthController;

101 changes: 101 additions & 0 deletions controllers/LoansController.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@

import { _ } from 'underscore';
import loans from '../utils/sample.loans';
import repayments from '../utils/sample.repayments';

const LoansController = {

get_all_loans: (req, res) => {
const { status, repaid } = req.query;
const boolean_repaid = (req.query.repaid === 'true');

// if both status and repaid is unspecified
if ((status !== undefined) && (repaid !== undefined)) {
const data = _.filter(loans, loan => (
loan.status === status && loan.repaid === boolean_repaid
));
res.send({ status: 200, data });
}
else {
const data = [];
_.map(loans, loan => {
data.push(loan);
});
res.send({ status: 200, data });
}
},

get_loan: (req, res) => {
const { id } = req.params;
const data = _.filter(loans, loan => (loan.id === id));
if (data.length === 0) {
res.send({ status: 404, error: `Loan with id ${id} not found` });
}
else res.send({ status: 200, data });
},

create_loan: (req, res) => {
const { email } = req.body;
const amount = Number(req.body.amount);
const tenor = Number(req.body.tenor);
const interest = 0.05 * amount;
const paymentInstallment = (amount + interest) / tenor;
const balance = amount - 0;

res.send({
status: 201,
data: {
loanId: '',
status: 'pending',
email,
amount,
tenor,
interest,
paymentInstallment,
balance
}
});
},

approve_loan: (req, res) => {
const { id } = req.params;
const loan = _.filter(loans, loan => (loan.id === id));
const [ data, ] = loan;
data.status = 'approved';
res.send({ status: 200, data });
},

reject_loan: (req, res) => {
const { id } = req.params;
const loan = _.filter(loans, loan => (loan.id === id));
const [ data, ] = loan;
data.status = 'rejected';
res.send({ status: 200, data });

},

loan_repayment_history: (req, res) => {
const { id } = req.params;
const data = _.filter(repayments, payment => (payment.loanId === id));
res.send({ status: 200, data });
},

post_repayment: (req, res) => {
const { id } = req.params;
const amount = Number(req.body.amount);

const repay = {
id: '',
createdOn: new Date(),
loanId: id,
amount,
};
// update loan balance
const [ loan, ] = _.filter(loans, loan => (loan.id === id));
loan.balance = loan.balance + amount;

res.send({ status: 201, data: repay });
},
};

export default LoansController;
63 changes: 63 additions & 0 deletions controllers/UsersController.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
import { _ } from 'underscore';
import users from '../utils/sample.users';

const UsersController = {

dashboard: (req, res) => {
res.render( 'dashboard', { title: 'Dashboard' });
},

verify_user: (req, res) => {
const { email } = req.params;
const [ user, ] = _.filter(users, user => (user.email === email));

if (user) {
user.status = 'verified';
res.send({ status: 200, data: user });
}
else res.send({ status: 404, error: 'User not found' });
},

get_users: (req, res) => {

const { status } = req.query;

if (status) {
const data = _.filter(users, user => (user.status === status));
res.send({ status: 200, data });
}
else {
const data = [];
_.map(users, (user) => {
data.push(user);
});
res.send({ status: 200, data });
}
},

get_user: (req, res) => {
const { id } = req.params;
const data = _.filter(users, user => (user.id === id));
if (data.length === 0) {
res.send({ status: 404, error: `User with id ${id} not found` });
}
else res.send({ status: 200, data: data[0] });
},

update_user: (req, res) => {
const { id } = req.params;
const { firstName, lastName, phone, home, office } = req.body;
const [ user, ] = _.filter(users, user => (user.id === id));

user.firstName = firstName;
user.lastName = lastName;
user.phone = phone;
user.address.home = home;
user.address.office = office;

res.send({ status: 200, data: user });
},

};

export default UsersController;
Loading

0 comments on commit 50dffc1

Please sign in to comment.