Skip to content

Commit

Permalink
Merge 4c2f016 into 31eee0b
Browse files Browse the repository at this point in the history
  • Loading branch information
emmaadesile committed Nov 14, 2018
2 parents 31eee0b + 4c2f016 commit 4b6fbf6
Show file tree
Hide file tree
Showing 3 changed files with 140 additions and 37 deletions.
105 changes: 81 additions & 24 deletions server/controllers/notificationController.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,33 +51,60 @@ const notificationController = {
})
.then((notify) => {
if (!notify) {
return res.redirect('/api/v1/notifications');
return res.status(404).json({
status: 'error',
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).json({
status: '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).json({
status: 'error',
error: 'Notification does not exist'
});
}

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

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

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).json({
status: 'success',
message: 'Notification successfully marked as read',
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'));
},
markAllAsRead: (req, res) => {
Notifications
Expand All @@ -91,29 +118,56 @@ 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).json({
status: 'success',
message: '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).json({
status: 'error',
error: 'No notifications found'
});
}
res.status(200).json({
status: 'success',
message: '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).json({
status: 'error',
error: 'No notifications found'
});
}
return res.status(200).json({
status: 'success',
message: '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 +176,11 @@ 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).json({
status: 'success',
message: 'All read notifications have been deleted'
}))
.catch(() => res.status(500).jsend.error('Oops, something has gone wrong on the server'));
}
};
export default notificationController;
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
61 changes: 49 additions & 12 deletions test/notify.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,33 +45,69 @@ 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.body.status).to.equal('success');
expect(res.body.data.user);
expect(res.body).to.have.property('notify');
expect(res.body.notify).to.have.property('receiverId');
expect(res.body.notify).to.have.property('actorId');
expect(res.body.notify).to.have.property('action');
expect(res.body.notify).to.have.property('notifiable');
expect(res.body.notify).to.have.property('notifiableId');
expect(res.body.notify).to.have.property('isRead');
expect(res.body.notify).to.have.property('createdAt');
expect(res.body.notify).to.have.property('updatedAt');
done(err);
});
});
it('should mark one notifications as read ', (done) => {
it('should mark a notifications as read', (done) => {
chai
.request(app)
.put('/api/v1/notifications/7')
.put('/api/v1/notifications/4')
.set('authorization', userToken)
.set('Accept', 'application/json')
.send({ readStatus: 'setAsRead' })
.end((err, res) => {
expect(res.body.status).to.equal('success');
expect(res.body.data.notifications);
expect(res.body.message).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.body.status).to.equal('error');
expect(res.body.error).to.equal('Notification has all ready been marked as read');
done(err);
});
});
it('should clear one notifications', (done) => {
it('returns an error if no body is provided', (done) => {
chai
.request(app)
.delete('/api/v1/notifications/2')
.put('/api/v1/notifications/4')
.set('authorization', userToken)
.set('Accept', 'application/json')
.send({ readStatus: '' })
.end((err, res) => {
expect(res.body.status).to.equal('error');
expect(res.body.error).to.equal('You need to set this notification as read');
done(err);
});
});
it('should clear one notification', (done) => {
chai
.request(app)
.delete('/api/v1/notifications/3')
.set('authorization', userToken)
.end((err, res) => {
expect(res.body.status).to.equal('success');
expect(res.body.data.notifications);
expect(res.body.message).to.equal('Notification deleted successfully');
done(err);
});
});
Expand All @@ -82,7 +118,7 @@ describe('TEST ALL NOTIFICATION ENDPOINTS', () => {
.set('authorization', userToken)
.end((err, res) => {
expect(res.body.status).to.equal('success');
expect(res.body.data.notifications);
expect(res.body.message).to.equal('All read notifications have been deleted');
done(err);
});
});
Expand All @@ -93,18 +129,19 @@ describe('TEST ALL NOTIFICATION ENDPOINTS', () => {
.set('authorization', userToken)
.end((err, res) => {
expect(res.body.status).to.equal('success');
expect(res.body.data.notifications);
expect(res.body.message).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.body.status).to.equal('success');
expect(res.body.data.notifications);
expect(res.body.message).to.equal('All Notifications have been deleted');
done(err);
});
});
Expand Down

0 comments on commit 4b6fbf6

Please sign in to comment.