From 6dd900e94d2a55533ab4b0f7416f8488b026b180 Mon Sep 17 00:00:00 2001 From: Jason Date: Thu, 20 Oct 2016 21:59:35 -0700 Subject: [PATCH 1/2] Add auth route' ; ' ' --- server/routes/auth.js | 9 +++++++++ 1 file changed, 9 insertions(+) create mode 100644 server/routes/auth.js diff --git a/server/routes/auth.js b/server/routes/auth.js new file mode 100644 index 0000000..3a6e196 --- /dev/null +++ b/server/routes/auth.js @@ -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; From e3b5542853a1708a84253299c44e79202f68570b Mon Sep 17 00:00:00 2001 From: David Walsh Date: Sun, 23 Oct 2016 16:57:34 -0700 Subject: [PATCH 2/2] (refactor) Align with style guide --- server/db.js | 12 ++++++------ server/dbControllers/billController.js | 13 ++++++------- server/dbControllers/itemController.js | 5 ++--- server/handlers/apiHandler.js | 7 ++++--- server/spec/dbControllersSpec.js | 23 +++++++++++------------ 5 files changed, 29 insertions(+), 31 deletions(-) diff --git a/server/db.js b/server/db.js index 88e91c2..cf12dd6 100644 --- a/server/db.js +++ b/server/db.js @@ -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); @@ -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: { @@ -79,7 +79,7 @@ User.belongsToMany(Bill, { foreignKey: 'debtorId', }); -Bill.belongsToMany(User, {through: BillDebtors}); +Bill.belongsToMany(User, { through: BillDebtors }); Bill.belongsTo(User, { as: 'Payer', diff --git a/server/dbControllers/billController.js b/server/dbControllers/billController.js index 2b92f0f..d35da2f 100644 --- a/server/dbControllers/billController.js +++ b/server/dbControllers/billController.js @@ -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')); @@ -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); }); }); diff --git a/server/dbControllers/itemController.js b/server/dbControllers/itemController.js index b573969..6e92465 100644 --- a/server/dbControllers/itemController.js +++ b/server/dbControllers/itemController.js @@ -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 = { diff --git a/server/handlers/apiHandler.js b/server/handlers/apiHandler.js index c5d0ff5..840a416 100644 --- a/server/handlers/apiHandler.js +++ b/server/handlers/apiHandler.js @@ -3,15 +3,15 @@ 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: { @@ -19,6 +19,7 @@ const saveBill = (request, response) => { }, }); }); +}; }; diff --git a/server/spec/dbControllersSpec.js b/server/spec/dbControllersSpec.js index 6178c22..8958619 100644 --- a/server/spec/dbControllersSpec.js +++ b/server/spec/dbControllersSpec.js @@ -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(), @@ -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, @@ -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(); }); }); - });