-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feature(notifications): manager can get notification
Manager can opt in to certain notification mode(email / in-app) Manager can opt out to certain notification mode [Finishes #169817550]
- Loading branch information
1 parent
b204c16
commit 239733f
Showing
43 changed files
with
1,504 additions
and
52 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
import ResponseService from '../services/response.service'; | ||
import PreferenceService from '../services/preference.service'; | ||
import NotificationService from '../services/notification.service'; | ||
|
||
/** | ||
* @class NotificationController | ||
* @description Handles notifications. | ||
* */ | ||
class NotificationController { | ||
/** | ||
* @param {object} req - Request object | ||
* @param {object} res - Response object | ||
* @returns {object} response | ||
* */ | ||
static async setNotificationMode(req, res) { | ||
if (req.body.isInAppNotification) { // If request is isInAppNotification | ||
await PreferenceService | ||
.updatePreference( | ||
{ userId: req.userData.id }, | ||
{ isInAppNotification: req.body.isInAppNotification } | ||
); | ||
ResponseService.setSuccess(200, 'Successfully updated your in-app notification preferences.'); | ||
return ResponseService.send(res); | ||
} | ||
// If request is isEmailNotification | ||
await PreferenceService | ||
.updatePreference( | ||
{ userId: req.userData.id }, | ||
{ isEmailNotification: req.body.isEmailNotification } | ||
); | ||
ResponseService.setSuccess(200, 'Successfully updated your email notification preferences.'); | ||
return ResponseService.send(res); | ||
} | ||
|
||
/** | ||
* @param {object} req request | ||
* @param {object} res response | ||
* @return {function} requests | ||
*/ | ||
static async getAllNotifications(req, res) { | ||
const data = await NotificationService.getAllNotifications({ userId: req.userData.id }); | ||
ResponseService.setSuccess(200, 'Your Notifications have been retrieved successfully', data); | ||
return ResponseService.send(res); | ||
} | ||
} | ||
export default NotificationController; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
import EventEmmiter from 'events'; | ||
|
||
export default new EventEmmiter(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
const io = require('socket.io')(); | ||
const socketioJwt = require('socketio-jwt'); | ||
|
||
const users = {}; | ||
const startSocket = (server) => { | ||
io.attach(server); | ||
let userName; | ||
|
||
io.sockets | ||
.on('connection', socketioJwt.authorize({ // Verify authorization for token | ||
secret: process.env.SECRET_KEY, | ||
timeout: 15000 // 15 seconds to send the authentication message | ||
})) | ||
.on('authenticated', (socket) => { | ||
socket.emit('success', socket.decoded_token.email); // Emit success message for front-end | ||
socket.on('new-user', (user) => { // save a new user for future needs | ||
userName = user; | ||
users[user] = socket; | ||
io.sockets.emit('user-connected', `${user} has joined.`); | ||
}); | ||
socket.on('private message', (msg) => { | ||
const fromMsg = { from: userName, txt: msg.txt }; | ||
users[msg.to].emit('private message', fromMsg); | ||
}); | ||
socket.on('disconnect', () => { | ||
delete users[userName]; | ||
}); | ||
}); | ||
}; | ||
export { startSocket, io, users }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
const tripEmailBody = (managerNames, notification, unsubscribeUrl, data) => `<body style="margin: 0; padding: 0;"> | ||
<table border="0" cellpadding="0" cellspacing="0" width="900px" style="padding: 0 40px 0 40px; background-color:#f1f2f3;"> | ||
<tr><td align="center" style="background-color:#7041EE; margin: 0 50px 0 50px;"> | ||
<a><p style="color: #ffffff; font-family: Arial, sans-serif; font-size: 32px; line-height: 40px;">Barefoot Nomad<p></a></td> | ||
</tr><tr><td align = "center" style="padding: 0 50px 0 50px;"> | ||
<table border="0" cellpadding="0" cellspacing="0" width="100%" style="background-color:#ffffff; padding: 0 0 0 20px;"> | ||
<tr><td align="left" style="font-family: Arial, sans-serif; font-size: 24px; color: #050505;"> | ||
<p>Dear ${managerNames},</p></td></tr><tr> | ||
<td align="left" style="color: #153643; font-family: Arial, sans-serif; font-size: 16px; line-height: 20px;"> | ||
<p>${notification}</p> | ||
</td></tr> | ||
<tr><td align="left" style="font-family: Arial, sans-serif; font-size: 18px; color: #050505;"> | ||
<p>With the following details:</p></td></tr> | ||
<table style="font-family: arial, sans-serif; width: 70%;"> | ||
<tr style = "background-color: #dddddd;"> | ||
<td style = "border: 1px solid #dddddd; text-align: left; padding: 8px; font-weight:bold;"> From:</td> | ||
<td style = "border: 1px solid #dddddd; text-align: left; padding: 8px;"> ${data.departure}</td> | ||
</tr> | ||
<tr> | ||
<td style = "border: 1px solid #dddddd; text-align: left; padding: 8px; font-weight:bold;"> Destination:</td> | ||
<td style = "border: 1px solid #dddddd; text-align: left; padding: 8px;"> ${data.destination}</td> | ||
</tr> | ||
<tr style = "background-color: #dddddd;"> | ||
<td style = "border: 1px solid #dddddd; text-align: left; padding: 8px; font-weight:bold;"> Departure date:</td> | ||
<td style = "border: 1px solid #dddddd; text-align: left; padding: 8px;"> ${data.travelDate}</td> | ||
</tr> | ||
<tr> | ||
<td style = "border: 1px solid #dddddd; text-align: left; padding: 8px; font-weight:bold;"> Travel reasons:</td> | ||
<td style = "border: 1px solid #dddddd; text-align: left; padding: 8px;">${data.travelReasons}</td> | ||
</tr> | ||
<tr style = "background-color: #dddddd;"> | ||
<td style = "border: 1px solid #dddddd; text-align: left; padding: 8px; font-weight:bold;"> Accomodation:</td> | ||
<td style = "border: 1px solid #dddddd; text-align: left; padding: 8px;"> ${data.accommodation}</td> | ||
</tr> | ||
</table> | ||
<tr><td align="left" style="color: #153643; font-family: Arial, sans-serif; font-size: 16px; line-height: 20px;"> | ||
<p> You are receiving this email because you subscribed to receive email from Barefoot Nomad. </p> | ||
<p> contact <a href=#>support@barefoot.com</a> if you feel this is wrong</p> | ||
<p>Thank you for using Bare Foot Nomad</p> | ||
<p>Don't want to receive such emails from barefootNomad? <a href="${unsubscribeUrl}">Unsubscribe from email notification</a></p> | ||
</td></tr></table></tr><tr> | ||
<td align="center" style="padding: 30px 30px 30px 30px;">© BAREFOOT NOMAD, 2020<br/> | ||
</td></tr></table></body>`; | ||
|
||
export default tripEmailBody; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
import TripNotification from '../../services/trip-notification.service'; | ||
|
||
const NotificationListeners = () => { | ||
TripNotification.sendTripNotification(); | ||
}; | ||
|
||
export default NotificationListeners; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
import ResponseService from '../services/response.service'; | ||
|
||
const transformErrorHandler = (schema, body, res, next) => { | ||
const { error } = schema.validate(body); | ||
|
||
if (error) { | ||
// const errors = []; | ||
const { details } = error; | ||
const errors = details.map(({ message }) => message); | ||
ResponseService.setError(400, errors); | ||
return ResponseService.send(res); | ||
} | ||
next(); | ||
}; | ||
|
||
export default transformErrorHandler; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
import PreferenceService from '../services/preference.service'; | ||
import ResponseService from '../services/response.service'; | ||
|
||
const checkIfOptionExists = async (req, res, next) => { | ||
const prefs = await PreferenceService.findPreferences({ userId: req.userData.id }); | ||
const { dataValues } = prefs; | ||
|
||
if (req.body.isInAppNotification | ||
&& req.body.isInAppNotification.toString() === dataValues.isInAppNotification.toString()) { | ||
ResponseService.setSuccess(200, 'This option has been already set for this notification mode'); | ||
return ResponseService.send(res); | ||
} | ||
if (req.body.isEmailNotification | ||
&& req.body.isEmailNotification.toString() === dataValues.isEmailNotification.toString()) { | ||
ResponseService.setSuccess(200, 'This option has been already set for this notification mode'); | ||
return ResponseService.send(res); | ||
} | ||
next(); | ||
}; | ||
|
||
export default checkIfOptionExists; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
|
||
const notificationMigrations = { | ||
up: (queryInterface, Sequelize) => queryInterface.createTable('Notifications', { | ||
id: { | ||
allowNull: false, | ||
autoIncrement: true, | ||
primaryKey: true, | ||
type: Sequelize.INTEGER | ||
}, | ||
userId: { | ||
type: Sequelize.INTEGER | ||
}, | ||
message: { | ||
type: Sequelize.TEXT | ||
}, | ||
isRead: { | ||
type: Sequelize.BOOLEAN, | ||
defaultValue: false | ||
}, | ||
requestId: { | ||
type: Sequelize.INTEGER | ||
}, | ||
type: Sequelize.STRING, | ||
createdAt: { | ||
allowNull: false, | ||
type: Sequelize.DATE | ||
}, | ||
updatedAt: { | ||
allowNull: false, | ||
type: Sequelize.DATE | ||
} | ||
}), | ||
down: (queryInterface) => queryInterface.dropTable('Notifications') | ||
}; | ||
export default notificationMigrations; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
|
||
const preferenceMigrations = { | ||
up: (queryInterface, Sequelize) => queryInterface.createTable('Preferences', { | ||
id: { | ||
allowNull: false, | ||
autoIncrement: true, | ||
primaryKey: true, | ||
type: Sequelize.INTEGER | ||
}, | ||
userId: { | ||
type: Sequelize.INTEGER | ||
}, | ||
isEmailNotification: { | ||
type: Sequelize.BOOLEAN, | ||
defaultValue: 'false' | ||
}, | ||
isInAppNotification: { | ||
type: Sequelize.BOOLEAN, | ||
defaultValue: 'true' | ||
}, | ||
createdAt: { | ||
allowNull: false, | ||
type: Sequelize.DATE | ||
}, | ||
updatedAt: { | ||
allowNull: false, | ||
type: Sequelize.DATE | ||
} | ||
}), | ||
down: (queryInterface) => queryInterface.dropTable('Preferences') | ||
}; | ||
|
||
export default preferenceMigrations; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
export default (sequelize, DataTypes) => { | ||
const Notifications = sequelize.define('Notifications', { | ||
userId: DataTypes.INTEGER, | ||
message: { | ||
type: DataTypes.STRING, | ||
allowNull: false, | ||
}, | ||
isRead: DataTypes.BOOLEAN, | ||
type: DataTypes.BOOLEAN, | ||
requestId: DataTypes.STRING | ||
}, {}); | ||
Notifications.associate = (models) => { | ||
// associations can be defined here | ||
Notifications.belongsTo(models.Users, { | ||
foreignKey: 'userId', | ||
targetKey: 'id', | ||
}); | ||
}; | ||
return Notifications; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
export default (sequelize, DataTypes) => { | ||
const Preferences = sequelize.define('Preferences', { | ||
userId: DataTypes.INTEGER, | ||
isEmailNotification: DataTypes.STRING, | ||
isInAppNotification: DataTypes.STRING | ||
}, {}); | ||
Preferences.associate = (models) => { | ||
// associations can be defined here | ||
Preferences.belongsTo(models.Users, { | ||
foreignKey: 'userId', | ||
targetKey: 'id', | ||
}); | ||
}; | ||
return Preferences; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.