backend-manager 4.2.0 → 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.0",
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,12 +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')
236
+ .count()
209
237
  .get()
210
- .then(function(querySnapshot) {
211
- return resolve(querySnapshot.size)
238
+ .then((snap) => {
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;
212
247
  })
213
- .catch(function(e) {
248
+ .catch((e) => {
214
249
  return reject(e)
215
250
  });
216
251
  });
@@ -219,10 +254,14 @@ Module.prototype.getAllNotifications = function () {
219
254
  Module.prototype.getAllSubscriptions = function () {
220
255
  const self = this;
221
256
  return new Promise(async function(resolve, reject) {
257
+ // Log
258
+ self.assistant.log(`getAllSubscriptions(): Starting...`);
259
+
260
+ // Get subscriptions
222
261
  await self.libraries.admin.firestore().collection('users')
223
262
  .where('plan.expires.timestampUNIX', '>=', new Date().getTime() / 1000)
224
263
  .get()
225
- .then(function(snapshot) {
264
+ .then((snapshot) => {
226
265
  const stats = {
227
266
  totals: {
228
267
  total: 0,
@@ -231,6 +270,7 @@ Module.prototype.getAllSubscriptions = function () {
231
270
  plans: {}
232
271
  };
233
272
 
273
+ // Loop through
234
274
  snapshot
235
275
  .forEach((doc, i) => {
236
276
  const data = doc.data();
@@ -239,6 +279,7 @@ Module.prototype.getAllSubscriptions = function () {
239
279
  const isAdmin = data?.roles?.admin || false;
240
280
  const isVip = data?.roles?.vip || false;
241
281
 
282
+ // Set initial plan
242
283
  if (!stats.plans[planId]) {
243
284
  stats.plans[planId] = {
244
285
  total: 0,
@@ -248,20 +289,26 @@ Module.prototype.getAllSubscriptions = function () {
248
289
  }
249
290
  }
250
291
 
292
+ // Increment exempt
251
293
  if (isAdmin || isVip) {
252
294
  stats.totals.exempt++;
253
295
  stats.plans[planId].exempt++;
254
296
  return
255
297
  }
256
298
 
299
+ // Increment
257
300
  stats.totals.total++;
258
301
  stats.plans[planId].total++;
259
302
  stats.plans[planId][frequency] = (stats.plans[planId][frequency] || 0) + 1
260
303
  });
261
304
 
305
+ // Log
306
+ self.assistant.log(`getAllSubscriptions(): Completed with ${stats.totals.total} subscriptions`, stats);
307
+
308
+ // Return
262
309
  return resolve(stats);
263
310
  })
264
- .catch(function(e) {
311
+ .catch((e) => {
265
312
  return reject(e)
266
313
  });
267
314
 
@@ -270,23 +317,33 @@ Module.prototype.getAllSubscriptions = function () {
270
317
 
271
318
  function getUsersBatch(self, nextPageToken) {
272
319
  return new Promise(async function(resolve, reject) {
320
+ // Log
321
+ self.assistant.log(`getUsersBatch(): Starting...`);
322
+
323
+ // Get users
273
324
  self.libraries.admin.auth().listUsers(1000, nextPageToken)
274
- .then(function(listUsersResult) {
325
+ .then((listUsersResult) => {
326
+ // Concat users
275
327
  self.users = self.users.concat(listUsersResult.users);
276
- if (listUsersResult.pageToken) {
277
- // List next batch of users.
278
- getUsersBatch(self, listUsersResult.pageToken)
279
- .then(() => {
280
- return resolve(listUsersResult.users);
281
- })
282
- .catch((e) => {
283
- return reject(e);
284
- })
285
- } 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) {
286
334
  return resolve(listUsersResult.users);
287
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
+ })
288
345
  })
289
- .catch(function(e) {
346
+ .catch((e) => {
290
347
  return reject(e);
291
348
  });
292
349
  });