-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feature(notitfication):user comment notification
Implementation of user comment notification. Users should receive comment notification when comments are created on thier trip request. Finished[167728039]
- Loading branch information
Showing
10 changed files
with
224 additions
and
88 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 |
---|---|---|
|
@@ -74,7 +74,8 @@ | |
], | ||
"reporter": [ | ||
"lcov", | ||
"text" | ||
"text", | ||
"text-summary" | ||
], | ||
"cache": false, | ||
"require": [ | ||
|
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,101 @@ | ||
/* eslint-disable */ | ||
import Model from '../models'; | ||
import { setHeaders, addHook } from '../utils/NotificationHelpers'; | ||
import tripRepository from '../repositories/TripRequestRepository'; | ||
import NotificationRepository from '../repositories/NotificationRepository'; | ||
import dotenv from 'dotenv'; | ||
|
||
dotenv.config(); | ||
|
||
const { Comment } = Model; | ||
|
||
/** | ||
* @description Comment controller | ||
*/ | ||
class CommentNotifications { | ||
/** | ||
* @param {Object} req - HTTP request object | ||
* | ||
* @param {Object} res - HTTP response object | ||
* | ||
* @param {Function} next - Function to trigger next middleware | ||
* | ||
* @return {Object} Return success message and account creation status | ||
*/ | ||
|
||
constructor() { | ||
this.model = Comment; | ||
} | ||
|
||
/** | ||
* @description Add notifications for comment model | ||
* | ||
* @param {Object} req - HTTP request object | ||
* | ||
* @param {Object} res - HTTP response object | ||
* | ||
* @param {Function} next - Function to execute next function | ||
* | ||
* @returns {Void} Nothing is been returned. Just sets hooks to a given model | ||
*/ | ||
async create (req, res, next) { | ||
|
||
const appURL = process.env['APP_BASE_PATH'] || 'localhost:3000'; | ||
let tripsObj = []; | ||
const currentUserUUID = req.userData.uuid; | ||
|
||
// Set HTTP headers for SSE | ||
setHeaders(req, res); | ||
|
||
try { | ||
// Get all unread notifications | ||
const notifs = await NotificationRepository.getAll({ | ||
user_uuid: currentUserUUID, | ||
status: 'unread', | ||
notification_type: 'comment' | ||
}); | ||
|
||
// Construct comment summary and link to comment. | ||
notifs.reduce((trips, currentTrip)=>{ | ||
return tripsObj.push({ | ||
uuid: currentTrip.uuid, | ||
message: currentTrip.message, | ||
link: `${appURL}/trips/${currentTrip.uuid}` | ||
}) | ||
}, tripsObj); | ||
|
||
// Send unread comments to user | ||
res.write('event: comment\n'); | ||
res.write(`data: ${JSON.stringify(tripsObj)}\n\n`); | ||
|
||
// Method to execute for every comment creation | ||
const helper = async (comment) => { | ||
const {dataValues: {trip_request_uuid}} = comment; | ||
|
||
const { | ||
dataValues: { | ||
user_uuid: requester_id, | ||
uuid: trip_uuid | ||
} | ||
} = await tripRepository.findById({uuid: trip_request_uuid}); | ||
|
||
console.log("inside error zone", requester_id); | ||
if(requester_id == currentUserUUID){ | ||
res.write(`event: comment\n`); | ||
res.write(`data: link: https://localhost:3000/trips/${trip_uuid}\n`); | ||
res.write(`data: ${JSON.stringify({ | ||
message: comment.message | ||
})}\n\n`); | ||
} | ||
} | ||
|
||
// Add realtime notification. Triggered after new comment is created. | ||
addHook(Comment, 'afterCreate', 'notification', helper); | ||
|
||
} catch (error) { | ||
next(error); | ||
} | ||
} | ||
} | ||
|
||
export default new CommentNotifications(); |
This file was deleted.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -25,6 +25,16 @@ export default { | |
created_at: Sequelize.literal('NOW()'), | ||
updated_at: Sequelize.literal('NOW()') | ||
}, | ||
{ | ||
uuid: uuid(), | ||
name: 'Wokoro Samuel Douye', | ||
email: '[email protected]', | ||
role: 'Requester', | ||
is_verified: true, | ||
password: hashPassword('Samsizzy777'), | ||
created_at: Sequelize.literal('NOW()'), | ||
updated_at: Sequelize.literal('NOW()') | ||
}, | ||
{ | ||
uuid: uuid(), | ||
name: 'Makaraba Blessing', | ||
|
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 @@ | ||
import uuid from 'uuid'; | ||
|
||
'use strict'; | ||
|
||
module.exports = { | ||
up: (queryInterface, Sequelize) => { | ||
const notificationData = [ | ||
{ | ||
uuid: uuid(), | ||
user_uuid: 'abef6009-48be-4b38-80d0-b38c1bc39922', | ||
message: 'Can i have my trip request approved?', | ||
status: 'unread', | ||
notification_type: 'comment', | ||
created_at: new Date(), | ||
updated_at: new Date() | ||
}, | ||
{ | ||
uuid: uuid(), | ||
user_uuid: 'abef6009-48be-4b38-80d0-b38c1bc39922', | ||
message: 'Please i need my trip request approved', | ||
status: 'unread', | ||
notification_type: 'comment', | ||
created_at: new Date(), | ||
updated_at: new Date() | ||
} | ||
]; | ||
return queryInterface.bulkInsert('Notifications', notificationData, {}); | ||
}, | ||
|
||
down: (queryInterface, Sequelize) => { | ||
return queryInterface.bulkDelete('Notifications', null, {}); | ||
} | ||
}; |
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 |
---|---|---|
@@ -1,18 +1,11 @@ | ||
import { Router } from 'express'; | ||
|
||
import userNotificationController from '../controllers/ExampleUserNotificationController'; | ||
import authenticateUser from '../middlewares/authenticateUser' | ||
import commentNotificationController from '../controllers/CommentNotificationController'; | ||
|
||
const notificationRoutes = Router(); | ||
|
||
// Live notification endpoint for user creation. | ||
notificationRoutes.get('/user', userNotificationController.create); | ||
|
||
// Example Live notification endpoint for request. | ||
|
||
// notificationRoutes.get( | ||
// '/request', | ||
// authenticateUser, | ||
// notificationController.requestNotifications | ||
// ); | ||
notificationRoutes.get('/comment', authenticateUser, commentNotificationController.create); | ||
|
||
export default notificationRoutes; |
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,56 @@ | ||
import { describe, it } from 'mocha'; | ||
import sinon from 'sinon'; | ||
import sinonChai from 'sinon-chai'; | ||
import chai, { expect } from 'chai'; | ||
import chaiHttp from 'chai-http'; | ||
import CommentNotificationController from '../src/controllers/CommentNotificationController'; | ||
import {req, res, next} from './mock.data'; | ||
import NotificationRepository from '../src/repositories/NotificationRepository'; | ||
import tripRepository from '../src/repositories/TripRequestRepository'; | ||
|
||
chai.use(chaiHttp); | ||
chai.use(sinonChai); | ||
|
||
describe('Comment notification', () => { | ||
|
||
before(()=>{ | ||
sinon.spy(res, 'writeHead'); | ||
sinon.spy(res, 'write'); | ||
}); | ||
|
||
after(()=>{ sinon.restore(); }); | ||
|
||
it('it should set headers should be set', ()=>{ | ||
CommentNotificationController.create(req, res, next); | ||
expect(res.writeHead).to.be.calledWith(200, { | ||
'Content-Type': 'text/event-stream', | ||
'Cache-Control': 'no-cache', | ||
Connection: 'keep-alive' | ||
}); | ||
expect(res.write).to.be.calledWith('\n'); | ||
}); | ||
|
||
it('it should return unread comments', ()=>{ | ||
|
||
sinon.stub(NotificationRepository, 'getAll').returns([ | ||
{ | ||
uuid: 'abef6009-48be-4b38-80d0-b38c1bc39922', | ||
user_uuid: 'abef6009-48be-4b38-80d0-b38c1bc39922', | ||
message: 'Please i need my trip request approved', | ||
status: 'unread', | ||
notification_type: 'comment', | ||
created_at: new Date(), | ||
updated_at: new Date() | ||
} | ||
]); | ||
CommentNotificationController.create(req, res, next); | ||
expect(res.write.called).to.be.true; | ||
NotificationRepository.getAll.restore(); | ||
}); | ||
|
||
it('it should call next function for server error', ()=>{ | ||
sinon.stub(NotificationRepository, 'getAll').throws(); | ||
expect(CommentNotificationController.create).throws | ||
NotificationRepository.getAll.restore(); | ||
}); | ||
}); |
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 |
---|---|---|
|
@@ -398,5 +398,6 @@ describe('Test Create Trip Request', () => { | |
expect(next.called).to.true; | ||
sinon.restore(); | ||
}); | ||
|
||
}); | ||
}); |