summaryrefslogtreecommitdiff
path: root/src/api/common/notify.ts
blob: 9152b85cdb0b8e8165b57b8e9a3f2ffbe8e2b608 (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
import * as mongo from 'mongodb';
import Notification from '../models/notification';
import event from '../event';
import serialize from '../serializers/notification';

export default (
	notifiee: mongo.ObjectID,
	notifier: mongo.ObjectID,
	type: string,
	content: any
) => new Promise<any>(async (resolve, reject) => {
	if (notifiee.equals(notifier)) {
		return resolve();
	}

	// Create notification
	const notification = await Notification.insert(Object.assign({
		created_at: new Date(),
		notifiee_id: notifiee,
		notifier_id: notifier,
		type: type,
		is_read: false
	}, content));

	resolve(notification);

	// Publish notification event
	event(notifiee, 'notification',
		await serialize(notification));
});