From 2a9cba25a89b5cf2394a22696ee0fb67140076a9 Mon Sep 17 00:00:00 2001 From: syuilo Date: Fri, 3 Mar 2017 08:24:48 +0900 Subject: wip --- src/api/endpoints/notifications/mark_as_read.js | 53 ------------------------- src/api/endpoints/notifications/mark_as_read.ts | 50 +++++++++++++++++++++++ 2 files changed, 50 insertions(+), 53 deletions(-) delete mode 100644 src/api/endpoints/notifications/mark_as_read.js create mode 100644 src/api/endpoints/notifications/mark_as_read.ts (limited to 'src/api/endpoints/notifications') diff --git a/src/api/endpoints/notifications/mark_as_read.js b/src/api/endpoints/notifications/mark_as_read.js deleted file mode 100644 index 9c8a5ee64b..0000000000 --- a/src/api/endpoints/notifications/mark_as_read.js +++ /dev/null @@ -1,53 +0,0 @@ -'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 - * - * @param {any} params - * @param {any} user - * @return {Promise} - */ -module.exports = (params, user) => - new Promise(async (res, rej) => { - const notificationId = params.notification; - - if (notificationId === undefined || notificationId === null) { - return rej('notification is required'); - } - - // Get notification - const notification = await Notification - .findOne({ - _id: new mongo.ObjectID(notificationId), - i: user._id - }); - - if (notification === null) { - return rej('notification-not-found'); - } - - // Update - notification.is_read = true; - Notification.update({ _id: notification._id }, { - $set: { - is_read: true - } - }); - - // Response - res(); - - // Serialize - const notificationObj = await serialize(notification); - - // Publish read_notification event - event(user._id, 'read_notification', notificationObj); - }); diff --git a/src/api/endpoints/notifications/mark_as_read.ts b/src/api/endpoints/notifications/mark_as_read.ts new file mode 100644 index 0000000000..6e75927cfa --- /dev/null +++ b/src/api/endpoints/notifications/mark_as_read.ts @@ -0,0 +1,50 @@ +'use strict'; + +/** + * Module dependencies + */ +import it from '../../it'; +import Notification from '../../models/notification'; +import serialize from '../../serializers/notification'; +import event from '../../event'; + +/** + * Mark as read a notification + * + * @param {any} params + * @param {any} user + * @return {Promise} + */ +module.exports = (params, user) => + new Promise(async (res, rej) => { + const [notificationId, notificationIdErr] = it(params.notification_id).expect.id().required().qed(); + if (notificationIdErr) return rej('invalid notification_id param'); + + // Get notification + const notification = await Notification + .findOne({ + _id: notificationId, + i: user._id + }); + + if (notification === null) { + return rej('notification-not-found'); + } + + // Update + notification.is_read = true; + Notification.update({ _id: notification._id }, { + $set: { + is_read: true + } + }); + + // Response + res(); + + // Serialize + const notificationObj = await serialize(notification); + + // Publish read_notification event + event(user._id, 'read_notification', notificationObj); + }); -- cgit v1.2.3-freya