@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.
@@ -2,16 +2,14 @@
2
2
 
3
3
  module.exports = function(mongoose, config) {
4
4
 
5
- var Schema = mongoose.Schema;
6
- var env = process.env.NODE_ENV || 'development';
7
- var CalendarEvent = mongoose.model('CalendarEvent');
8
- var Note = mongoose.model('Note');
9
- var Organization = mongoose.model('Organization');
10
- var Person = mongoose.model('Person');
11
- var utilities = require('@dhyasama/ffvc-utilities');
12
- var moment = require('moment');
5
+ let Schema = mongoose.Schema;
6
+ let async = require('async');
7
+ let Note = mongoose.model('Note');
8
+ let utilities = require('@dhyasama/ffvc-utilities');
9
+ let moment = require('moment');
10
+ let helpers = require('../helpers');
13
11
 
14
- var Interaction = new Schema({
12
+ let Interaction = new Schema({
15
13
 
16
14
  calendarEventId: { type: Schema.ObjectId, ref: 'CalendarEvent', required: true }, // the event from the original pull
17
15
  providerEventId: { type: String, required: true, index: true, trim: true }, // unique id from the calendar provider
@@ -32,6 +30,13 @@ module.exports = function(mongoose, config) {
32
30
 
33
31
  Interaction.statics.addNote = function(interactionId, creatorPersonId, text, isPrivate, cb) {
34
32
 
33
+ if (!cb) { throw new Error('cb is required'); }
34
+ if (!interactionId) { return cb(new Error('interactionId is required'), null); }
35
+ if (!mongoose.Types.ObjectId.isValid(interactionId)) { return cb(new Error('interactionId is not a valid ObjectId'), null); }
36
+ if (!creatorPersonId) { return cb(new Error('creatorPersonId is required'), null); }
37
+ if (!mongoose.Types.ObjectId.isValid(creatorPersonId)) { return cb(new Error('creatorPersonId is not a valid ObjectId'), null); }
38
+ if (!text) { return cb(new Error('text is required'), null); }
39
+
35
40
  Note.createForModel({
36
41
  createdBy: creatorPersonId,
37
42
  text: text,
@@ -42,6 +47,12 @@ module.exports = function(mongoose, config) {
42
47
 
43
48
  Interaction.statics.delete = function(providerEventId, totemCustomerId, cb) {
44
49
 
50
+ if (!cb) { throw new Error('cb is required'); }
51
+ if (!providerEventId) { return cb(new Error('personIds is required'), null); }
52
+ if (!mongoose.Types.ObjectId.isValid(providerEventId)) { return cb(new Error('providerEventId is not a valid ObjectId'), null); }
53
+ if (!totemCustomerId) { return cb(new Error('totemCustomerId is required'), null); }
54
+ if (!mongoose.Types.ObjectId.isValid(totemCustomerId)) { return cb(new Error('totemCustomerId is not a valid ObjectId'), null); }
55
+
45
56
  var self = this;
46
57
 
47
58
  self.findOneAndRemove({
@@ -55,6 +66,10 @@ module.exports = function(mongoose, config) {
55
66
 
56
67
  // Delete the note itself along with any references
57
68
 
69
+ if (!cb) { throw new Error('cb is required'); }
70
+ if (!noteId) { return cb(new Error('noteId is required'), null); }
71
+ if (!mongoose.Types.ObjectId.isValid(noteId)) { return cb(new Error('noteId is not a valid ObjectId'), null); }
72
+
58
73
  var self = this;
59
74
 
60
75
  var removeReferences = function removeReferences(callback) {
@@ -74,8 +89,13 @@ module.exports = function(mongoose, config) {
74
89
 
75
90
  Interaction.statics.getForCustomer = function getForCustomer(customerId, since, cb) {
76
91
 
77
- since = since || moment().subtract(20, 'years').startOf('day').toString()
92
+ if (!cb) { throw new Error('cb is required'); }
93
+ if (!customerId) { return cb(new Error('customerId is required'), null); }
94
+ if (!mongoose.Types.ObjectId.isValid(customerId)) { return cb(new Error('customerId is not a valid ObjectId'), null); }
95
+
96
+ since = since || moment().subtract(20, 'years').startOf('day').toString();
78
97
  var sinceObjectId = utilities.convertTimestampToObjectId(since);
98
+ if (!mongoose.Types.ObjectId.isValid(sinceObjectId)) { return cb(new Error('sinceObjectId is not a valid ObjectId'), null); }
79
99
 
80
100
  var self = this;
81
101
  var query = self.find({ totemCustomerId: customerId, _id: { $gt: sinceObjectId } });
@@ -86,8 +106,14 @@ module.exports = function(mongoose, config) {
86
106
 
87
107
  Interaction.statics.getForCustomerSkinny = function getForCustomer(customerId, since, cb) {
88
108
 
89
- since = since || moment().subtract(20, 'years').startOf('day').toString()
109
+ if (!cb) { throw new Error('cb is required'); }
110
+ if (!customerId) { return cb(new Error('customerId is required'), null); }
111
+ if (!mongoose.Types.ObjectId.isValid(customerId)) { return cb(new Error('customerId is not a valid ObjectId'), null); }
112
+ if (!since) { return cb(new Error('since is required'), null); }
113
+
114
+ since = since || moment().subtract(20, 'years').startOf('day').toString();
90
115
  var sinceObjectId = utilities.convertTimestampToObjectId(since);
116
+ if (!mongoose.Types.ObjectId.isValid(sinceObjectId)) { return cb(new Error('sinceObjectId is not a valid ObjectId'), null); }
91
117
 
92
118
  var self = this;
93
119
  var query = self.find({ totemCustomerId: customerId, _id: { $gt: sinceObjectId } });
@@ -97,10 +123,15 @@ module.exports = function(mongoose, config) {
97
123
 
98
124
  Interaction.statics.getForPeople = function getForPeople(personIds, options, cb) {
99
125
 
100
- // This filters by customer so is NOT usable by admin tools
101
- if (config.CUSTOMER_ID == 'GLOBAL_PROCESS') return cb(new Error('Access denied. Use getForPeopleLite.'), null);
126
+ if (!cb) { throw new Error('cb is required'); }
127
+ if (!personIds) { return cb(new Error('personIds is required'), null); }
128
+ if (!options) { return cb(new Error('options is required'), null); }
129
+ if (!options.CUSTOMER_ID) { return cb(new Error('options.CUSTOMER_ID is required'), null); }
130
+ if (!mongoose.Types.ObjectId.isValid(options.CUSTOMER_ID)) { return cb(new Error('options.CUSTOMER_ID is not a valid ObjectId'), null); }
102
131
 
103
- var self = this;
132
+ // NOTE: This is not for global process workers. Use getForPeopleLite instead.
133
+
134
+ let self = this;
104
135
 
105
136
  options.limit = options.limit || -1;
106
137
  options.skip = options.skip || -1;
@@ -112,24 +143,24 @@ module.exports = function(mongoose, config) {
112
143
  options.before = options.before || moment().toISOString();
113
144
  options.after = options.after || moment().subtract(20, 'year').toISOString();
114
145
 
115
- var query = self.find({ totemCustomerId: config.CUSTOMER_ID });
146
+ let query = self.find({ totemCustomerId: options.CUSTOMER_ID });
116
147
 
117
148
  query.where({'attendees.external': { $in : personIds }});
118
149
  query.where({ startTime: { $gte: options.after }});
119
150
  query.where({ startTime: { $lte: options.before }});
151
+
120
152
  query.populate('attendees.internal', 'name title avatarUrl doNotDisplay');
121
153
  query.populate('attendees.external', 'name title avatarUrl doNotDisplay');
122
- query.populate({
123
- path: 'notes',
124
- match: { customer: config.CUSTOMER_ID }
125
- });
154
+ query.populate({ path: 'notes', match: { customer: options.CUSTOMER_ID } });
155
+
126
156
  query.deepPopulate([
127
157
  'notes.createdBy'
128
158
  ], {
129
159
  populate: {
130
160
  'notes.createdBy': { select: 'name avatarUrl title' }
131
161
  }
132
- })
162
+ });
163
+
133
164
  query.sort(options.sort);
134
165
 
135
166
  if (options.attendee) { query.where({'attendees.internal': options.attendee}); }
@@ -138,19 +169,24 @@ module.exports = function(mongoose, config) {
138
169
 
139
170
  query.exec(cb);
140
171
 
141
- // todo
142
- //options.includeRecurring = options.includeRecurring || true;
143
- //query.where({ recurring: options.recurring });
144
-
145
172
  };
146
173
 
147
- Interaction.statics.getForPeopleLite = function getForPeopleLite(personIds, cb) {
174
+ Interaction.statics.getForPeopleLite = function getForPeopleLite(personIds, options, cb) {
148
175
 
149
176
  // This doesn't filter by customer so is only usable by admin tools
150
- if (config.CUSTOMER_ID != 'GLOBAL_PROCESS') return cb(new Error('Access denied'), null);
151
177
 
152
- var self = this;
153
- var query = self.find({'attendees.external': { $in : personIds }});
178
+ let self = this;
179
+
180
+ if (!cb) { throw new Error('cb is required'); }
181
+ if (!personIds) { return cb(new Error('personIds is required'), null); }
182
+ if (!options) { return cb(new Error('options is required'), null); }
183
+
184
+ options = helpers.getDefaultOptions(options);
185
+
186
+ if (!options.isWorkerProcess) { return cb(null, []); }
187
+
188
+ let query = self.find({'attendees.external': { $in : personIds }});
189
+
154
190
  query.exec(cb);
155
191
 
156
192
  };
@@ -161,6 +197,11 @@ module.exports = function(mongoose, config) {
161
197
  // Typically used to get the last interaction the firm had with an org
162
198
  // So you'd pass in the id of the customer and a list of the people at an org and get back the most recent interaction
163
199
 
200
+ if (!cb) { throw new Error('cb is required'); }
201
+ if (!personIds) { return cb(new Error('personIds is required'), null); }
202
+ if (!customerId) { return cb(new Error('customerId is required'), null); }
203
+ if (!mongoose.Types.ObjectId.isValid(customerId)) { return cb(new Error('customerId is not a valid ObjectId'), null); }
204
+
164
205
  var self = this;
165
206
 
166
207
  var query = self
@@ -174,13 +215,19 @@ module.exports = function(mongoose, config) {
174
215
 
175
216
  Interaction.statics.getNotes = function getNotes(personIds, options, cb) {
176
217
 
218
+ if (!cb) { throw new Error('cb is required'); }
219
+ if (!personIds) { return cb(new Error('personIds is required'), null); }
220
+ if (!options) { return cb(new Error('options is required'), null); }
221
+ if (!options.CUSTOMER_ID) { return cb(new Error('CUSTOMER_ID is required'), null); }
222
+ if (!mongoose.Types.ObjectId.isValid(options.CUSTOMER_ID)) { return cb(new Error('CUSTOMER_ID is not a valid ObjectId'), null); }
223
+
177
224
  var self = this;
178
- var query = self.find({ totemCustomerId: config.CUSTOMER_ID });
225
+ var query = self.find({ totemCustomerId: options.CUSTOMER_ID });
179
226
 
180
227
  query.where({'attendees.external': { $in : personIds }});
181
228
  query.populate({
182
229
  path: 'notes',
183
- match: { customer: config.CUSTOMER_ID }
230
+ match: { customer: options.CUSTOMER_ID }
184
231
  });
185
232
  query.deepPopulate([
186
233
  'notes.createdBy'
@@ -201,11 +248,11 @@ module.exports = function(mongoose, config) {
201
248
 
202
249
  if (!cb) throw new Error('cb is required');
203
250
  if (!id) { return cb(new Error('id is required'), null); }
251
+ if (!mongoose.Types.ObjectId.isValid(id)) { return cb(new Error('id is not a valid ObjectId'), null); }
204
252
  if (!update) { return cb(new Error('update is required'), null); }
205
253
 
206
254
  var self = this;
207
255
 
208
- // https://mongoosejs.com/docs/api.html#model_Model.findOneAndUpdate
209
256
  // options runValidators defaults false which is ok since we have upsert false
210
257
  // new returns the updated document
211
258
 
@@ -215,6 +262,7 @@ module.exports = function(mongoose, config) {
215
262
 
216
263
  Interaction.statics.upsert = function upsert(interaction, cb) {
217
264
 
265
+ if (!cb) { throw new Error('cb is required'); }
218
266
  if (!interaction) { return cb(new Error('Interaction is required'), null); }
219
267
 
220
268
  interaction.save(cb);
@@ -222,7 +270,6 @@ module.exports = function(mongoose, config) {
222
270
  };
223
271
 
224
272
  Interaction.set('autoIndex', false);
225
- Interaction.set('usePushEach', true);
226
273
  Interaction.on('index', function(err) { console.log('error building interaction indexes: ' + err); });
227
274
 
228
275
  var deepPopulate = require('mongoose-deep-populate')(mongoose);
package/lib/Investment.js CHANGED
@@ -63,8 +63,7 @@ module.exports = function(mongoose, config) {
63
63
  // Catch-all for customer specific key-value pairs from sheet that aren't supported by our schema
64
64
  details: [{
65
65
  key: { type: String, trim: true, required: true },
66
- value: { type: Schema.Types.Mixed, required: true },
67
- format: { type: String, enum: [null, 'number', 'decimal', 'money', 'percentage', 'date']},
66
+ value: { type: String, trim: true, required: true },
68
67
  _id: false
69
68
  }],
70
69
 
@@ -110,14 +109,14 @@ module.exports = function(mongoose, config) {
110
109
  // post-remove hooks only fire on document.remove not schema.remove, hence the loop below to call
111
110
  // remove on each matching investment.
112
111
 
113
- var self = this;
112
+ let self = this;
114
113
 
115
114
  self
116
- .find({customer: customerId})
115
+ .find({ customer: customerId })
117
116
  .select('_id')
118
117
  .exec(function(err, investments) {
119
118
 
120
- if (err) return cb(err, null);
119
+ if (err) { return cb(err, null); }
121
120
 
122
121
  async.each(investments, function(investment, callback) {
123
122
  investment.remove(callback);
@@ -130,10 +129,6 @@ module.exports = function(mongoose, config) {
130
129
  };
131
130
 
132
131
  Investment.statics.upsert = function upsert(investment, cb) {
133
-
134
- // Required for mixed types
135
- investment.markModified('details');
136
-
137
132
  if (!investment) { return cb(new Error('investment is required'), null); }
138
133
  investment.save(cb);
139
134
  };
@@ -171,7 +166,6 @@ module.exports = function(mongoose, config) {
171
166
  ///////////////////////////////////////////////////////////////////////////////////////
172
167
 
173
168
  Investment.set('toJSON', { virtuals: true });
174
- Investment.set('usePushEach', true);
175
169
  Investment.set('autoIndex', false);
176
170
 
177
171
  Investment.on('index', function(err) { console.log('error building Investment indexes: ' + err); });