Skip to content
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
5 changes: 4 additions & 1 deletion sequelize/migrations/04-create-demo-entity.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,10 @@ module.exports = {
},
email: {
type: Sequelize.JSON,
},
},
checkIn: {
type: Sequelize.JSON,
},
createdAt: {
allowNull: false,
type: Sequelize.DATE
Expand Down
27 changes: 27 additions & 0 deletions sequelize/migrations/05-create-demo-check-in.js
Original file line number Diff line number Diff line change
@@ -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');
}
};
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ module.exports = {
down: function(queryInterface, Sequelize) {
return queryInterface.removeColumn(
'Contacts',
'user_id'
'UserId'
);
}
}
26 changes: 26 additions & 0 deletions sequelize/migrations/07-add-demo-entity-contact.js
Original file line number Diff line number Diff line change
@@ -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'
);
}
}
File renamed without changes.
27 changes: 18 additions & 9 deletions src/models/contact.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 : {
Expand All @@ -33,6 +41,7 @@ const contact = (sequelize, DataTypes) => {

Contact.associate = models => {
Contact.belongsTo(models.User);
Contact.belongsTo(models.Entity);
}

Contact.findById = async (id) => {
Expand All @@ -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 }
Expand All @@ -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;
};

Expand Down
17 changes: 17 additions & 0 deletions src/models/entity.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,19 @@ const entity = (sequelize, DataTypes) => {
},
email: {
type: DataTypes.JSON,
},
checkIn: {
type: DataTypes.JSON,
}
},
{
schema: process.env.DATABASE_SCHEMA
});

Entity.associate = models => {
Entity.hasMany(models.Contact);
}

Entity.findById = async (id) => {
const entity = await Entity.findOne({
where: { id }
Expand All @@ -40,6 +47,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;
};

Expand Down
2 changes: 1 addition & 1 deletion src/models/user.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
28 changes: 13 additions & 15 deletions src/routes/contact.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Router } from 'express';
import validator from 'validator';
// import validator from 'validator';
import utils from '../utils';

const router = new Router();
Expand All @@ -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', 'email', 'phone', 'createdAt', 'updatedAt']
});
return res.send({
_meta: {
Expand All @@ -33,7 +32,6 @@ router.get('/:contact_id', async (req, res) => {
where: {
id: req.params.contact_id
},
attributes: ['id', 'name', 'email', 'phone', 'createdAt', 'updatedAt']
});
return res.send(contact);
}
Expand All @@ -49,10 +47,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');
}
}
Expand All @@ -68,22 +64,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();
Expand All @@ -110,7 +107,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');
}
});
Expand Down
81 changes: 65 additions & 16 deletions src/routes/entity.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Router } from 'express';
import validator from 'validator';
// import validator from 'validator';
import utils from '../utils';

const router = new Router();
Expand All @@ -9,8 +9,8 @@ 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']
});

return res.send({
_meta: {
total: entities.length
Expand All @@ -32,14 +32,27 @@ 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 = []

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');
}
});
Expand All @@ -49,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');
}
}
Expand All @@ -68,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();
Expand All @@ -109,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');
}
});
Expand Down
Loading