@dhyasama/totem-models 12.16.1 → 12.17.2

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/Financials.js CHANGED
@@ -826,6 +826,12 @@ module.exports = function(mongoose, config) {
826
826
  const run = async () => {
827
827
  var now = new Date();
828
828
 
829
+ // Lean + projection re-applied here despite e513aab's broad lean removal.
830
+ // Eligibility is filtered at the mongo level on stored snapshot.status, and
831
+ // no caller reads pre('init') updateStatus output from this result:
832
+ // scheduleCompanyEmails re-fetches via findOne, summarizeFinancialsToSend
833
+ // reads only ObjectIds + snapshot.period/year. Hydrating the full doc
834
+ // (notably notifications.*.email bodies) was driving lambda OOMs.
829
835
  var query = this.find({
830
836
  'snapshots': {
831
837
  $elemMatch: {
@@ -835,7 +841,17 @@ module.exports = function(mongoose, config) {
835
841
  'status': { $nin: ['Pending', 'Completed', 'Archived'] }
836
842
  }
837
843
  }
838
- });
844
+ }).select({
845
+ customer: 1,
846
+ organization: 1,
847
+ 'snapshots._id': 1,
848
+ 'snapshots.uuid': 1,
849
+ 'snapshots.year': 1,
850
+ 'snapshots.period': 1,
851
+ 'snapshots.notifications.company.initial.sendTo': 1,
852
+ 'snapshots.notifications.company.initial.sendOn': 1,
853
+ 'snapshots.notifications.company.initial.sentOn': 1,
854
+ }).lean();
839
855
 
840
856
  const result = await query;
841
857
  var results = [];
@@ -881,6 +897,7 @@ module.exports = function(mongoose, config) {
881
897
  const run = async () => {
882
898
  var now = new Date();
883
899
 
900
+ // see lean+projection note on getCompanyInitialToSend
884
901
  var query = this.find({
885
902
  'snapshots': {
886
903
  $elemMatch: {
@@ -890,7 +907,17 @@ module.exports = function(mongoose, config) {
890
907
  'status': { $nin: ['Pending', 'Completed', 'Archived'] }
891
908
  }
892
909
  }
893
- });
910
+ }).select({
911
+ customer: 1,
912
+ organization: 1,
913
+ 'snapshots._id': 1,
914
+ 'snapshots.uuid': 1,
915
+ 'snapshots.year': 1,
916
+ 'snapshots.period': 1,
917
+ 'snapshots.notifications.company.reminders.sendTo': 1,
918
+ 'snapshots.notifications.company.reminders.sendOn': 1,
919
+ 'snapshots.notifications.company.reminders.sentOn': 1,
920
+ }).lean();
894
921
 
895
922
  const result = await query;
896
923
  var results = [];
@@ -940,6 +967,7 @@ module.exports = function(mongoose, config) {
940
967
  const run = async () => {
941
968
  var now = new Date();
942
969
 
970
+ // see lean+projection note on getCompanyInitialToSend
943
971
  var query = this.find({
944
972
  'snapshots': {
945
973
  $elemMatch: {
@@ -949,7 +977,17 @@ module.exports = function(mongoose, config) {
949
977
  'status': { $nin: ['Pending', 'Completed', 'Archived'] }
950
978
  }
951
979
  }
952
- });
980
+ }).select({
981
+ customer: 1,
982
+ organization: 1,
983
+ 'snapshots._id': 1,
984
+ 'snapshots.uuid': 1,
985
+ 'snapshots.year': 1,
986
+ 'snapshots.period': 1,
987
+ 'snapshots.notifications.company.rejections.sendTo': 1,
988
+ 'snapshots.notifications.company.rejections.sendOn': 1,
989
+ 'snapshots.notifications.company.rejections.sentOn': 1,
990
+ }).lean();
953
991
 
954
992
  const result = await query;
955
993
  var results = [];
package/lib/Rate.js CHANGED
@@ -52,17 +52,28 @@ module.exports = function(mongoose, config) {
52
52
  Rate.statics.upsert = function (rate, cb) {
53
53
 
54
54
  const run = async () => {
55
- if (typeof rate.save === 'function') {
56
- return await rate.save();
55
+ if (!rate) { throw new Error('rate is required'); }
56
+
57
+ const data = typeof rate.toObject === 'function' ? rate.toObject() : rate;
58
+ if (!data.date || !data.currency) {
59
+ throw new Error('rate.date and rate.currency are required');
57
60
  }
58
- const { _id, ...update } = rate;
59
- return await this.findByIdAndUpdate(_id, update, { new: true });
61
+
62
+ const { _id, ...update } = data;
63
+ return await this.findOneAndUpdate(
64
+ { date: data.date, currency: data.currency },
65
+ { $set: update },
66
+ { upsert: true, new: true, setDefaultsOnInsert: true }
67
+ ).lean({ virtuals: true });
60
68
  };
61
69
 
62
70
  if (typeof cb === 'function') { run().then(result => cb(null, result), err => cb(err)); return; }
63
71
  else { return run(); }
64
72
 
65
73
  };
74
+
75
+ Rate.index({ currency: 1, date: 1 }, { unique: true });
76
+
66
77
  Rate.set('autoIndex', false);
67
78
  Rate.on('index', function(err) { console.log('error building rate indexes: ' + err); });
68
79
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@dhyasama/totem-models",
3
- "version": "12.16.1",
3
+ "version": "12.17.2",
4
4
  "author": "Jason Reynolds",
5
5
  "license": "UNLICENSED",
6
6
  "description": "Models for Totem platform",