Skip to content

Commit d2de8b4

Browse files
committed
Moving checkIn to the Entity object.
1 parent 6c61de5 commit d2de8b4

File tree

11 files changed

+59
-170
lines changed

11 files changed

+59
-170
lines changed

sequelize/migrations/04-create-demo-entity.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,10 @@ module.exports = {
1717
},
1818
email: {
1919
type: Sequelize.JSON,
20-
},
20+
},
21+
checkIn: {
22+
type: Sequelize.JSON,
23+
},
2124
createdAt: {
2225
allowNull: false,
2326
type: Sequelize.DATE

sequelize/migrations/07-add-demo-check-in-contact.js

Lines changed: 0 additions & 26 deletions
This file was deleted.

sequelize/migrations/08-add-demo-check-in-user.js

Lines changed: 0 additions & 26 deletions
This file was deleted.

sequelize/migrations/10-add-demo-check-in-entity.js

Lines changed: 0 additions & 26 deletions
This file was deleted.

src/models/check-in.js

Lines changed: 0 additions & 69 deletions
This file was deleted.

src/models/entity.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,9 @@ const entity = (sequelize, DataTypes) => {
1818
},
1919
email: {
2020
type: DataTypes.JSON,
21+
},
22+
checkIn: {
23+
type: DataTypes.JSON,
2124
}
2225
},
2326
{

src/models/user.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ const user = (sequelize, DataTypes) => {
7171
* @return {Boolean}
7272
*/
7373
User.validateToken = async token => {
74-
const expiry = jwt.decode(token).exp;
74+
const expiry = jwt.decode(token);
7575
const now = new Date();
7676

7777
if (now.getTime() < expiry * 1000) {

src/routes/contact.js

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { Router } from 'express';
2-
import validator from 'validator';
2+
// import validator from 'validator';
33
import utils from '../utils';
44

55
const router = new Router();
@@ -9,7 +9,6 @@ router.get('/', async (req, res) => {
99
try {
1010
if (await utils.validateToken(req, res)) {
1111
const contacts = await req.context.models.Contact.findAll({
12-
// attributes: ['id', 'name', 'updatedAt']
1312
});
1413
return res.send({
1514
_meta: {
@@ -33,7 +32,6 @@ router.get('/:contact_id', async (req, res) => {
3332
where: {
3433
id: req.params.contact_id
3534
},
36-
// attributes: ['id', 'name', 'email', 'phone', 'UserId', 'createdAt', 'updatedAt']
3735
});
3836
return res.send(contact);
3937
}

src/routes/entity.js

Lines changed: 49 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { Router } from 'express';
2-
import validator from 'validator';
2+
// import validator from 'validator';
33
import utils from '../utils';
44

55
const router = new Router();
@@ -9,7 +9,6 @@ router.get('/', async (req, res) => {
99
try {
1010
if (await utils.validateToken(req, res)) {
1111
const entities = await req.context.models.Entity.findAll({
12-
attributes: ['id', 'name', 'updatedAt']
1312
});
1413

1514
return res.send({
@@ -33,8 +32,7 @@ router.get('/:entity_id', async (req, res) => {
3332
const entity = await req.context.models.Entity.findOne({
3433
where: {
3534
id: req.params.entity_id
36-
},
37-
attributes: ['id', 'name', 'email', 'phone', 'createdAt', 'updatedAt']
35+
}
3836
});
3937
entity.dataValues.contacts = []
4038

@@ -64,10 +62,18 @@ router.post('/', async (req, res) => {
6462
try {
6563
if (await utils.validateToken(req, res)) {
6664
if (req.body.name !== undefined) {
67-
const { name, phone, email } = req.body;
68-
console.log(email)
65+
let { name, phone, email, checkIn } = req.body;
66+
67+
if (!checkIn) {
68+
checkIn = {
69+
_meta: {
70+
total: 0
71+
},
72+
checkIns: []
73+
}
74+
}
6975

70-
const entity = await req.context.models.Entity.create({ name, email, phone });
76+
const entity = await req.context.models.Entity.create({ name, email, phone, checkIn });
7177
return res.send(entity.id + ' created');
7278
}
7379
}
@@ -83,21 +89,48 @@ router.post('/', async (req, res) => {
8389
router.put('/', async (req, res) => {
8490
try {
8591
if (await utils.validateToken(req, res)) {
86-
const { id, name, phone, email } = req.body;
92+
let { id, name, phone, email, checkIn } = req.body;
8793

8894
/** @todo validate emails */
8995
// Validating emails
9096
// if (await !utils.validateEmails(email)) res.status(400).send('Invalid input');
91-
92-
const entity = await req.context.models.Entity.findOne({
97+
98+
let entity = await req.context.models.Entity.findOne({
9399
where: {
94100
id: id
95101
}
96102
});
97-
98-
entity.name = name;
99-
entity.phone = phone;
100-
entity.email = email;
103+
104+
entity.name = (name) ? name : entity.name;
105+
entity.phone = (phone) ? phone : entity.phone;
106+
entity.email = (email) ? email : entity.email;
107+
/** @todo validate checkIn JSON */
108+
if (entity.checkIn === null && checkIn) {
109+
const checkIns = {
110+
_meta: {
111+
total: 1
112+
},
113+
checkIns: [
114+
checkIn
115+
]
116+
}
117+
118+
entity.checkIn = checkIns;
119+
} else if (entity.checkIn !== null && checkIn) {
120+
let checkIns = entity.checkIn.checkIns;
121+
checkIns.push(checkIn);
122+
let total = entity.checkIn._meta.total + 1
123+
124+
const update = {
125+
_meta: {
126+
total: total
127+
},
128+
checkIns: checkIns
129+
}
130+
131+
entity.checkIn = update;
132+
}
133+
101134
entity.updatedAt = new Date();
102135

103136
await entity.save();
@@ -124,7 +157,8 @@ router.delete('/:entity_id', async (req, res) => {
124157
return res.send(req.params.entity_id + ' deleted');
125158
}
126159
throw new Error('Invalid input');
127-
} catch {
160+
} catch (e) {
161+
console.error(e);
128162
res.status(400).send('Invalid input');
129163
}
130164
});

0 commit comments

Comments
 (0)