-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #25 from chidimo/ch-update-to-es6-165849435
#165849435 Update all code to es6
- Loading branch information
Showing
22 changed files
with
1,235 additions
and
351 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
{ | ||
"presets": ["@babel/preset-env"], | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,3 +2,4 @@ node_modules/ | |
yarn-error.log | ||
coverage/ | ||
.nyc_output/ | ||
dist/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
Oops, something went wrong.