2016-12-29 07:49:51 +09:00
|
|
|
'use strict';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Module dependencies
|
|
|
|
*/
|
|
|
|
import * as mongo from 'mongodb';
|
|
|
|
import Notification from '../../../models/notification';
|
|
|
|
import serialize from '../../../serializers/notification';
|
|
|
|
import event from '../../../event';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Mark as read a notification
|
|
|
|
*
|
2017-03-01 17:37:01 +09:00
|
|
|
* @param {any} params
|
|
|
|
* @param {any} user
|
|
|
|
* @return {Promise<any>}
|
2016-12-29 07:49:51 +09:00
|
|
|
*/
|
|
|
|
module.exports = (params, user) =>
|
2017-02-27 16:48:19 +09:00
|
|
|
new Promise(async (res, rej) => {
|
|
|
|
const notificationId = params.notification;
|
|
|
|
|
|
|
|
if (notificationId === undefined || notificationId === null) {
|
|
|
|
return rej('notification is required');
|
|
|
|
}
|
2016-12-29 07:49:51 +09:00
|
|
|
|
2017-02-27 16:48:39 +09:00
|
|
|
// Get notification
|
2017-02-27 16:48:19 +09:00
|
|
|
const notification = await Notification
|
|
|
|
.findOne({
|
|
|
|
_id: new mongo.ObjectID(notificationId),
|
|
|
|
i: user._id
|
|
|
|
});
|
2016-12-29 07:49:51 +09:00
|
|
|
|
2017-02-27 16:48:19 +09:00
|
|
|
if (notification === null) {
|
|
|
|
return rej('notification-not-found');
|
2016-12-29 07:49:51 +09:00
|
|
|
}
|
|
|
|
|
2017-02-27 16:48:19 +09:00
|
|
|
// Update
|
|
|
|
notification.is_read = true;
|
|
|
|
Notification.update({ _id: notification._id }, {
|
|
|
|
$set: {
|
|
|
|
is_read: true
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
// Response
|
|
|
|
res();
|
2016-12-29 07:49:51 +09:00
|
|
|
|
2017-02-27 16:48:19 +09:00
|
|
|
// Serialize
|
|
|
|
const notificationObj = await serialize(notification);
|
2016-12-29 07:49:51 +09:00
|
|
|
|
2017-02-27 16:48:19 +09:00
|
|
|
// Publish read_notification event
|
|
|
|
event(user._id, 'read_notification', notificationObj);
|
|
|
|
});
|