@dhyasama/totem-models 10.27.0 → 10.28.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
@@ -340,44 +340,6 @@ module.exports = function(mongoose, config) {
340
340
 
341
341
  };
342
342
 
343
- Deal.statics.modifyCustomField = function modifyCustomField(id, key, value, customerId, options, cb) {
344
-
345
- const self = this;
346
-
347
- if (!cb) { throw new Error('cb is required'); }
348
- if (!id) { return cb(new Error('id is required'), null); }
349
- if (!mongoose.Types.ObjectId.isValid(id)) { return cb(new Error('id is not a valid ObjectId'), null); }
350
- if (!key) { return cb(new Error('key is required'), null); }
351
- if (!customerId) { return cb(new Error('customerId is required'), null); }
352
- if (!mongoose.Types.ObjectId.isValid(customerId)) { return cb(new Error('customerId is not a valid ObjectId'), null); }
353
-
354
- // Dynamic custom field name requires building object this way, rather than { 'customField.' + key: value }
355
- let keyValue = {};
356
- keyValue['customFields.' + key] = value;
357
-
358
- const historyItem = {
359
- timestamp: new Date(),
360
- account: options.account,
361
- stage: null,
362
- description: 'The deal details were updated'
363
- };
364
-
365
- const update = {
366
- $set: keyValue,
367
- $push: { history: historyItem }
368
- };
369
-
370
- const match = {
371
- _id: id,
372
- customer: customerId
373
- };
374
-
375
- const query = self.updateOne(match, update, { upsert: false, new: true });
376
-
377
- query.exec(cb);
378
-
379
- };
380
-
381
343
  Deal.statics.modifyPoll = function modifyPoll(id, key, voterid, vote, customerId, options, cb) {
382
344
 
383
345
  // Poll is the custom field type, but the actual field name/key can be anything
@@ -419,6 +381,17 @@ module.exports = function(mongoose, config) {
419
381
  votedOn: new Date()
420
382
  };
421
383
 
384
+ let addFields = {};
385
+ addFields[path] = {
386
+ "$cond": {
387
+ "if": {
388
+ "$ne": [ { "$type": "$" + path }, "array" ]
389
+ },
390
+ "then": [],
391
+ "else": "$" + path
392
+ }
393
+ };
394
+
422
395
  // Dynamic custom field name requires building object this way, rather than { 'customField.' + key: value }
423
396
  let set = {};
424
397
  set[path] = {
@@ -457,7 +430,10 @@ module.exports = function(mongoose, config) {
457
430
  '_id': id,
458
431
  customer: customerId
459
432
  },
460
- [{ $set: set }], // must be part of aggregation pipeline for set conditions to work
433
+ [
434
+ { $addFields: addFields },
435
+ { $set: set }
436
+ ], // must be part of aggregation pipeline for conditions to work
461
437
  { upsert: false, new: true }
462
438
  );
463
439
 
@@ -518,6 +494,43 @@ module.exports = function(mongoose, config) {
518
494
 
519
495
  };
520
496
 
497
+ Deal.statics.modifyProperty = function modifyProperty(id, key, value, customerId, options, cb) {
498
+
499
+ const self = this;
500
+
501
+ if (!cb) { throw new Error('cb is required'); }
502
+ if (!id) { return cb(new Error('id is required'), null); }
503
+ if (!mongoose.Types.ObjectId.isValid(id)) { return cb(new Error('id is not a valid ObjectId'), null); }
504
+ if (!key) { return cb(new Error('key is required'), null); }
505
+ if (!customerId) { return cb(new Error('customerId is required'), null); }
506
+ if (!mongoose.Types.ObjectId.isValid(customerId)) { return cb(new Error('customerId is not a valid ObjectId'), null); }
507
+
508
+ let keyValue = {};
509
+ keyValue[key] = value;
510
+
511
+ const historyItem = {
512
+ timestamp: new Date(),
513
+ account: options.account,
514
+ stage: null,
515
+ description: 'The deal details were updated'
516
+ };
517
+
518
+ const update = {
519
+ $set: keyValue,
520
+ $push: { history: historyItem }
521
+ };
522
+
523
+ const match = {
524
+ _id: id,
525
+ customer: customerId
526
+ };
527
+
528
+ const query = self.updateOne(match, update, { upsert: false, new: true });
529
+
530
+ query.exec(cb);
531
+
532
+ };
533
+
521
534
  Deal.statics.upsert = function upsert(deal, options, cb) {
522
535
 
523
536
  // Deal is dumb and doesn't validate customer stages or duplicate deals. That responsibility is with the org.
@@ -230,6 +230,38 @@ module.exports = function(mongoose, config) {
230
230
 
231
231
  };
232
232
 
233
+ Interaction.statics.getMostRecent2 = function getMostRecent2(customerId, personIds, timestamp, cb) {
234
+
235
+ // Gets the most recent interaction with a customer across a list of people
236
+ // Typically used to get the last interaction the firm had with an org
237
+ // 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
238
+
239
+ if (!cb) { throw new Error('cb is required'); }
240
+ if (!personIds) { return cb(new Error('personIds is required'), null); }
241
+ if (personIds.length === 0) { return cb(new Error('personIds has no person ids'), null); }
242
+ if (!customerId) { return cb(new Error('customerId is required'), null); }
243
+ if (!mongoose.Types.ObjectId.isValid(customerId)) { return cb(new Error('customerId is not a valid ObjectId'), null); }
244
+
245
+ // Note - This filters by customer so is NOT usable by admin tools
246
+
247
+ const self = this;
248
+
249
+ // only for this customer
250
+ let query = self.findOne({ totemCustomerId: customerId });
251
+
252
+ // with these outside attendees
253
+ query.where({ 'attendees.external': { $in : personIds } });
254
+
255
+ // only events that have already happened
256
+ query.where({'startTime': { $lte: new Date() }});
257
+
258
+ // Optionally limit to events newer than timestamp (useful for seeing if anything since a previous event)
259
+ if (timestamp) query.where({'startTime': { $gt: timestamp }});
260
+
261
+ query.exec(cb);
262
+
263
+ };
264
+
233
265
  Interaction.statics.getNotes = function getNotes(personIds, options, cb) {
234
266
 
235
267
  if (!cb) { throw new Error('cb is required'); }
package/lib/Message.js CHANGED
@@ -249,6 +249,40 @@ module.exports = function(mongoose, config) {
249
249
 
250
250
  };
251
251
 
252
+ Message.statics.getMostRecent2 = function getMostRecent2(orgId, personIds, options, cb) {
253
+
254
+ if (!cb) { throw new Error('cb is required'); }
255
+ if (!orgId) { return cb(new Error('orgId is required'), null); }
256
+ if (!mongoose.Types.ObjectId.isValid(orgId)) { return cb(new Error('orgId is not a valid ObjectId'), null); }
257
+ if (!options) { return cb(new Error('options is required'), null); }
258
+ if (!options.CUSTOMER_ID) { return cb(new Error('options.CUSTOMER_ID is required'), null); }
259
+ if (!mongoose.Types.ObjectId.isValid(options.CUSTOMER_ID)) { return cb(new Error('options.CUSTOMER_ID is not a valid ObjectId'), null); }
260
+
261
+ // Note - This filters by customer so is NOT usable by admin tools
262
+
263
+ const self = this;
264
+
265
+ // Must match customer
266
+ let query = self.findOne({ customer: options.CUSTOMER_ID });
267
+
268
+ personIds = personIds || [];
269
+
270
+ // Match either the org or a list of people
271
+ if (personIds.length >= 1) { query.where({ $or: [{organization: orgId}, {'recipients.external': { $in : personIds }}] }); }
272
+
273
+ // No people, just do the org
274
+ else { query.where({ organization: orgId }); }
275
+
276
+ // Optionally limit search space to message newer than some timestamp
277
+ if (options.timestamp) query.where({'messageDate': { $gt: options.timestamp }});
278
+
279
+ // Don't need the full raw message
280
+ query.select({ 'raw': 0 });
281
+
282
+ query.exec(cb);
283
+
284
+ };
285
+
252
286
  Message.statics.getForPeople = function getForPeople(personIds, options, cb) {
253
287
 
254
288
  if (!cb) { throw new Error('cb is required'); }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@dhyasama/totem-models",
3
- "version": "10.27.0",
3
+ "version": "10.28.0",
4
4
  "author": "Jason Reynolds",
5
5
  "license": "UNLICENSED",
6
6
  "description": "Models for Totem platform",