-
Notifications
You must be signed in to change notification settings - Fork 5
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Implementation for user comment notification #54
base: develop
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -74,7 +74,8 @@ | |
], | ||
"reporter": [ | ||
"lcov", | ||
"text" | ||
"text", | ||
"text-summary" | ||
], | ||
"cache": false, | ||
"require": [ | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
/* 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 appBaseURL = process.env['APP_BASE_PATH']; | ||
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: `${appBaseURL}/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}); | ||
|
||
if(requester_id == currentUserUUID){ | ||
res.write(`event: comment\n`); | ||
res.write(`data: ${JSON.stringify({ | ||
message: comment.message, | ||
link: `${appBaseURL}/trips/${trip_uuid}` | ||
})}\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.
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', | ||
|
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) => { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 'Sequelize' is defined but never used no-unused-vars |
||
return queryInterface.bulkDelete('Notifications', null, {}); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Expected indentation of 4 spaces but found 6 indent |
||
} | ||
}; |
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' | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Missing semicolon semi |
||
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; |
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'; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. A space is required after '{' object-curly-spacing |
||
import NotificationRepository from '../src/repositories/NotificationRepository'; | ||
import tripRepository from '../src/repositories/TripRequestRepository'; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 'tripRepository' is defined but never used no-unused-vars |
||
|
||
chai.use(chaiHttp); | ||
chai.use(sinonChai); | ||
|
||
describe('Comment notification', () => { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Block must not be padded by blank lines padded-blocks |
||
|
||
before(()=>{ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Missing space after => arrow-spacing |
||
sinon.spy(res, 'writeHead'); | ||
sinon.spy(res, 'write'); | ||
}); | ||
|
||
after(()=>{ sinon.restore(); }); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Missing space after => arrow-spacing |
||
|
||
it('it should set headers should be set', ()=>{ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Missing space after => arrow-spacing |
||
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', ()=>{ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Block must not be padded by blank lines padded-blocks |
||
|
||
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', ()=>{ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Missing space after => arrow-spacing |
||
sinon.stub(NotificationRepository, 'getAll').throws(); | ||
expect(CommentNotificationController.create).throws | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Missing semicolon semi |
||
NotificationRepository.getAll.restore(); | ||
}); | ||
}); |
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(); | ||
}); | ||
|
||
}); | ||
}); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
'Sequelize' is defined but never used no-unused-vars