backend-manager 2.0.19 → 2.0.22

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.
Files changed (26) hide show
  1. package/package.json +3 -2
  2. package/src/cli/cli.js +1 -1
  3. package/src/manager/functions/core/actions/api/admin/create-post.js +1 -11
  4. package/src/manager/functions/core/actions/api/admin/firestore-query.js +1 -11
  5. package/src/manager/functions/core/actions/api/admin/firestore-read.js +1 -11
  6. package/src/manager/functions/core/actions/api/admin/firestore-write.js +1 -11
  7. package/src/manager/functions/core/actions/api/admin/get-stats.js +1 -11
  8. package/src/manager/functions/core/actions/api/admin/payment-processor.js +1 -11
  9. package/src/manager/functions/core/actions/api/admin/send-notification.js +1 -11
  10. package/src/manager/functions/core/actions/api/general/generate-uuid.js +2 -13
  11. package/src/manager/functions/core/actions/api/handler/create-post.js +1 -11
  12. package/src/manager/functions/core/actions/api/special/setup-electron-manager-client.js +71 -0
  13. package/src/manager/functions/core/actions/api/template.js +1 -11
  14. package/src/manager/functions/core/actions/api/test/authenticate.js +1 -11
  15. package/src/manager/functions/core/actions/api/test/create-test-accounts.js +1 -11
  16. package/src/manager/functions/core/actions/api/test/webhook.js +1 -11
  17. package/src/manager/functions/core/actions/api/user/create-custom-token.js +1 -11
  18. package/src/manager/functions/core/actions/api/user/delete.js +1 -11
  19. package/src/manager/functions/core/actions/api/user/get-subscription-info.js +1 -11
  20. package/src/manager/functions/core/actions/api/user/sign-out-all-sessions.js +1 -11
  21. package/src/manager/functions/core/actions/api/user/sign-up.js +1 -11
  22. package/src/manager/functions/core/actions/api.js +59 -16
  23. package/src/manager/helpers/api-manager.js +74 -33
  24. package/src/manager/helpers/user.js +2 -2
  25. package/src/manager/index.js +4 -3
  26. package/templates/backend-manager-config.json +3 -0
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "backend-manager",
3
- "version": "2.0.19",
3
+ "version": "2.0.22",
4
4
  "description": "Quick tools for developing Firebase functions",
5
5
  "main": "src/manager/index.js",
6
6
  "bin": {
@@ -57,6 +57,7 @@
57
57
  "ultimate-jekyll-poster": "^0.0.10",
58
58
  "universal-analytics": "^0.5.3",
59
59
  "uuid": "^8.3.2",
60
+ "wonderful-fetch": "^0.0.3",
60
61
  "yargs": "^17.4.1"
61
62
  },
62
63
  "files": [
@@ -64,4 +65,4 @@
64
65
  "src/",
65
66
  "templates/"
66
67
  ]
67
- }
68
+ }
package/src/cli/cli.js CHANGED
@@ -385,7 +385,7 @@ Main.prototype.setup = async function () {
385
385
  let item = ogPaths[i];
386
386
  if (!item) {continue}
387
387
  pass = (_.get(bemConfig, item, undefined));
388
- if (typeof pass === 'undefined') {
388
+ if (typeof pass === 'undefined' || typeof pass === '') {
389
389
  break;
390
390
  }
391
391
  }
@@ -7,20 +7,10 @@ function Module() {
7
7
 
8
8
  }
9
9
 
10
- Module.prototype.init = async function (s, payload) {
11
- const self = this;
12
- self.Api = s;
13
- self.Manager = s.Manager;
14
- self.libraries = s.Manager.libraries;
15
- self.assistant = s.assistant;
16
- self.payload = payload;
17
-
18
- return self;
19
- };
20
-
21
10
  Module.prototype.main = function () {
22
11
  const self = this;
23
12
  const Manager = self.Manager;
13
+ const Api = self.Api;
24
14
  const assistant = self.assistant;
25
15
  const payload = self.payload;
26
16
 
@@ -5,20 +5,10 @@ function Module() {
5
5
 
6
6
  }
7
7
 
8
- Module.prototype.init = async function (s, payload) {
9
- const self = this;
10
- self.Api = s;
11
- self.Manager = s.Manager;
12
- self.libraries = s.Manager.libraries;
13
- self.assistant = s.Manager.assistant;
14
- self.payload = payload;
15
-
16
- return self;
17
- };
18
-
19
8
  Module.prototype.main = function () {
20
9
  const self = this;
21
10
  const Manager = self.Manager;
11
+ const Api = self.Api;
22
12
  const assistant = self.assistant;
23
13
  const payload = self.payload;
24
14
 
@@ -2,20 +2,10 @@ function Module() {
2
2
 
3
3
  }
4
4
 
5
- Module.prototype.init = async function (s, payload) {
6
- const self = this;
7
- self.Api = s;
8
- self.Manager = s.Manager;
9
- self.libraries = s.Manager.libraries;
10
- self.assistant = s.Manager.assistant;
11
- self.payload = payload;
12
-
13
- return self;
14
- };
15
-
16
5
  Module.prototype.main = function () {
17
6
  const self = this;
18
7
  const Manager = self.Manager;
8
+ const Api = self.Api;
19
9
  const assistant = self.assistant;
20
10
  const payload = self.payload;
21
11
 
@@ -2,20 +2,10 @@ function Module() {
2
2
 
3
3
  }
4
4
 
5
- Module.prototype.init = async function (s, payload) {
6
- const self = this;
7
- self.Api = s;
8
- self.Manager = s.Manager;
9
- self.libraries = s.Manager.libraries;
10
- self.assistant = s.Manager.assistant;
11
- self.payload = payload;
12
-
13
- return self;
14
- };
15
-
16
5
  Module.prototype.main = function () {
17
6
  const self = this;
18
7
  const Manager = self.Manager;
8
+ const Api = self.Api;
19
9
  const assistant = self.assistant;
20
10
  const payload = self.payload;
21
11
 
@@ -2,20 +2,10 @@ function Module() {
2
2
 
3
3
  }
4
4
 
5
- Module.prototype.init = async function (s, payload) {
6
- const self = this;
7
- self.Api = s;
8
- self.Manager = s.Manager;
9
- self.libraries = s.Manager.libraries;
10
- self.assistant = s.Manager.assistant;
11
- self.payload = payload;
12
-
13
- return self;
14
- };
15
-
16
5
  Module.prototype.main = function () {
17
6
  const self = this;
18
7
  const Manager = self.Manager;
8
+ const Api = self.Api;
19
9
  const assistant = self.assistant;
20
10
  const payload = self.payload;
21
11
 
@@ -4,20 +4,10 @@ function Module() {
4
4
 
5
5
  }
6
6
 
7
- Module.prototype.init = async function (s, payload) {
8
- const self = this;
9
- self.Api = s;
10
- self.Manager = s.Manager;
11
- self.libraries = s.Manager.libraries;
12
- self.assistant = s.Manager.assistant;
13
- self.payload = payload;
14
-
15
- return self;
16
- };
17
-
18
7
  Module.prototype.main = function () {
19
8
  const self = this;
20
9
  const Manager = self.Manager;
10
+ const Api = self.Api;
21
11
  const assistant = self.assistant;
22
12
  const payload = self.payload;
23
13
 
@@ -6,20 +6,10 @@ function Module() {
6
6
 
7
7
  }
8
8
 
9
- Module.prototype.init = async function (s, payload) {
10
- const self = this;
11
- self.Api = s;
12
- self.Manager = s.Manager;
13
- self.libraries = s.Manager.libraries;
14
- self.assistant = s.assistant;
15
- self.payload = payload;
16
-
17
- return self;
18
- };
19
-
20
9
  Module.prototype.main = function () {
21
10
  const self = this;
22
11
  const Manager = self.Manager;
12
+ const Api = self.Api;
23
13
  const assistant = self.assistant;
24
14
  const payload = self.payload;
25
15
 
@@ -4,20 +4,10 @@ function Module() {
4
4
 
5
5
  }
6
6
 
7
- Module.prototype.init = async function (s, payload) {
8
- const self = this;
9
- self.Api = s;
10
- self.Manager = s.Manager;
11
- self.libraries = s.Manager.libraries;
12
- self.assistant = s.Manager.assistant;
13
- self.payload = payload;
14
-
15
- return self;
16
- };
17
-
18
7
  Module.prototype.main = function () {
19
8
  const self = this;
20
9
  const Manager = self.Manager;
10
+ const Api = self.Api;
21
11
  const assistant = self.assistant;
22
12
  const payload = self.payload;
23
13
 
@@ -42,8 +32,7 @@ Module.prototype.main = function () {
42
32
 
43
33
  assistant.log('UUID Generated', payload.data.payload, result, {environment: 'production'});
44
34
 
45
- // Return the timestamp so electron-manager can save an http request >:)
46
- return resolve({data: {uuid: result, timestamp: new Date().toISOString()}});
35
+ return resolve({data: {uuid: result}});
47
36
 
48
37
  });
49
38
 
@@ -5,20 +5,10 @@ function Module() {
5
5
 
6
6
  }
7
7
 
8
- Module.prototype.init = async function (s, payload) {
9
- const self = this;
10
- self.Api = s;
11
- self.Manager = s.Manager;
12
- self.libraries = s.Manager.libraries;
13
- self.assistant = s.Manager.assistant;
14
- self.payload = payload;
15
-
16
- return self;
17
- };
18
-
19
8
  Module.prototype.main = function () {
20
9
  const self = this;
21
10
  const Manager = self.Manager;
11
+ const Api = self.Api;
22
12
  const assistant = self.assistant;
23
13
  const payload = self.payload;
24
14
 
@@ -0,0 +1,71 @@
1
+ function Module() {
2
+
3
+ }
4
+
5
+ Module.prototype.main = function () {
6
+ const self = this;
7
+ const Manager = self.Manager;
8
+ const Api = self.Api;
9
+ const assistant = self.assistant;
10
+ const payload = self.payload;
11
+
12
+ return new Promise(async function(resolve, reject) {
13
+
14
+ const fetch = Manager.require('wonderful-fetch');
15
+
16
+ const uid = payload.data.payload.uid;
17
+ const app = payload.data.payload.appId || payload.data.payload.app || Manager.config.app.id;
18
+
19
+ let uuid = null;
20
+ let error;
21
+
22
+ // Generate uuid
23
+ if (uid) {
24
+ await Api.import('general:generate-uuid', {version: 5, name: uid})
25
+ .then(library => {
26
+
27
+ library.main()
28
+ .then(result => {
29
+ uuid = result.data.uuid;
30
+ })
31
+ .catch(e => {
32
+ error = e;
33
+ })
34
+ })
35
+ }
36
+
37
+ if (error) {
38
+ return reject(error)
39
+ }
40
+
41
+ // Fetch app details
42
+ await fetch('https://us-central1-itw-creative-works.cloudfunctions.net/getApp', {
43
+ method: 'post',
44
+ timeout: 30000,
45
+ tries: 3,
46
+ json: true,
47
+ body: {
48
+ id: app,
49
+ },
50
+ })
51
+ .then(result => {
52
+ return resolve({
53
+ data: {
54
+ uuid: uuid,
55
+ timestamp: new Date().toISOString(),
56
+ ip: assistant.request.ip,
57
+ country: assistant.request.country,
58
+ app: result,
59
+ }
60
+ });
61
+ })
62
+ .catch(e => {
63
+ return reject(new Error(`Error fetching app details: ${e}`))
64
+ })
65
+
66
+ });
67
+
68
+ };
69
+
70
+
71
+ module.exports = Module;
@@ -2,20 +2,10 @@ function Module() {
2
2
 
3
3
  }
4
4
 
5
- Module.prototype.init = async function (s, payload) {
6
- const self = this;
7
- self.Api = s;
8
- self.Manager = s.Manager;
9
- self.libraries = s.Manager.libraries;
10
- self.assistant = s.Manager.assistant;
11
- self.payload = payload;
12
-
13
- return self;
14
- };
15
-
16
5
  Module.prototype.main = function () {
17
6
  const self = this;
18
7
  const Manager = self.Manager;
8
+ const Api = self.Api;
19
9
  const assistant = self.assistant;
20
10
  const payload = self.payload;
21
11
 
@@ -2,20 +2,10 @@ function Module() {
2
2
 
3
3
  }
4
4
 
5
- Module.prototype.init = async function (s, payload) {
6
- const self = this;
7
- self.Api = s;
8
- self.Manager = s.Manager;
9
- self.libraries = s.Manager.libraries;
10
- self.assistant = s.Manager.assistant;
11
- self.payload = payload;
12
-
13
- return self;
14
- };
15
-
16
5
  Module.prototype.main = function () {
17
6
  const self = this;
18
7
  const Manager = self.Manager;
8
+ const Api = self.Api;
19
9
  const assistant = self.assistant;
20
10
  const payload = self.payload;
21
11
 
@@ -2,20 +2,10 @@ function Module() {
2
2
 
3
3
  }
4
4
 
5
- Module.prototype.init = async function (s, payload) {
6
- const self = this;
7
- self.Api = s;
8
- self.Manager = s.Manager;
9
- self.libraries = s.Manager.libraries;
10
- self.assistant = s.Manager.assistant;
11
- self.payload = payload;
12
-
13
- return self;
14
- };
15
-
16
5
  Module.prototype.main = function () {
17
6
  const self = this;
18
7
  const Manager = self.Manager;
8
+ const Api = self.Api;
19
9
  const assistant = self.assistant;
20
10
  const payload = self.payload;
21
11
 
@@ -2,20 +2,10 @@ function Module() {
2
2
 
3
3
  }
4
4
 
5
- Module.prototype.init = async function (s, payload) {
6
- const self = this;
7
- self.Api = s;
8
- self.Manager = s.Manager;
9
- self.libraries = s.Manager.libraries;
10
- self.assistant = s.Manager.assistant;
11
- self.payload = payload;
12
-
13
- return self;
14
- };
15
-
16
5
  Module.prototype.main = function () {
17
6
  const self = this;
18
7
  const Manager = self.Manager;
8
+ const Api = self.Api;
19
9
  const assistant = self.assistant;
20
10
  const payload = self.payload;
21
11
 
@@ -4,20 +4,10 @@ function Module() {
4
4
 
5
5
  }
6
6
 
7
- Module.prototype.init = async function (s, payload) {
8
- const self = this;
9
- self.Api = s;
10
- self.Manager = s.Manager;
11
- self.libraries = s.Manager.libraries;
12
- self.assistant = s.Manager.assistant;
13
- self.payload = payload;
14
-
15
- return self;
16
- };
17
-
18
7
  Module.prototype.main = function () {
19
8
  const self = this;
20
9
  const Manager = self.Manager;
10
+ const Api = self.Api;
21
11
  const assistant = self.assistant;
22
12
  const payload = self.payload;
23
13
 
@@ -4,20 +4,10 @@ function Module() {
4
4
 
5
5
  }
6
6
 
7
- Module.prototype.init = async function (s, payload) {
8
- const self = this;
9
- self.Api = s;
10
- self.Manager = s.Manager;
11
- self.libraries = s.Manager.libraries;
12
- self.assistant = s.Manager.assistant;
13
- self.payload = payload;
14
-
15
- return self;
16
- };
17
-
18
7
  Module.prototype.main = function () {
19
8
  const self = this;
20
9
  const Manager = self.Manager;
10
+ const Api = self.Api;
21
11
  const assistant = self.assistant;
22
12
  const payload = self.payload;
23
13
 
@@ -4,20 +4,10 @@ function Module() {
4
4
 
5
5
  }
6
6
 
7
- Module.prototype.init = async function (s, payload) {
8
- const self = this;
9
- self.Api = s;
10
- self.Manager = s.Manager;
11
- self.libraries = s.Manager.libraries;
12
- self.assistant = s.Manager.assistant;
13
- self.payload = payload;
14
-
15
- return self;
16
- };
17
-
18
7
  Module.prototype.main = function () {
19
8
  const self = this;
20
9
  const Manager = self.Manager;
10
+ const Api = self.Api;
21
11
  const assistant = self.assistant;
22
12
  const payload = self.payload;
23
13
 
@@ -4,20 +4,10 @@ function Module() {
4
4
 
5
5
  }
6
6
 
7
- Module.prototype.init = async function (s, payload) {
8
- const self = this;
9
- self.Api = s;
10
- self.Manager = s.Manager;
11
- self.libraries = s.Manager.libraries;
12
- self.assistant = s.Manager.assistant;
13
- self.payload = payload;
14
-
15
- return self;
16
- };
17
-
18
7
  Module.prototype.main = function () {
19
8
  const self = this;
20
9
  const Manager = self.Manager;
10
+ const Api = self.Api;
21
11
  const assistant = self.assistant;
22
12
  const payload = self.payload;
23
13
 
@@ -5,20 +5,10 @@ function Module() {
5
5
 
6
6
  }
7
7
 
8
- Module.prototype.init = async function (s, payload) {
9
- const self = this;
10
- self.Api = s;
11
- self.Manager = s.Manager;
12
- self.libraries = s.Manager.libraries;
13
- self.assistant = s.Manager.assistant;
14
- self.payload = payload;
15
-
16
- return self;
17
- };
18
-
19
8
  Module.prototype.main = function () {
20
9
  const self = this;
21
10
  const Manager = self.Manager;
11
+ const Api = self.Api;
22
12
  const assistant = self.assistant;
23
13
  const payload = self.payload;
24
14
 
@@ -36,19 +36,18 @@ Module.prototype.main = function() {
36
36
  self.payload.data = assistant.request.data;
37
37
  self.payload.user = await assistant.authenticate();
38
38
 
39
- const command = self.resolveCommand(self.payload.data.command);
40
- const commandPath = './' + path.join('./api/', `${command.replace(/\.\.\//g, '').replace(/\:/, '/')}.js`);
39
+ const resolved = self.resolveCommand(self.payload.data.command);
41
40
 
42
- self.assistant.log(`Executing: ${command}`, self.payload, JSON.stringify(self.payload), {environment: 'production'})
41
+ self.assistant.log(`Executing: ${resolved.command}`, self.payload, JSON.stringify(self.payload), {environment: 'production'})
43
42
 
44
- try {
45
- const lib = new (require(commandPath))();
43
+ await self.import(resolved.command)
44
+ .then(async lib => {
46
45
  try {
47
- await lib.init(self, self.payload);
46
+ // Call main function
48
47
  await lib.main()
49
- .then(r => {
50
- self.payload.response.status = r.status || 200;
51
- self.payload.response.data = r.data || {};
48
+ .then(result => {
49
+ self.payload.response.status = result.status || 200;
50
+ self.payload.response.data = result.data || {};
52
51
  })
53
52
  .catch(e => {
54
53
  self.payload.response.status = e.code || 500;
@@ -58,22 +57,59 @@ Module.prototype.main = function() {
58
57
  self.payload.response.status = 500;
59
58
  self.payload.response.error = e || new Error('Unknown error occured');
60
59
  }
61
- } catch (e) {
60
+ })
61
+ .catch(e => {
62
62
  self.payload.response.status = 400;
63
- self.payload.response.error = new Error(`Improper command supplied: ${command}`);
64
- assistant.log('Dev error log', e)
65
- }
63
+ self.payload.response.error = new Error(`Failed to import: ${e}`);
64
+ })
66
65
 
67
66
  if (self.payload.response.status === 200) {
68
67
  return res.status(self.payload.response.status).json(self.payload.response.data);
69
68
  } else {
70
- console.error(`Error executing ${command} @ ${commandPath}`, self.payload.response.error)
69
+ console.error(`Error executing ${self.payload.data.command} => ${resolved.command} @ ${resolved.path}`, self.payload.response.error)
71
70
  // return res.status(self.payload.response.status).send(self.payload.response.error.message);
72
71
  return res.status(self.payload.response.status).send(`${self.payload.response.error}`);
73
72
  }
74
73
  });
75
74
  }
76
75
 
76
+ Module.prototype.import = function (command, payload, user, response) {
77
+ const self = this;
78
+
79
+ return new Promise(function(resolve, reject) {
80
+ const resolved = self.resolveCommand(command);
81
+
82
+ try {
83
+ const lib = new (require(resolved.path))();
84
+
85
+ // Initialize
86
+ lib.Api = self;
87
+ lib.Manager = self.Manager;
88
+ lib.libraries = self.Manager.libraries;
89
+ lib.assistant = self.assistant;
90
+ lib.payload = _.cloneDeep({
91
+ data: {
92
+ payload: payload ? payload : self.payload.data.payload,
93
+ },
94
+ user: user ? user : self.payload.user,
95
+ response: response ? response : self.payload.response,
96
+ });
97
+
98
+ // lib.payload = {};
99
+ //
100
+ // // Set payload and user if it's provided
101
+ // lib.payload.data.payload = payload ? _.cloneDeep(payload) : lib.payload.data.payload;
102
+ // lib.payload.user = user ? _.cloneDeep(user) : lib.payload.user;
103
+ // lib.payload.response = response ? _.cloneDeep(response) : lib.payload.response;
104
+
105
+ return resolve(lib);
106
+ } catch (e) {
107
+ return reject(e);
108
+ }
109
+
110
+ });
111
+ }
112
+
77
113
  Module.prototype.resolveCommand = function (command) {
78
114
  const self = this;
79
115
 
@@ -116,6 +152,10 @@ Module.prototype.resolveCommand = function (command) {
116
152
  } else if (command === 'admin:payment-processor' || command === 'payment-processor') { // rename: admin:payment-processor
117
153
  command = 'admin:payment-processor';
118
154
 
155
+ // Special
156
+ } else if (command === 'special:setup-electron-manager-client' || command === 'setup-electron-manager-client') {
157
+ command = 'special:setup-electron-manager-client';
158
+
119
159
  // Test
120
160
  } else if (command === 'test:authenticate' || command === 'authenticate') {
121
161
  command = 'test:authenticate';
@@ -126,10 +166,13 @@ Module.prototype.resolveCommand = function (command) {
126
166
 
127
167
  // End
128
168
  } else {
129
- command = '';
169
+ command = 'error:error';
130
170
  }
131
171
 
132
- return command;
172
+ return {
173
+ command: command,
174
+ path: './' + path.join('./api/', `${command.replace(/\.\.\//g, '').replace(/\:/, '/')}.js`),
175
+ };
133
176
  }
134
177
 
135
178
  Module.prototype.resolveUser = function (options) {
@@ -13,9 +13,11 @@ let sampleUser = {
13
13
  }
14
14
  },
15
15
  authenticated: false,
16
- _meta: {
17
- lastStatsReset: new Date(),
18
- lastUserFetch: new Date(),
16
+ _APIManager: {
17
+ meta: {
18
+ lastStatsReset: new Date(),
19
+ lastUserFetch: new Date(),
20
+ }
19
21
  }
20
22
  }
21
23
 
@@ -42,7 +44,7 @@ ApiManager.prototype.init = function (options) {
42
44
  options.plans = options.plans || {};
43
45
 
44
46
  // await self.Manager.libraries.admin.firestore
45
- // options.plans.basic = options.plans.basic || {requests: 93};
47
+ // options.plans.basic = options.plans.basic || {requests: 100};
46
48
 
47
49
  options.maxUsersStored = options.maxUsersStored || 10000;
48
50
  options.refetchInterval = options.refetchInterval || 60;
@@ -89,9 +91,18 @@ ApiManager.prototype.init = function (options) {
89
91
 
90
92
  ApiManager.prototype._createNewUser = function (authenticatedUser, planId, persistentData, isRefetch, apiKey) {
91
93
  const self = this;
94
+ const _APIManager_default = {
95
+ stats: {
96
+ requests: 0,
97
+ },
98
+ meta: {
99
+ lastStatsReset: new Date(),
100
+ lastUserFetch: new Date(),
101
+ },
102
+ providedAPIKey: apiKey,
103
+ }
92
104
  persistentData = persistentData || {};
93
- persistentData._meta = persistentData._meta || {};
94
- persistentData._stats = persistentData._stats || {};
105
+ persistentData._APIManager = persistentData._APIManager || merge({}, _APIManager_default);
95
106
 
96
107
  let newUser = {
97
108
  api: get(authenticatedUser, 'api', {}),
@@ -99,36 +110,43 @@ ApiManager.prototype._createNewUser = function (authenticatedUser, planId, persi
99
110
  plan: {
100
111
  id: planId,
101
112
  limits: {
102
- requests: get(authenticatedUser, 'plan.limits.requests', get(self.options, `plans.${planId}.limits.requests`, 93)),
103
113
  }
104
114
  },
105
115
  authenticated: authenticatedUser.authenticated,
106
116
  ip: authenticatedUser.ip,
107
117
  country: authenticatedUser.country,
108
- _stats: {
109
- requests: 0,
110
- },
111
- _meta: {
112
- lastStatsReset: new Date(),
113
- lastUserFetch: new Date(),
114
- },
115
- _providedAPIKey: apiKey,
118
+ _APIManager: merge({}, _APIManager_default),
116
119
  }
117
- // console.log('-----MIN', moment().diff(moment(persistentData._meta.lastStatsReset), 'minutes', true), self.options.resetInterval);
118
- if (moment().diff(moment(persistentData._meta.lastStatsReset), 'minutes', true) < self.options.resetInterval) {
119
- newUser._meta.lastStatsReset = persistentData._meta.lastStatsReset || newUser._meta.lastStatsReset;
120
- newUser._meta.lastUserFetch = persistentData._meta.lastUserFetch || newUser._meta.lastUserFetch;
121
- Object.keys(persistentData._stats)
120
+
121
+ // Setup newUser
122
+ const currentPlan = get(self.options, `plans.${planId}.limits`, {})
123
+ Object.keys(currentPlan)
124
+ .forEach((id, i) => {
125
+ // console.log('----id', id);
126
+ // console.log('======currentPlan[id]', currentPlan[id]);
127
+ newUser.plan.limits[id] = get(authenticatedUser, `plan.limits.${id}`, currentPlan[id])
128
+ // const product = data.products[id]
129
+ // options.plans[product.planId] = {}
130
+ // options.plans[product.planId].limits = product.limits || {};
131
+ });
132
+
133
+
134
+
135
+ // console.log('-----MIN', moment().diff(moment(persistentData._APIManager.meta.lastStatsReset), 'minutes', true), self.options.resetInterval);
136
+ if (moment().diff(moment(persistentData._APIManager.meta.lastStatsReset), 'minutes', true) < self.options.resetInterval) {
137
+ newUser._APIManager.meta.lastStatsReset = persistentData._APIManager.meta.lastStatsReset || newUser._APIManager.meta.lastStatsReset;
138
+ newUser._APIManager.meta.lastUserFetch = persistentData._APIManager.meta.lastUserFetch || newUser._APIManager.meta.lastUserFetch;
139
+ Object.keys(persistentData._APIManager.stats)
122
140
  .forEach((key, i) => {
123
- newUser._stats[key] = persistentData._stats[key];
141
+ newUser._APIManager.stats[key] = persistentData._APIManager.stats[key];
124
142
  });
125
143
  } else {
126
144
  // console.log('---RESSET INTERVAL REACHED');
127
- newUser._meta.lastUserFetch = persistentData._meta.lastUserFetch;
145
+ newUser._APIManager.meta.lastUserFetch = persistentData._APIManager.meta.lastUserFetch;
128
146
  }
129
147
 
130
148
  if (isRefetch) {
131
- newUser._meta.lastUserFetch = new Date();
149
+ newUser._APIManager.meta.lastUserFetch = new Date();
132
150
  }
133
151
 
134
152
  return newUser;
@@ -145,14 +163,14 @@ ApiManager.prototype.getUser = async function (assistant) {
145
163
  if (apiKey) {
146
164
  newUser = self.userList.filter(user => user.api.privateKey === apiKey);
147
165
  if (newUser[0]) {
148
- if (newUser.length > 1 || moment().diff(moment(newUser[0]._meta.lastUserFetch), 'minutes', true) > self.options.refetchInterval) {
166
+ if (newUser.length > 1 || moment().diff(moment(newUser[0]._APIManager.meta.lastUserFetch), 'minutes', true) > self.options.refetchInterval) {
149
167
  // console.log('----REFETCHING');
150
- persistentData = {set: true, _stats: merge({}, newUser[0]._stats), _meta: merge({}, newUser[0]._meta)};
168
+ persistentData = {set: true, _APIManager: merge({}, newUser[0]._APIManager)};
151
169
 
152
170
  self.userList = self.userList.filter(user => user.api.privateKey !== apiKey)
153
171
  newUser = null;
154
172
  } else {
155
- persistentData = {set: true, _stats: merge({}, newUser[0]._stats), _meta: merge({}, newUser[0]._meta)};
173
+ persistentData = {set: true, _APIManager: merge({}, newUser[0]._APIManager)};
156
174
 
157
175
  newUser = newUser[0];
158
176
  }
@@ -178,10 +196,10 @@ ApiManager.prototype.getUser = async function (assistant) {
178
196
  let existingUser = self.userList.find(user => user.auth.uid === workingUID);
179
197
  if (existingUser) {
180
198
  // console.log('---actually does exist so setting');
181
- // console.log('----1111 MIN lastUserFetch', moment().diff(moment(existingUser._meta.lastUserFetch), 'minutes', true), self.options.refetchInterval);
182
- persistentData = !persistentData.set ? {set: true, _stats: merge({}, existingUser._stats), _meta: merge({}, existingUser._meta)} : persistentData;
199
+ // console.log('----1111 MIN lastUserFetch', moment().diff(moment(existingUser._APIManager.meta.lastUserFetch), 'minutes', true), self.options.refetchInterval);
200
+ persistentData = !persistentData.set ? {set: true, _APIManager: merge({}, existingUser._APIManager)} : persistentData;
183
201
  // console.log('----persistentData 2', persistentData);
184
- if (moment().diff(moment(existingUser._meta.lastUserFetch), 'minutes', true) > self.options.refetchInterval) {
202
+ if (moment().diff(moment(existingUser._APIManager.meta.lastUserFetch), 'minutes', true) > self.options.refetchInterval) {
185
203
  // console.log('----REFETCHING');
186
204
  self.userList = self.userList.filter(user => user.auth.uid !== workingUID)
187
205
  existingUser = self._createNewUser(authenticatedUser, planId, persistentData, true, apiKey);
@@ -205,17 +223,40 @@ ApiManager.prototype.getUser = async function (assistant) {
205
223
 
206
224
  function _getUserStat(self, user, stat, def) {
207
225
  const isWhitelistedAPIKey = self.options.whitelistedAPIKeys.includes(
208
- get(user, `api.privateKey`, get(user, `_providedAPIKey`))
226
+ get(user, `api.privateKey`, get(user, `_APIManager.providedAPIKey`))
209
227
  );
210
228
  // console.log('----user', user);
211
229
  // console.log('----isWhitelistedAPIKey', isWhitelistedAPIKey);
212
230
  return {
213
- current: !isWhitelistedAPIKey ? get(user, `_stats.${stat}`, typeof def !== 'undefined' ? def : 0) : 0,
231
+ current: !isWhitelistedAPIKey ? get(user, `_APIManager.stats.${stat}`, typeof def !== 'undefined' ? def : 0) : 0,
214
232
  limit: !isWhitelistedAPIKey ? get(user, `plan.limits.${stat}`, typeof def !== 'undefined' ? def : 0) : Infinity,
215
233
  }
216
234
  }
217
235
 
218
- ApiManager.prototype.getUserStat = function (user, stat, def) {
236
+ ApiManager.prototype.isUserOverStat = function (user, stat, def, frame) {
237
+ const self = this;
238
+ if (!user || !stat) {
239
+ throw new Error('<user> and <stat> required')
240
+ }
241
+ const result = self.getUserStat(user, stat, def);
242
+ frame = frame || 'daily';
243
+ let limit = result.limit;
244
+ // console.log('---result', result);
245
+ // console.log('---typeof result.current', typeof result.current);
246
+ // console.log('----limit', limit);
247
+ if (typeof result.limit === 'number') {
248
+ if (frame === 'daily') {
249
+ limit = Math.floor(result.limit / 31);
250
+ }
251
+ // console.log('----limit', limit);
252
+ // console.log('-----result.current < limit', result.current < limit);
253
+ return limit >= result.current;
254
+ }
255
+
256
+ return false;
257
+ }
258
+
259
+ ApiManager.prototype.getUserStat = function (user, stat, def, ) {
219
260
  const self = this;
220
261
  if (!user || !stat) {
221
262
  throw new Error('<user> and <stat> required')
@@ -228,7 +269,7 @@ ApiManager.prototype.incrementUserStat = function (user, stat, amount) {
228
269
  if (!user || !stat) {
229
270
  throw new Error('<user> and <stat> required')
230
271
  }
231
- set(user, `_stats.${stat}`, get(user, `_stats.${stat}`, 0) + amount)
272
+ set(user, `_APIManager.stats.${stat}`, get(user, `_APIManager.stats.${stat}`, 0) + amount)
232
273
  return _getUserStat(self, user, stat, 0);
233
274
  }
234
275
 
@@ -11,7 +11,7 @@ function User(settings, options) {
11
11
  options = options || {};
12
12
  let now = powertools.timestamp(new Date(), {output: 'string'});
13
13
  let nowUNIX = powertools.timestamp(now, {output: 'unix'});
14
- let oldDate = powertools.timestamp(new Date('1999/01/01'), {output: 'string'})
14
+ let oldDate = powertools.timestamp(new Date(0), {output: 'string'})
15
15
  let oldDateUNIX = powertools.timestamp(oldDate, {output: 'unix'});
16
16
 
17
17
  const useDefaults = typeof options.defaults === 'undefined' ? true : options.defaults;
@@ -34,7 +34,7 @@ function User(settings, options) {
34
34
  timestampUNIX: _.get(settings, 'plan.expires.timestampUNIX', useDefaults ? oldDateUNIX : null),
35
35
  },
36
36
  limits: {
37
- devices: _.get(settings, 'plan.limits.devices', useDefaults ? 1 : null),
37
+ // devices: _.get(settings, 'plan.limits.devices', null),
38
38
  },
39
39
  payment: {
40
40
  processor: _.get(settings, 'plan.payment.processor', null), // paypal | stripe | chargebee, etc
@@ -90,13 +90,14 @@ Manager.prototype.init = function (exporter, options) {
90
90
  }
91
91
 
92
92
  if (self.options.sentry) {
93
- // console.log('Setting up sentry:', `${self.project.projectId}@${self.package.version}`);
94
- // console.log('self.config.sentry.dsn', self.config.sentry.dsn);
93
+ const sentryRelease = `${self.config.app.id || self.project.projectId}@${self.package.version}`;
95
94
  const sentryDSN = get(self.config, 'sentry.dsn', '');
95
+ // console.log('Sentry', sentryRelease, sentryDSN);
96
+
96
97
  self.libraries.sentry = require('@sentry/node');
97
98
  self.libraries.sentry.init({
98
99
  dsn: sentryDSN,
99
- release: `${self.project.projectId}@${self.package.version}`,
100
+ release: sentryRelease,
100
101
  beforeSend(event, hint) {
101
102
  if (self.assistant.meta.environment === 'development' && !self.options.reportErrorsInDev) {
102
103
  self.assistant.error('Skipping Sentry because DEV')
@@ -1,4 +1,7 @@
1
1
  {
2
+ "app": {
3
+ "id": "my-app"
4
+ },
2
5
  "brand": {
3
6
  "name": "My Brand",
4
7
  "url": "https://example.com",