From 5b22e46303368b6a2c3ef56fb7e6ccc6cd14000c Mon Sep 17 00:00:00 2001 From: Chris Date: Sun, 8 Sep 2019 10:35:44 -0400 Subject: [PATCH] feature(create notifications for new travel requests): [finishes #167727632] --- src/controllers/RequestController.js | 6 ++--- .../requestController.test.js | 8 +++++++ src/test/unitTests/sendEmail.test.js | 6 +++++ src/utils/notifications.js | 24 ++++++++++++------- src/utils/sendEmail.js | 24 ++++++++++++------- 5 files changed, 48 insertions(+), 20 deletions(-) diff --git a/src/controllers/RequestController.js b/src/controllers/RequestController.js index 6890391..57c544e 100644 --- a/src/controllers/RequestController.js +++ b/src/controllers/RequestController.js @@ -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, @@ -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, @@ -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, diff --git a/src/test/integrationTests/requestController.test.js b/src/test/integrationTests/requestController.test.js index 26cc3a7..3e41f54 100644 --- a/src/test/integrationTests/requestController.test.js +++ b/src/test/integrationTests/requestController.test.js @@ -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 diff --git a/src/test/unitTests/sendEmail.test.js b/src/test/unitTests/sendEmail.test.js index 4fe5734..861fdd6 100644 --- a/src/test/unitTests/sendEmail.test.js +++ b/src/test/unitTests/sendEmail.test.js @@ -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', diff --git a/src/utils/notifications.js b/src/utils/notifications.js index 9c46b81..2b2c314 100644 --- a/src/utils/notifications.js +++ b/src/utils/notifications.js @@ -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({ diff --git a/src/utils/sendEmail.js b/src/utils/sendEmail.js index 779ba5e..0c07880 100644 --- a/src/utils/sendEmail.js +++ b/src/utils/sendEmail.js @@ -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: `'
@@ -76,23 +76,31 @@ class SendEmail {

Barefoot Nomad

-

Hi! ${firstName}

+

Hi! ${emailParams.managerName}

-

A user requested for a ${requestType}. Your action is needed

-

Click on the button below to approve or reject the request.

+

There is a new request from user: ${emailParams.requester} that needs your action

+

Below are the details of the request:


+
    +
  • Origin: ${emailParams.origin}
  • +
  • Destination: ${emailParams.destination}
  • +
  • Reason: ${emailParams.reason}
  • +
  • Type: ${emailParams.type}
  • +
  • Flight Date: ${emailParams.flightDate}
  • +
  • Requester: ${emailParams.requester}
  • +