summaryrefslogtreecommitdiff
path: root/src/api/endpoints/notifications/mark_as_read.js
blob: 16eb2009ac18f0baabc313abbdf693cb7c4b4c1c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
'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 {Object} params
 * @param {Object} user
 * @return {Promise<object>}
 */
module.exports = (params, user) =>
	new Promise(async (res, rej) =>
{
	const notificationId = params.notification;

	if (notificationId === undefined || notificationId === null) {
		return rej('notification is required');
	}

	// Get notifcation
	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.updateOne({ _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);
});