From 94694026b743927fe4dfa4015869e01099a891ec Mon Sep 17 00:00:00 2001 From: Jason Anton Date: Fri, 20 Mar 2020 19:23:08 -0400 Subject: [PATCH 1/2] feat: Adding check-in endpoints This will add check-in models. The endpoints will actually come in another Commit. This commit also cleans up some foreign key lookups for other tables. resolves #6 --- .../migrations/05-create-demo-check-in.js | 27 ++++++++ ...contact.js => 06-add-demo-user-contact.js} | 2 +- .../07-add-demo-check-in-contact.js | 26 +++++++ .../migrations/08-add-demo-check-in-user.js | 26 +++++++ .../migrations/09-add-demo-contact-entity.js | 26 +++++++ .../migrations/10-add-demo-check-in-entity.js | 26 +++++++ .../{03-demo-user.js => 02-demo-user.js} | 0 ...{02-demo-contact.js => 03-demo-contact.js} | 0 src/models/check-in.js | 69 +++++++++++++++++++ src/models/contact.js | 27 +++++--- src/models/entity.js | 14 ++++ src/routes/contact.js | 28 ++++---- src/routes/entity.js | 19 ++++- src/routes/user.js | 17 ++++- src/tests/user.routes.spec.js | 2 +- 15 files changed, 279 insertions(+), 30 deletions(-) create mode 100644 sequelize/migrations/05-create-demo-check-in.js rename sequelize/migrations/{05-add-demo-user-contact.js => 06-add-demo-user-contact.js} (96%) create mode 100644 sequelize/migrations/07-add-demo-check-in-contact.js create mode 100644 sequelize/migrations/08-add-demo-check-in-user.js create mode 100644 sequelize/migrations/09-add-demo-contact-entity.js create mode 100644 sequelize/migrations/10-add-demo-check-in-entity.js rename sequelize/seeders/{03-demo-user.js => 02-demo-user.js} (100%) rename sequelize/seeders/{02-demo-contact.js => 03-demo-contact.js} (100%) create mode 100644 src/models/check-in.js diff --git a/sequelize/migrations/05-create-demo-check-in.js b/sequelize/migrations/05-create-demo-check-in.js new file mode 100644 index 00000000..3d9e515d --- /dev/null +++ b/sequelize/migrations/05-create-demo-check-in.js @@ -0,0 +1,27 @@ +'use strict'; +module.exports = { + up: (queryInterface, Sequelize) => { + return queryInterface.createTable('CheckIns', { + id: { + type: Sequelize.UUID, + primaryKey: true, + allowNull: false, + autoIncrement: false, + }, + notes : { + type: Sequelize.JSON, + }, + createdAt: { + allowNull: false, + type: Sequelize.DATE + }, + updatedAt: { + allowNull: false, + type: Sequelize.DATE + } + }); + }, + down: queryInterface => { + return queryInterface.dropTable('CheckIns'); + } +}; diff --git a/sequelize/migrations/05-add-demo-user-contact.js b/sequelize/migrations/06-add-demo-user-contact.js similarity index 96% rename from sequelize/migrations/05-add-demo-user-contact.js rename to sequelize/migrations/06-add-demo-user-contact.js index 2e777c3d..0b93eb42 100644 --- a/sequelize/migrations/05-add-demo-user-contact.js +++ b/sequelize/migrations/06-add-demo-user-contact.js @@ -20,7 +20,7 @@ module.exports = { down: function(queryInterface, Sequelize) { return queryInterface.removeColumn( 'Contacts', - 'user_id' + 'UserId' ); } } \ No newline at end of file diff --git a/sequelize/migrations/07-add-demo-check-in-contact.js b/sequelize/migrations/07-add-demo-check-in-contact.js new file mode 100644 index 00000000..da1a74b3 --- /dev/null +++ b/sequelize/migrations/07-add-demo-check-in-contact.js @@ -0,0 +1,26 @@ +'use strict'; +module.exports = { + up: function(queryInterface, Sequelize) { + return queryInterface.addColumn( + 'CheckIns', + 'ContactId', + { + type: Sequelize.UUID, + references: { + model: 'Contacts', + key: 'id' + }, + onUpdate: 'CASCADE', + onDelete: 'SET NULL' + } + ); + + }, + + down: function(queryInterface, Sequelize) { + return queryInterface.removeColumn( + 'CheckIns', + 'ContactId' + ); + } + } \ No newline at end of file diff --git a/sequelize/migrations/08-add-demo-check-in-user.js b/sequelize/migrations/08-add-demo-check-in-user.js new file mode 100644 index 00000000..f86fc21e --- /dev/null +++ b/sequelize/migrations/08-add-demo-check-in-user.js @@ -0,0 +1,26 @@ +'use strict'; +module.exports = { + up: function(queryInterface, Sequelize) { + return queryInterface.addColumn( + 'CheckIns', + 'UserId', + { + type: Sequelize.UUID, + references: { + model: 'Users', + key: 'id' + }, + onUpdate: 'CASCADE', + onDelete: 'SET NULL' + } + ); + + }, + + down: function(queryInterface, Sequelize) { + return queryInterface.removeColumn( + 'CheckIns', + 'UserId' + ); + } + } \ No newline at end of file diff --git a/sequelize/migrations/09-add-demo-contact-entity.js b/sequelize/migrations/09-add-demo-contact-entity.js new file mode 100644 index 00000000..c11f965b --- /dev/null +++ b/sequelize/migrations/09-add-demo-contact-entity.js @@ -0,0 +1,26 @@ +'use strict'; +module.exports = { + up: function(queryInterface, Sequelize) { + return queryInterface.addColumn( + 'Contacts', + 'EntityId', + { + type: Sequelize.UUID, + references: { + model: 'Entities', + key: 'id' + }, + onUpdate: 'CASCADE', + onDelete: 'SET NULL' + } + ); + + }, + + down: function(queryInterface, Sequelize) { + return queryInterface.removeColumn( + 'Contacts', + 'EntityId' + ); + } + } \ No newline at end of file diff --git a/sequelize/migrations/10-add-demo-check-in-entity.js b/sequelize/migrations/10-add-demo-check-in-entity.js new file mode 100644 index 00000000..6f28a884 --- /dev/null +++ b/sequelize/migrations/10-add-demo-check-in-entity.js @@ -0,0 +1,26 @@ +'use strict'; +module.exports = { + up: function(queryInterface, Sequelize) { + return queryInterface.addColumn( + 'CheckIns', + 'EntityId', + { + type: Sequelize.UUID, + references: { + model: 'Entities', + key: 'id' + }, + onUpdate: 'CASCADE', + onDelete: 'SET NULL' + } + ); + + }, + + down: function(queryInterface, Sequelize) { + return queryInterface.removeColumn( + 'CheckIns', + 'EntityId' + ); + } + } \ No newline at end of file diff --git a/sequelize/seeders/03-demo-user.js b/sequelize/seeders/02-demo-user.js similarity index 100% rename from sequelize/seeders/03-demo-user.js rename to sequelize/seeders/02-demo-user.js diff --git a/sequelize/seeders/02-demo-contact.js b/sequelize/seeders/03-demo-contact.js similarity index 100% rename from sequelize/seeders/02-demo-contact.js rename to sequelize/seeders/03-demo-contact.js diff --git a/src/models/check-in.js b/src/models/check-in.js new file mode 100644 index 00000000..44380b89 --- /dev/null +++ b/src/models/check-in.js @@ -0,0 +1,69 @@ +import { UUIDV4 } from "sequelize"; + +const checkIn = (sequelize, DataTypes) => { + // Defining our checkIn table and setting CheckIn object. + const CheckIn = sequelize.define('CheckIn', { + id: { + type: DataTypes.UUID, + unique: true, + primaryKey: true, + defaultValue: UUIDV4 + }, + UserId: { + type: DataTypes.STRING, + references: { + model: 'User', + key: 'id', + } + }, + ContactId: { + type: DataTypes.STRING, + references: { + model: 'Contact', + key: 'id', + } + }, + EntityId: { + type: DataTypes.STRING, + references: { + model: 'Entity', + key: 'id', + } + }, + notes: { + type: DataTypes.JSON, + }, + history: { + type: DataTypes.JSON + } + }, + { + schema: process.env.DATABASE_SCHEMA + }); + + CheckIn.associations = models => { + CheckIn.belongsTo(models.User); + CheckIn.belongsTo(models.Contact); + CheckIn.belongsTo(models.Entity); + } + + CheckIn.findById = async (id) => { + const checkIn = await CheckIn.findOne({ + where: { id } + }); + + return checkIn; + }; + + CheckIn.findByName = async (name) => { + const checkIn = await CheckIn.findOne({ + where: { name } + }); + + return checkIn; + }; + + return CheckIn; +}; + +export default checkIn; \ No newline at end of file diff --git a/src/models/contact.js b/src/models/contact.js index 63e2425e..194c7078 100644 --- a/src/models/contact.js +++ b/src/models/contact.js @@ -10,10 +10,18 @@ const contact = (sequelize, DataTypes) => { defaultValue: UUIDV4 }, UserId: { - type: DataTypes.STRING, + type: DataTypes.UUID, references: { model: 'User', key: 'id', + }, + unique: true + }, + EntityId: { + type: DataTypes.UUID, + references: { + model: 'Entity', + key: 'id' } }, name : { @@ -33,6 +41,7 @@ const contact = (sequelize, DataTypes) => { Contact.associate = models => { Contact.belongsTo(models.User); + Contact.belongsTo(models.Entity); } Contact.findById = async (id) => { @@ -43,6 +52,14 @@ const contact = (sequelize, DataTypes) => { return contact; }; + Contact.findByUserId = async (UserId) => { + const contact = await Contact.findOne({ + where: { UserId } + }); + + return contact.dataValues; + } + Contact.findByName = async (name) => { const contact = await Contact.findOne({ where: { name } @@ -51,14 +68,6 @@ const contact = (sequelize, DataTypes) => { return contact; }; - // Contact.findByPhone = async (phone) => { - // const contact = await Contact.findOne({ - // where: { phone } - // }); - - // return contact; - // }; - return Contact; }; diff --git a/src/models/entity.js b/src/models/entity.js index 6e77e2d1..e2e7ef04 100644 --- a/src/models/entity.js +++ b/src/models/entity.js @@ -24,6 +24,10 @@ const entity = (sequelize, DataTypes) => { schema: process.env.DATABASE_SCHEMA }); + Entity.associate = models => { + Entity.hasMany(models.Contact); + } + Entity.findById = async (id) => { const entity = await Entity.findOne({ where: { id } @@ -40,6 +44,16 @@ const entity = (sequelize, DataTypes) => { return entity; }; + Entity.findContacts = async (id) => { + const entity = await Entity.findOne({ + where: { id } + }); + + const contacts = await entity.getContacts(); + + return contacts; + } + return Entity; }; diff --git a/src/routes/contact.js b/src/routes/contact.js index 23edc806..1289a01b 100644 --- a/src/routes/contact.js +++ b/src/routes/contact.js @@ -9,7 +9,7 @@ router.get('/', async (req, res) => { try { if (await utils.validateToken(req, res)) { const contacts = await req.context.models.Contact.findAll({ - attributes: ['id', 'name', 'email', 'phone', 'createdAt', 'updatedAt'] + // attributes: ['id', 'name', 'updatedAt'] }); return res.send({ _meta: { @@ -33,7 +33,7 @@ router.get('/:contact_id', async (req, res) => { where: { id: req.params.contact_id }, - attributes: ['id', 'name', 'email', 'phone', 'createdAt', 'updatedAt'] + // attributes: ['id', 'name', 'email', 'phone', 'UserId', 'createdAt', 'updatedAt'] }); return res.send(contact); } @@ -49,10 +49,8 @@ router.post('/', async (req, res) => { try { if (await utils.validateToken(req, res)) { if (req.body.name !== undefined) { - const { name, phone, email } = req.body; - - - const contact = await req.context.models.Contact.create({ name, email, phone }); + const { name, phone, email, UserId, EntityId } = req.body; + const contact = await req.context.models.Contact.create({ name, email, phone, UserId, EntityId }); return res.send(contact.id + ' created'); } } @@ -68,22 +66,23 @@ router.post('/', async (req, res) => { router.put('/', async (req, res) => { try { if (await utils.validateToken(req, res)) { - const { id, name, phone, email, UserId } = req.body; + const { id, name, phone, email, UserId, EntityId } = req.body; /** @todo validate emails */ // Validating emails // if (await !utils.validateEmails(email)) res.status(400).send('Invalid input'); - + const contact = await req.context.models.Contact.findOne({ where: { id: id } }); - - contact.name = name; - contact.UserId = UserId; - contact.phone = phone; - contact.email = email; + + contact.name = (name) ? name : contact.name; + contact.phone = (phone) ? phone : contact.phone; + contact.email = (email) ? email : contact.email; + contact.UserId = (UserId) ? UserId : contact.UserId; + contact.EntityId = (EntityId) ? EntityId : contact.EntityId; contact.updatedAt = new Date(); await contact.save(); @@ -110,7 +109,8 @@ router.delete('/:contact_id', async (req, res) => { return res.send(req.params.contact_id + ' deleted'); } throw new Error('Invalid input'); - } catch { + } catch (e) { + console.error(e); res.status(400).send('Invalid input'); } }); diff --git a/src/routes/entity.js b/src/routes/entity.js index 677a82c7..5da950fc 100644 --- a/src/routes/entity.js +++ b/src/routes/entity.js @@ -9,8 +9,9 @@ router.get('/', async (req, res) => { try { if (await utils.validateToken(req, res)) { const entities = await req.context.models.Entity.findAll({ - attributes: ['id', 'name', 'email', 'phone', 'createdAt', 'updatedAt'] + attributes: ['id', 'name', 'updatedAt'] }); + return res.send({ _meta: { total: entities.length @@ -35,11 +36,25 @@ router.get('/:entity_id', async (req, res) => { }, attributes: ['id', 'name', 'email', 'phone', 'createdAt', 'updatedAt'] }); + entity.dataValues.contacts = [] + + const contacts = await req.context.models.Entity.findContacts(req.params.entity_id); + + for (let contact of contacts) { + entity.dataValues.contacts.push({ + id: contact.dataValues.id, + name: contact.dataValues.name, + }); + } + + console.log(entity) + return res.send(entity); } throw new Error('Invalid input'); - } catch { + } catch (e) { + console.error(e); res.status(400).send('Invalid payload'); } }); diff --git a/src/routes/user.js b/src/routes/user.js index 742928b8..6eebc931 100644 --- a/src/routes/user.js +++ b/src/routes/user.js @@ -34,7 +34,12 @@ router.get('/', async (req, res) => { user.roles = await req.context.models.UserRole.findRoles(user.roles); } - return res.send(users); + return res.send({ + _meta: { + total: users.length + }, + results: users + }); } throw new Error('Invalid input'); @@ -53,12 +58,18 @@ router.get('/:email', async (req, res) => { }, attributes: ['id', 'email', 'roles', 'displayName', 'phone', 'createdAt', 'updatedAt'] }); - if (user) user.roles = await req.context.models.UserRole.findRoles(user.roles); + if (user) { + user.roles = await req.context.models.UserRole.findRoles(user.roles); + /** @todo add contact info for users */ + // user.dataValues.contact = await req.context.models.Contact.findByUserId(user.id); + } + return res.send(user); } throw new Error('Invalid input'); - } catch { + } catch (e) { + console.error(e); res.status(400).send('Invalid payload'); } }); diff --git a/src/tests/user.routes.spec.js b/src/tests/user.routes.spec.js index 3a11e69c..b83fb797 100644 --- a/src/tests/user.routes.spec.js +++ b/src/tests/user.routes.spec.js @@ -42,7 +42,7 @@ describe('User positive tests', () => { .expect(200) .end((err, res) => { if (err) return done(err); - expect(res.body.length).to.be.greaterThan(0); + expect(res.body._meta.total).to.be.greaterThan(0); done(); }); }); From d2de8b446c8855c9d6bb22e8529069e7fdbd2713 Mon Sep 17 00:00:00 2001 From: Jason Anton Date: Sat, 21 Mar 2020 15:29:33 -0400 Subject: [PATCH 2/2] Moving checkIn to the Entity object. --- sequelize/migrations/04-create-demo-entity.js | 5 +- .../07-add-demo-check-in-contact.js | 26 ------- ...ntity.js => 07-add-demo-entity-contact.js} | 0 .../migrations/08-add-demo-check-in-user.js | 26 ------- .../migrations/10-add-demo-check-in-entity.js | 26 ------- src/models/check-in.js | 69 ------------------- src/models/entity.js | 3 + src/models/user.js | 2 +- src/routes/contact.js | 4 +- src/routes/entity.js | 64 +++++++++++++---- src/routes/user-role.js | 4 +- 11 files changed, 59 insertions(+), 170 deletions(-) delete mode 100644 sequelize/migrations/07-add-demo-check-in-contact.js rename sequelize/migrations/{09-add-demo-contact-entity.js => 07-add-demo-entity-contact.js} (100%) delete mode 100644 sequelize/migrations/08-add-demo-check-in-user.js delete mode 100644 sequelize/migrations/10-add-demo-check-in-entity.js delete mode 100644 src/models/check-in.js diff --git a/sequelize/migrations/04-create-demo-entity.js b/sequelize/migrations/04-create-demo-entity.js index 3f21f06e..fb77dd02 100644 --- a/sequelize/migrations/04-create-demo-entity.js +++ b/sequelize/migrations/04-create-demo-entity.js @@ -17,7 +17,10 @@ module.exports = { }, email: { type: Sequelize.JSON, - }, + }, + checkIn: { + type: Sequelize.JSON, + }, createdAt: { allowNull: false, type: Sequelize.DATE diff --git a/sequelize/migrations/07-add-demo-check-in-contact.js b/sequelize/migrations/07-add-demo-check-in-contact.js deleted file mode 100644 index da1a74b3..00000000 --- a/sequelize/migrations/07-add-demo-check-in-contact.js +++ /dev/null @@ -1,26 +0,0 @@ -'use strict'; -module.exports = { - up: function(queryInterface, Sequelize) { - return queryInterface.addColumn( - 'CheckIns', - 'ContactId', - { - type: Sequelize.UUID, - references: { - model: 'Contacts', - key: 'id' - }, - onUpdate: 'CASCADE', - onDelete: 'SET NULL' - } - ); - - }, - - down: function(queryInterface, Sequelize) { - return queryInterface.removeColumn( - 'CheckIns', - 'ContactId' - ); - } - } \ No newline at end of file diff --git a/sequelize/migrations/09-add-demo-contact-entity.js b/sequelize/migrations/07-add-demo-entity-contact.js similarity index 100% rename from sequelize/migrations/09-add-demo-contact-entity.js rename to sequelize/migrations/07-add-demo-entity-contact.js diff --git a/sequelize/migrations/08-add-demo-check-in-user.js b/sequelize/migrations/08-add-demo-check-in-user.js deleted file mode 100644 index f86fc21e..00000000 --- a/sequelize/migrations/08-add-demo-check-in-user.js +++ /dev/null @@ -1,26 +0,0 @@ -'use strict'; -module.exports = { - up: function(queryInterface, Sequelize) { - return queryInterface.addColumn( - 'CheckIns', - 'UserId', - { - type: Sequelize.UUID, - references: { - model: 'Users', - key: 'id' - }, - onUpdate: 'CASCADE', - onDelete: 'SET NULL' - } - ); - - }, - - down: function(queryInterface, Sequelize) { - return queryInterface.removeColumn( - 'CheckIns', - 'UserId' - ); - } - } \ No newline at end of file diff --git a/sequelize/migrations/10-add-demo-check-in-entity.js b/sequelize/migrations/10-add-demo-check-in-entity.js deleted file mode 100644 index 6f28a884..00000000 --- a/sequelize/migrations/10-add-demo-check-in-entity.js +++ /dev/null @@ -1,26 +0,0 @@ -'use strict'; -module.exports = { - up: function(queryInterface, Sequelize) { - return queryInterface.addColumn( - 'CheckIns', - 'EntityId', - { - type: Sequelize.UUID, - references: { - model: 'Entities', - key: 'id' - }, - onUpdate: 'CASCADE', - onDelete: 'SET NULL' - } - ); - - }, - - down: function(queryInterface, Sequelize) { - return queryInterface.removeColumn( - 'CheckIns', - 'EntityId' - ); - } - } \ No newline at end of file diff --git a/src/models/check-in.js b/src/models/check-in.js deleted file mode 100644 index 44380b89..00000000 --- a/src/models/check-in.js +++ /dev/null @@ -1,69 +0,0 @@ -import { UUIDV4 } from "sequelize"; - -const checkIn = (sequelize, DataTypes) => { - // Defining our checkIn table and setting CheckIn object. - const CheckIn = sequelize.define('CheckIn', { - id: { - type: DataTypes.UUID, - unique: true, - primaryKey: true, - defaultValue: UUIDV4 - }, - UserId: { - type: DataTypes.STRING, - references: { - model: 'User', - key: 'id', - } - }, - ContactId: { - type: DataTypes.STRING, - references: { - model: 'Contact', - key: 'id', - } - }, - EntityId: { - type: DataTypes.STRING, - references: { - model: 'Entity', - key: 'id', - } - }, - notes: { - type: DataTypes.JSON, - }, - history: { - type: DataTypes.JSON - } - }, - { - schema: process.env.DATABASE_SCHEMA - }); - - CheckIn.associations = models => { - CheckIn.belongsTo(models.User); - CheckIn.belongsTo(models.Contact); - CheckIn.belongsTo(models.Entity); - } - - CheckIn.findById = async (id) => { - const checkIn = await CheckIn.findOne({ - where: { id } - }); - - return checkIn; - }; - - CheckIn.findByName = async (name) => { - const checkIn = await CheckIn.findOne({ - where: { name } - }); - - return checkIn; - }; - - return CheckIn; -}; - -export default checkIn; \ No newline at end of file diff --git a/src/models/entity.js b/src/models/entity.js index e2e7ef04..02669f81 100644 --- a/src/models/entity.js +++ b/src/models/entity.js @@ -18,6 +18,9 @@ const entity = (sequelize, DataTypes) => { }, email: { type: DataTypes.JSON, + }, + checkIn: { + type: DataTypes.JSON, } }, { diff --git a/src/models/user.js b/src/models/user.js index d874c5ad..3a00d875 100644 --- a/src/models/user.js +++ b/src/models/user.js @@ -71,7 +71,7 @@ const user = (sequelize, DataTypes) => { * @return {Boolean} */ User.validateToken = async token => { - const expiry = jwt.decode(token).exp; + const expiry = jwt.decode(token); const now = new Date(); if (now.getTime() < expiry * 1000) { diff --git a/src/routes/contact.js b/src/routes/contact.js index 1289a01b..3f0278ba 100644 --- a/src/routes/contact.js +++ b/src/routes/contact.js @@ -1,5 +1,5 @@ import { Router } from 'express'; -import validator from 'validator'; +// import validator from 'validator'; import utils from '../utils'; const router = new Router(); @@ -9,7 +9,6 @@ router.get('/', async (req, res) => { try { if (await utils.validateToken(req, res)) { const contacts = await req.context.models.Contact.findAll({ - // attributes: ['id', 'name', 'updatedAt'] }); return res.send({ _meta: { @@ -33,7 +32,6 @@ router.get('/:contact_id', async (req, res) => { where: { id: req.params.contact_id }, - // attributes: ['id', 'name', 'email', 'phone', 'UserId', 'createdAt', 'updatedAt'] }); return res.send(contact); } diff --git a/src/routes/entity.js b/src/routes/entity.js index 5da950fc..d8ccc889 100644 --- a/src/routes/entity.js +++ b/src/routes/entity.js @@ -1,5 +1,5 @@ import { Router } from 'express'; -import validator from 'validator'; +// import validator from 'validator'; import utils from '../utils'; const router = new Router(); @@ -9,7 +9,6 @@ router.get('/', async (req, res) => { try { if (await utils.validateToken(req, res)) { const entities = await req.context.models.Entity.findAll({ - attributes: ['id', 'name', 'updatedAt'] }); return res.send({ @@ -33,8 +32,7 @@ router.get('/:entity_id', async (req, res) => { const entity = await req.context.models.Entity.findOne({ where: { id: req.params.entity_id - }, - attributes: ['id', 'name', 'email', 'phone', 'createdAt', 'updatedAt'] + } }); entity.dataValues.contacts = [] @@ -64,10 +62,18 @@ router.post('/', async (req, res) => { try { if (await utils.validateToken(req, res)) { if (req.body.name !== undefined) { - const { name, phone, email } = req.body; - console.log(email) + let { name, phone, email, checkIn } = req.body; + + if (!checkIn) { + checkIn = { + _meta: { + total: 0 + }, + checkIns: [] + } + } - const entity = await req.context.models.Entity.create({ name, email, phone }); + const entity = await req.context.models.Entity.create({ name, email, phone, checkIn }); return res.send(entity.id + ' created'); } } @@ -83,21 +89,48 @@ router.post('/', async (req, res) => { router.put('/', async (req, res) => { try { if (await utils.validateToken(req, res)) { - const { id, name, phone, email } = req.body; + let { id, name, phone, email, checkIn } = req.body; /** @todo validate emails */ // Validating emails // if (await !utils.validateEmails(email)) res.status(400).send('Invalid input'); - - const entity = await req.context.models.Entity.findOne({ + + let entity = await req.context.models.Entity.findOne({ where: { id: id } }); - - entity.name = name; - entity.phone = phone; - entity.email = email; + + entity.name = (name) ? name : entity.name; + entity.phone = (phone) ? phone : entity.phone; + entity.email = (email) ? email : entity.email; + /** @todo validate checkIn JSON */ + if (entity.checkIn === null && checkIn) { + const checkIns = { + _meta: { + total: 1 + }, + checkIns: [ + checkIn + ] + } + + entity.checkIn = checkIns; + } else if (entity.checkIn !== null && checkIn) { + let checkIns = entity.checkIn.checkIns; + checkIns.push(checkIn); + let total = entity.checkIn._meta.total + 1 + + const update = { + _meta: { + total: total + }, + checkIns: checkIns + } + + entity.checkIn = update; + } + entity.updatedAt = new Date(); await entity.save(); @@ -124,7 +157,8 @@ router.delete('/:entity_id', async (req, res) => { return res.send(req.params.entity_id + ' deleted'); } throw new Error('Invalid input'); - } catch { + } catch (e) { + console.error(e); res.status(400).send('Invalid input'); } }); diff --git a/src/routes/user-role.js b/src/routes/user-role.js index 3c7542fb..c6ef3219 100644 --- a/src/routes/user-role.js +++ b/src/routes/user-role.js @@ -9,7 +9,6 @@ router.get('/', async (req, res) => { try { if (await utils.validateToken(req, res)) { const roles = await req.context.models.UserRole.findAll({ - attributes: ['id', 'role', 'description', 'createdAt', 'updatedAt'] }); return res.send(roles); } @@ -27,8 +26,7 @@ router.get('/:role', async (req, res) => { const role = await req.context.models.UserRole.findOne({ where: { role: req.params.role - }, - attributes: ['id', 'role', 'description'] + } }); return res.send(role); }