@dhyasama/totem-models 5.9.2 → 5.10.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/Deal.js +94 -13
- package/package.json +1 -1
package/lib/Deal.js
CHANGED
|
@@ -46,14 +46,19 @@ module.exports = function(mongoose, config) {
|
|
|
46
46
|
|
|
47
47
|
history: [{
|
|
48
48
|
timestamp: { type: Date, required: true, default: Date.now },
|
|
49
|
-
account: { type: Schema.ObjectId, ref: '
|
|
49
|
+
account: { type: Schema.ObjectId, ref: 'Person', required: true },
|
|
50
50
|
stage: { type: String, required: true },
|
|
51
|
+
fundraise: {
|
|
52
|
+
amount: { type: Number, default: 0 },
|
|
53
|
+
valuation: { type: Number, default: 0 },
|
|
54
|
+
details: { type: String, trim: true }
|
|
55
|
+
},
|
|
51
56
|
description: { type: String, trim: true, required: true }
|
|
52
57
|
}],
|
|
53
58
|
|
|
54
59
|
votes: [{
|
|
55
60
|
timestamp: { type: Date, required: true, default: Date.now },
|
|
56
|
-
account: { type: Schema.ObjectId, ref: '
|
|
61
|
+
account: { type: Schema.ObjectId, ref: 'Person', required: true },
|
|
57
62
|
value: { type: Number }
|
|
58
63
|
}]
|
|
59
64
|
|
|
@@ -218,8 +223,27 @@ module.exports = function(mongoose, config) {
|
|
|
218
223
|
path: 'documents',
|
|
219
224
|
match: { customer: config.CUSTOMER_ID }
|
|
220
225
|
})
|
|
221
|
-
.
|
|
222
|
-
|
|
226
|
+
.exec(function(err, deal) {
|
|
227
|
+
deal.deepPopulate('history.account.person', {
|
|
228
|
+
populate: { 'history.account.person': { select: 'name avatarUrl title doNotDisplay' } }
|
|
229
|
+
}, cb);
|
|
230
|
+
});
|
|
231
|
+
};
|
|
232
|
+
|
|
233
|
+
Deal.statics.addVote = function(dealId, user, vote, cb) {
|
|
234
|
+
|
|
235
|
+
var self = this;
|
|
236
|
+
|
|
237
|
+
self.findByIdAndUpdate(dealId, {$push:
|
|
238
|
+
{
|
|
239
|
+
votes: {
|
|
240
|
+
timestamp: new Date(),
|
|
241
|
+
account: user,
|
|
242
|
+
value: vote
|
|
243
|
+
}
|
|
244
|
+
}
|
|
245
|
+
}, cb);
|
|
246
|
+
|
|
223
247
|
};
|
|
224
248
|
|
|
225
249
|
Deal.statics.upsert = function upsert(deal, user, cb) {
|
|
@@ -239,21 +263,78 @@ module.exports = function(mongoose, config) {
|
|
|
239
263
|
|
|
240
264
|
};
|
|
241
265
|
|
|
266
|
+
var getLastFundraiseAmount = function() {
|
|
267
|
+
|
|
268
|
+
if (!deal.history || deal.history.length == 0) return null;
|
|
269
|
+
|
|
270
|
+
var history = _.sortBy(deal.history);
|
|
271
|
+
var last = history[history.length - 1];
|
|
272
|
+
|
|
273
|
+
return last.fundraise.amount;
|
|
274
|
+
|
|
275
|
+
};
|
|
276
|
+
|
|
277
|
+
var getLastFundraiseValuation = function() {
|
|
278
|
+
|
|
279
|
+
if (!deal.history || deal.history.length == 0) return null;
|
|
280
|
+
|
|
281
|
+
var history = _.sortBy(deal.history);
|
|
282
|
+
var last = history[history.length - 1];
|
|
283
|
+
|
|
284
|
+
return last.fundraise.valuation;
|
|
285
|
+
|
|
286
|
+
};
|
|
287
|
+
|
|
288
|
+
var getLastFundraiseDetails = function () {
|
|
289
|
+
|
|
290
|
+
if (!deal.history || deal.history.length == 0 || !deal.fundraise.description) return null;
|
|
291
|
+
|
|
292
|
+
var history = _.sortBy(deal.history);
|
|
293
|
+
var last = history[history.length - 1];
|
|
294
|
+
|
|
295
|
+
return last.fundraise.details;
|
|
296
|
+
|
|
297
|
+
};
|
|
298
|
+
|
|
299
|
+
var formatMoney = function(n, c, d, t) {
|
|
300
|
+
var c = isNaN(c = Math.abs(c)) ? 2 : c,
|
|
301
|
+
d = d == undefined ? "." : d,
|
|
302
|
+
t = t == undefined ? "," : t,
|
|
303
|
+
s = n < 0 ? "-" : "",
|
|
304
|
+
i = String(parseInt(n = Math.abs(Number(n) || 0).toFixed(c))),
|
|
305
|
+
j = (j = i.length) > 3 ? j % 3 : 0;
|
|
306
|
+
|
|
307
|
+
return s + (j ? i.substr(0, j) + t : "") + i.substr(j).replace(/(\d{3})(?=\d)/g, "$1" + t) + (c ? d + Math.abs(n - i).toFixed(c).slice(2) : "");
|
|
308
|
+
};
|
|
309
|
+
|
|
242
310
|
var description = '';
|
|
243
311
|
var lastStage = getLastStage();
|
|
312
|
+
var lastFundraiseAmount = getLastFundraiseAmount();
|
|
313
|
+
var lastFundraiseValuation = getLastFundraiseValuation();
|
|
314
|
+
var lastFundraiseDetails = getLastFundraiseDetails();
|
|
244
315
|
|
|
245
316
|
if (lastStage == null) description = 'The deal was created';
|
|
246
|
-
else
|
|
247
|
-
|
|
317
|
+
else {
|
|
318
|
+
if (lastStage != deal.stage) description += 'The deal was updated from ' + lastStage + ' to ' + deal.stage;
|
|
319
|
+
if (lastFundraiseAmount != deal.fundraise.amount) description += 'Fundraise amount was updated from $' + formatMoney(lastFundraiseAmount, 0) + ' to $' + formatMoney(deal.fundraise.amount, 0);
|
|
320
|
+
if (lastFundraiseValuation != deal.fundraise.valuation) description += 'Fundraise valuation was updated from $' + formatMoney(lastFundraiseValuation, 0) + ' to $' + formatMoney(deal.fundraise.valuation, 0);
|
|
321
|
+
if (lastFundraiseDetails != deal.fundraise.details) description += 'Fundraise details were updated from $' + lastFundraiseDetails + ' to ' + deal.fundraise.details;
|
|
322
|
+
}
|
|
248
323
|
|
|
249
|
-
|
|
324
|
+
// only add history if the description is set from the above conditions
|
|
325
|
+
if(description != '') {
|
|
250
326
|
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
327
|
+
deal.history = deal.history || [];
|
|
328
|
+
|
|
329
|
+
deal.history.push({
|
|
330
|
+
timestamp: new Date(),
|
|
331
|
+
account: user,
|
|
332
|
+
stage: deal.stage,
|
|
333
|
+
fundraise: deal.fundraise,
|
|
334
|
+
description: description
|
|
335
|
+
});
|
|
336
|
+
|
|
337
|
+
}
|
|
257
338
|
|
|
258
339
|
deal.save(cb);
|
|
259
340
|
|