@dhyasama/totem-models 10.27.1 → 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.
@@ -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.1",
3
+ "version": "10.28.0",
4
4
  "author": "Jason Reynolds",
5
5
  "license": "UNLICENSED",
6
6
  "description": "Models for Totem platform",