backend-manager 4.2.1 → 4.2.3

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": "4.2.1",
3
+ "version": "4.2.3",
4
4
  "description": "Quick tools for developing Firebase functions",
5
5
  "main": "src/manager/index.js",
6
6
  "bin": {
@@ -23,8 +23,10 @@ Module.prototype.main = function () {
23
23
  return reject(assistant.errorify(`Admin required.`, {code: 401}));
24
24
  }
25
25
 
26
+ // Get stats ref
27
+ const stats = self.libraries.admin.firestore().doc(`meta/stats`);
28
+
26
29
  // Get stats
27
- const stats = self.libraries.admin.firestore().doc(`meta/stats`)
28
30
  await stats
29
31
  .get()
30
32
  .then(async (doc) => {
@@ -36,6 +38,7 @@ Module.prototype.main = function () {
36
38
  .catch(e => data = e)
37
39
  }
38
40
 
41
+ // Reject if error
39
42
  if (data instanceof Error) {
40
43
  return reject(assistant.errorify(data, {code: 500}));
41
44
  }
@@ -49,10 +52,12 @@ Module.prototype.main = function () {
49
52
  .catch(e => data = e)
50
53
 
51
54
 
55
+ // Reject if error
52
56
  if (data instanceof Error) {
53
57
  return reject(assistant.errorify(data, {code: 500}));
54
58
  }
55
59
 
60
+ // Return
56
61
  return resolve({data: data})
57
62
  })
58
63
  .catch((e) => {
@@ -68,8 +73,6 @@ Module.prototype.fixStats = function (data) {
68
73
  return new Promise(async function(resolve, reject) {
69
74
  const stats = self.libraries.admin.firestore().doc(`meta/stats`);
70
75
 
71
-
72
-
73
76
  return resolve();
74
77
  });
75
78
  }
@@ -79,35 +82,20 @@ Module.prototype.updateStats = function (existingData, update) {
79
82
  const self = this;
80
83
 
81
84
  return new Promise(async function(resolve, reject) {
85
+ // Get refs
82
86
  const stats = self.libraries.admin.firestore().doc(`meta/stats`);
83
87
  const gatheringOnline = self.libraries.admin.database().ref(`gatherings/online`);
84
88
  const sessionsApp = self.libraries.admin.database().ref(`sessions/app`);
85
89
  const sessionsOnline = self.libraries.admin.database().ref(`sessions/online`);
86
90
 
91
+ // Set defaults
87
92
  let error = null;
88
93
  let newData = {
89
94
  app: self.Manager.config?.app?.id || null,
90
95
  };
91
96
 
92
- // Fix user stats
93
- if (
94
- !existingData?.users?.total
95
- || update === true
96
- || update?.users
97
- ) {
98
- await self.getAllUsers()
99
- .then(r => {
100
- _.set(newData, 'users.total', r.length)
101
- })
102
- .catch(e => {
103
- error = new Error(`Failed fixing stats: ${e}`);
104
- })
105
- }
106
-
107
- // Reject if error
108
- if (error) {
109
- return reject(error);
110
- }
97
+ // Log
98
+ self.assistant.log(`updateStats(): Starting...`);
111
99
 
112
100
  // Fetch new notification stats
113
101
  if (
@@ -135,6 +123,21 @@ Module.prototype.updateStats = function (existingData, update) {
135
123
  })
136
124
  }
137
125
 
126
+ // Fix user stats
127
+ if (
128
+ !existingData?.users?.total
129
+ || update === true
130
+ || update?.users
131
+ ) {
132
+ await self.getAllUsers()
133
+ .then(r => {
134
+ _.set(newData, 'users.total', r.length)
135
+ })
136
+ .catch(e => {
137
+ error = new Error(`Failed fixing stats: ${e}`);
138
+ })
139
+ }
140
+
138
141
  // Reject if error
139
142
  if (error) {
140
143
  return reject(error);
@@ -149,7 +152,7 @@ Module.prototype.updateStats = function (existingData, update) {
149
152
  const existing = newData?.users?.online || 0;
150
153
 
151
154
  // Set new value
152
- _.set(newData, 'users.online', existing + keys.length)
155
+ _.set(newData, 'users.online', existing + keys.length);
153
156
  })
154
157
  .catch(e => {
155
158
  error = new Error(`Failed getting online users: ${e}`);
@@ -178,6 +181,9 @@ Module.prototype.updateStats = function (existingData, update) {
178
181
  // Set metadata
179
182
  newData.metadata = self.Manager.Metadata().set({tag: 'admin:get-stats'})
180
183
 
184
+ // Log
185
+ self.assistant.log(`updateStats(): newData`, newData);
186
+
181
187
  // newData stats
182
188
  await stats
183
189
  .set(newData, { merge: true })
@@ -193,11 +199,22 @@ Module.prototype.updateStats = function (existingData, update) {
193
199
  Module.prototype.getAllUsers = function () {
194
200
  const self = this;
195
201
  return new Promise(async function(resolve, reject) {
202
+ // Set initial users
196
203
  self.users = [];
204
+
205
+ // Log
206
+ self.assistant.log(`getAllUsers(): Starting...`);
207
+
208
+ // Get users
197
209
  await getUsersBatch(self)
198
210
  .catch(e => {
199
211
  return reject(e);
200
212
  })
213
+
214
+ // Log
215
+ self.assistant.log(`getAllUsers(): Completed with ${self.users.length} users`);
216
+
217
+ // Return
201
218
  return resolve(self.users);
202
219
  });
203
220
  }
@@ -205,13 +222,25 @@ Module.prototype.getAllUsers = function () {
205
222
  Module.prototype.getAllNotifications = function () {
206
223
  const self = this;
207
224
  return new Promise(async function(resolve, reject) {
225
+
226
+ // Log
227
+ self.assistant.log(`getAllNotifications(): Starting...`);
228
+
229
+ // Get notifications
208
230
  await self.libraries.admin.firestore().collection('notifications')
209
231
  .count()
210
232
  .get()
211
233
  .then((snap) => {
212
- return snap.data().count;
234
+ // Set count
235
+ const count = snap.data().count;
236
+
237
+ // Log
238
+ self.assistant.log(`getAllNotifications(): Completed with ${count} notifications`);
239
+
240
+ // Return
241
+ return resolve(count);
213
242
  })
214
- .catch(function(e) {
243
+ .catch((e) => {
215
244
  return reject(e)
216
245
  });
217
246
  });
@@ -220,10 +249,14 @@ Module.prototype.getAllNotifications = function () {
220
249
  Module.prototype.getAllSubscriptions = function () {
221
250
  const self = this;
222
251
  return new Promise(async function(resolve, reject) {
252
+ // Log
253
+ self.assistant.log(`getAllSubscriptions(): Starting...`);
254
+
255
+ // Get subscriptions
223
256
  await self.libraries.admin.firestore().collection('users')
224
257
  .where('plan.expires.timestampUNIX', '>=', new Date().getTime() / 1000)
225
258
  .get()
226
- .then(function(snapshot) {
259
+ .then((snapshot) => {
227
260
  const stats = {
228
261
  totals: {
229
262
  total: 0,
@@ -232,6 +265,7 @@ Module.prototype.getAllSubscriptions = function () {
232
265
  plans: {}
233
266
  };
234
267
 
268
+ // Loop through
235
269
  snapshot
236
270
  .forEach((doc, i) => {
237
271
  const data = doc.data();
@@ -240,6 +274,7 @@ Module.prototype.getAllSubscriptions = function () {
240
274
  const isAdmin = data?.roles?.admin || false;
241
275
  const isVip = data?.roles?.vip || false;
242
276
 
277
+ // Set initial plan
243
278
  if (!stats.plans[planId]) {
244
279
  stats.plans[planId] = {
245
280
  total: 0,
@@ -249,20 +284,26 @@ Module.prototype.getAllSubscriptions = function () {
249
284
  }
250
285
  }
251
286
 
287
+ // Increment exempt
252
288
  if (isAdmin || isVip) {
253
289
  stats.totals.exempt++;
254
290
  stats.plans[planId].exempt++;
255
291
  return
256
292
  }
257
293
 
294
+ // Increment
258
295
  stats.totals.total++;
259
296
  stats.plans[planId].total++;
260
297
  stats.plans[planId][frequency] = (stats.plans[planId][frequency] || 0) + 1
261
298
  });
262
299
 
300
+ // Log
301
+ self.assistant.log(`getAllSubscriptions(): Completed with ${stats.totals.total} subscriptions`, stats);
302
+
303
+ // Return
263
304
  return resolve(stats);
264
305
  })
265
- .catch(function(e) {
306
+ .catch((e) => {
266
307
  return reject(e)
267
308
  });
268
309
 
@@ -271,23 +312,33 @@ Module.prototype.getAllSubscriptions = function () {
271
312
 
272
313
  function getUsersBatch(self, nextPageToken) {
273
314
  return new Promise(async function(resolve, reject) {
315
+ // Log
316
+ self.assistant.log(`getUsersBatch(): Starting...`);
317
+
318
+ // Get users
274
319
  self.libraries.admin.auth().listUsers(1000, nextPageToken)
275
- .then(function(listUsersResult) {
320
+ .then((listUsersResult) => {
321
+ // Concat users
276
322
  self.users = self.users.concat(listUsersResult.users);
277
- if (listUsersResult.pageToken) {
278
- // List next batch of users.
279
- getUsersBatch(self, listUsersResult.pageToken)
280
- .then(() => {
281
- return resolve(listUsersResult.users);
282
- })
283
- .catch((e) => {
284
- return reject(e);
285
- })
286
- } else {
323
+
324
+ // Log
325
+ self.assistant.log(`getUsersBatch(): Completed with ${self.users.length} users`);
326
+
327
+ // Quit if no more users
328
+ if (!listUsersResult.pageToken) {
287
329
  return resolve(listUsersResult.users);
288
330
  }
331
+
332
+ // List next batch of users
333
+ getUsersBatch(self, listUsersResult.pageToken)
334
+ .then(() => {
335
+ return resolve(listUsersResult.users);
336
+ })
337
+ .catch((e) => {
338
+ return reject(e);
339
+ })
289
340
  })
290
- .catch(function(e) {
341
+ .catch((e) => {
291
342
  return reject(e);
292
343
  });
293
344
  });