2016-12-28 14:49:51 -08:00
|
|
|
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,
|
2017-03-04 19:09:34 -08:00
|
|
|
content?: any
|
2016-12-28 14:49:51 -08:00
|
|
|
) => new Promise<any>(async (resolve, reject) => {
|
|
|
|
if (notifiee.equals(notifier)) {
|
|
|
|
return resolve();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create notification
|
2017-01-17 12:28:12 -08:00
|
|
|
const notification = await Notification.insert(Object.assign({
|
2016-12-28 14:49:51 -08:00
|
|
|
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));
|
|
|
|
});
|