backend-manager 4.2.1 → 4.2.2

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.2",
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,16 +82,21 @@ 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
 
97
+ // Log
98
+ self.assistant.log(`updateStats(): Starting...`);
99
+
92
100
  // Fix user stats
93
101
  if (
94
102
  !existingData?.users?.total
@@ -178,6 +186,9 @@ Module.prototype.updateStats = function (existingData, update) {
178
186
  // Set metadata
179
187
  newData.metadata = self.Manager.Metadata().set({tag: 'admin:get-stats'})
180
188
 
189
+ // Log
190
+ self.assistant.log(`updateStats(): newData`, newData);
191
+
181
192
  // newData stats
182
193
  await stats
183
194
  .set(newData, { merge: true })
@@ -193,11 +204,22 @@ Module.prototype.updateStats = function (existingData, update) {
193
204
  Module.prototype.getAllUsers = function () {
194
205
  const self = this;
195
206
  return new Promise(async function(resolve, reject) {
207
+ // Set initial users
196
208
  self.users = [];
209
+
210
+ // Log
211
+ self.assistant.log(`getAllUsers(): Starting...`);
212
+
213
+ // Get users
197
214
  await getUsersBatch(self)
198
215
  .catch(e => {
199
216
  return reject(e);
200
217
  })
218
+
219
+ // Log
220
+ self.assistant.log(`getAllUsers(): Completed with ${self.users.length} users`);
221
+
222
+ // Return
201
223
  return resolve(self.users);
202
224
  });
203
225
  }
@@ -205,13 +227,25 @@ Module.prototype.getAllUsers = function () {
205
227
  Module.prototype.getAllNotifications = function () {
206
228
  const self = this;
207
229
  return new Promise(async function(resolve, reject) {
230
+
231
+ // Log
232
+ self.assistant.log(`getAllNotifications(): Starting...`);
233
+
234
+ // Get notifications
208
235
  await self.libraries.admin.firestore().collection('notifications')
209
236
  .count()
210
237
  .get()
211
238
  .then((snap) => {
212
- return snap.data().count;
239
+ // Set count
240
+ const count = snap.data().count;
241
+
242
+ // Log
243
+ self.assistant.log(`getAllNotifications(): Completed with ${count} notifications`);
244
+
245
+ // Return
246
+ return count;
213
247
  })
214
- .catch(function(e) {
248
+ .catch((e) => {
215
249
  return reject(e)
216
250
  });
217
251
  });
@@ -220,10 +254,14 @@ Module.prototype.getAllNotifications = function () {
220
254
  Module.prototype.getAllSubscriptions = function () {
221
255
  const self = this;
222
256
  return new Promise(async function(resolve, reject) {
257
+ // Log
258
+ self.assistant.log(`getAllSubscriptions(): Starting...`);
259
+
260
+ // Get subscriptions
223
261
  await self.libraries.admin.firestore().collection('users')
224
262
  .where('plan.expires.timestampUNIX', '>=', new Date().getTime() / 1000)
225
263
  .get()
226
- .then(function(snapshot) {
264
+ .then((snapshot) => {
227
265
  const stats = {
228
266
  totals: {
229
267
  total: 0,
@@ -232,6 +270,7 @@ Module.prototype.getAllSubscriptions = function () {
232
270
  plans: {}
233
271
  };
234
272
 
273
+ // Loop through
235
274
  snapshot
236
275
  .forEach((doc, i) => {
237
276
  const data = doc.data();
@@ -240,6 +279,7 @@ Module.prototype.getAllSubscriptions = function () {
240
279
  const isAdmin = data?.roles?.admin || false;
241
280
  const isVip = data?.roles?.vip || false;
242
281
 
282
+ // Set initial plan
243
283
  if (!stats.plans[planId]) {
244
284
  stats.plans[planId] = {
245
285
  total: 0,
@@ -249,20 +289,26 @@ Module.prototype.getAllSubscriptions = function () {
249
289
  }
250
290
  }
251
291
 
292
+ // Increment exempt
252
293
  if (isAdmin || isVip) {
253
294
  stats.totals.exempt++;
254
295
  stats.plans[planId].exempt++;
255
296
  return
256
297
  }
257
298
 
299
+ // Increment
258
300
  stats.totals.total++;
259
301
  stats.plans[planId].total++;
260
302
  stats.plans[planId][frequency] = (stats.plans[planId][frequency] || 0) + 1
261
303
  });
262
304
 
305
+ // Log
306
+ self.assistant.log(`getAllSubscriptions(): Completed with ${stats.totals.total} subscriptions`, stats);
307
+
308
+ // Return
263
309
  return resolve(stats);
264
310
  })
265
- .catch(function(e) {
311
+ .catch((e) => {
266
312
  return reject(e)
267
313
  });
268
314
 
@@ -271,23 +317,33 @@ Module.prototype.getAllSubscriptions = function () {
271
317
 
272
318
  function getUsersBatch(self, nextPageToken) {
273
319
  return new Promise(async function(resolve, reject) {
320
+ // Log
321
+ self.assistant.log(`getUsersBatch(): Starting...`);
322
+
323
+ // Get users
274
324
  self.libraries.admin.auth().listUsers(1000, nextPageToken)
275
- .then(function(listUsersResult) {
325
+ .then((listUsersResult) => {
326
+ // Concat users
276
327
  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 {
328
+
329
+ // Log
330
+ self.assistant.log(`getUsersBatch(): Completed with ${self.users.length} users`);
331
+
332
+ // Quit if no more users
333
+ if (!listUsersResult.pageToken) {
287
334
  return resolve(listUsersResult.users);
288
335
  }
336
+
337
+ // List next batch of users
338
+ getUsersBatch(self, listUsersResult.pageToken)
339
+ .then(() => {
340
+ return resolve(listUsersResult.users);
341
+ })
342
+ .catch((e) => {
343
+ return reject(e);
344
+ })
289
345
  })
290
- .catch(function(e) {
346
+ .catch((e) => {
291
347
  return reject(e);
292
348
  });
293
349
  });