/
notification.model.js
145 lines (139 loc) · 4.03 KB
/
notification.model.js
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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
'use strict'
const Q = require('q')
const RestHapi = require('rest-hapi')
const _ = require('lodash')
const errorHelper = require('../utilities/error-helper')
const Config = require('../../config')
const notificationUpdateAuth = require('../policies/notification-auth.policy')
const NOTIFICATION_TYPES = Config.get('/constants/NOTIFICATION_TYPES')
// TODO: policy/etc to give primary user root access
// TODO: add an event type
module.exports = function(mongoose) {
var modelName = 'notification'
var Types = mongoose.Schema.Types
var Schema = new mongoose.Schema(
{
type: {
type: Types.String,
enum: _.values(NOTIFICATION_TYPES),
required: true
},
hasRead: {
type: Types.Boolean,
required: true,
default: false
},
primaryUser: {
type: Types.ObjectId,
ref: 'user'
},
actingUser: {
type: Types.ObjectId,
ref: 'user'
}
},
{ collection: modelName }
)
Schema.statics = {
collectionName: modelName,
routeOptions: {
policies: {
// only the primaryUser can update a notification
updatePolicies: [notificationUpdateAuth(mongoose)]
},
associations: {
primaryUser: {
type: 'MANY_ONE',
model: 'user'
},
actingUser: {
type: 'ONE_ONE',
model: 'user'
// duplicate: ['firstName', 'lastName', 'profileImageUrl']
}
}
},
/**
* Create a notification based on a new or updated connection
* @param connnection: used to set the user properties
* @param connectionPayload: used to determine the notification type
* @param server
* @param logger
*/
async createConnectionNotification(
connnection,
connectionPayload,
server,
logger
) {
const Log = logger.bind()
try {
const Notification = mongoose.model('notification')
const User = mongoose.model('user')
let notification = {
primaryUser: connnection.connectedUser,
actingUser: connnection.primaryUser
}
if (connectionPayload.isContact) {
notification.type = NOTIFICATION_TYPES.CONTACT
} else if (connectionPayload.isFollowing) {
notification.type = NOTIFICATION_TYPES.FOLLOW
}
if (notification.type) {
let promises = []
promises.push(RestHapi.create(Notification, notification, Log))
promises.push(
RestHapi.find(
User,
notification.actingUser,
{ $select: ['firstName', 'lastName', 'profileImageUrl'] },
Log
)
)
let result = await Promise.all(promises)
let notification = result[0]
notification.actingUser = result[1]
server.publish(
'/notification/' + notification.primaryUser,
notification
)
}
} catch (err) {
errorHelper.handleError(err, Log)
}
},
/**
* Create a notification based on a shared document
* @param notification: object used to create the notification
* @param server
* @param logger
*/
async createDocumentNotification(notification, server, logger) {
const Log = logger.bind()
try {
const Notification = mongoose.model('notification')
const User = mongoose.model('user')
let promises = []
promises.push(RestHapi.create(Notification, notification, Log))
promises.push(
RestHapi.find(
User,
notification.actingUser,
{ $select: ['firstName', 'lastName', 'profileImageUrl'] },
Log
)
)
let result = await Promise.all(promises)
let notification = result[0]
notification.actingUser = result[1]
server.publish(
'/notification/' + notification.primaryUser,
notification
)
} catch (err) {
errorHelper.handleError(err, Log)
}
}
}
return Schema
}