Skip to content

Commit

Permalink
feature(create notifications for new travel requests): [finishes #167…
Browse files Browse the repository at this point in the history
…727632]
  • Loading branch information
chrismeeky committed Sep 9, 2019
1 parent 3aae5b1 commit 5b22e46
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 20 deletions.
6 changes: 3 additions & 3 deletions src/controllers/RequestController.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@ class RequestController {
if (dataValues.id) {
Notification.sendNewRequestNotifications(res, {
id,
requestId: dataValues.id,
type: 'single trip',
dataValues,
});
HelperMethods.requestSuccessful(res, {
success: true,
Expand Down Expand Up @@ -61,8 +61,8 @@ class RequestController {
if (dataValues.id) {
Notification.sendNewRequestNotifications(res, {
id,
requestId: dataValues.id,
type: 'return trip',
dataValues,
});
return HelperMethods.requestSuccessful(res, {
success: true,
Expand Down Expand Up @@ -266,8 +266,8 @@ class RequestController {
if (dataValues.id) {
Notification.sendNewRequestNotifications(res, {
id,
requestId: dataValues.id,
type: 'multi-city trip',
dataValues,
});
return HelperMethods.requestSuccessful(res, {
success: true,
Expand Down
8 changes: 8 additions & 0 deletions src/test/integrationTests/requestController.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,14 @@ describe('Integration tests for the request controller', () => {
nonLineManagerToken = nonLineManager.body.data.userDetails.token;
});

describe('Test notification controller', () => {
it('should serve a html file', async () => {
const response = await chai.request(app)
.get('/api/v1/managerNotification/3821b930-ce48-4ac8-9ddf-ee3bf7980d08');
expect(response.headers['content-type']).to.equal('text/html; charset=utf-8');
});
});

describe('Authentication tests', () => {
it('should return an error if the authentication token is missing', async () => {
const response = await chai
Expand Down
6 changes: 6 additions & 0 deletions src/test/unitTests/sendEmail.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,12 @@ describe('Utility to send emails', () => {
expect(response).to.equal(true);
stubEmailSender.restore();
});
it('should send verification email after new trip request', async () => {
const stubEmailSender = sinon.stub(SendEmail, 'emailSender').returns(true);
const response = await SendEmail.sendRequestNotification('jideajayi11@gmail.com');
expect(response).to.equal(true);
stubEmailSender.restore();
});
it('should send email when passed the email details', async () => {
const details = {
email: 'jideajayi11@gmail.com',
Expand Down
24 changes: 15 additions & 9 deletions src/utils/notifications.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,19 +16,25 @@ class Notification {
*/
static async sendNewRequestNotifications(res, notificationParams) {
try {
const { id, requestId, type } = notificationParams;
const { id, dataValues, type } = notificationParams;
const user = await User.findByPk(id);
const manager = await User.findByPk(user.lineManager);
const { firstName, email, isSubscribed } = manager.dataValues;
const message = `${user.username} created a travel request`;
io.emit(user.lineManager, message);
const manager = await User.findByPk(user.lineManager);
const { firstName, email } = manager.dataValues;
if (user.isSubscribed) {
await SendEmail.sendRequestNotification(
email,
firstName,
requestId,

if (isSubscribed) {
await SendEmail.sendRequestNotification({
managerEmail: email,
managerName: firstName,
requester: user.firstName,
requestId: dataValues.id,
origin: dataValues.origin,
destination: dataValues.destination,
flightDate: dataValues.flightDate,
reason: dataValues.reason,
type
);
});
}

await Message.create({
Expand Down
24 changes: 16 additions & 8 deletions src/utils/sendEmail.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,15 +58,15 @@ class SendEmail {
}

/**
* @param {string} email - email address to send the message to
* @param {string} emailParams - email address to send the message to
* @param {string} firstName - User's first name
* @param {string} requestId - The Id of the request
* @param {string} requestType - multicity || single Trip || Return Trip
* @returns {boolean} specifies if the email was sent successfully
*/
static sendRequestNotification(email, firstName, requestId, requestType) {
static sendRequestNotification(emailParams) {
const details = {
email,
email: emailParams.managerEmail,
subject: 'New pending request - BareFoot-Nomad',
html: `'<div style="width: 90%; margin: 5em auto;
box-shadow: 0 0 10px rgba(0,0,0,.9);">
Expand All @@ -76,23 +76,31 @@ class SendEmail {
<h2 style="text-align: center; color: white;
padding-top: 10px;">Barefoot Nomad</h2>
</div>
<h4 style="text-align: center">Hi! ${firstName}</h4>
<h1 style="text-align: center">Hi! ${emailParams.managerName}</h1>
</div>
<div style=" padding: 0px 20px 20px 20px">
<div>
<p>A user requested for a ${requestType}. Your action is needed</p>
<p>Click on the button below to approve or reject the request.</p>
<p>There is a new request from user: <strong>${emailParams.requester}</strong> that needs your action</p>
<p>Below are the details of the request:</p></br>
<ul>
<li>Origin: ${emailParams.origin}</li>
<li>Destination: ${emailParams.destination}</li>
<li>Reason: ${emailParams.reason}</li>
<li>Type: ${emailParams.type}</li>
<li>Flight Date: ${emailParams.flightDate}</li>
<li>Requester: ${emailParams.requester}</li>
</ul>
<button style="color: white; background-color: #1AC124;
border: none; border-radius: 10px; text-align: center;
padding: 10px;">
<a href="${baseUrl}/request/approve/${requestId}"
<a href="${baseUrl}/request/approve/${emailParams.requestId}"
style="text-decoration: none;
color: white;">Approve Request</a></button>
<button style="color: white; background-color: #DE4727;
border: none; border-radius: 10px; text-align: center;
padding: 10px;">
<a href="${baseUrl}/request/reject/${requestId}"
<a href="${baseUrl}/request/reject/${emailParams.requestId}"
style="text-decoration: none;
color: white;">Reject Request</a></button>
</div>
Expand Down

0 comments on commit 5b22e46

Please sign in to comment.