@dhyasama/totem-models 11.2.0 → 11.4.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/index.js +1 -0
- package/lib/DiffbotOrganization.js +6 -6
- package/lib/Folder.js +84 -0
- package/lib/Interaction.js +43 -0
- package/lib/Message.js +15 -2
- package/lib/News.js +19 -8
- package/package.json +1 -1
package/index.js
CHANGED
|
@@ -67,6 +67,7 @@ var bootstrap = function(mongoose, config) {
|
|
|
67
67
|
require('./lib/Event.js')(mongoose, config);
|
|
68
68
|
require('./lib/EventAttendee.js')(mongoose, config);
|
|
69
69
|
require('./lib/Financials.js')(mongoose, config);
|
|
70
|
+
require('./lib/Folder.js')(mongoose, config);
|
|
70
71
|
require('./lib/Interaction.js')(mongoose, config);
|
|
71
72
|
require('./lib/Investment.js')(mongoose, config);
|
|
72
73
|
require('./lib/List.js')(mongoose, config);
|
|
@@ -12,13 +12,13 @@ module.exports = function(mongoose, config) {
|
|
|
12
12
|
org: {
|
|
13
13
|
type: Schema.ObjectId,
|
|
14
14
|
ref: 'Organization',
|
|
15
|
-
required:
|
|
16
|
-
|
|
15
|
+
required: true,
|
|
16
|
+
unique: true
|
|
17
17
|
},
|
|
18
18
|
|
|
19
|
-
updatedOn: { type: Date, required:
|
|
19
|
+
updatedOn: { type: Date, required: true, default: new Date() },
|
|
20
20
|
|
|
21
|
-
entity: { type: Schema.Types.Mixed, required:
|
|
21
|
+
entity: { type: Schema.Types.Mixed, required: true },
|
|
22
22
|
|
|
23
23
|
});
|
|
24
24
|
|
|
@@ -33,7 +33,7 @@ module.exports = function(mongoose, config) {
|
|
|
33
33
|
options = helpers.getDefaultOptions(options);
|
|
34
34
|
|
|
35
35
|
self
|
|
36
|
-
.
|
|
36
|
+
.findOne({ org: id })
|
|
37
37
|
.populate('org')
|
|
38
38
|
.exec(cb);
|
|
39
39
|
|
|
@@ -50,7 +50,7 @@ module.exports = function(mongoose, config) {
|
|
|
50
50
|
options = helpers.getDefaultOptions(options);
|
|
51
51
|
|
|
52
52
|
self
|
|
53
|
-
.
|
|
53
|
+
.findOne({ org: id })
|
|
54
54
|
.exec(cb);
|
|
55
55
|
|
|
56
56
|
};
|
package/lib/Folder.js
ADDED
|
@@ -0,0 +1,84 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
module.exports = function(mongoose, config) {
|
|
4
|
+
|
|
5
|
+
var Schema = mongoose.Schema;
|
|
6
|
+
var _ = require('underscore');
|
|
7
|
+
|
|
8
|
+
var Folder = new Schema({
|
|
9
|
+
name: { type: String, required: true },
|
|
10
|
+
customer: { type: Schema.ObjectId, ref: 'Organization', required: true },
|
|
11
|
+
entity: {
|
|
12
|
+
id: { type: Schema.ObjectId, required: true },
|
|
13
|
+
model: { type: String, required: true, enum: ['Organization', 'Person', 'LimitedPartner'] }
|
|
14
|
+
},
|
|
15
|
+
parent: { type: Schema.ObjectId, ref: 'Folder' }
|
|
16
|
+
});
|
|
17
|
+
|
|
18
|
+
Folder.statics.modifyById = function(id, update, cb) {
|
|
19
|
+
|
|
20
|
+
// VERY IMPORTANT NOTE
|
|
21
|
+
// findByIdAndUpdate and findOneAndUpdate do not trigger pre-save hook so that code will not run here
|
|
22
|
+
// at this time, the only known and approved use of this method is for the org edit form in totem web
|
|
23
|
+
|
|
24
|
+
if (!cb) throw new Error('cb is required');
|
|
25
|
+
if (!id) { return cb(new Error('id is required'), null); }
|
|
26
|
+
if (!mongoose.Types.ObjectId.isValid(id)) { return cb(new Error('id is not a valid ObjectId'), null); }
|
|
27
|
+
if (!update) { return cb(new Error('update is required'), null); }
|
|
28
|
+
|
|
29
|
+
var self = this;
|
|
30
|
+
|
|
31
|
+
// https://mongoosejs.com/docs/api.html#model_Model.findOneAndUpdate
|
|
32
|
+
// options runValidators defaults false which is ok since we have upsert false
|
|
33
|
+
// new returns the updated document
|
|
34
|
+
|
|
35
|
+
self.findByIdAndUpdate(id, update, { upsert: false, new: true }, cb);
|
|
36
|
+
|
|
37
|
+
};
|
|
38
|
+
|
|
39
|
+
Folder.statics.getByEntity = function getByEntity(entity, options, cb) {
|
|
40
|
+
|
|
41
|
+
if (!cb) { throw new Error('cb is required'); }
|
|
42
|
+
if (!entity) { return cb(new Error('entity is required'), null); }
|
|
43
|
+
if (!entity.id) { return cb(new Error('entity id is required'), null); }
|
|
44
|
+
if (!mongoose.Types.ObjectId.isValid(entity.id)) { return cb(new Error('entity id is not a valid ObjectId'), null); }
|
|
45
|
+
if (!entity.model) { return cb(new Error('entity model is required'), null); }
|
|
46
|
+
if (!options) { return cb(new Error('options is required'), null); }
|
|
47
|
+
if (!options.CUSTOMER_ID) { return cb(new Error('options.CUSTOMER_ID is required'), null); }
|
|
48
|
+
if (!mongoose.Types.ObjectId.isValid(options.CUSTOMER_ID)) { return cb(new Error('options.CUSTOMER_ID is not a valid ObjectId'), null); }
|
|
49
|
+
|
|
50
|
+
const self = this;
|
|
51
|
+
let query = self.find({
|
|
52
|
+
'entity.id': entity.id,
|
|
53
|
+
'entity.model': entity.model,
|
|
54
|
+
customer: options.CUSTOMER_ID
|
|
55
|
+
});
|
|
56
|
+
|
|
57
|
+
query.exec(cb);
|
|
58
|
+
|
|
59
|
+
};
|
|
60
|
+
|
|
61
|
+
Folder.statics.upsert = function (folder, cb) {
|
|
62
|
+
folder.save(cb);
|
|
63
|
+
};
|
|
64
|
+
|
|
65
|
+
Folder.statics.delete = function (id, options, cb) {
|
|
66
|
+
|
|
67
|
+
const self = this;
|
|
68
|
+
|
|
69
|
+
if (!cb) { throw new Error('cb is required'); }
|
|
70
|
+
if (!id) { return cb(new Error('id is required'), null); }
|
|
71
|
+
if (!mongoose.Types.ObjectId.isValid(id)) { return cb(new Error('id is not a valid ObjectId'), null); }
|
|
72
|
+
|
|
73
|
+
self.findByIdAndRemove(id, cb);
|
|
74
|
+
|
|
75
|
+
};
|
|
76
|
+
|
|
77
|
+
Folder.set('usePushEach', true);
|
|
78
|
+
Folder.set('autoIndex', false);
|
|
79
|
+
Folder.set('usePushEach', true);
|
|
80
|
+
Folder.on('index', function(err) { console.log('error building folder indexes: ' + err); });
|
|
81
|
+
|
|
82
|
+
mongoose.model('Folder', Folder);
|
|
83
|
+
|
|
84
|
+
};
|
package/lib/Interaction.js
CHANGED
|
@@ -189,6 +189,49 @@ module.exports = function(mongoose, config) {
|
|
|
189
189
|
|
|
190
190
|
};
|
|
191
191
|
|
|
192
|
+
Interaction.statics.getForTimeline = function getForTimeline(customerId, options, cb) {
|
|
193
|
+
|
|
194
|
+
if (!cb) { throw new Error('cb is required'); }
|
|
195
|
+
if (!customerId) { return cb(new Error('customerId is required'), null); }
|
|
196
|
+
if (!mongoose.Types.ObjectId.isValid(customerId)) { return cb(new Error('customerId is not a valid ObjectId'), null); }
|
|
197
|
+
|
|
198
|
+
const self = this;
|
|
199
|
+
const startDate = options.startDate || moment().subtract(20, 'years').startOf('day').toISOString();
|
|
200
|
+
const endDate = options.endDate || moment().toISOString();
|
|
201
|
+
|
|
202
|
+
self.aggregate([
|
|
203
|
+
{
|
|
204
|
+
$match: {
|
|
205
|
+
totemCustomerId: customerId,
|
|
206
|
+
startTime: {
|
|
207
|
+
$gte: startDate,
|
|
208
|
+
$lte: endDate
|
|
209
|
+
}
|
|
210
|
+
}
|
|
211
|
+
},
|
|
212
|
+
{
|
|
213
|
+
$sort: {
|
|
214
|
+
startTime: -1
|
|
215
|
+
}
|
|
216
|
+
},
|
|
217
|
+
{
|
|
218
|
+
$group: {
|
|
219
|
+
_id: "$providerEventId", // Group by providerEventId to deduplicate
|
|
220
|
+
doc: { $first: "$$ROOT" } // Take the first (latest) document encountered for each providerEventId after sorting
|
|
221
|
+
}
|
|
222
|
+
},
|
|
223
|
+
{
|
|
224
|
+
$replaceRoot: { newRoot: "$doc" } // Replace the root to output the original document structure
|
|
225
|
+
},
|
|
226
|
+
{
|
|
227
|
+
$sort: {
|
|
228
|
+
startTime: -1 // Final sort by startTime in descending order to order the output
|
|
229
|
+
}
|
|
230
|
+
}
|
|
231
|
+
]).exec(cb);
|
|
232
|
+
|
|
233
|
+
};
|
|
234
|
+
|
|
192
235
|
Interaction.statics.getMostRecent = function getMostRecent(customerId, personIds, cb) {
|
|
193
236
|
|
|
194
237
|
// Gets the most recent interaction with a customer across a list of people
|
package/lib/Message.js
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
|
|
3
|
+
const moment = require("moment/moment");
|
|
3
4
|
module.exports = function(mongoose, config) {
|
|
4
5
|
|
|
5
6
|
let
|
|
@@ -7,6 +8,7 @@ module.exports = function(mongoose, config) {
|
|
|
7
8
|
Schema = mongoose.Schema,
|
|
8
9
|
async = require('async'),
|
|
9
10
|
_ = require('underscore'),
|
|
11
|
+
moment = require('moment'),
|
|
10
12
|
helpers = require('../helpers'),
|
|
11
13
|
Document = mongoose.model('Document'),
|
|
12
14
|
Note = mongoose.model('Note');
|
|
@@ -246,9 +248,20 @@ module.exports = function(mongoose, config) {
|
|
|
246
248
|
|
|
247
249
|
const self = this;
|
|
248
250
|
|
|
249
|
-
let query = self.find({ customer: options.CUSTOMER_ID
|
|
251
|
+
let query = self.find({ customer: options.CUSTOMER_ID });
|
|
252
|
+
query.find({ organization: { $in : orgIds }});
|
|
253
|
+
|
|
254
|
+
if (options.startDate) {
|
|
255
|
+
const startDate = options.startDate || moment().subtract(20, 'years').startOf('day').toISOString();
|
|
256
|
+
query.where('messageDate').gte(startDate)
|
|
257
|
+
}
|
|
258
|
+
|
|
259
|
+
if (options.endDate) {
|
|
260
|
+
const endDate = options.endDate || moment().toISOString();
|
|
261
|
+
query.where('messageDate').lte(endDate)
|
|
262
|
+
}
|
|
263
|
+
|
|
250
264
|
query.populate('documents');
|
|
251
|
-
|
|
252
265
|
query.exec(cb);
|
|
253
266
|
|
|
254
267
|
};
|
package/lib/News.js
CHANGED
|
@@ -1,11 +1,13 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
|
|
3
|
+
const moment = require("moment/moment");
|
|
3
4
|
module.exports = function(mongoose, config) {
|
|
4
5
|
|
|
5
6
|
let
|
|
6
7
|
|
|
7
8
|
Schema = mongoose.Schema,
|
|
8
9
|
helpers = require('../helpers'),
|
|
10
|
+
moment = require('moment'),
|
|
9
11
|
|
|
10
12
|
News = new Schema({
|
|
11
13
|
|
|
@@ -25,7 +27,7 @@ module.exports = function(mongoose, config) {
|
|
|
25
27
|
|
|
26
28
|
sentiment: { type: Number, required: false, default: null },
|
|
27
29
|
|
|
28
|
-
diffbotUri: { type: String, required:
|
|
30
|
+
diffbotUri: { type: String, required: true, unique: true },
|
|
29
31
|
|
|
30
32
|
org: {
|
|
31
33
|
type: Schema.ObjectId,
|
|
@@ -60,18 +62,28 @@ module.exports = function(mongoose, config) {
|
|
|
60
62
|
|
|
61
63
|
News.statics.getByIds = function getByIds(ids, options, cb) {
|
|
62
64
|
|
|
63
|
-
const self = this;
|
|
64
|
-
|
|
65
65
|
if (!cb) { throw new Error('cb is required'); }
|
|
66
66
|
if (!ids) { return cb(new Error('ids is required'), null); }
|
|
67
67
|
if (!options) { return cb(new Error('options is required'), null); }
|
|
68
68
|
|
|
69
69
|
options = helpers.getDefaultOptions(options);
|
|
70
70
|
|
|
71
|
-
self
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
71
|
+
const self = this;
|
|
72
|
+
|
|
73
|
+
let query = self.find({ org: { $in : ids } })
|
|
74
|
+
|
|
75
|
+
if (options.startDate) {
|
|
76
|
+
const startDate = options.startDate || moment().subtract(20, 'years').startOf('day').toISOString();
|
|
77
|
+
query.where('publishedOn').gte(startDate)
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
if (options.endDate) {
|
|
81
|
+
const endDate = options.endDate || moment().toISOString();
|
|
82
|
+
query.where('publishedOn').lte(endDate)
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
query.sort('-createdOn')
|
|
86
|
+
query.exec(cb);
|
|
75
87
|
|
|
76
88
|
};
|
|
77
89
|
|
|
@@ -95,7 +107,6 @@ module.exports = function(mongoose, config) {
|
|
|
95
107
|
};
|
|
96
108
|
|
|
97
109
|
News.statics.upsert = function (news, cb) {
|
|
98
|
-
news.markModified('extracted');
|
|
99
110
|
news.save(cb);
|
|
100
111
|
};
|
|
101
112
|
|