@dhyasama/totem-models 10.27.0 → 10.27.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.
Files changed (2) hide show
  1. package/lib/Deal.js +52 -39
  2. package/package.json +1 -1
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.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@dhyasama/totem-models",
3
- "version": "10.27.0",
3
+ "version": "10.27.1",
4
4
  "author": "Jason Reynolds",
5
5
  "license": "UNLICENSED",
6
6
  "description": "Models for Totem platform",