@dhyasama/totem-models 7.57.0 → 8.0.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/Document.js CHANGED
@@ -1,14 +1,11 @@
1
- "use strict";
1
+ 'use strict';
2
2
 
3
3
  module.exports = function(mongoose, config) {
4
4
 
5
5
  var
6
6
 
7
7
  Schema = mongoose.Schema,
8
- env = process.env.NODE_ENV || 'development',
9
- s3 = require('@dhyasama/ffvc-s3'),
10
- utils = require('@dhyasama/ffvc-utilities'),
11
- postFind = require('mongoose-post-find'),
8
+ utilities = require('@dhyasama/ffvc-utilities'),
12
9
  _ = require('underscore');
13
10
 
14
11
  var Document = new Schema({
@@ -50,14 +47,14 @@ module.exports = function(mongoose, config) {
50
47
 
51
48
  // Probably an Exchange file
52
49
  // Exchange creates files such as ATT00001.htm and ATT00002.txt, one for each attachment
53
- if (name.indexOf('ATT00') == 0) return true;
50
+ if (name.indexOf('ATT00') === 0) return true;
54
51
 
55
52
  // Google Calendar invitation
56
53
  // No need to show since it's in-lined
57
- else if (name == 'INVITE.ICS' || name == 'INVITATION.ICS') return true;
54
+ else if (name === 'INVITE.ICS' || name === 'INVITATION.ICS') return true;
58
55
 
59
56
  // Probably a signature logo
60
- else if (name.indexOf('IMAGE0') == 0 && contentLength < 3000) return true;
57
+ else if (name.indexOf('IMAGE0') === 0 && contentLength < 3000) return true;
61
58
 
62
59
  else return false;
63
60
 
@@ -79,7 +76,7 @@ module.exports = function(mongoose, config) {
79
76
  ///////////////////////////////////////////////////////////////////////////////////////
80
77
 
81
78
  Document.methods.friendlyFilesize = function() {
82
- return utils.humanizeFilesize(this.contentLength);
79
+ return utilities.humanizeFilesize(this.contentLength);
83
80
  };
84
81
 
85
82
 
@@ -91,6 +88,8 @@ module.exports = function(mongoose, config) {
91
88
 
92
89
  Document.statics.createForModel = function(doc, Model, modelId, cb) {
93
90
 
91
+ // No population so no need to scrub
92
+
94
93
  var addToModel = function(err, createdDoc) {
95
94
  if (err) return cb(err, null);
96
95
  Model.findByIdAndUpdate(modelId, { $push: { documents: createdDoc }}, { new: true, upsert: false }, function(err, addedTo) {
@@ -107,52 +106,83 @@ module.exports = function(mongoose, config) {
107
106
 
108
107
  };
109
108
 
110
- Document.statics.delete = function(id, cb) {
109
+ Document.statics.delete = function(id, options, cb) {
111
110
 
112
- var self = this;
111
+ let self = this;
112
+
113
+ if (!cb) { throw new Error('cb is required'); }
114
+ if (!id) { return cb(new Error('id is required'), null); }
115
+ if (!mongoose.Types.ObjectId.isValid(id)) { return cb(new Error('id is not a valid ObjectId'), null); }
116
+ if (!options) { return cb(new Error('options is required'), null); }
117
+ if (!options.CUSTOMER_ID) { return cb(new Error('options.CUSTOMER_ID is required'), null); }
118
+ if (!mongoose.Types.ObjectId.isValid(options.CUSTOMER_ID)) { return cb(new Error('options.CUSTOMER_ID is not a valid ObjectId'), null); }
113
119
 
114
120
  // Not strictly necessary but provides verification that the document being deleted belongs to the customer doing the deleting
115
- self.findOne({
121
+
122
+ let query = self.findOne({
116
123
  '_id': id,
117
- 'customer': config.CUSTOMER_ID
118
- }, function(err, doc) {
124
+ 'customer': options.CUSTOMER_ID
125
+ });
126
+
127
+ query.exec(function(err, doc) {
119
128
 
120
129
  if (err) return cb(err, null);
121
130
  else if (!doc) return cb(null, null);
122
131
 
132
+ // No population so no need to scrub
133
+
123
134
  doc.remove(cb);
124
135
 
125
136
  });
126
137
 
127
138
  };
128
139
 
129
- Document.statics.getById = function (id, cb) {
140
+ Document.statics.getById = function (id, options, cb) {
130
141
 
131
- var self = this;
142
+ let self = this;
132
143
 
133
- self.findOne({
144
+ if (!cb) { throw new Error('cb is required'); }
145
+ if (!id) { return cb(new Error('id is required'), null); }
146
+ if (!mongoose.Types.ObjectId.isValid(id)) { return cb(new Error('id is not a valid ObjectId'), null); }
147
+ if (!options) { return cb(new Error('options is required'), null); }
148
+ if (!options.CUSTOMER_ID) { return cb(new Error('options.CUSTOMER_ID is required'), null); }
149
+ if (!mongoose.Types.ObjectId.isValid(options.CUSTOMER_ID)) { return cb(new Error('options.CUSTOMER_ID is not a valid ObjectId'), null); }
150
+
151
+ let query = self.findOne({
134
152
  '_id': id,
135
- 'customer': config.CUSTOMER_ID
136
- }).exec(cb);
137
-
153
+ 'customer': options.CUSTOMER_ID
154
+ });
155
+
156
+ // No population so no need to scrub
157
+
158
+ query.exec(cb);
159
+
138
160
  };
139
161
 
140
- Document.statics.modifyById = function(id, update, cb) {
162
+ Document.statics.modifyById = function(id, update, options, cb) {
163
+
164
+ let self = this;
141
165
 
142
166
  // VERY IMPORTANT NOTE
143
167
  // findByIdAndUpdate and findOneAndUpdate do not trigger pre-save hook so that code will not run here
144
168
 
145
- if (!cb) throw new Error('cb is required');
169
+ if (!cb) { throw new Error('cb is required'); }
146
170
  if (!id) { return cb(new Error('id is required'), null); }
171
+ if (!mongoose.Types.ObjectId.isValid(id)) { return cb(new Error('id is not a valid ObjectId'), null); }
147
172
  if (!update) { return cb(new Error('update is required'), null); }
148
-
149
- var self = this;
173
+ if (!options) { return cb(new Error('options is required'), null); }
174
+ if (!options.CUSTOMER_ID) { return cb(new Error('options.CUSTOMER_ID is required'), null); }
175
+ if (!mongoose.Types.ObjectId.isValid(options.CUSTOMER_ID)) { return cb(new Error('options.CUSTOMER_ID is not a valid ObjectId'), null); }
150
176
 
151
177
  // https://mongoosejs.com/docs/api.html#model_Model.findOneAndUpdate
152
178
  // options runValidators defaults false which is ok since we have upsert false
153
179
  // new returns the updated document
154
180
 
155
- self.findByIdAndUpdate(id, update, { upsert: false, new: true }, cb);
181
+ let query = self.findOneAndUpdate({ _id: id, customer: options.CUSTOMER_ID }, update, { upsert: false, new: true });
182
+
183
+ // No population so no need to scrub
184
+
185
+ query.exec(cb);
156
186
 
157
187
  };
158
188
 
@@ -160,40 +190,12 @@ module.exports = function(mongoose, config) {
160
190
 
161
191
  if (!doc) { return cb(new Error('doc is required'), null); }
162
192
 
193
+ // No population so no need to scrub
194
+
163
195
  doc.save(cb);
164
196
 
165
197
  };
166
198
 
167
- Document.plugin(postFind, {
168
-
169
- find: function(results, done) {
170
-
171
- var CUSTOMER_ID = config.CUSTOMER_ID;
172
-
173
- if (CUSTOMER_ID == 'GLOBAL_PROCESS') return done(null, results);
174
-
175
- // Reject any item that is for a different customer
176
- results = _.reject(results, function(item) {
177
- return item.customer.toString() != CUSTOMER_ID;
178
- });
179
-
180
- return done(null, results);
181
-
182
- },
183
-
184
- findOne: function(result, done) {
185
-
186
- var CUSTOMER_ID = config.CUSTOMER_ID;
187
-
188
- if (!result) return done(null, null);
189
- else if (CUSTOMER_ID == 'GLOBAL_PROCESS') return done(null, result);
190
- else if (result.customer.toString() == CUSTOMER_ID) return done(null, result);
191
- else return done(null, null);
192
-
193
- }
194
-
195
- });
196
-
197
199
 
198
200
 
199
201
  ///////////////////////////////////////////////////////////////////////////////////////
@@ -202,7 +204,6 @@ module.exports = function(mongoose, config) {
202
204
 
203
205
  Document.set('toJSON', { virtuals: true });
204
206
  Document.set('autoIndex', false);
205
- Document.set('usePushEach', true);
206
207
  Document.on('index', function(err) { console.log('error building document indexes: ' + err); });
207
208
 
208
209
  mongoose.model('Document', Document);