backend-manager 2.5.81 → 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.81",
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: {
@@ -57,13 +61,22 @@ SubscriptionResolver.prototype.resolve = function (options) {
57
61
 
58
62
  // Set provider if not set
59
63
  if (!profile.processor) {
60
- if (resource.billing_info) {
64
+ if (
65
+ // resource.billing_info
66
+ resource.create_time
67
+ ) {
61
68
  profile.processor = 'paypal';
62
- } else if (resource.billing_period_unit) {
69
+ } else if (
70
+ resource.billing_period_unit
71
+ ) {
63
72
  profile.processor = 'chargebee';
64
- } else if (resource.customer) {
73
+ } else if (
74
+ resource.customer
75
+ ) {
65
76
  profile.processor = 'stripe';
66
- } else if (resource.addresses) {
77
+ } else if (
78
+ resource.addresses
79
+ ) {
67
80
  profile.processor = 'coinbase';
68
81
  } else {
69
82
  throw new Error('Unable to determine subscription provider');
@@ -116,6 +129,13 @@ SubscriptionResolver.prototype.resolve = function (options) {
116
129
  get(resource, 'billing_info.last_payment.time', 0)
117
130
  )
118
131
 
132
+ // Set cancelled
133
+ if (resolved.status === 'cancelled') {
134
+ resolved.cancelled.timestamp = moment(
135
+ get(resource, 'status_update_time', 0)
136
+ )
137
+ }
138
+
119
139
  // Set last payment
120
140
  if (get(resource, 'billing_info.last_payment')) {
121
141
  resolved.lastPayment.amount = parseFloat(resource.billing_info.last_payment.amount.value);
@@ -194,6 +214,13 @@ SubscriptionResolver.prototype.resolve = function (options) {
194
214
  ) * 1000
195
215
  )
196
216
 
217
+ // Set cancelled
218
+ if (resolved.status === 'cancelled') {
219
+ resolved.cancelled.timestamp = moment(
220
+ get(resource, 'cancelled_at', 0)
221
+ )
222
+ }
223
+
197
224
  // Set last payment
198
225
  if (resource.total_dues) {
199
226
  resolved.lastPayment.amount = resource.total_dues / 100;
@@ -261,6 +288,13 @@ SubscriptionResolver.prototype.resolve = function (options) {
261
288
  get(resource, 'current_period_start', 0) * 1000
262
289
  );
263
290
 
291
+ // Set cancelled
292
+ if (resolved.status === 'cancelled') {
293
+ resolved.cancelled.timestamp = moment(
294
+ get(resource, 'canceled_at', 0)
295
+ )
296
+ }
297
+
264
298
  // Set last payment
265
299
  if (resource.latest_invoice) {
266
300
  resolved.lastPayment.amount = resource.latest_invoice.amount_paid / 100;
@@ -319,6 +353,11 @@ SubscriptionResolver.prototype.resolve = function (options) {
319
353
  get(resource, 'created_at', 0)
320
354
  );
321
355
 
356
+ // Set cancelled
357
+ resolved.cancelled.timestamp = moment(
358
+ get(resource, 'created_at', 0)
359
+ )
360
+
322
361
  // Retrieve last payment
323
362
  const lastPayment = resource.payments.find(p => p.status === 'CONFIRMED');
324
363
 
@@ -380,11 +419,14 @@ SubscriptionResolver.prototype.resolve = function (options) {
380
419
  }
381
420
 
382
421
  // Fix timestamps
422
+ resolved.start.timestampUNIX = resolved.start.timestamp.unix();
423
+ resolved.start.timestamp = resolved.start.timestamp.toISOString();
424
+
383
425
  resolved.expires.timestampUNIX = resolved.expires.timestamp.unix();
384
426
  resolved.expires.timestamp = resolved.expires.timestamp.toISOString();
385
427
 
386
- resolved.start.timestampUNIX = resolved.start.timestamp.unix();
387
- resolved.start.timestamp = resolved.start.timestamp.toISOString();
428
+ resolved.cancelled.timestampUNIX = resolved.cancelled.timestamp.unix();
429
+ resolved.cancelled.timestamp = resolved.cancelled.timestamp.toISOString();
388
430
 
389
431
  // Fix trial days
390
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');