backend-manager 2.5.82 → 2.5.83

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "backend-manager",
3
- "version": "2.5.82",
3
+ "version": "2.5.83",
4
4
  "description": "Quick tools for developing Firebase functions",
5
5
  "main": "src/manager/index.js",
6
6
  "bin": {
@@ -49,11 +49,11 @@
49
49
  "moment": "^2.29.4",
50
50
  "nanoid": "^3.3.6",
51
51
  "node-fetch": "^2.6.9",
52
- "node-powertools": "^0.0.23",
52
+ "node-powertools": "^1.0.2",
53
53
  "npm-api": "^1.0.1",
54
54
  "paypal-server-api": "^1.0.2",
55
55
  "pushid": "^1.0.0",
56
- "semver": "^7.3.8",
56
+ "semver": "^7.4.0",
57
57
  "shortid": "^2.2.16",
58
58
  "uid-generator": "^2.0.0",
59
59
  "ultimate-jekyll-poster": "^0.0.15",
@@ -0,0 +1,84 @@
1
+ const _ = require('lodash')
2
+ const jetpack = require('fs-jetpack')
3
+ const powertools = require('node-powertools')
4
+ const path = require('path')
5
+
6
+ function Module() {
7
+
8
+ }
9
+
10
+ Module.prototype.main = function () {
11
+ const self = this;
12
+ const Manager = self.Manager;
13
+ const Api = self.Api;
14
+ const assistant = self.assistant;
15
+ const payload = self.payload;
16
+
17
+ return new Promise(async function(resolve, reject) {
18
+ self.Api.resolveUser({adminRequired: true})
19
+ .then(async (user) => {
20
+
21
+ payload.data.payload.defaultsPath = payload.data.payload.defaultsPath || '';
22
+ payload.data.payload.existingSettings = payload.data.payload.existingSettings || {};
23
+ payload.data.payload.newSettings = payload.data.payload.newSettings || {};
24
+
25
+ const settings = _.merge({}, payload.data.payload.existingSettings, payload.data.payload.newSettings);
26
+
27
+ const resolvedPath = path.join(process.cwd(), `defaults.js`);
28
+
29
+ // Check if the file exists
30
+ if (!jetpack.exists(resolvedPath)) {
31
+ return reject(assistant.errorManager(`Defaults file at ${resolvedPath} does not exist, please add it manually.`, {code: 500, sentry: true, send: false, log: true}).error);
32
+ }
33
+
34
+ // Load the file
35
+ try {
36
+ const defaults = _.get(require(resolvedPath)(), payload.data.payload.defaultsPath);
37
+ const combined = combine(defaults.all, defaults[user.plan.id] || {})
38
+
39
+ assistant.log('Combined settings', combined, {environment: 'development'})
40
+
41
+ return resolve({data: powertools.defaults(settings, combined)});
42
+ } catch (e) {
43
+ return reject(assistant.errorManager(`Unable to load file at ${resolvedPath}: ${e}`, {code: 500, sentry: true, send: false, log: true}).error);
44
+ }
45
+
46
+ })
47
+ .catch(e => {
48
+ return reject(e);
49
+ })
50
+ });
51
+
52
+ };
53
+
54
+ function isObject(value) {
55
+ return value && typeof value === 'object' && !Array.isArray(value);
56
+ }
57
+
58
+ function combine(one, two) {
59
+ const done = [];
60
+
61
+ // Iterate through the keys of the second object
62
+ powertools.getKeys(two)
63
+ .forEach(path => {
64
+ const pathMinusLast = path.split('.').slice(0, -1).join('.');
65
+ const valueAtPath = _.get(two, path);
66
+ const valueAtParent = _.get(two, pathMinusLast);
67
+
68
+ if (
69
+ done.includes(pathMinusLast)
70
+ || isObject(valueAtPath)
71
+ ) {
72
+ return;
73
+ }
74
+
75
+ // If the path is an object, merge the two object using lodash
76
+ _.set(one, pathMinusLast, valueAtParent)
77
+
78
+ done.push(pathMinusLast);
79
+ })
80
+ return one;
81
+ }
82
+
83
+
84
+ module.exports = Module;
@@ -161,6 +161,11 @@ Module.prototype.import = function (command, payload, user, response) {
161
161
  lib.payload.data.authenticationToken = self.payload.data.authenticationToken;
162
162
  }
163
163
 
164
+ // Set itself to the instance
165
+ self.lib = lib;
166
+
167
+ // console.log('***** import() lib.payload 4', lib.payload);
168
+
164
169
  // lib.payload = {};
165
170
  //
166
171
  // // Set payload and user if it's provided
@@ -255,12 +260,13 @@ Module.prototype.resolveCommand = function (command) {
255
260
 
256
261
  Module.prototype.resolveUser = function (options) {
257
262
  const self = this;
263
+
258
264
  return new Promise(async function(resolve, reject) {
259
265
  let user = null;
260
266
 
261
267
  options = options || {};
262
- options.uid = typeof options.uid !== 'undefined' ? options.uid : self.payload.data.payload.uid;
263
- options.admin = typeof options.admin !== 'undefined' ? options.admin : self.payload.user.roles.admin;
268
+ options.uid = typeof options.uid !== 'undefined' ? options.uid : _.get(self.payload, 'data.payload.uid');
269
+ options.admin = typeof options.admin !== 'undefined' ? options.admin : _.get(self.payload, 'user.roles.admin');
264
270
  options.adminRequired = typeof options.adminRequired !== 'undefined' ? options.adminRequired : true;
265
271
 
266
272
  if (options.uid) {
@@ -283,6 +289,8 @@ Module.prototype.resolveUser = function (options) {
283
289
  }
284
290
  } else if (self.payload.user.authenticated) {
285
291
  user = self.payload.user;
292
+ } else if (_.get(self.lib, 'payload.user.authenticated')) {
293
+ user = self.lib.payload.user;
286
294
  }
287
295
 
288
296
  if (user instanceof Error) {
@@ -23,14 +23,18 @@ SubscriptionResolver.prototype.resolve = function (options) {
23
23
  payment: {
24
24
  completed: false,
25
25
  },
26
- expires: {
26
+ start: {
27
27
  timestamp: moment(0),
28
28
  timestampUNIX: moment(0),
29
29
  },
30
- start: {
30
+ expires: {
31
31
  timestamp: moment(0),
32
32
  timestampUNIX: moment(0),
33
33
  },
34
+ cancelled: {
35
+ timestamp: moment(0),
36
+ timestampUNIX: moment(0),
37
+ },
34
38
  lastPayment: {
35
39
  amount: 0,
36
40
  date: {
@@ -125,6 +129,13 @@ SubscriptionResolver.prototype.resolve = function (options) {
125
129
  get(resource, 'billing_info.last_payment.time', 0)
126
130
  )
127
131
 
132
+ // Set cancelled
133
+ if (resolved.status === 'cancelled') {
134
+ resolved.cancelled.timestamp = moment(
135
+ get(resource, 'status_update_time', 0)
136
+ )
137
+ }
138
+
128
139
  // Set last payment
129
140
  if (get(resource, 'billing_info.last_payment')) {
130
141
  resolved.lastPayment.amount = parseFloat(resource.billing_info.last_payment.amount.value);
@@ -203,6 +214,13 @@ SubscriptionResolver.prototype.resolve = function (options) {
203
214
  ) * 1000
204
215
  )
205
216
 
217
+ // Set cancelled
218
+ if (resolved.status === 'cancelled') {
219
+ resolved.cancelled.timestamp = moment(
220
+ get(resource, 'cancelled_at', 0)
221
+ )
222
+ }
223
+
206
224
  // Set last payment
207
225
  if (resource.total_dues) {
208
226
  resolved.lastPayment.amount = resource.total_dues / 100;
@@ -270,6 +288,13 @@ SubscriptionResolver.prototype.resolve = function (options) {
270
288
  get(resource, 'current_period_start', 0) * 1000
271
289
  );
272
290
 
291
+ // Set cancelled
292
+ if (resolved.status === 'cancelled') {
293
+ resolved.cancelled.timestamp = moment(
294
+ get(resource, 'canceled_at', 0)
295
+ )
296
+ }
297
+
273
298
  // Set last payment
274
299
  if (resource.latest_invoice) {
275
300
  resolved.lastPayment.amount = resource.latest_invoice.amount_paid / 100;
@@ -328,6 +353,11 @@ SubscriptionResolver.prototype.resolve = function (options) {
328
353
  get(resource, 'created_at', 0)
329
354
  );
330
355
 
356
+ // Set cancelled
357
+ resolved.cancelled.timestamp = moment(
358
+ get(resource, 'created_at', 0)
359
+ )
360
+
331
361
  // Retrieve last payment
332
362
  const lastPayment = resource.payments.find(p => p.status === 'CONFIRMED');
333
363
 
@@ -389,11 +419,14 @@ SubscriptionResolver.prototype.resolve = function (options) {
389
419
  }
390
420
 
391
421
  // Fix timestamps
422
+ resolved.start.timestampUNIX = resolved.start.timestamp.unix();
423
+ resolved.start.timestamp = resolved.start.timestamp.toISOString();
424
+
392
425
  resolved.expires.timestampUNIX = resolved.expires.timestamp.unix();
393
426
  resolved.expires.timestamp = resolved.expires.timestamp.toISOString();
394
427
 
395
- resolved.start.timestampUNIX = resolved.start.timestamp.unix();
396
- resolved.start.timestamp = resolved.start.timestamp.toISOString();
428
+ resolved.cancelled.timestampUNIX = resolved.cancelled.timestamp.unix();
429
+ resolved.cancelled.timestamp = resolved.cancelled.timestamp.toISOString();
397
430
 
398
431
  // Fix trial days
399
432
  resolved.trial.daysLeft = resolved.trial.daysLeft < 0 ? 0 : resolved.trial.daysLeft;
@@ -599,6 +599,20 @@ Manager.prototype.Api = function () {
599
599
  return Api;
600
600
  };
601
601
 
602
+ // Manager.prototype.Api = function () {
603
+ // const self = this;
604
+ // // self.libraries.Api = self.libraries.Api || require('./helpers/subscription-resolver.js');
605
+ // // return new self.libraries.Api(...arguments);
606
+ // // return self._process((new (require(`${core}/actions/api.js`))()).init(self, { req: req, res: res, }))
607
+ // return new Promise(function(resolve, reject) {
608
+ // const Api = (new (require(`${core}/actions/api.js`))()).init(self, { req: {}, res: {}, });
609
+
610
+ // Api.main()
611
+
612
+ // return Api;
613
+ // });
614
+ // };
615
+
602
616
  // Manager.prototype.Utilities = function () {
603
617
  // const self = this;
604
618
  // self.libraries.Utilities = self.libraries.Utilities || require('./helpers/utilities.js');