backend-manager 2.3.11 → 2.3.14

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.3.11",
3
+ "version": "2.3.14",
4
4
  "description": "Quick tools for developing Firebase functions",
5
5
  "main": "src/manager/index.js",
6
6
  "bin": {
package/src/cli/cli.js CHANGED
@@ -895,7 +895,7 @@ function fix_firestoreRulesFile(self) {
895
895
 
896
896
  if (!hasTemplate) {
897
897
  log(chalk.red(`Could not find rules template. Please edit ${name} file and add`), chalk.red(`{{backend-manager}}`), chalk.red(`to it.`));
898
- reject()
898
+ return resolve()
899
899
  }
900
900
 
901
901
  let matchesVersion = contents.match(self.default.rulesVersionRegex);
@@ -927,7 +927,7 @@ function fix_realtimeRulesFile(self) {
927
927
 
928
928
  if (!hasTemplate) {
929
929
  log(chalk.red(`Could not find rules template. Please edit ${name} file and add`), chalk.red(`{{backend-manager}}`), chalk.red(`to it.`));
930
- reject()
930
+ return resolve()
931
931
  }
932
932
 
933
933
  let matchesVersion = contents.match(self.default.rulesVersionRegex);
@@ -75,7 +75,8 @@ Module.prototype.updateStats = function (existingData) {
75
75
 
76
76
  return new Promise(async function(resolve, reject) {
77
77
  const stats = self.libraries.admin.firestore().doc(`meta/stats`);
78
- const online = self.libraries.admin.database().ref(`gatherings/online`);
78
+ const gatheringOnline = self.libraries.admin.database().ref(`gatherings/online`);
79
+ const sessionsApp = self.libraries.admin.database().ref(`sessions/app`);
79
80
 
80
81
  let error = null;
81
82
  let update = {};
@@ -106,7 +107,7 @@ Module.prototype.updateStats = function (existingData) {
106
107
 
107
108
  await self.getAllSubscriptions()
108
109
  .then(r => {
109
- _.set(update, 'subscriptions.total', r)
110
+ _.set(update, 'subscriptions', r)
110
111
  })
111
112
  .catch(e => {
112
113
  error = new Error(`Failed getting subscriptions: ${e}`);
@@ -116,12 +117,25 @@ Module.prototype.updateStats = function (existingData) {
116
117
  return reject(error);
117
118
  }
118
119
 
119
- await online
120
+ await gatheringOnline
120
121
  .once('value')
121
122
  .then((snap) => {
122
- let data = snap.val() || {};
123
- let keys = Object.keys(data);
124
- _.set(update, 'users.online', keys.length)
123
+ const data = snap.val() || {};
124
+ const keys = Object.keys(data);
125
+ const existing = _.get(update, 'users.online', 0)
126
+ _.set(update, 'users.online', existing + keys.length)
127
+ })
128
+ .catch(e => {
129
+ error = new Error(`Failed getting online users: ${e}`);
130
+ })
131
+
132
+ await sessionsApp
133
+ .once('value')
134
+ .then((snap) => {
135
+ const data = snap.val() || {};
136
+ const keys = Object.keys(data);
137
+ const existing = _.get(update, 'users.online', 0)
138
+ _.set(update, 'users.online', existing + keys.length)
125
139
  })
126
140
  .catch(e => {
127
141
  error = new Error(`Failed getting online users: ${e}`);
@@ -174,18 +188,43 @@ Module.prototype.getAllSubscriptions = function () {
174
188
  .where('plan.expires.timestampUNIX', '>=', new Date().getTime() / 1000)
175
189
  .get()
176
190
  .then(function(snapshot) {
177
- let count = 0;
191
+ const stats = {
192
+ totals: {
193
+ total: 0,
194
+ exempt: 0,
195
+ },
196
+ plans: {}
197
+ };
178
198
 
179
199
  snapshot
180
200
  .forEach((doc, i) => {
181
201
  const data = doc.data();
182
202
  const planId = _.get(data, 'plan.id', 'basic');
183
- if (!['', 'basic', 'free'].includes(planId)) {
184
- count++;
203
+ const frequency = _.get(data, 'plan.payment.frequency', 'unknown');
204
+ const isAdmin = _.get(data, 'roles.admin', false);
205
+ const isVip = _.get(data, 'roles.vip', false);
206
+
207
+ if (!stats.plans[planId]) {
208
+ stats.plans[planId] = {
209
+ total: 0,
210
+ monthly: 0,
211
+ annually: 0,
212
+ exempt: 0,
213
+ }
185
214
  }
215
+
216
+ if (isAdmin || isVip) {
217
+ stats.totals.exempt++;
218
+ stats.plans[planId].exempt++;
219
+ return
220
+ }
221
+
222
+ stats.totals.total++;
223
+ stats.plans[planId].total++;
224
+ stats.plans[planId][frequency] = (stats.plans[planId][frequency] || 0) + 1
186
225
  });
187
226
 
188
- return resolve(count);
227
+ return resolve(stats);
189
228
  })
190
229
  .catch(function(e) {
191
230
  return reject(e)
@@ -16,16 +16,17 @@ Module.prototype.main = function () {
16
16
  self.Api.resolveUser({adminRequired: true})
17
17
  .then(async (user) => {
18
18
  const uid = _.get(user, 'auth.uid', null);
19
- const id = _.get(payload.data.payload, 'id', 'sessions/app');
19
+ const id = _.get(payload.data.payload, 'id', 'app');
20
+ const session = `sessions/${id}`;
20
21
 
21
- assistant.log(`Getting active sessions for ${uid} @ ${id}`, {environment: 'production'})
22
+ assistant.log(`Getting active sessions for ${uid} @ ${session}`, {environment: 'production'})
22
23
 
23
- await self.libraries.admin.database().ref(id)
24
+ await self.libraries.admin.database().ref(session)
24
25
  .orderByChild('uid')
25
26
  .equalTo(uid)
26
27
  .once('value')
27
28
  .then(async (snap) => {
28
- const data = (snap.val() || []).filter(i => i);
29
+ const data = snap.val() || {};
29
30
  return resolve({data: data});
30
31
  })
31
32
  .catch(e => {
@@ -16,34 +16,20 @@ Module.prototype.main = function () {
16
16
  self.Api.resolveUser({adminRequired: true})
17
17
  .then(async (user) => {
18
18
  const uid = _.get(user, 'auth.uid', null);
19
- const id = _.get(payload.data.payload, 'id', 'sessions/app');
20
-
21
- assistant.log(`Signing out of all active sessions for ${uid} @ ${id}`, {environment: 'production'})
22
-
23
- await self.libraries.admin.database().ref(id)
24
- .orderByChild('uid')
25
- .equalTo(uid)
26
- .once('value')
27
- .then(async (snap) => {
28
- const data = snap.val();
29
- const keys = Object.keys(data || {});
30
- for (var i = 0; i < keys.length; i++) {
31
- const key = keys[i];
32
- self.assistant.log(`Signing out: ${key}`, {environment: 'production'});
33
- await self.libraries.admin.database().ref(`${id}/${key}/command`).set('signout').catch(e => self.assistant.error(`Failed to signout ${key}`, e))
34
- await powertools.wait(3000);
35
- await self.libraries.admin.database().ref(`${id}/${key}`).remove().catch(e => self.assistant.error(`Failed to delete ${key}`, e))
36
- }
37
- })
38
- .catch(e => {
39
- console.error('Session query error', e);
40
- })
19
+ const id = _.get(payload.data.payload, 'id', 'app');
20
+ const session = `sessions/${id}`;
21
+
22
+ let count = 0;
23
+
24
+ await self.signOutOfSession(uid, session).then(r => count += r);
25
+ // Legacy for somiibo and old electron-manager
26
+ await self.signOutOfSession(uid, 'gatherings/online').then(r => count += r);
41
27
 
42
28
  await self.libraries.admin
43
29
  .auth()
44
30
  .revokeRefreshTokens(uid)
45
31
  .then(() => {
46
- return resolve({data: {message: `Successfully signed ${uid} out of all sessions`}});
32
+ return resolve({data: {sessions: count, message: `Successfully signed ${uid} out of all sessions`}});
47
33
  })
48
34
  .catch(e => {
49
35
  return reject(assistant.errorManager(`Failed to sign out of all sessions: ${e}`, {code: 500, sentry: false, send: false, log: false}).error)
@@ -56,5 +42,39 @@ Module.prototype.main = function () {
56
42
 
57
43
  };
58
44
 
45
+ Module.prototype.signOutOfSession = function (uid, session) {
46
+ const self = this;
47
+ const Manager = self.Manager;
48
+ const Api = self.Api;
49
+ const assistant = self.assistant;
50
+ const payload = self.payload;
51
+
52
+ return new Promise(async function(resolve, reject) {
53
+ assistant.log(`Signing out of all active sessions for ${uid} @ ${session}`, {environment: 'production'})
54
+ let count = 0;
55
+
56
+ await self.libraries.admin.database().ref(session)
57
+ .orderByChild('uid')
58
+ .equalTo(uid)
59
+ .once('value')
60
+ .then(async (snap) => {
61
+ const data = snap.val() || {};
62
+ const keys = Object.keys(data);
63
+ for (var i = 0; i < keys.length; i++) {
64
+ const key = keys[i];
65
+ self.assistant.log(`Signing out: ${key}`, {environment: 'production'});
66
+ await self.libraries.admin.database().ref(`${session}/${key}/command`).set('signout').catch(e => self.assistant.error(`Failed to signout ${key}`, e))
67
+ // await powertools.wait(3000);
68
+ // await self.libraries.admin.database().ref(`${session}/${key}`).remove().catch(e => self.assistant.error(`Failed to delete ${key}`, e))
69
+ count++;
70
+ }
71
+ return resolve(count);
72
+ })
73
+ .catch(e => {
74
+ assistant.errorManager(`Session query error for session ${session}: ${e}`, {code: 500, sentry: true, send: false, log: true})
75
+ return reject(count)
76
+ })
77
+ });
78
+ }
59
79
 
60
80
  module.exports = Module;