Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

(refactor) Style guide updates #29

Merged
merged 2 commits into from
Oct 24, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 6 additions & 6 deletions server/db.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
const config = require('../config.js');
const Sequelize = require('sequelize');
const Hashids = require('hashids');

const hashIds = new Hashids('manatee salt', 5);

console.log('db path:', config.db.path);
Expand All @@ -26,13 +27,12 @@ const Bill = sequelize.define('bill', {
type: Sequelize.DECIMAL(10, 2), // eslint-disable-line
},
},
{
hooks: {
afterCreate: (bill, options) => {
return bill.update({shortId: hashIds.encode(bill.dataValues.id)});
{
hooks: {
afterCreate: bill => bill.update({ shortId: hashIds.encode(bill.dataValues.id) }),
},
},
});
);

const Item = sequelize.define('item', {
description: {
Expand Down Expand Up @@ -79,7 +79,7 @@ User.belongsToMany(Bill, {
foreignKey: 'debtorId',
});

Bill.belongsToMany(User, {through: BillDebtors});
Bill.belongsToMany(User, { through: BillDebtors });

Bill.belongsTo(User, {
as: 'Payer',
Expand Down
13 changes: 6 additions & 7 deletions server/dbControllers/billController.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@ const Bill = require('../db').models.Bill;

const itemController = require('./itemController');

const createBill = bill => {

const createBill = (bill) => {
return new Promise((resolve, reject) => {
if (!bill.payer) {
return reject(new Error('Bill payer id required'));
Expand All @@ -12,18 +11,18 @@ const createBill = bill => {
return reject(new Error('At least one bill item is required'));
}
// Need to find user and validate
Bill.create(bill)
.then(billRecord => {
return Bill.create(bill)
.then((billRecord) => {
// have bill, now create the items
itemController.createItemsForBill(billRecord.dataValues.id, bill.items)
.then(items => {
.then(() => {
resolve(billRecord);
})
.catch(err => {
.catch((err) => {
reject(err);
});
})
.catch(err => {
.catch((err) => {
reject(err);
});
});
Expand Down
5 changes: 2 additions & 3 deletions server/dbControllers/itemController.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,10 @@ const createItemsForBill = (billId, items) => {

const itemsPromises = [];

items.forEach(item => {
itemsPromises.push(Item.create(Object.assign(item, {billId})));
items.forEach((item) => {
itemsPromises.push(Item.create(Object.assign(item, { billId })));
});
return Promise.all(itemsPromises);

};

module.exports = {
Expand Down
7 changes: 4 additions & 3 deletions server/handlers/apiHandler.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,23 @@ const billController = require('../dbControllers/billController');
const saveBill = (request, response) => {
const bill = request.body;
billController.createBill(bill)
.then(bill => {
.then((billRecord) => {
response.status(201);
response.json({
data: {
shortId: bill.shortId,
shortId: billRecord.shortId,
},
});
})
.catch(error => {
.catch((error) => {
response.status(400);
response.json({
error: {
message: error.message,
},
});
});
};

};

Expand Down
9 changes: 9 additions & 0 deletions server/routes/auth.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
const express = require('express');

const router = express.Router(); // eslint-disable-line


router.get('/', /* funct to handle bill*/);
router.post('/', /* funct to create bill */);

module.exports = router;
23 changes: 11 additions & 12 deletions server/spec/dbControllersSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,36 +13,36 @@ if (/test/.test(config.db.path) === false) {
}

describe('Item controller', () => {
before(done => {
before((done) => {
Promise.all([
db.models.Item.sync()
db.models.Item.sync(),
])
.then(() => {
done();
});
});

it('should create items', done => {
it('should create items', (done) => {
const items = [
{description: 'Grandma\'s curry', price: 10.99},
{description: 'Dr. Pepper', price: 1.99}
{ description: 'Grandma\'s curry', price: 10.99 },
{ description: 'Dr. Pepper', price: 1.99 },
];
const billId = 21;
itemController.createItemsForBill(21, items)
.then(itemsRecords => {
.then((itemsRecords) => {
expect(itemsRecords.length).to.equal(2);
expect(itemsRecords[0].dataValues.description).to.equal(items[0].description);
expect(itemsRecords[1].dataValues.billId).to.equal(billId);
done();
})
.catch(err => {
.catch(() => {
done();
});
});
});

describe('Bill controller', () => {
before(done => {
before((done) => {
Promise.all([
db.models.Item.sync(),
db.models.Bill.sync(),
Expand All @@ -52,7 +52,7 @@ describe('Bill controller', () => {
});
});

it('should create bills', done => {
it('should create bills', (done) => {
const bill = {
description: 'Tu Lan lunch',
tax: 2.46,
Expand All @@ -65,14 +65,13 @@ describe('Bill controller', () => {
],
};
billController.createBill(bill)
.then(billRecord => {
.then((billRecord) => {
expect(billRecord.dataValues.description).to.equal(bill.description);
expect(billRecord.dataValues.shortId).to.match(/\w{5,}/);
done();
})
.catch(err => {
.catch(() => {
done();
});
});

});