backend-manager 2.0.15 → 2.0.18

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.0.15",
3
+ "version": "2.0.18",
4
4
  "description": "Quick tools for developing Firebase functions",
5
5
  "main": "src/manager/index.js",
6
6
  "bin": {
@@ -31,7 +31,7 @@
31
31
  "@google-cloud/storage": "^5.19.4",
32
32
  "@sendgrid/mail": "^7.6.2",
33
33
  "@sentry/node": "^6.19.7",
34
- "backend-assistant": "^0.0.61",
34
+ "backend-assistant": "^0.0.63",
35
35
  "busboy": "^1.6.0",
36
36
  "chalk": "^4.1.2",
37
37
  "cors": "^2.8.5",
@@ -64,4 +64,4 @@
64
64
  "src/",
65
65
  "templates/"
66
66
  ]
67
- }
67
+ }
@@ -1,4 +1,5 @@
1
1
  const moment = require('moment');
2
+ const fetch = require('node-fetch');
2
3
  const uuidv5 = require('uuid').v5;
3
4
  const { get, set, merge } = require('lodash');
4
5
 
@@ -18,10 +19,12 @@ let sampleUser = {
18
19
  }
19
20
  }
20
21
 
21
- function ApiManager() {
22
+ function ApiManager(m) {
22
23
  const self = this;
24
+ self.Manager = m;
23
25
  self.options = {
24
- planData: {},
26
+ appId: '',
27
+ plans: {},
25
28
  maxUsersStored: 10000,
26
29
  refetchInterval: 60,
27
30
  resetInterval: 60 * 24,
@@ -33,20 +36,54 @@ function ApiManager() {
33
36
 
34
37
  ApiManager.prototype.init = function (options) {
35
38
  const self = this;
36
- options = options || {};
37
- options.planData = options.planData || {};
38
- options.planData.basic = options.planData.basic || {requests: 93};
39
+ return new Promise(async function(resolve, reject) {
40
+ options = options || {};
41
+ options.app = options.app || '';
42
+ options.plans = options.plans || {};
39
43
 
40
- options.maxUsersStored = options.maxUsersStored || 10000;
41
- options.refetchInterval = options.refetchInterval || 60;
42
- options.resetInterval = options.resetInterval || (60 * 24);
43
- options.officialAPIKeys = options.officialAPIKeys || [];
44
+ // await self.Manager.libraries.admin.firestore
45
+ // options.plans.basic = options.plans.basic || {requests: 93};
44
46
 
45
- self.options = options;
47
+ options.maxUsersStored = options.maxUsersStored || 10000;
48
+ options.refetchInterval = options.refetchInterval || 60;
49
+ options.resetInterval = options.resetInterval || (60 * 24);
50
+ options.officialAPIKeys = options.officialAPIKeys || [];
46
51
 
47
- self.initialized = true;
52
+ await fetch('https://us-central1-itw-creative-works.cloudfunctions.net/getApp', {
53
+ method: 'POST',
54
+ headers: { 'Content-Type': 'application/json' },
55
+ body: JSON.stringify({
56
+ id: options.app,
57
+ }),
58
+ })
59
+ .then(res => {
60
+ res.text()
61
+ .then(text => {
62
+ if (res.ok) {
63
+ const data = JSON.parse(text);
48
64
 
49
- return self;
65
+ options.plans = {};
66
+
67
+ Object.keys(data.products)
68
+ .forEach((id, i) => {
69
+ const product = data.products[id]
70
+ options.plans[product.planId] = {}
71
+ options.plans[product.planId].limits = product.limits || {};
72
+ });
73
+
74
+ self.options = options;
75
+ self.initialized = true;
76
+
77
+ return resolve(self);
78
+ } else {
79
+ throw new Error(text || res.statusText || 'Unknown error.')
80
+ }
81
+ })
82
+ })
83
+ .catch(e => {
84
+ return reject(e)
85
+ })
86
+ });
50
87
  };
51
88
 
52
89
  ApiManager.prototype._createNewUser = function (authenticatedUser, planId, persistentData, isRefetch) {
@@ -61,7 +98,7 @@ ApiManager.prototype._createNewUser = function (authenticatedUser, planId, persi
61
98
  plan: {
62
99
  id: planId,
63
100
  limits: {
64
- requests: get(authenticatedUser, 'plan.limits.requests', get(self.options, `planData.${planId}.limits.requests`, 93)),
101
+ requests: get(authenticatedUser, 'plan.limits.requests', get(self.options, `plans.${planId}.limits.requests`, 93)),
65
102
  }
66
103
  },
67
104
  authenticated: authenticatedUser.authenticated,