Skip to content

Commit

Permalink
Merge 66b74d7 into ebd4d2c
Browse files Browse the repository at this point in the history
  • Loading branch information
emmaadesile committed Nov 16, 2018
2 parents ebd4d2c + 66b74d7 commit b48e326
Show file tree
Hide file tree
Showing 4 changed files with 143 additions and 38 deletions.
67 changes: 43 additions & 24 deletions server/controllers/notificationController.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,33 +51,40 @@ const notificationController = {
})
.then((notify) => {
if (!notify) {
return res.redirect('/api/v1/notifications');
return res.status(404).jsend.error('Notification does not exist');
}
notify.isRead = true;
notify.save()
.catch(() => res.status(500).jsend.error('Oops, something has gone wrong on the server'));
const route = {
article: `/api/v1/articles/${notify.notifiableId}`,
user: `/api/v1/users/${notify.notifiableId}`,
};
return res.redirect(route[notify.notifiable]);
return res.status(200).jsend.success(notify);
})
.catch(() => res.status(500).jsend.error('Oops, something has gone wrong on the serve'));
.catch(() => res.status(500).jsend.error('Oops, something has gone wrong on the server'));
},
markAsRead: (req, res) => {
const { readStatus } = req.body;

Notifications
.findById(req.params.notifyId)
.scope(null)
.findByPk(req.params.notifyId)
.then((notify) => {
if (!notify) {
return res.redirect('/api/v1/notifications');
return res.status(404).jsend.error('Notification does not exist');
}

if (!readStatus || readStatus !== 'setAsRead') {
return res.status(400).jsend.error('You need to set this notification as read');
}

if (notify.isRead) {
return res.status(406).jsend.error('Notification has all ready been marked as read');
}

notify.isRead = true;
notify.save()
.catch(() => res.status(500).jsend.error('Oops, something has gone wrong on the serve'));
return res.redirect('/api/v1/notifications');
.catch(() => res.status(500).jsend.error('Oops, something has gone wrong on the server'));
return res.status(200).jsend.success('Notification successfully marked as read');
})
.catch(() => res.status(500).jsend.error('Oops, something has gone wrong on the serve'));
.catch(() => res.status(500).jsend.error('Oops, something has gone wrong on the server'));
},
markAllAsRead: (req, res) => {
Notifications
Expand All @@ -91,29 +98,41 @@ const notificationController = {
},
}
)
.then(() => res.redirect('/api/v1/notifications'))
.catch(() => res.status(500).jsend.error('Oops, something has gone wrong on the serve'));
.then(() => res.status(200).jsend.success('All notifications have been marked as read'))
.catch(() => res.status(500).jsend.error('Oops, something has gone wrong on the server'));
},
clearHistory: (req, res) => {
Notifications
Notifications.scope(null)
.destroy({
where: {
receiverId: req.currentUser.id
}
})
.then(() => res.redirect('/api/v1/notifications'))
.catch(() => res.status(500).jsend.error('Oops, something has gone wrong on the serve'));
.then((notifications) => {
if (!notifications) {
return res.status(404).jsend.error('No notifications found');
}
res.status(200).jsend.success('All Notifications have been deleted');
})
.catch(() => res.status(500).jsend.error('Oops, something has gone wrong on the server'));
},
clearOne: (req, res) => {
Notifications
const id = req.params.notifyId;
const receiverId = req.currentUser.id;

return Notifications.scope(null)
.destroy({
where: {
receiverId: req.currentUser.id,
id: req.params.notifyId
id, receiverId
}
})
.then(() => res.redirect('/api/v1/notifications'))
.catch(() => res.status(500).jsend.error('Oops, something has gone wrong on the serve'));
.then((notifications) => {
if (!notifications) {
return res.status(404).jsend.error('Notificaiton not found');
}
return res.status(200).jsend.success('Notification deleted successfully');
})
.catch(() => res.status(500).jsend.error('Oops, something has gone wrong on the server'));
},
clearRead: (req, res) => {
Notifications.scope('read')
Expand All @@ -122,8 +141,8 @@ const notificationController = {
receiverId: req.currentUser.id
}
})
.then(() => res.redirect('/api/v1/notifications'))
.catch(() => res.status(500).jsend.error('Oops, something has gone wrong on the serve'));
.then(() => res.status(200).jsend.success('All read notifications have been deleted'))
.catch(() => res.status(500).jsend.error('Oops, something has gone wrong on the server'));
}
};
export default notificationController;
2 changes: 1 addition & 1 deletion server/helpers/notify.js
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ const notify = {
notify.createNotifications(res, bulkData);
}
})
.catch(err => err.message);
.catch((err) => { res.status(500).jsend.error({ message: err }); });
},

/**
Expand Down
11 changes: 10 additions & 1 deletion server/seeders/20180924142120-notifications.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ module.exports = {
actorId: 3,
notifiable: 'articles',
notifiableId: 4,
isRead: true,
isRead: false,
createdAt: '2018-09-08',
updatedAt: '2018-09-08'
},
Expand All @@ -35,6 +35,15 @@ module.exports = {
createdAt: '2018-09-08',
updatedAt: '2018-09-08'
},
{
receiverId: 1,
actorId: 8,
notifiable: 'articles',
notifiableId: 5,
isRead: false,
createdAt: '2018-09-08',
updatedAt: '2018-09-08'
},
]),

down: queryInterface => queryInterface.bulkDelete('Notifications')
Expand Down
101 changes: 89 additions & 12 deletions test/notify.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ describe('TEST ALL NOTIFICATION ENDPOINTS', () => {
.get('/api/v1/notifications')
.set('authorization', userToken)
.end((err, res) => {
expect(res).to.have.status(200);
expect(res.body.status).to.equal('success');
expect(res.body.data.notifications);
expect(res.body.data.notifications[0].isRead).to.equal(false);
Expand All @@ -37,6 +38,7 @@ describe('TEST ALL NOTIFICATION ENDPOINTS', () => {
.get('/api/v1/notifications/history')
.set('authorization', userToken)
.end((err, res) => {
expect(res).to.have.status(200);
expect(res.body.status).to.equal('success');
expect(res.body.data.notifications);
done(err);
Expand All @@ -45,44 +47,104 @@ describe('TEST ALL NOTIFICATION ENDPOINTS', () => {
it('should get one notifications', (done) => {
chai
.request(app)
.get('/api/v1/notifications/8')
.get('/api/v1/notifications/2')
.set('authorization', userToken)
.end((err, res) => {
expect(res).to.have.status(200);
expect(res.body.status).to.equal('success');
expect(res.body.data.user);
expect(res.body.data.receiverId).to.equal(1);
expect(res.body.data.actorId).to.equal(3);
expect(res.body.data.notifiable).to.equal('articles');
expect(res.body.data.notifiableId).to.equal(4);
expect(res.body.data.isRead).to.equal(true);
done(err);
});
});
it('should mark one notifications as read ', (done) => {
it('returns an error if a notification does not exist', () => {
chai
.request(app)
.put('/api/v1/notifications/7')
.get('/api/v1/notifications/100')
.set('authorization', userToken)
.end((err, res) => {
expect(res).to.have.status(404);
expect(res.body.status).to.equal('error');
expect(res.body.message).to.equal('Notification does not exist');
});
});
it('should mark a notifications as read', (done) => {
chai
.request(app)
.put('/api/v1/notifications/4')
.set('authorization', userToken)
.set('Accept', 'application/json')
.send({ readStatus: 'setAsRead' })
.end((err, res) => {
expect(res).to.have.status(200);
expect(res.body.status).to.equal('success');
expect(res.body.data.notifications);
expect(res.body.data).to.equal('Notification successfully marked as read');
done(err);
});
});
it('should not mark a notifications as read twice', (done) => {
chai
.request(app)
.put('/api/v1/notifications/4')
.set('authorization', userToken)
.set('Accept', 'application/json')
.send({ readStatus: 'setAsRead' })
.end((err, res) => {
expect(res).to.have.status(406);
expect(res.body.status).to.equal('error');
expect(res.body.message).to.equal('Notification has all ready been marked as read');
done(err);
});
});
it('returns an error if no body is provided', (done) => {
chai
.request(app)
.put('/api/v1/notifications/4')
.set('authorization', userToken)
.set('Accept', 'application/json')
.send({ readStatus: '' })
.end((err, res) => {
expect(res).to.have.status(400);
expect(res.body.status).to.equal('error');
expect(res.body.message).to.equal('You need to set this notification as read');
done(err);
});
});
it('should clear one notifications', (done) => {
it('should clear one notification', (done) => {
chai
.request(app)
.delete('/api/v1/notifications/2')
.delete('/api/v1/notifications/3')
.set('authorization', userToken)
.end((err, res) => {
expect(res).to.have.status(200);
expect(res.body.status).to.equal('success');
expect(res.body.data.notifications);
expect(res.body.data).to.equal('Notification deleted successfully');
done(err);
});
});
it('returns an error if user tries to cldar a notification that does not exist', () => {
chai
.request(app)
.delete('/api/v1/notifications/120')
.set('authorization', userToken)
.end((err, res) => {
expect(res).to.have.status(404);
expect(res.body.status).to.equal('error');
expect(res.body.message).to.equal('Notificaiton not found');
});
});
it('should clear all read notifications', (done) => {
chai
.request(app)
.delete('/api/v1/notifications/read')
.set('authorization', userToken)
.end((err, res) => {
expect(res).to.have.status(200);
expect(res.body.status).to.equal('success');
expect(res.body.data.notifications);
expect(res.body.data).to.equal('All read notifications have been deleted');
done(err);
});
});
Expand All @@ -92,20 +154,35 @@ describe('TEST ALL NOTIFICATION ENDPOINTS', () => {
.put('/api/v1/notifications')
.set('authorization', userToken)
.end((err, res) => {
expect(res).to.have.status(200);
expect(res.body.status).to.equal('success');
expect(res.body.data.notifications);
expect(res.body.data).to.equal('All notifications have been marked as read');
done(err);
});
});
it('should clear all notifications', (done) => {
it('should delete all notifications of a user', (done) => {
chai
.request(app)
.delete('/api/v1/notifications')
.set('authorization', userToken)
.set('Accept', 'application/json')
.end((err, res) => {
expect(res).to.have.status(200);
expect(res.body.status).to.equal('success');
expect(res.body.data.notifications);
expect(res.body.data).to.equal('All Notifications have been deleted');
done(err);
});
});
it('returns an error if no notification exists for a user', () => {
chai
.request(app)
.delete('/api/v1/notifications')
.set('authorization', userToken)
.set('Accept', 'application/json')
.end((err, res) => {
expect(res).to.have.status(404);
expect(res.body.status).to.equal('error');
expect(res.body.message).to.equal('No notifications found');
});
});
});

0 comments on commit b48e326

Please sign in to comment.