@dhyasama/totem-models 7.58.0 → 8.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.npmignore +14 -0
- package/helpers.js +89 -0
- package/index.js +0 -1
- package/lib/Account.js +125 -79
- package/lib/Activity.js +0 -2
- package/lib/CalendarEvent.js +1 -7
- package/lib/CapTable.js +0 -1
- package/lib/Deal.js +215 -151
- package/lib/Document.js +57 -56
- package/lib/Financials.js +115 -176
- package/lib/Flag.js +37 -14
- package/lib/Fund.js +14 -34
- package/lib/Interaction.js +79 -32
- package/lib/Investment.js +4 -10
- package/lib/LimitedPartner.js +303 -634
- package/lib/List.js +105 -147
- package/lib/Message.js +100 -51
- package/lib/News.js +1 -53
- package/lib/Note.js +60 -66
- package/lib/Organization.js +471 -645
- package/lib/Person.js +505 -787
- package/lib/Round.js +191 -134
- package/lib/Snapshot.js +3 -5
- package/lib/Webhook.js +1 -4
- package/package-lock.json +1927 -0
- package/package.json +3 -7
- package/test/Account.js +53 -38
- package/test/Deal.js +14 -30
- package/test/Document.js +51 -50
- package/test/Financials.js +5 -3
- package/test/Flag.js +18 -14
- package/test/Interaction.js +1 -31
- package/test/Investment.js +2 -3
- package/test/LimitedPartner.js +399 -554
- package/test/List.js +24 -29
- package/test/Message.js +7 -46
- package/test/News.js +12 -29
- package/test/Note.js +23 -22
- package/test/Organization.js +33 -307
- package/test/Person.js +6 -253
- package/test/Round.js +11 -11
- package/lib/Sync.js +0 -48
package/lib/Message.js
CHANGED
|
@@ -2,17 +2,15 @@
|
|
|
2
2
|
|
|
3
3
|
module.exports = function(mongoose, config) {
|
|
4
4
|
|
|
5
|
-
|
|
5
|
+
let
|
|
6
6
|
|
|
7
7
|
Schema = mongoose.Schema,
|
|
8
|
-
env = process.env.NODE_ENV || 'development',
|
|
9
|
-
_ = require('underscore'),
|
|
10
8
|
async = require('async'),
|
|
11
|
-
|
|
9
|
+
helpers = require('../helpers'),
|
|
12
10
|
Document = mongoose.model('Document'),
|
|
13
11
|
Note = mongoose.model('Note');
|
|
14
12
|
|
|
15
|
-
|
|
13
|
+
let Message = new Schema({
|
|
16
14
|
|
|
17
15
|
createdOn: { type: Date, required: true },
|
|
18
16
|
updatedOn: { type: Date, required: true },
|
|
@@ -42,7 +40,7 @@ module.exports = function(mongoose, config) {
|
|
|
42
40
|
// the original message date isn't always contained
|
|
43
41
|
// in the email. In its absence, use created on.
|
|
44
42
|
|
|
45
|
-
|
|
43
|
+
const self = this;
|
|
46
44
|
|
|
47
45
|
return self.originalMessageDate || self.createdOn;
|
|
48
46
|
|
|
@@ -68,7 +66,7 @@ module.exports = function(mongoose, config) {
|
|
|
68
66
|
|
|
69
67
|
Message.statics.delete = function(messageId, customerId, cb) {
|
|
70
68
|
|
|
71
|
-
|
|
69
|
+
const self = this;
|
|
72
70
|
|
|
73
71
|
self.findOneAndRemove({
|
|
74
72
|
'_id': messageId,
|
|
@@ -81,9 +79,9 @@ module.exports = function(mongoose, config) {
|
|
|
81
79
|
|
|
82
80
|
// Delete the doc itself along with any references
|
|
83
81
|
|
|
84
|
-
|
|
82
|
+
const self = this;
|
|
85
83
|
|
|
86
|
-
|
|
84
|
+
let removeReferences = function removeReferences(callback) {
|
|
87
85
|
self.update({}, {
|
|
88
86
|
$pull: { 'documents' : [docId] }
|
|
89
87
|
}, callback);
|
|
@@ -102,9 +100,9 @@ module.exports = function(mongoose, config) {
|
|
|
102
100
|
|
|
103
101
|
// Delete the note itself along with any references
|
|
104
102
|
|
|
105
|
-
|
|
103
|
+
const self = this;
|
|
106
104
|
|
|
107
|
-
|
|
105
|
+
let removeReferences = function removeReferences(callback) {
|
|
108
106
|
self.update({}, {
|
|
109
107
|
$pull: { 'notes' : [noteId] }
|
|
110
108
|
}, callback);
|
|
@@ -121,13 +119,19 @@ module.exports = function(mongoose, config) {
|
|
|
121
119
|
|
|
122
120
|
Message.statics.getDocuments = function getDocuments(personIds, options, cb) {
|
|
123
121
|
|
|
124
|
-
|
|
125
|
-
|
|
122
|
+
if (!cb) { throw new Error('cb is required'); }
|
|
123
|
+
if (!personIds) { return cb(new Error('personIds is required'), null); }
|
|
124
|
+
if (!options) { return cb(new Error('options is required'), null); }
|
|
125
|
+
if (!options.CUSTOMER_ID) { return cb(new Error('options.CUSTOMER_ID is required'), null); }
|
|
126
|
+
if (!mongoose.Types.ObjectId.isValid(options.CUSTOMER_ID)) { return cb(new Error('options.CUSTOMER_ID is not a valid ObjectId'), null); }
|
|
127
|
+
|
|
128
|
+
const self = this;
|
|
129
|
+
let query = self.find({ customer: options.CUSTOMER_ID });
|
|
126
130
|
|
|
127
131
|
query.where({'attendees.external': { $in : personIds }});
|
|
128
132
|
query.populate({
|
|
129
133
|
path: 'documents',
|
|
130
|
-
match: { customer:
|
|
134
|
+
match: { customer: options.CUSTOMER_ID }
|
|
131
135
|
});
|
|
132
136
|
query.deepPopulate([
|
|
133
137
|
'documents.createdBy'
|
|
@@ -136,56 +140,78 @@ module.exports = function(mongoose, config) {
|
|
|
136
140
|
'documents.createdBy': { select: 'name avatarUrl title' }
|
|
137
141
|
}
|
|
138
142
|
});
|
|
143
|
+
|
|
139
144
|
query.exec(cb);
|
|
140
145
|
|
|
141
146
|
};
|
|
142
147
|
|
|
143
|
-
Message.statics.getById = function getById(id, cb) {
|
|
148
|
+
Message.statics.getById = function getById(id, options, cb) {
|
|
149
|
+
|
|
150
|
+
if (!cb) { throw new Error('cb is required'); }
|
|
151
|
+
if (!id) { return cb(new Error('id is required'), null); }
|
|
152
|
+
if (!mongoose.Types.ObjectId.isValid(id)) { return cb(new Error('id is not a valid ObjectId'), null); }
|
|
153
|
+
if (!options) { return cb(new Error('options is required'), null); }
|
|
154
|
+
if (!options.CUSTOMER_ID) { return cb(new Error('options.CUSTOMER_ID is required'), null); }
|
|
155
|
+
if (!mongoose.Types.ObjectId.isValid(options.CUSTOMER_ID)) { return cb(new Error('options.CUSTOMER_ID is not a valid ObjectId'), null); }
|
|
144
156
|
|
|
145
|
-
|
|
146
|
-
|
|
157
|
+
const self = this;
|
|
158
|
+
let query = self.findOne({
|
|
147
159
|
_id: id,
|
|
148
|
-
customer:
|
|
160
|
+
customer: options.CUSTOMER_ID
|
|
149
161
|
});
|
|
150
162
|
query.populate('organization', 'name logoUrl');
|
|
151
163
|
query.populate('recipients.internal', 'name avatarUrl title doNotDisplay');
|
|
152
164
|
query.populate('recipients.external', 'name avatarUrl title doNotDisplay');
|
|
153
165
|
query.populate({
|
|
154
166
|
path: 'notes',
|
|
155
|
-
match: { customer:
|
|
167
|
+
match: { customer: options.CUSTOMER_ID }
|
|
156
168
|
});
|
|
157
169
|
query.populate({
|
|
158
170
|
path: 'documents',
|
|
159
|
-
match: { customer:
|
|
171
|
+
match: { customer: options.CUSTOMER_ID }
|
|
160
172
|
});
|
|
173
|
+
|
|
161
174
|
query.exec(cb);
|
|
162
175
|
|
|
163
176
|
};
|
|
164
177
|
|
|
165
178
|
Message.statics.getForCustomer = function getForCustomer(customerId, cb) {
|
|
166
179
|
|
|
167
|
-
|
|
168
|
-
|
|
180
|
+
if (!cb) { throw new Error('cb is required'); }
|
|
181
|
+
if (!customerId) { return cb(new Error('customerId is required'), null); }
|
|
182
|
+
if (!mongoose.Types.ObjectId.isValid(customerId)) { return cb(new Error('customerId is not a valid ObjectId'), null); }
|
|
183
|
+
|
|
184
|
+
const self = this;
|
|
185
|
+
let query = self.find({ customer: customerId });
|
|
186
|
+
|
|
169
187
|
query.exec(cb);
|
|
170
188
|
|
|
171
189
|
};
|
|
172
190
|
|
|
173
191
|
Message.statics.getForOrg = function getForOrg(orgId, options, cb) {
|
|
174
192
|
|
|
175
|
-
|
|
176
|
-
if (
|
|
193
|
+
if (!cb) { throw new Error('cb is required'); }
|
|
194
|
+
if (!orgId) { return cb(new Error('orgId is required'), null); }
|
|
195
|
+
if (!mongoose.Types.ObjectId.isValid(orgId)) { return cb(new Error('orgId is not a valid ObjectId'), null); }
|
|
196
|
+
if (!options) { return cb(new Error('options is required'), null); }
|
|
197
|
+
if (!options.CUSTOMER_ID) { return cb(new Error('options.CUSTOMER_ID is required'), null); }
|
|
198
|
+
if (!mongoose.Types.ObjectId.isValid(options.CUSTOMER_ID)) { return cb(new Error('options.CUSTOMER_ID is not a valid ObjectId'), null); }
|
|
199
|
+
|
|
200
|
+
// Note - This filters by customer so is NOT usable by admin tools
|
|
201
|
+
|
|
202
|
+
const self = this;
|
|
203
|
+
|
|
204
|
+
let query = self.find({ customer: options.CUSTOMER_ID, organization: orgId });
|
|
177
205
|
|
|
178
|
-
var self = this;
|
|
179
|
-
var query = self.find({ customer: config.CUSTOMER_ID, organization: orgId });
|
|
180
206
|
query.populate('recipients.internal', 'name title avatarUrl doNotDisplay');
|
|
181
207
|
query.populate('recipients.external', 'name title avatarUrl doNotDisplay');
|
|
182
208
|
query.populate({
|
|
183
209
|
path: 'notes',
|
|
184
|
-
match: { customer:
|
|
210
|
+
match: { customer: options.CUSTOMER_ID }
|
|
185
211
|
});
|
|
186
212
|
query.populate({
|
|
187
213
|
path: 'documents',
|
|
188
|
-
match: { customer:
|
|
214
|
+
match: { customer: options.CUSTOMER_ID }
|
|
189
215
|
});
|
|
190
216
|
query.deepPopulate([
|
|
191
217
|
'notes.createdBy',
|
|
@@ -196,28 +222,35 @@ module.exports = function(mongoose, config) {
|
|
|
196
222
|
'documents.createdBy': { select: 'name avatarUrl title' }
|
|
197
223
|
}
|
|
198
224
|
});
|
|
225
|
+
|
|
199
226
|
query.exec(cb);
|
|
200
227
|
|
|
201
228
|
};
|
|
202
229
|
|
|
203
230
|
Message.statics.getForPeople = function getForPeople(personIds, options, cb) {
|
|
204
231
|
|
|
205
|
-
|
|
206
|
-
if (
|
|
232
|
+
if (!cb) { throw new Error('cb is required'); }
|
|
233
|
+
if (!personIds) { return cb(new Error('personIds is required'), null); }
|
|
234
|
+
if (!options) { return cb(new Error('options is required'), null); }
|
|
235
|
+
if (!options.CUSTOMER_ID) { return cb(new Error('options.CUSTOMER_ID is required'), null); }
|
|
236
|
+
if (!mongoose.Types.ObjectId.isValid(options.CUSTOMER_ID)) { return cb(new Error('options.CUSTOMER_ID is not a valid ObjectId'), null); }
|
|
237
|
+
|
|
238
|
+
// Note: This filters by customer so is NOT usable by admin tools
|
|
207
239
|
|
|
208
|
-
|
|
209
|
-
|
|
240
|
+
const self = this;
|
|
241
|
+
|
|
242
|
+
let query = self.find({ customer: options.CUSTOMER_ID });
|
|
210
243
|
|
|
211
244
|
query.where({'recipients.external': { $in : personIds }});
|
|
212
245
|
query.populate('recipients.internal', 'name title avatarUrl doNotDisplay');
|
|
213
246
|
query.populate('recipients.external', 'name title avatarUrl doNotDisplay');
|
|
214
247
|
query.populate({
|
|
215
248
|
path: 'notes',
|
|
216
|
-
match: { customer:
|
|
249
|
+
match: { customer: options.CUSTOMER_ID }
|
|
217
250
|
});
|
|
218
251
|
query.populate({
|
|
219
252
|
path: 'documents',
|
|
220
|
-
match: { customer:
|
|
253
|
+
match: { customer: options.CUSTOMER_ID }
|
|
221
254
|
});
|
|
222
255
|
query.deepPopulate([
|
|
223
256
|
'notes.createdBy',
|
|
@@ -228,30 +261,45 @@ module.exports = function(mongoose, config) {
|
|
|
228
261
|
'documents.createdBy': { select: 'name avatarUrl title' }
|
|
229
262
|
}
|
|
230
263
|
});
|
|
264
|
+
|
|
231
265
|
query.exec(cb);
|
|
232
266
|
|
|
233
267
|
};
|
|
234
268
|
|
|
235
|
-
Message.statics.getForPeopleLite = function getForPeopleLite(personIds, cb) {
|
|
269
|
+
Message.statics.getForPeopleLite = function getForPeopleLite(personIds, options, cb) {
|
|
270
|
+
|
|
271
|
+
// Note - This doesn't filter by customer so is only usable by admin tools
|
|
272
|
+
|
|
273
|
+
if (!cb) { throw new Error('cb is required'); }
|
|
274
|
+
if (!personIds) { return cb(new Error('personIds is required'), null); }
|
|
275
|
+
|
|
276
|
+
options = helpers.getDefaultOptions(options);
|
|
277
|
+
|
|
278
|
+
if (!options.isWorkerProcess) { return cb(null, []); }
|
|
236
279
|
|
|
237
|
-
|
|
238
|
-
|
|
280
|
+
const self = this;
|
|
281
|
+
|
|
282
|
+
let query = self.find({'recipients.external': { $in : personIds }});
|
|
239
283
|
|
|
240
|
-
var self = this;
|
|
241
|
-
var query = self.find({'recipients.external': { $in : personIds }});
|
|
242
284
|
query.exec(cb);
|
|
243
285
|
|
|
244
286
|
};
|
|
245
287
|
|
|
246
288
|
Message.statics.getNotes = function getNotes(personIds, options, cb) {
|
|
247
289
|
|
|
248
|
-
|
|
249
|
-
|
|
290
|
+
if (!cb) { throw new Error('cb is required'); }
|
|
291
|
+
if (!personIds) { return cb(new Error('fundId is required'), null); }
|
|
292
|
+
if (!options) { return cb(new Error('options is required'), null); }
|
|
293
|
+
if (!options.CUSTOMER_ID) { return cb(new Error('options.CUSTOMER_ID is required'), null); }
|
|
294
|
+
if (!mongoose.Types.ObjectId.isValid(options.CUSTOMER_ID)) { return cb(new Error('options.CUSTOMER_ID is not a valid ObjectId'), null); }
|
|
295
|
+
|
|
296
|
+
const self = this;
|
|
297
|
+
let query = self.find({ customer: options.CUSTOMER_ID });
|
|
250
298
|
|
|
251
299
|
query.where({'attendees.external': { $in : personIds }});
|
|
252
300
|
query.populate({
|
|
253
301
|
path: 'notes',
|
|
254
|
-
match: { customer:
|
|
302
|
+
match: { customer: options.CUSTOMER_ID }
|
|
255
303
|
});
|
|
256
304
|
query.deepPopulate([
|
|
257
305
|
'notes.createdBy'
|
|
@@ -260,14 +308,15 @@ module.exports = function(mongoose, config) {
|
|
|
260
308
|
'notes.createdBy': { select: 'name avatarUrl title' }
|
|
261
309
|
}
|
|
262
310
|
});
|
|
311
|
+
|
|
263
312
|
query.exec(cb);
|
|
264
313
|
|
|
265
314
|
};
|
|
266
315
|
|
|
267
316
|
Message.statics.getUnprocessedDeals = function getUnprocessedDeals(customerId, cb) {
|
|
268
317
|
|
|
269
|
-
|
|
270
|
-
|
|
318
|
+
const self = this;
|
|
319
|
+
let query = self.find({
|
|
271
320
|
customer: customerId,
|
|
272
321
|
subtype: 'deals',
|
|
273
322
|
organization: null
|
|
@@ -276,14 +325,15 @@ module.exports = function(mongoose, config) {
|
|
|
276
325
|
path: 'documents',
|
|
277
326
|
match: { customer: customerId }
|
|
278
327
|
});
|
|
328
|
+
|
|
279
329
|
query.exec(cb);
|
|
280
330
|
|
|
281
331
|
};
|
|
282
332
|
|
|
283
333
|
Message.statics.getUnprocessedPortfolio = function getUnprocessedPortfolio(customerId, cb) {
|
|
284
334
|
|
|
285
|
-
|
|
286
|
-
|
|
335
|
+
const self = this;
|
|
336
|
+
let query = self.find({
|
|
287
337
|
customer: customerId,
|
|
288
338
|
subtype: 'portfolio',
|
|
289
339
|
organization: { $exists: false }
|
|
@@ -292,6 +342,7 @@ module.exports = function(mongoose, config) {
|
|
|
292
342
|
path: 'documents',
|
|
293
343
|
match: { customer: customerId }
|
|
294
344
|
});
|
|
345
|
+
|
|
295
346
|
query.exec(cb);
|
|
296
347
|
|
|
297
348
|
};
|
|
@@ -305,9 +356,8 @@ module.exports = function(mongoose, config) {
|
|
|
305
356
|
if (!id) { return cb(new Error('id is required'), null); }
|
|
306
357
|
if (!update) { return cb(new Error('update is required'), null); }
|
|
307
358
|
|
|
308
|
-
|
|
359
|
+
const self = this;
|
|
309
360
|
|
|
310
|
-
// https://mongoosejs.com/docs/api.html#model_Model.findOneAndUpdate
|
|
311
361
|
// options runValidators defaults false which is ok since we have upsert false
|
|
312
362
|
// new returns the updated document
|
|
313
363
|
|
|
@@ -317,7 +367,7 @@ module.exports = function(mongoose, config) {
|
|
|
317
367
|
|
|
318
368
|
Message.statics.upsert = function(message, cb) {
|
|
319
369
|
|
|
320
|
-
|
|
370
|
+
const timestamp = new Date();
|
|
321
371
|
|
|
322
372
|
message.createdOn = message.createdOn || timestamp;
|
|
323
373
|
message.updatedOn = timestamp;
|
|
@@ -329,10 +379,9 @@ module.exports = function(mongoose, config) {
|
|
|
329
379
|
///////////////////////////////////////
|
|
330
380
|
|
|
331
381
|
Message.set('autoIndex', false);
|
|
332
|
-
Message.set('usePushEach', true);
|
|
333
382
|
Message.set('toJSON', { virtuals: true });
|
|
334
383
|
|
|
335
|
-
|
|
384
|
+
let deepPopulate = require('mongoose-deep-populate')(mongoose);
|
|
336
385
|
Message.plugin(deepPopulate);
|
|
337
386
|
|
|
338
387
|
mongoose.model('Message', Message);
|
package/lib/News.js
CHANGED
|
@@ -2,10 +2,9 @@
|
|
|
2
2
|
|
|
3
3
|
module.exports = function(mongoose, config) {
|
|
4
4
|
|
|
5
|
-
|
|
5
|
+
let
|
|
6
6
|
|
|
7
7
|
Schema = mongoose.Schema,
|
|
8
|
-
env = process.env.NODE_ENV || 'development',
|
|
9
8
|
_ = require('underscore'),
|
|
10
9
|
|
|
11
10
|
News = new Schema({
|
|
@@ -35,29 +34,6 @@ module.exports = function(mongoose, config) {
|
|
|
35
34
|
|
|
36
35
|
});
|
|
37
36
|
|
|
38
|
-
News.statics.getById = function (id, cb) {
|
|
39
|
-
|
|
40
|
-
this
|
|
41
|
-
.findById(id)
|
|
42
|
-
.populate('org')
|
|
43
|
-
.exec(cb);
|
|
44
|
-
|
|
45
|
-
};
|
|
46
|
-
|
|
47
|
-
News.statics.list = function (cb) {
|
|
48
|
-
|
|
49
|
-
this
|
|
50
|
-
.find()
|
|
51
|
-
.populate('org')
|
|
52
|
-
.sort('-createdOn')
|
|
53
|
-
.exec(cb);
|
|
54
|
-
|
|
55
|
-
};
|
|
56
|
-
|
|
57
|
-
News.statics.listForCompanies = function listForCompanies(orgids, cb) {
|
|
58
|
-
this.find({ 'org': { $in : orgids }, 'deleted': {$ne: true} }).exec(cb);
|
|
59
|
-
};
|
|
60
|
-
|
|
61
37
|
News.statics.listForCompany = function (id, cb) {
|
|
62
38
|
|
|
63
39
|
this
|
|
@@ -68,26 +44,6 @@ module.exports = function(mongoose, config) {
|
|
|
68
44
|
|
|
69
45
|
};
|
|
70
46
|
|
|
71
|
-
News.statics.listForCompanyBySlug = function (slug, cb) {
|
|
72
|
-
|
|
73
|
-
this
|
|
74
|
-
.find()
|
|
75
|
-
.populate('org')
|
|
76
|
-
.sort('-createdOn')
|
|
77
|
-
.exec(function (err, news) {
|
|
78
|
-
|
|
79
|
-
if (err) { return cb(err, null); }
|
|
80
|
-
|
|
81
|
-
var filtered = _.filter(news, function(item) {
|
|
82
|
-
return item.org && item.org.slug == slug;
|
|
83
|
-
});
|
|
84
|
-
|
|
85
|
-
return cb(null, filtered);
|
|
86
|
-
|
|
87
|
-
});
|
|
88
|
-
|
|
89
|
-
};
|
|
90
|
-
|
|
91
47
|
News.statics.modifyById = function(id, update, cb) {
|
|
92
48
|
|
|
93
49
|
// VERY IMPORTANT NOTE
|
|
@@ -112,15 +68,7 @@ module.exports = function(mongoose, config) {
|
|
|
112
68
|
news.save(cb);
|
|
113
69
|
};
|
|
114
70
|
|
|
115
|
-
News.statics.delete = function(id, cb) {
|
|
116
|
-
this.getById(id, function(err, item) {
|
|
117
|
-
if (err) return cb(err, null);
|
|
118
|
-
item.remove(cb);
|
|
119
|
-
});
|
|
120
|
-
};
|
|
121
|
-
|
|
122
71
|
News.set('autoIndex', false);
|
|
123
|
-
News.set('usePushEach', true);
|
|
124
72
|
News.on('index', function(err) { console.log('error building news indexes: ' + err); });
|
|
125
73
|
|
|
126
74
|
mongoose.model('News', News);
|
package/lib/Note.js
CHANGED
|
@@ -2,16 +2,12 @@
|
|
|
2
2
|
|
|
3
3
|
module.exports = function(mongoose, config) {
|
|
4
4
|
|
|
5
|
-
|
|
5
|
+
let
|
|
6
6
|
|
|
7
7
|
Schema = mongoose.Schema,
|
|
8
|
-
env = process.env.NODE_ENV || 'development',
|
|
9
|
-
async = require('async'),
|
|
10
|
-
_ = require('underscore'),
|
|
11
|
-
postFind = require('mongoose-post-find'),
|
|
12
8
|
querystring = require('querystring');
|
|
13
9
|
|
|
14
|
-
|
|
10
|
+
let Note = new Schema({
|
|
15
11
|
|
|
16
12
|
customer: { type: Schema.ObjectId, ref: 'Organization', index: true, required: true },
|
|
17
13
|
|
|
@@ -25,8 +21,44 @@ module.exports = function(mongoose, config) {
|
|
|
25
21
|
|
|
26
22
|
});
|
|
27
23
|
|
|
24
|
+
Note.statics.delete = function(noteId, customerId, cb) {
|
|
25
|
+
|
|
26
|
+
if (!cb) { throw new Error('cb is required'); }
|
|
27
|
+
if (!noteId) { throw new Error('noteId is required'); }
|
|
28
|
+
if (!mongoose.Types.ObjectId.isValid(noteId)) { return cb(new Error('noteId is not a valid ObjectId'), null); }
|
|
29
|
+
if (!customerId) { return cb(new Error('customerId is required'), null); }
|
|
30
|
+
if (!mongoose.Types.ObjectId.isValid(customerId)) { return cb(new Error('customerId is not a valid ObjectId'), null); }
|
|
31
|
+
|
|
32
|
+
const self = this;
|
|
33
|
+
|
|
34
|
+
// Not strictly necessary but provides verification that the note being deleted belongs to the customer doing the deleting
|
|
35
|
+
|
|
36
|
+
let query = self.findOne({
|
|
37
|
+
'_id': noteId,
|
|
38
|
+
'customer': customerId
|
|
39
|
+
});
|
|
40
|
+
|
|
41
|
+
query.exec(function(err, note) {
|
|
42
|
+
|
|
43
|
+
if (err) return cb(err, null);
|
|
44
|
+
else if (!note) return cb(null, null);
|
|
45
|
+
|
|
46
|
+
note.remove(cb);
|
|
47
|
+
|
|
48
|
+
});
|
|
49
|
+
|
|
50
|
+
};
|
|
51
|
+
|
|
28
52
|
Note.statics.createForModel = function(note, Model, modelId, cb) {
|
|
29
53
|
|
|
54
|
+
if (!cb) { throw new Error('cb is required'); }
|
|
55
|
+
if (!note) { return cb(new Error('note is required'), null); }
|
|
56
|
+
if (!Model) { return cb(new Error('Model is required'), null); }
|
|
57
|
+
if (!modelId) { return cb(new Error('modelId is required'), null); }
|
|
58
|
+
if (!mongoose.Types.ObjectId.isValid(modelId)) { return cb(new Error('modelId is not a valid ObjectId'), null); }
|
|
59
|
+
|
|
60
|
+
const self = this;
|
|
61
|
+
|
|
30
62
|
var addToModel = function(err, createdNote) {
|
|
31
63
|
if (err) return cb(err, null);
|
|
32
64
|
Model.findByIdAndUpdate(modelId, { $push: { notes: createdNote }}, { new: true, upsert: false }, function(err, addedTo) {
|
|
@@ -38,58 +70,51 @@ module.exports = function(mongoose, config) {
|
|
|
38
70
|
};
|
|
39
71
|
|
|
40
72
|
note.createdOn = new Date();
|
|
41
|
-
note.customer = config.CUSTOMER_ID;
|
|
42
73
|
|
|
43
|
-
|
|
74
|
+
self.create(note, addToModel);
|
|
44
75
|
|
|
45
76
|
};
|
|
46
77
|
|
|
47
|
-
|
|
48
|
-
// this.find({ 'createdBy': personId }).exec(cb);
|
|
49
|
-
// };
|
|
50
|
-
|
|
51
|
-
Note.statics.getById = function(id, cb) {
|
|
52
|
-
this.findOne({
|
|
53
|
-
'_id': id,
|
|
54
|
-
'customer': config.CUSTOMER_ID
|
|
55
|
-
}).exec(cb);
|
|
56
|
-
};
|
|
78
|
+
Note.statics.getById = function(id, options, cb) {
|
|
57
79
|
|
|
58
|
-
|
|
80
|
+
if (!cb) { throw new Error('cb is required'); }
|
|
81
|
+
if (!id) { throw new Error('id is required'); }
|
|
82
|
+
if (!mongoose.Types.ObjectId.isValid(id)) { return cb(new Error('id is not a valid ObjectId'), null); }
|
|
83
|
+
if (!options) { return cb(new Error('options is required'), null); }
|
|
84
|
+
if (!options.CUSTOMER_ID) { return cb(new Error('options.CUSTOMER_ID is required'), null); }
|
|
85
|
+
if (!mongoose.Types.ObjectId.isValid(options.CUSTOMER_ID)) { return cb(new Error('options.CUSTOMER_ID is not a valid ObjectId'), null); }
|
|
59
86
|
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
// Not strictly necessary but provides verification that the note being deleted belongs to the customer doing the deleting
|
|
63
|
-
self.findOne({
|
|
64
|
-
'_id': noteId,
|
|
65
|
-
'customer': config.CUSTOMER_ID
|
|
66
|
-
}, function(err, note) {
|
|
67
|
-
|
|
68
|
-
if (err) return cb(err, null);
|
|
69
|
-
else if (!note) return cb(null, null);
|
|
70
|
-
|
|
71
|
-
note.remove(cb);
|
|
87
|
+
const self = this;
|
|
72
88
|
|
|
89
|
+
let query = self.findOne({
|
|
90
|
+
'_id': id,
|
|
91
|
+
'customer': options.CUSTOMER_ID
|
|
73
92
|
});
|
|
74
93
|
|
|
94
|
+
query.exec(cb);
|
|
95
|
+
|
|
75
96
|
};
|
|
76
97
|
|
|
77
|
-
Note.statics.modifyById = function(id, update, cb) {
|
|
98
|
+
Note.statics.modifyById = function(id, update, options, cb) {
|
|
78
99
|
|
|
79
100
|
// VERY IMPORTANT NOTE
|
|
80
101
|
// findByIdAndUpdate and findOneAndUpdate do not trigger pre-save hook so that code will not run here
|
|
81
102
|
|
|
82
|
-
if (!cb) throw new Error('cb is required');
|
|
103
|
+
if (!cb) { throw new Error('cb is required'); }
|
|
83
104
|
if (!id) { return cb(new Error('id is required'), null); }
|
|
105
|
+
if (!mongoose.Types.ObjectId.isValid(id)) { return cb(new Error('id is not a valid ObjectId'), null); }
|
|
84
106
|
if (!update) { return cb(new Error('update is required'), null); }
|
|
107
|
+
if (!options) { return cb(new Error('options is required'), null); }
|
|
108
|
+
if (!options.CUSTOMER_ID) { return cb(new Error('options.CUSTOMER_ID is required'), null); }
|
|
109
|
+
if (!mongoose.Types.ObjectId.isValid(options.CUSTOMER_ID)) { return cb(new Error('options.CUSTOMER_ID is not a valid ObjectId'), null); }
|
|
85
110
|
|
|
86
|
-
|
|
111
|
+
const self = this;
|
|
87
112
|
|
|
88
113
|
// https://mongoosejs.com/docs/api.html#model_Model.findOneAndUpdate
|
|
89
114
|
// options runValidators defaults false which is ok since we have upsert false
|
|
90
115
|
// new returns the updated document
|
|
91
116
|
|
|
92
|
-
self.
|
|
117
|
+
self.findOneAndUpdate({ _id: id, customer: options.CUSTOMER_ID }, update, { upsert: false, new: true }, cb);
|
|
93
118
|
|
|
94
119
|
};
|
|
95
120
|
|
|
@@ -102,38 +127,7 @@ module.exports = function(mongoose, config) {
|
|
|
102
127
|
self.text = querystring.unescape(self.text);
|
|
103
128
|
});
|
|
104
129
|
|
|
105
|
-
Note.plugin(postFind, {
|
|
106
|
-
|
|
107
|
-
find: function(results, done) {
|
|
108
|
-
|
|
109
|
-
var CUSTOMER_ID = config.CUSTOMER_ID;
|
|
110
|
-
|
|
111
|
-
if (CUSTOMER_ID == 'GLOBAL_PROCESS') return done(null, results);
|
|
112
|
-
|
|
113
|
-
// Reject any item that is for a different customer
|
|
114
|
-
results = _.reject(results, function(item) {
|
|
115
|
-
return item.customer.toString() != CUSTOMER_ID;
|
|
116
|
-
});
|
|
117
|
-
|
|
118
|
-
return done(null, results);
|
|
119
|
-
|
|
120
|
-
},
|
|
121
|
-
|
|
122
|
-
findOne: function(result, done) {
|
|
123
|
-
|
|
124
|
-
var CUSTOMER_ID = config.CUSTOMER_ID;
|
|
125
|
-
|
|
126
|
-
if (!result) return done(null, null);
|
|
127
|
-
else if (CUSTOMER_ID == 'GLOBAL_PROCESS') return done(null, result);
|
|
128
|
-
else if (result.customer.toString() == CUSTOMER_ID) return done(null, result);
|
|
129
|
-
else return done(null, null);
|
|
130
|
-
|
|
131
|
-
}
|
|
132
|
-
|
|
133
|
-
});
|
|
134
|
-
|
|
135
130
|
Note.set('autoIndex', false);
|
|
136
|
-
Note.set('usePushEach', true);
|
|
137
131
|
Note.on('index', function(err) { console.log('error building note indexes: ' + err); });
|
|
138
132
|
|
|
139
133
|
mongoose.model('Note', Note);
|