Initial commit 🍀
This commit is contained in:
32
src/api/common/notify.ts
Normal file
32
src/api/common/notify.ts
Normal file
@@ -0,0 +1,32 @@
|
||||
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 res = await Notification.insert(Object.assign({
|
||||
created_at: new Date(),
|
||||
notifiee_id: notifiee,
|
||||
notifier_id: notifier,
|
||||
type: type,
|
||||
is_read: false
|
||||
}, content));
|
||||
|
||||
const notification = res.ops[0];
|
||||
|
||||
resolve(notification);
|
||||
|
||||
// Publish notification event
|
||||
event(notifiee, 'notification',
|
||||
await serialize(notification));
|
||||
});
|
Reference in New Issue
Block a user