Skip to content

Commit

Permalink
feature/send-notifications
Browse files Browse the repository at this point in the history
- implement controller for ending notifications when an event is cancelled
- install nodemailer
- write custom functions for handling email notification
- modify the user database table
  • Loading branch information
Billmike committed Apr 18, 2018
1 parent ea836e9 commit dc6ec45
Show file tree
Hide file tree
Showing 14 changed files with 262 additions and 15 deletions.
3 changes: 3 additions & 0 deletions .env.sample
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
SECRET=
EMAIL_ADDRESS=
PASSWORD=
108 changes: 108 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,11 @@
"helmet": "^3.12.0",
"jest": "^22.4.3",
"jsonwebtoken": "^8.2.1",
"libphonenumber-js": "^1.1.10",
"lodash": "^4.17.5",
"morgan": "^1.9.0",
"nexmo": "^2.2.0",
"nodemailer": "^4.6.4",
"pg": "^7.4.1",
"pg-hstore": "^2.3.2",
"sequelize": "^4.37.6"
Expand Down
30 changes: 23 additions & 7 deletions server/controllers/event.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import db from '../models';
import mailer from './mailer';
import serverError from '../errorHandler/serverError';
import validateAddEvent from '../validators/validateAddEvent';

const { Event, Center } = db;
const { Event, Center, User } = db;

class Events {
static addEvent(request, response) {
Expand Down Expand Up @@ -93,6 +94,7 @@ class Events {
});
});
}

static deleteEvent(request, response) {
Event.findById(request.params.eventId)
.then((event) => {
Expand All @@ -101,17 +103,31 @@ class Events {
message: 'No event found.'
});
}
if (event.organizer != request.userDetails.id) {
if (event.organizer != request.userDetails.id
&& request.userDetails.username !== 'adminuser') {
return response.status(401).json({
message: 'You do not have the privilege to modify this resource'
});
}
return event.destroy().then(() => {
response.status(200).json({
message: 'Event successfully deleted.',
eventDetails: event
User.findById(event.organizer)
.then((foundUser) => {
return event.destroy().then(() => {
const mailOptions = {
from: process.env.EMAIL_ADDRESS,
to: foundUser.dataValues.email,
subject: 'Your event has been cancelled'
};
response.status(200).json({
message: 'Event successfully deleted.',
eventDetails: event
});
mailer.sendMail(mailOptions, (err) => {
if (err) {
return err;
}
});
});
});
});
}).catch(() => {
return response.status(500).json({
message: serverError
Expand Down
15 changes: 15 additions & 0 deletions server/controllers/mailer.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import nodemailer from 'nodemailer';

const mailer = nodemailer.createTransport({
service: 'Gmail',
host: 'smtp.gmail.com',
port: 587,
secure: false,
requireTLS: true,
auth: {
user: process.env.EMAIL_ADDRESS,
pass: process.env.PASSWORD,
}
});

export default mailer;
10 changes: 7 additions & 3 deletions server/controllers/user.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,15 @@ class Users {
User.create({
username: request.body.username,
email: request.body.email,
password: hashedPassword
password: hashedPassword,
phoneNumber: request.body.phoneNumber
})
.then((newUser) => {
const userToken = jwt.sign(
{
id: newUser.id,
username: newUser.username
username: newUser.username,
email: newUser.email
},
process.env.SECRET,
{ expiresIn: '10h' }
Expand All @@ -46,6 +48,7 @@ class Users {
});
})
.catch((error) => {
console.log(error);
if (error.errors[0].message === 'username must be unique') {
return response.status(409).json({
message: 'This username is already taken'
Expand Down Expand Up @@ -84,7 +87,8 @@ class Users {
const userToken = jwt.sign(
{
id: user.id,
username: user.username
username: user.username,
email: user.email
},
process.env.SECRET,
{ expiresIn: '10h' }
Expand Down
26 changes: 26 additions & 0 deletions server/migrations/20180418075722-add-phone-number.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
'use strict';

module.exports = {
up: (queryInterface, Sequelize) => {
/*
Add altering commands here.
Return a promise to correctly handle asynchronicity.
Example:
return queryInterface.createTable('users', { id: Sequelize.INTEGER });
*/
return queryInterface.addColumn('Users', 'phoneNumber', {
type: Sequelize.INTEGER
});
},

down: (queryInterface, Sequelize) => {
/*
Add reverting commands here.
Return a promise to correctly handle asynchronicity.
Example:
return queryInterface.dropTable('users');
*/
}
};
25 changes: 25 additions & 0 deletions server/migrations/20180418081732-dropPhone.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
'use strict';

module.exports = {
up: (queryInterface, Sequelize) => {
/*
Add altering commands here.
Return a promise to correctly handle asynchronicity.
Example:
return queryInterface.createTable('users', { id: Sequelize.INTEGER });
*/
return queryInterface.removeColumn('Users', 'phoneNumber');
},


down: (queryInterface, Sequelize) => {
/*
Add reverting commands here.
Return a promise to correctly handle asynchronicity.
Example:
return queryInterface.dropTable('users');
*/
}
};
26 changes: 26 additions & 0 deletions server/migrations/20180418081852-readdphone.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
'use strict';

module.exports = {
up: (queryInterface, Sequelize) => {
/*
Add altering commands here.
Return a promise to correctly handle asynchronicity.
Example:
return queryInterface.createTable('users', { id: Sequelize.INTEGER });
*/
return queryInterface.addColumn('Users', 'phoneNumber', {
type: Sequelize.BIGINT
});
},

down: (queryInterface, Sequelize) => {
/*
Add reverting commands here.
Return a promise to correctly handle asynchronicity.
Example:
return queryInterface.dropTable('users');
*/
}
};
3 changes: 3 additions & 0 deletions server/models/user.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ module.exports = (sequelize, DataTypes) => {
isEmail: true
}
},
phoneNumber: {
type: DataTypes.INTEGER
},
password: {
allowNull: false,
type: DataTypes.STRING
Expand Down
Loading

0 comments on commit dc6ec45

Please sign in to comment.