-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feature(create-notifications):create notifications for travel requests
[Finishes #167727462]
- Loading branch information
1 parent
49c24c7
commit 54c1417
Showing
14 changed files
with
507 additions
and
121 deletions.
There are no files selected for viewing
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,59 @@ | ||
import models from '../db/models'; | ||
import Response from '../utils/response.utils'; | ||
import sender from '../services/email.service'; | ||
import logger from '../logs/winston'; | ||
|
||
const { Notification, User } = models; | ||
|
||
/** | ||
* This class creates the comment controller | ||
*/ | ||
export default class NotificationController { | ||
/** | ||
* Handles creating of new notification | ||
* @param {object} notificationData the data for notification | ||
* @param {object} payload the payload of the email to be sent | ||
* @returns {boolean} true | ||
*/ | ||
static async createNotification(notificationData, payload) { | ||
try { | ||
await Notification.create({ | ||
...notificationData | ||
}, { returning: true }); | ||
const notificationStatus = await NotificationController | ||
.checkNotificationStatus(notificationData.receiver_id); | ||
if (notificationStatus) await sender.sendEmail(process.env.SENDER_EMAIL, payload.manager_email, 'travel_request_notification', payload); | ||
return true; | ||
} catch (error) { | ||
logger.error(error); | ||
} | ||
} | ||
|
||
/** | ||
* @param {string} userId The notification details | ||
* @returns {boolean} true or false | ||
*/ | ||
static async checkNotificationStatus(userId) { | ||
try { | ||
const { email_notification } = await User.findOne({ where: { id: userId } }); | ||
return email_notification; | ||
} catch (error) { | ||
logger.error(error); | ||
} | ||
} | ||
|
||
/** | ||
* Handles getting a manager's notification | ||
* @param {ServerRequest} req | ||
* @param {ServerResponse} res | ||
* @returns {ServerResponse} response | ||
*/ | ||
static async getNotifications(req, res) { | ||
try { | ||
const result = await Notification.findAll({ attributes: ['id', 'subject', 'notification', 'request_id'], where: { receiver_id: req.body.user_id, read: false } }); | ||
return Response.Success(res, result, 200); | ||
} catch (error) { | ||
return Response.InternalServerError(res, 'Could not populate 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
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,11 @@ | ||
import express from 'express'; | ||
|
||
import NotificationController from '../../controllers/notification.controller'; | ||
import Auth from '../../middlewares/auth.middleware'; | ||
|
||
const router = express.Router(); | ||
|
||
/* Notification Routes Here */ | ||
router.get('/notifications', Auth.verifyToken, NotificationController.getNotifications); | ||
|
||
export default router; |
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,29 @@ | ||
import chai from 'chai'; | ||
import chaiHttp from 'chai-http'; | ||
import sinon from 'sinon'; | ||
import Sinonchai from 'sinon-chai'; | ||
import app from '../index'; | ||
import EmailService from '../services/email.service'; | ||
|
||
chai.use(Sinonchai); | ||
chai.use(chaiHttp); | ||
chai.should(); | ||
|
||
const { expect } = chai; | ||
|
||
describe('/Email service', () => { | ||
|
||
it('fakes server email service', (done) => { | ||
const req = { body: {} }; | ||
const res = { | ||
status() { }, | ||
send() { } | ||
}; | ||
|
||
sinon.stub(res, 'status').returnsThis(); | ||
|
||
EmailService.sendEmail(req, res); | ||
(res.status).should.have.callCount(0); | ||
done(); | ||
}); | ||
}); |
Oops, something went wrong.