Skip to content
This repository has been archived by the owner on Jul 20, 2020. It is now read-only.

Commit

Permalink
ft(notification): add notifying user for posted comment
Browse files Browse the repository at this point in the history
- add a send notification function
- add tests for the feature

[Finishes #170947568]
  • Loading branch information
Jean Bien Aime Byiringiro authored and Jean Bien Aime Byiringiro committed Mar 12, 2020
1 parent d618e0a commit af090cf
Show file tree
Hide file tree
Showing 7 changed files with 122 additions and 10 deletions.
22 changes: 22 additions & 0 deletions src/controllers/commentController.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import db from '../models';
import Response from '../utils/ResponseHandler';
import notifService from '../services/notificationService';
import SendNotification from '../utils/sendNotification';

/**
*
Expand All @@ -20,11 +22,31 @@ export default class CommentController {
const { user } = req;
const { comment } = req.body;
const { requestId } = req.params;
const { email } = req.managerDetails;
const newComment = await db.Comments.create({
requestId,
commmentOwner: user.id,
comment,
});
if (user.role === 'manager') {
const notifyUser = await notifService.createNotif(req.request.userId, req.request.email, 'your manager posted a comment', '#');
const content = {
intro: req.__(notifyUser.content),
instruction: req.__('Your Manager commented: %s', newComment.comment),
text: req.__('View comment'),
signature: req.__('signature')
};
await SendNotification.sendNotif(notifyUser, req, content);
} else {
const notifyUser = await notifService.createNotif(req.request.managerId, email, 'your requester posted a comment', '#');
const content = {
intro: req.__(notifyUser.content),
instruction: req.__('Your requester commented: %s', newComment.comment),
text: req.__('View comment'),
signature: req.__('signature')
};
await SendNotification.sendNotif(notifyUser, req, content);
}
return Response.success(res, 200, res.__('Comment is successfully posted'), newComment);
} catch (error) {
return Response.errorResponse(res, 500, res.__('server error'));
Expand Down
8 changes: 6 additions & 2 deletions src/middlewares/protectRoute.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,7 @@ export default class protectRoutes {
*/
static async verifyUser(req, res, next) {
try {
// const { token } = req.headers;
const token = req.headers.token || localStorage.getItem('token');
const { token } = req.headers;
if (!token) {
return Response.errorResponse(res, 401, res.__('No token provided'));
}
Expand Down Expand Up @@ -164,13 +163,18 @@ export default class protectRoutes {
const requestExist = await db.Request.findOne({
where: { id: requestId },
});

if (!requestExist) {
return Response.errorResponse(res, 404, res.__('Request not found'));
}
const managerDetails = await db.User.findOne({
where: { id: requestExist.managerId },
});
if (user.email !== requestExist.email && user.id !== requestExist.managerId) {
return Response.success(res, 401, res.__('This request has been a created by another user and belongs to another manager'));
}
req.request = requestExist;
req.managerDetails = managerDetails;
next();
}

Expand Down
12 changes: 12 additions & 0 deletions src/seeders/20200212121323-User.js
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,18 @@ module.exports = {
createdAt: new Date(),
updatedAt: new Date(),
},
{
id: 'a3426db1-1d2f-4c2c-8f25-c99962aff3fe',
firstName: 'bien',
lastName: 'aime',
email: 'jean@okay.com',
password: bcrypt.hashSync('Bien@BAR789', Number(process.env.passwordHashSalt)),
isVerified: true,
role: 'requester',
managerId: '79660e6f-4b7d-4d21-81ad-74f64e9e1c8a',
createdAt: new Date(),
updatedAt: new Date(),
},
],
{},
),
Expand Down
19 changes: 17 additions & 2 deletions src/seeders/20200222234112-requests.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ module.exports = {
type: 'two way',
status: 'open',
confirm: false,
managerId: '0119b84a-99a4-41c0-8a0e-6e0b6c385165',
returnDate: '2020-02-15',
createdAt: new Date(),
updatedAt: new Date(),
Expand All @@ -25,10 +24,10 @@ module.exports = {
destination: 'kigali',
reason: 'meeting with engineers',
departureDate: '2020-12-01',
managerId: '0119b84a-99a4-41c0-8a0e-6e0b6c385165',
email: 'jdev@andela.com',
type: 'two way',
status: 'rejected',
managerId: "0119b84a-99a4-41c0-8a0e-6e0b6c385165",
confirm: false,
returnDate: '2021-02-15',
createdAt: new Date(),
Expand Down Expand Up @@ -113,6 +112,22 @@ module.exports = {
managerId: '0119b84a-99a4-41c0-8a0e-6e0b6c385165',
createdAt: new Date(),
updatedAt: new Date(),
},
{
id: 'b7600a6c-58f8-4de3-9e7d-aa9d83b8b37b',
location: 'boston',
userId: 'a3426db1-1d2f-4c2c-8f25-c99962aff3fe',
destination: 'bisenyi',
reason: 'the reason is obvious needless to say it again',
departureDate: '2020-10-01',
email: 'jean@okay.com',
status: 'open',
type: 'two way',
confirm: false,
returnDate: '2021-02-15',
managerId: '79660e6f-4b7d-4d21-81ad-74f64e9e1c8a',
createdAt: new Date(),
updatedAt: new Date(),
}],
{},
),
Expand Down
19 changes: 17 additions & 2 deletions src/services/localesServices/locales/en.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
{
"Welcome to devRepublic Barefoot Nomad API": "Welcome to devRepublic Barefoot Nomad API",
"User is successfully logged in": "User is successfully logged in",
"An open request with that id is not found in your direct report": "An open request with that id is not found in your direct report",
"Your trip to bisenyi on 2020-10-01 you requested has been approved": "Your trip to bisenyi on 2020-10-01 you requested has been approved",
Expand Down Expand Up @@ -69,7 +70,6 @@
"amenities are required": "amenities are required",
"services are required": "services are required",
"facility already created": "facility already created",
"Welcome to devRepublic Barefoot Nomad API": "Welcome to devRepublic Barefoot Nomad API",
"Route %s not found": "Route %s not found",
"user has liked facility": "user has liked facility",
"user has already liked facility": "user has already liked facility",
Expand Down Expand Up @@ -125,5 +125,20 @@
"that you requested has been": "that you requested has been",
"To view this %s request you made click below": "To view this %s request you made click below",
"To view this edited request click below": "To view this edited request click below",
"request with id %s has been edited": "request with id %s has been edited"
"request with id %s has been edited": "request with id %s has been edited",
"Your manager posted a comment to your request": "Your manager posted a comment to your request",
"To view this As a your manager I can add any comment on your request. I can also cancel or approve your request but let me comment first request you made click below": "To view this As a your manager I can add any comment on your request. I can also cancel or approve your request but let me comment first request you made click below",
"View comment": "View comment",
"To view this thanks for your comment manager. This is my request too. And please Approve my request after this comment request you made click below": "To view this thanks for your comment manager. This is my request too. And please Approve my request after this comment request you made click below",
"To view this yeahahaha 252525626262 request you made click below": "To view this yeahahaha 252525626262 request you made click below",
"As a your manager I can add any comment on your request. I can also cancel or approve your request but let me comment first": "As a your manager I can add any comment on your request. I can also cancel or approve your request but let me comment first",
"thanks for your comment manager. This is my request too. And please Approve my request after this comment": "thanks for your comment manager. This is my request too. And please Approve my request after this comment",
"your manager posted a comment": "your manager posted a comment",
"your requester posted a comment": "your requester posted a comment",
"To view this thanks for your comment manager. This is my request too. And please Approve my request after this comment click below": "To view this thanks for your comment manager. This is my request too. And please Approve my request after this comment click below",
"To view this As a your manager I can add any comment on your request. I can also cancel or approve your request but let me comment first click below": "To view this As a your manager I can add any comment on your request. I can also cancel or approve your request but let me comment first click below",
"To view this %s click below": "To view this %s click below",
"Your Manager commented: %s": "Your Manager commented: %s",
"Your requsester commented: %s": "Your requsester commented: %s",
"Your requester commented: %s": "Your requester commented: %s"
}
9 changes: 8 additions & 1 deletion src/services/localesServices/locales/fr.json
Original file line number Diff line number Diff line change
Expand Up @@ -152,5 +152,12 @@
"approved": "approuvée",
"To view this %s request you made click below": "Pour voir cela %s demande que vous avez faite cliquez ci-dessous",
"request with id %s has been edited": "la demande avec l'ID %s a été modifiée",
"To view this edited request click below": "Pour voir cette demande modifiée, cliquez ci-dessous"
"To view this edited request click below": "Pour voir cette demande modifiée, cliquez ci-dessous",
"To view this %s click below": "Pour voir ce %s, cliquez ci-dessous",
"Your requester commented": "Votre demandeur a commenté",
"Your Manager commented": "Votre responsable a commenté",
"your manager posted a comment: %s": "votre manager a posté un commentaire: %s",
"Your Manager commented: %s": "Votre demandeur a commenté: %s",
"View comment": "Afficher le commentaire",
"your manager posted a comment": "votre manager a posté un commentaire"
}
43 changes: 40 additions & 3 deletions src/tests/comments.test.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,48 @@
import chai from 'chai';
import dotenv from 'dotenv';
import chaiHttp from 'chai-http';
import app from '../index';
import sinon from 'sinon';
import ioClient from 'socket.io-client';
import sgMail from '@sendgrid/mail';
import app, { server } from '../index';

import { provideToken } from '../utils/tokenHandler';

const {
expect
} = chai;

dotenv.config();
let requesterToken, managerToken, wrongManagerToken;
const requestId = '51e74db7-5510-4f50-9f15-e23710331ld5';
const wrongRequestId = '51e74db7-5510-4f50-9f15-e23710331ld555';
chai.use(chaiHttp);

describe('COMMENTS TESTS', () => {
const socketToken = provideToken('79660e6f-4b7d-4g21-81re-74f54e9e1c8a', true, 'jeanne@andela.com', 'requester');
let clientSocket;
const BASE_URL = process.env.BASE_URL || `http://localhost:${server.address().port}`;
beforeEach((done) => {
sinon.stub(sgMail, 'send').resolves({
to: 'jeanaime@andela.com',
from: 'devrepublic.team@gmail.com',
subject: 'barefoot nomad',
html: 'this is stubbing message'
});
clientSocket = ioClient.connect(BASE_URL, {
transportOptions: {
polling:
{ extraHeaders: { token: socketToken } }
},
'force new connection': true,
forceNew: true,
});
done();
});
afterEach((done) => {
clientSocket.disconnect();
sinon.restore();
done();
});
it('should login a user who will comment on a request', (done) => {
chai
.request(app)
Expand Down Expand Up @@ -79,8 +110,14 @@ describe('COMMENTS TESTS', () => {
expect(res.body.data.requestId).to.equal('51e74db7-5510-4f50-9f15-e23710331ld5');
expect(res.body.data.commmentOwner).to.equal('0119b84a-99a4-41c0-8a0e-6e0b6c385165');
expect(res.body.data.comment).to.equal('As a your manager I can add any comment on your request. I can also cancel or approve your request but let me comment first');
done();
});
clientSocket.on('notification', (msg) => {
expect(JSON.parse(msg)).to.be.an('object');
expect(JSON.parse(msg).receiverId).to.equal('79660e6f-4b7d-4g21-81re-74f54e9e1c8a');
expect(JSON.parse(msg).status).to.equal('unread');
expect(JSON.parse(msg).content).to.equal('your manager posted a comment');
done();
});
});
it('should not allow a manager to comment on a request which does not belong to him', (done) => {
chai
Expand Down

0 comments on commit af090cf

Please sign in to comment.