Skip to content

Commit

Permalink
ft(notification):user can mark all notification as read
Browse files Browse the repository at this point in the history
[finishes #167727473]
  • Loading branch information
danndav committed Sep 13, 2019
1 parent 69641c2 commit 1bdaa2c
Show file tree
Hide file tree
Showing 7 changed files with 144 additions and 7 deletions.
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

33 changes: 33 additions & 0 deletions src/controllers/notification.controller.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import models from '../db/models';

const { Notification } = models;


/* eslint-disable */
export default class NotificationContoller {
static markAsRead(req, res) {


Notification.update( { read: true },{where: { receiver_id: req.currentUser.id },attributes: ['read']})
.then((data) => {
if (data) {
return res.status(200).json({
status: 'success',
data:data[1],
message: 'All Notification marked as read'
});
}
return res.status(404).json({
status: 'error',
error: 'No notification found'
});
})
.catch((err) => {
res.status(500).json({
status: 'error',
message: err.message,
info: 'Internal Server Error',
});
});
}
}
4 changes: 2 additions & 2 deletions src/controllers/request.controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -219,12 +219,12 @@ export default class RequestController {
}
}

/**
/**
* @param {object} req
* @param {object} res
* @returns {json} request
*/
static async approveRequest(req, res) {
static async approveRequest(req, res) {
try {
const request = await Request.update(
{
Expand Down
6 changes: 3 additions & 3 deletions src/controllers/user.controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import sender from '../services/email.service';
import Response from '../utils/response.utils';
import UserUtils from '../utils/user.utils';
import JWTService from '../services/jwt.service';
import auth from "../middlewares/auth";
import auth from '../middlewares/auth';

const { User } = models;
const defaultPassword = crypto.createHash('sha1').update(Math.random().toString()).digest('hex');
Expand Down Expand Up @@ -51,7 +51,7 @@ export default class UserController {
}
}

/**
/**
* @param {object} req The user's ID
* @param {object} res The user's details returned after verification
* @returns {object} A verified user
Expand All @@ -70,7 +70,7 @@ export default class UserController {
);
const verificationResult = verified[1][0];
return res.status(200).json({
status: "success",
status: 'success',
data: verificationResult
});
} catch (error) {
Expand Down
11 changes: 11 additions & 0 deletions src/routes/api/notification.router.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import express from 'express';
import Notification from '../../controllers/notification.controller';
import auth from '../../middlewares/auth';


const router = express.Router();


router.get('/mark-read', auth.verifyUserToken, Notification.markAsRead);

export default router;
6 changes: 5 additions & 1 deletion src/routes/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import FacilitiesRouter from './api/facilities.router';
import RatingRouter from './api/rating.router';
import FeedbackRouter from './api/feedback.router';
import RatingAndFeedback from './api/rating_feedback.router';
import Notification from './api/notification.router';


const router = [
RequestsRoutes,
Expand All @@ -17,7 +19,9 @@ const router = [
RatingRouter,
FeedbackRouter,
BookingsRouter,
RatingAndFeedback
RatingAndFeedback,
Notification
];


export default router;
89 changes: 89 additions & 0 deletions src/test/notification.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
import chai from 'chai';
import chaiHttp from 'chai-http';
import sinon from 'sinon';
import app from '../index';
import models from '../db/models'


const {Notification} = models;
let userToken = '';
chai.use(chaiHttp);
chai.should();
const { expect } = chai;

before(() => {
it('it should login user', (done) => {
chai
.request(app)
.post('/api/v1/auth/signin')
.send({
email: 'jane_doe@email.com',
password: 'asdfghjkl',
})
.end((err, res) => {
userToken = res.body.data.token;
res.body.should.have.property('status').to.equals('success');
res.body.should.have.property('data').to.be.an('object');
done();
});
});
});

describe('UNIT TESTS FOR NOTIFICATION', () => {
describe('/POST REQUEST', () => {
it('it should mark all notification as read ', (done) => {
chai
.request(app)
.get('/api/v1/mark-read')
.set('authorization', `Bearer ${userToken}`)
.end((err, res) => {
res.should.have.status(200);
res.body.should.be.an('object');
res.body.should.have.property('status').to.equals('success');
res.body.should.have
.property('message')
.to.equals('All Notification marked as read');

done();
});
});
it('it should not mark all notification as read ', (done) => {
const stub = sinon.stub(Notification, 'update').returns(new Promise((resolve,reject) => {
resolve(null);
}))
chai
.request(app)
.get('/api/v1/mark-read')
.set('authorization', `Bearer ${userToken}`)
.end((err, res) => {
stub.restore();
res.should.have.status(404);
res.body.should.be.an('object');
res.body.should.have.property('status').to.equals('error');
res.body.should.have
.property('error')
.to.equals('No notification found');

done();
});
});
});
it('it should not mark all notification for internal server error 500', (done) => {
const stub = sinon.stub(Notification, 'update').throws(new Error())
chai.request(app)
.get('/api/v1/mark-read')
.set('authorization', `Bearer ${userToken}`)
.end((err, res) => {
stub.restore();
expect(res).to.have.status(500);
expect(res.body).to.have.keys('status','error');
expect(res.body.status).to.deep.equal('error');

done();
});
});
});




0 comments on commit 1bdaa2c

Please sign in to comment.