@dhyasama/totem-models 6.23.0 → 6.24.0

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/lib/Deal.js CHANGED
@@ -287,6 +287,25 @@ module.exports = function(mongoose, config) {
287
287
  });
288
288
  };
289
289
 
290
+ Deal.statics.modifyById = function(id, update, cb) {
291
+
292
+ // VERY IMPORTANT NOTE
293
+ // findByIdAndUpdate and findOneAndUpdate do not trigger pre-save hook so that code will not run here
294
+
295
+ if (!cb) throw new Error('cb is required');
296
+ if (!id) { return cb(new Error('id is required'), null); }
297
+ if (!update) { return cb(new Error('update is required'), null); }
298
+
299
+ var self = this;
300
+
301
+ // https://mongoosejs.com/docs/api.html#model_Model.findOneAndUpdate
302
+ // options runValidators defaults false which is ok since we have upsert false
303
+ // new returns the updated document
304
+
305
+ self.findByIdAndUpdate(id, update, { upsert: false, new: true }, cb);
306
+
307
+ };
308
+
290
309
  Deal.statics.upsert = function upsert(deal, user, cb) {
291
310
 
292
311
  // Deal is dumb and doesn't validate customer stages or duplicate deals. That responsibility is with the org.
package/lib/Document.js CHANGED
@@ -133,6 +133,25 @@ module.exports = function(mongoose, config) {
133
133
  }).exec(cb);
134
134
  };
135
135
 
136
+ Document.statics.modifyById = function(id, update, cb) {
137
+
138
+ // VERY IMPORTANT NOTE
139
+ // findByIdAndUpdate and findOneAndUpdate do not trigger pre-save hook so that code will not run here
140
+
141
+ if (!cb) throw new Error('cb is required');
142
+ if (!id) { return cb(new Error('id is required'), null); }
143
+ if (!update) { return cb(new Error('update is required'), null); }
144
+
145
+ var self = this;
146
+
147
+ // https://mongoosejs.com/docs/api.html#model_Model.findOneAndUpdate
148
+ // options runValidators defaults false which is ok since we have upsert false
149
+ // new returns the updated document
150
+
151
+ self.findByIdAndUpdate(id, update, { upsert: false, new: true }, cb);
152
+
153
+ };
154
+
136
155
  Document.statics.upsert = function upsert(doc, cb) {
137
156
 
138
157
  if (!doc) { return cb(new Error('doc is required'), null); }
package/lib/Flag.js CHANGED
@@ -66,6 +66,25 @@ module.exports = function(mongoose, config) {
66
66
  }).exec(cb);
67
67
  };
68
68
 
69
+ Flag.statics.modifyById = function(id, update, cb) {
70
+
71
+ // VERY IMPORTANT NOTE
72
+ // findByIdAndUpdate and findOneAndUpdate do not trigger pre-save hook so that code will not run here
73
+
74
+ if (!cb) throw new Error('cb is required');
75
+ if (!id) { return cb(new Error('id is required'), null); }
76
+ if (!update) { return cb(new Error('update is required'), null); }
77
+
78
+ var self = this;
79
+
80
+ // https://mongoosejs.com/docs/api.html#model_Model.findOneAndUpdate
81
+ // options runValidators defaults false which is ok since we have upsert false
82
+ // new returns the updated document
83
+
84
+ self.findByIdAndUpdate(id, update, { upsert: false, new: true }, cb);
85
+
86
+ };
87
+
69
88
  Flag.statics.upsert = function(flag, cb) {
70
89
  flag.save(cb);
71
90
  };
@@ -194,6 +194,25 @@ module.exports = function(mongoose, config) {
194
194
 
195
195
  };
196
196
 
197
+ Interaction.statics.modifyById = function(id, update, cb) {
198
+
199
+ // VERY IMPORTANT NOTE
200
+ // findByIdAndUpdate and findOneAndUpdate do not trigger pre-save hook so that code will not run here
201
+
202
+ if (!cb) throw new Error('cb is required');
203
+ if (!id) { return cb(new Error('id is required'), null); }
204
+ if (!update) { return cb(new Error('update is required'), null); }
205
+
206
+ var self = this;
207
+
208
+ // https://mongoosejs.com/docs/api.html#model_Model.findOneAndUpdate
209
+ // options runValidators defaults false which is ok since we have upsert false
210
+ // new returns the updated document
211
+
212
+ self.findByIdAndUpdate(id, update, { upsert: false, new: true }, cb);
213
+
214
+ };
215
+
197
216
  Interaction.statics.upsert = function upsert(interaction, cb) {
198
217
 
199
218
  if (!interaction) { return cb(new Error('Interaction is required'), null); }
@@ -726,6 +726,25 @@ module.exports = function(mongoose, config) {
726
726
 
727
727
  };
728
728
 
729
+ LimitedPartner.statics.modifyById = function(id, update, cb) {
730
+
731
+ // VERY IMPORTANT NOTE
732
+ // findByIdAndUpdate and findOneAndUpdate do not trigger pre-save hook so that code will not run here
733
+
734
+ if (!cb) throw new Error('cb is required');
735
+ if (!id) { return cb(new Error('id is required'), null); }
736
+ if (!update) { return cb(new Error('update is required'), null); }
737
+
738
+ var self = this;
739
+
740
+ // https://mongoosejs.com/docs/api.html#model_Model.findOneAndUpdate
741
+ // options runValidators defaults false which is ok since we have upsert false
742
+ // new returns the updated document
743
+
744
+ self.findByIdAndUpdate(id, update, { upsert: false, new: true }, cb);
745
+
746
+ };
747
+
729
748
  LimitedPartner.statics.upsert = function(lp, username, role, cb) {
730
749
 
731
750
  if (!lp) { return cb(new Error('lp is required'), null); }
package/lib/List.js CHANGED
@@ -209,6 +209,25 @@ module.exports = function(mongoose, config) {
209
209
 
210
210
  };
211
211
 
212
+ List.statics.modifyById = function(id, update, cb) {
213
+
214
+ // VERY IMPORTANT NOTE
215
+ // findByIdAndUpdate and findOneAndUpdate do not trigger pre-save hook so that code will not run here
216
+
217
+ if (!cb) throw new Error('cb is required');
218
+ if (!id) { return cb(new Error('id is required'), null); }
219
+ if (!update) { return cb(new Error('update is required'), null); }
220
+
221
+ var self = this;
222
+
223
+ // https://mongoosejs.com/docs/api.html#model_Model.findOneAndUpdate
224
+ // options runValidators defaults false which is ok since we have upsert false
225
+ // new returns the updated document
226
+
227
+ self.findByIdAndUpdate(id, update, { upsert: false, new: true }, cb);
228
+
229
+ };
230
+
212
231
  List.statics.remove = function(id, cb) {
213
232
  this.findOneAndRemove({
214
233
  '_id': id,
package/lib/Message.js CHANGED
@@ -296,6 +296,25 @@ module.exports = function(mongoose, config) {
296
296
 
297
297
  };
298
298
 
299
+ Message.statics.modifyById = function(id, update, cb) {
300
+
301
+ // VERY IMPORTANT NOTE
302
+ // findByIdAndUpdate and findOneAndUpdate do not trigger pre-save hook so that code will not run here
303
+
304
+ if (!cb) throw new Error('cb is required');
305
+ if (!id) { return cb(new Error('id is required'), null); }
306
+ if (!update) { return cb(new Error('update is required'), null); }
307
+
308
+ var self = this;
309
+
310
+ // https://mongoosejs.com/docs/api.html#model_Model.findOneAndUpdate
311
+ // options runValidators defaults false which is ok since we have upsert false
312
+ // new returns the updated document
313
+
314
+ self.findByIdAndUpdate(id, update, { upsert: false, new: true }, cb);
315
+
316
+ };
317
+
299
318
  Message.statics.upsert = function(message, cb) {
300
319
 
301
320
  var timestamp = new Date();
package/lib/News.js CHANGED
@@ -88,6 +88,25 @@ module.exports = function(mongoose, config) {
88
88
 
89
89
  };
90
90
 
91
+ News.statics.modifyById = function(id, update, cb) {
92
+
93
+ // VERY IMPORTANT NOTE
94
+ // findByIdAndUpdate and findOneAndUpdate do not trigger pre-save hook so that code will not run here
95
+
96
+ if (!cb) throw new Error('cb is required');
97
+ if (!id) { return cb(new Error('id is required'), null); }
98
+ if (!update) { return cb(new Error('update is required'), null); }
99
+
100
+ var self = this;
101
+
102
+ // https://mongoosejs.com/docs/api.html#model_Model.findOneAndUpdate
103
+ // options runValidators defaults false which is ok since we have upsert false
104
+ // new returns the updated document
105
+
106
+ self.findByIdAndUpdate(id, update, { upsert: false, new: true }, cb);
107
+
108
+ };
109
+
91
110
  News.statics.upsert = function (news, cb) {
92
111
  news.markModified('extracted');
93
112
  news.save(cb);
package/lib/Note.js CHANGED
@@ -74,6 +74,25 @@ module.exports = function(mongoose, config) {
74
74
 
75
75
  };
76
76
 
77
+ Note.statics.modifyById = function(id, update, cb) {
78
+
79
+ // VERY IMPORTANT NOTE
80
+ // findByIdAndUpdate and findOneAndUpdate do not trigger pre-save hook so that code will not run here
81
+
82
+ if (!cb) throw new Error('cb is required');
83
+ if (!id) { return cb(new Error('id is required'), null); }
84
+ if (!update) { return cb(new Error('update is required'), null); }
85
+
86
+ var self = this;
87
+
88
+ // https://mongoosejs.com/docs/api.html#model_Model.findOneAndUpdate
89
+ // options runValidators defaults false which is ok since we have upsert false
90
+ // new returns the updated document
91
+
92
+ self.findByIdAndUpdate(id, update, { upsert: false, new: true }, cb);
93
+
94
+ };
95
+
77
96
  Note.statics.upsert = function(note, cb) {
78
97
  note.save(cb);
79
98
  };
package/lib/Person.js CHANGED
@@ -1107,6 +1107,25 @@ module.exports = function(mongoose, config) {
1107
1107
  customerCompanyIds = ids;
1108
1108
  };
1109
1109
 
1110
+ Person.statics.modifyById = function(id, update, cb) {
1111
+
1112
+ // VERY IMPORTANT NOTE
1113
+ // findByIdAndUpdate and findOneAndUpdate do not trigger pre-save hook so that code will not run here
1114
+
1115
+ if (!cb) throw new Error('cb is required');
1116
+ if (!id) { return cb(new Error('id is required'), null); }
1117
+ if (!update) { return cb(new Error('update is required'), null); }
1118
+
1119
+ var self = this;
1120
+
1121
+ // https://mongoosejs.com/docs/api.html#model_Model.findOneAndUpdate
1122
+ // options runValidators defaults false which is ok since we have upsert false
1123
+ // new returns the updated document
1124
+
1125
+ self.findByIdAndUpdate(id, update, { upsert: false, new: true }, cb);
1126
+
1127
+ };
1128
+
1110
1129
  Person.statics.upsert = function(currentPerson, username, cb) {
1111
1130
 
1112
1131
  if (!currentPerson) { return cb(new Error('Current Person is required'), null); }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@dhyasama/totem-models",
3
- "version": "6.23.0",
3
+ "version": "6.24.0",
4
4
  "author": "Jason Reynolds",
5
5
  "license": "UNLICENSED",
6
6
  "description": "Models for Totem platform",
package/test/Deal.js CHANGED
@@ -92,7 +92,13 @@ describe('Deal', function() {
92
92
  var deal = new Deal({
93
93
  customer: customer,
94
94
  organization: org,
95
- stage: 'New'
95
+ stage: 'New',
96
+ source: {
97
+ person: new mongoose.Types.ObjectId()
98
+ },
99
+ referrer: {
100
+ person: new mongoose.Types.ObjectId()
101
+ }
96
102
  });
97
103
 
98
104
  Deal.upsert(deal, account, function(err, result) {
@@ -159,6 +165,33 @@ describe('Deal', function() {
159
165
 
160
166
  });
161
167
 
168
+ it('modifies a deal by id', function (done) {
169
+
170
+ Deal.getById(dealId, function(err, deal) {
171
+
172
+ should.not.exist(err);
173
+ should.exist(deal);
174
+ deal._id.toString().should.equal(dealId.toString());
175
+
176
+ var newSource = new mongoose.Types.ObjectId();
177
+
178
+ var update = {
179
+ $set: {
180
+ 'source.person': newSource
181
+ }
182
+ };
183
+
184
+ Deal.modifyById(dealId, update, function (err, result) {
185
+ should.not.exist(err);
186
+ should.exist(result);
187
+ result.source.person.toString().should.equal(newSource.toString());
188
+ done();
189
+ });
190
+
191
+ });
192
+
193
+ });
194
+
162
195
  it('gets all deals for a customer', function(done) {
163
196
 
164
197
  var d = new Deal({
package/test/Document.js CHANGED
@@ -325,6 +325,33 @@ describe('Document', function() {
325
325
  });
326
326
  });
327
327
 
328
+ it('modifies a doc by id', function (done) {
329
+
330
+ Document.getById(documentId, function(err, doc) {
331
+
332
+ should.not.exist(err);
333
+ should.exist(doc);
334
+ doc._id.toString().should.equal(documentId.toString());
335
+
336
+ var newCreatedBy = new mongoose.Types.ObjectId();
337
+
338
+ var update = {
339
+ $set: {
340
+ 'createdBy': newCreatedBy
341
+ }
342
+ };
343
+
344
+ Document.modifyById(documentId, update, function (err, result) {
345
+ should.not.exist(err);
346
+ should.exist(result);
347
+ result.createdBy.toString().should.equal(newCreatedBy.toString());
348
+ done();
349
+ });
350
+
351
+ });
352
+
353
+ });
354
+
328
355
  it('deletes a document', function(done) {
329
356
  Document.delete(documentId, function(err, result) {
330
357
  should.not.exist(err);
package/test/Flag.js CHANGED
@@ -236,6 +236,33 @@ describe('Flag', function() {
236
236
  });
237
237
  });
238
238
 
239
+ it('modifies a flag by id', function (done) {
240
+
241
+ Flag.getById(flagId, function(err, flag) {
242
+
243
+ should.not.exist(err);
244
+ should.exist(flag);
245
+ flag._id.toString().should.equal(flagId.toString());
246
+
247
+ var newCreatedBy = new mongoose.Types.ObjectId();
248
+
249
+ var update = {
250
+ $set: {
251
+ 'createdBy': newCreatedBy
252
+ }
253
+ };
254
+
255
+ Flag.modifyById(flagId, update, function (err, result) {
256
+ should.not.exist(err);
257
+ should.exist(result);
258
+ result.createdBy.toString().should.equal(newCreatedBy.toString());
259
+ done();
260
+ });
261
+
262
+ });
263
+
264
+ });
265
+
239
266
  it('deletes a flag', function(done) {
240
267
  Flag.delete(flagId, function(err, result) {
241
268
  should.not.exist(err);
package/test/List.js CHANGED
@@ -262,6 +262,41 @@ describe('List', function() {
262
262
 
263
263
  });
264
264
 
265
+ it('modifies a list by id', function (done) {
266
+
267
+ List.getById(myAwesomeList.id, function(err, list) {
268
+
269
+ should.not.exist(err);
270
+ should.exist(list);
271
+ list._id.toString().should.equal(myAwesomeList.id.toString());
272
+
273
+ var newCreatedBy = new mongoose.Types.ObjectId();
274
+ var newPerson = new mongoose.Types.ObjectId();
275
+ var newOrg = new mongoose.Types.ObjectId();
276
+
277
+ var update = {
278
+ $set: {
279
+ 'createdBy': newCreatedBy,
280
+ 'people': [newPerson],
281
+ 'organizations': [newOrg]
282
+ }
283
+ };
284
+
285
+ List.modifyById(myAwesomeList.id, update, function (err, result) {
286
+ should.not.exist(err);
287
+ should.exist(result);
288
+ result.createdBy.toString().should.equal(newCreatedBy.toString());
289
+ result.people.length.should.equal(1);
290
+ result.people[0].toString().should.equal(newPerson.toString());
291
+ result.organizations.length.should.equal(1);
292
+ result.organizations[0].toString().should.equal(newOrg.toString());
293
+ done();
294
+ });
295
+
296
+ });
297
+
298
+ });
299
+
265
300
  it('deletes a list', function(done) {
266
301
 
267
302
  List.remove(myAwesomeList.id, function(err, result) {
package/test/Message.js CHANGED
@@ -328,4 +328,23 @@ describe('Message', function() {
328
328
 
329
329
  });
330
330
 
331
+ it('modifies a message by id', function (done) {
332
+
333
+ var newOrg = new mongoose.Types.ObjectId();
334
+
335
+ var update = {
336
+ $set: {
337
+ 'organization': newOrg
338
+ }
339
+ };
340
+
341
+ Message.modifyById(messageOne.id, update, function (err, result) {
342
+ should.not.exist(err);
343
+ should.exist(result);
344
+ result.organization.toString().should.equal(newOrg.toString());
345
+ done();
346
+ });
347
+
348
+ });
349
+
331
350
  });
package/test/Note.js CHANGED
@@ -236,6 +236,33 @@ describe('Note', function() {
236
236
  });
237
237
  });
238
238
 
239
+ it('modifies a note by id', function (done) {
240
+
241
+ Note.getById(noteId, function(err, note) {
242
+
243
+ should.not.exist(err);
244
+ should.exist(note);
245
+ note._id.toString().should.equal(noteId.toString());
246
+
247
+ var newCreatedBy = new mongoose.Types.ObjectId();
248
+
249
+ var update = {
250
+ $set: {
251
+ 'createdBy': newCreatedBy
252
+ }
253
+ };
254
+
255
+ Note.modifyById(noteId, update, function (err, result) {
256
+ should.not.exist(err);
257
+ should.exist(result);
258
+ result.createdBy.toString().should.equal(newCreatedBy.toString());
259
+ done();
260
+ });
261
+
262
+ });
263
+
264
+ });
265
+
239
266
  it('deletes a note', function(done) {
240
267
  Note.delete(noteId, function(err, result) {
241
268
  should.not.exist(err);