@dhyasama/totem-models 2.0.0 → 2.1.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.
@@ -13,7 +13,7 @@ module.exports = function(mongoose, config) {
13
13
  var Interaction = new Schema({
14
14
 
15
15
  calendarEventId: { type: Schema.ObjectId, ref: 'CalendarEvent', required: true }, // the event from the original pull
16
- providerEventId: { type: String, required: true, index: true, trim: true }, // unique id from the calendar provider
16
+ providerEventId: { type: String, required: true, index: true, trim: true, unique: true }, // unique id from the calendar provider
17
17
  totemCustomerId: { type: Schema.ObjectId, ref: 'Organization', index: true, required: true },
18
18
  summary: { type: String, required: true, trim: true },
19
19
  startTime: { type: Date, index: true },
@@ -28,6 +28,15 @@ module.exports = function(mongoose, config) {
28
28
 
29
29
  ///////////////////////////////////////
30
30
 
31
+ Interaction.statics.addNote = function(interactionId, creatorPersonId, text, cb) {
32
+
33
+ Note.createForModel({
34
+ createdBy: creatorPersonId,
35
+ text: text
36
+ }, this, interactionId, cb);
37
+
38
+ };
39
+
31
40
  Interaction.statics.delete = function(providerEventId, totemCustomerId, cb) {
32
41
 
33
42
  var self = this;
@@ -39,6 +48,27 @@ module.exports = function(mongoose, config) {
39
48
 
40
49
  };
41
50
 
51
+ Interaction.statics.deleteNote = function(noteId, cb) {
52
+
53
+ // Delete the note itself along with any references
54
+
55
+ var self = this;
56
+
57
+ var removeReferences = function removeReferences(callback) {
58
+ self.update({}, {
59
+ $pull: { 'notes' : [noteId] }
60
+ }, callback);
61
+ };
62
+
63
+ async.series([
64
+ Note.delete.bind(Note, noteId),
65
+ removeReferences
66
+ ], function(err, results) {
67
+ return cb(err, null);
68
+ });
69
+
70
+ };
71
+
42
72
  Interaction.statics.getForCustomer = function getForCustomer(customerId, cb) {
43
73
 
44
74
  var self = this;
@@ -69,7 +99,10 @@ module.exports = function(mongoose, config) {
69
99
  query.where({ startTime: { $lte: options.before }});
70
100
  query.populate('attendees.internal', 'name title avatarUrl');
71
101
  query.populate('attendees.external', 'name title avatarUrl');
72
- query.populate('notes');
102
+ query.populate({
103
+ path: 'notes',
104
+ match: { customer: config.CUSTOMER_ID }
105
+ });
73
106
  query.sort(options.sort);
74
107
 
75
108
  if (options.attendee) { query.where({'attendees.internal': options.attendee}); }
@@ -84,6 +117,21 @@ module.exports = function(mongoose, config) {
84
117
 
85
118
  };
86
119
 
120
+ Interaction.statics.getNotes = function getForPeople(personIds, options, cb) {
121
+
122
+ var self = this;
123
+ var query = self.find({ totemCustomerId: config.CUSTOMER_ID });
124
+
125
+ query.where({'attendees.external': { $in : personIds }});
126
+ query.populate({
127
+ path: 'notes',
128
+ match: { customer: config.CUSTOMER_ID }
129
+ });
130
+ query.sort(options.sort);
131
+ query.exec(cb);
132
+
133
+ };
134
+
87
135
  Interaction.statics.upsert = function upsert(interaction, cb) {
88
136
 
89
137
  if (!interaction) { return cb(new Error('Interaction is required'), null); }
@@ -92,7 +140,7 @@ module.exports = function(mongoose, config) {
92
140
 
93
141
  };
94
142
 
95
- Interaction.set('autoIndex', false);
143
+ Interaction.set('autoIndex', true);
96
144
  Interaction.on('index', function(err) { console.log('error building interaction indexes: ' + err); });
97
145
 
98
146
  mongoose.model('Interaction', Interaction);
package/lib/Note.js CHANGED
@@ -23,65 +23,6 @@ module.exports = function(mongoose, config) {
23
23
 
24
24
  });
25
25
 
26
- Note.statics.createForCalendarEvent = function(providerEventId, creatorPersonId, text, cb) {
27
-
28
- var self = this;
29
- var Person = mongoose.model('Person');
30
-
31
- var queue = async.queue(function(task, callback) {
32
- Person.update(
33
- { '_id': task.personId, 'calendarEventSummaries.providerEventId': providerEventId },
34
- { "$push": { "calendarEventSummaries.$.notes": task.note } },
35
- callback);
36
- }, 8);
37
-
38
- // get all people with cal event summary with providerEventId
39
- Person
40
- .find({ 'calendarEventSummaries.providerEventId': providerEventId })
41
- .exec(function(err, externalAttendees) {
42
-
43
- if (err) return cb(err, null);
44
-
45
- var note = {
46
- createdBy: creatorPersonId,
47
- text: text,
48
- createdOn: new Date(),
49
- customer: config.CUSTOMER_ID
50
- };
51
-
52
- // create the note once
53
- self.create(note, function(err, result) {
54
-
55
- if (err) return cb(err, null);
56
-
57
- queue.drain = function() { return cb(null, result); };
58
-
59
- // add it to each person that attended the event
60
- _.each(externalAttendees, function(person) {
61
-
62
- // find the event summary
63
- var eventSummary = _.find(person.calendarEventSummaries, function(ces) {
64
- return ces.providerEventId == providerEventId;
65
- });
66
-
67
- // attach note reference to event summary
68
- if (eventSummary) {
69
- queue.push({
70
- personId: person._id,
71
- note: result
72
- }, function(err, result) {
73
- if (err) console.log('ERR:', err);
74
- });
75
- }
76
-
77
- });
78
-
79
- });
80
-
81
- });
82
-
83
- };
84
-
85
26
  Note.statics.createForModel = function(note, Model, modelId, cb) {
86
27
 
87
28
  var addToModel = function(err, createdNote) {
package/lib/Person.js CHANGED
@@ -1,4 +1,4 @@
1
- "use strict";
1
+ "use strict";
2
2
 
3
3
  module.exports = function(mongoose, config) {
4
4
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@dhyasama/totem-models",
3
- "version": "2.0.0",
3
+ "version": "2.1.1",
4
4
  "author": "Jason Reynolds",
5
5
  "license": "UNLICENSED",
6
6
  "description": "Models for Totem platform",