backend-manager 2.3.0 → 2.3.1

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.0",
3
+ "version": "2.3.1",
4
4
  "description": "Quick tools for developing Firebase functions",
5
5
  "main": "src/manager/index.js",
6
6
  "bin": {
@@ -66,4 +66,4 @@
66
66
  "src/",
67
67
  "templates/"
68
68
  ]
69
- }
69
+ }
@@ -1,3 +1,5 @@
1
+ let _;
2
+
1
3
  function Module() {
2
4
 
3
5
  }
@@ -11,6 +13,8 @@ Module.prototype.main = function () {
11
13
 
12
14
  return new Promise(async function(resolve, reject) {
13
15
 
16
+ _ = Manager.require('lodash')
17
+
14
18
  if (!payload.user.roles.admin) {
15
19
  return reject(assistant.errorManager(`Admin required.`, {code: 401, sentry: false, send: false, log: false}).error)
16
20
  } else {
@@ -21,15 +25,11 @@ Module.prototype.main = function () {
21
25
  let data = doc.data() || {};
22
26
  let error = null;
23
27
 
24
- await self.fixStats(data)
28
+ await self.updateStats(data)
25
29
  .catch(e => {
26
30
  error = e;
27
31
  })
28
32
 
29
- await self.updateStats()
30
- .catch(e => {
31
- error = e;
32
- })
33
33
 
34
34
  if (error) {
35
35
  return reject(assistant.errorManager(error, {code: 500, sentry: false, send: false, log: false}).error)
@@ -64,63 +64,64 @@ Module.prototype.fixStats = function (data) {
64
64
  return new Promise(async function(resolve, reject) {
65
65
  const stats = self.libraries.admin.firestore().doc(`meta/stats`);
66
66
 
67
- if (!data || !data.users || !data.users.total || !data.subscriptions || !data.subscriptions.total) {
68
- let usersTotal = 0;
69
- let subscriptionsTotal = 0;
70
- let error = null;
67
+
68
+
69
+ return resolve();
70
+ });
71
+ }
72
+
73
+ Module.prototype.updateStats = function (existingData) {
74
+ const self = this;
75
+
76
+ return new Promise(async function(resolve, reject) {
77
+ const stats = self.libraries.admin.firestore().doc(`meta/stats`);
78
+ const online = self.libraries.admin.database().ref(`gatherings/online`);
79
+
80
+ let error = null;
81
+ let update = {};
82
+
83
+ // Fix broken stats
84
+ if (!_.get(existingData, 'users.total', null)) {
71
85
  await self.getAllUsers()
72
86
  .then(r => {
73
- usersTotal = r.length
87
+ _.set(update, 'users.total', r.length)
74
88
  })
75
89
  .catch(e => {
76
90
  error = new Error(`Failed fixing stats: ${e}`);
77
- self.assistant.error(error, {environment: 'production'});
78
- })
79
- await self.getAllSubscriptions()
80
- .then(r => {
81
- subscriptionsTotal = r
82
- })
83
- .catch(e => {
84
- error = new Error(`Failed getting subscriptions: ${e}`);
85
- self.assistant.error(error, {environment: 'production'});
86
91
  })
92
+ }
87
93
 
88
- if (error) {
89
- return reject(error);
90
- }
91
- await stats
92
- .set({
93
- users: {
94
- total: usersTotal,
95
- },
96
- subscriptions: {
97
- total: subscriptionsTotal,
98
- },
99
- }, { merge: true })
100
- .catch(function (e) {
101
- return reject(e);
102
- })
94
+ if (error) {
95
+ return reject(error);
103
96
  }
104
97
 
105
- return resolve(data);
106
- });
107
- }
98
+ // Fetch new stats
99
+ await self.getAllNotifications()
100
+ .then(r => {
101
+ _.set(update, 'notifications.total', r)
102
+ })
103
+ .catch(e => {
104
+ error = new Error(`Failed getting notifications: ${e}`);
105
+ })
108
106
 
109
- Module.prototype.updateStats = function () {
110
- const self = this;
107
+ await self.getAllSubscriptions()
108
+ .then(r => {
109
+ _.set(update, 'subscriptions.total', r)
110
+ })
111
+ .catch(e => {
112
+ error = new Error(`Failed getting subscriptions: ${e}`);
113
+ })
111
114
 
112
- return new Promise(async function(resolve, reject) {
113
- const stats = self.libraries.admin.firestore().doc(`meta/stats`);
114
- let online = self.libraries.admin.database().ref(`gatherings/online`);
115
- let onlineCount = 0;
116
- let error = null;
115
+ if (error) {
116
+ return reject(error);
117
+ }
117
118
 
118
119
  await online
119
120
  .once('value')
120
121
  .then((snap) => {
121
122
  let data = snap.val() || {};
122
123
  let keys = Object.keys(data);
123
- onlineCount = keys.length;
124
+ _.set(update, 'users.online', keys.length)
124
125
  })
125
126
  .catch(e => {
126
127
  error = new Error(`Failed getting online users: ${e}`);
@@ -131,13 +132,9 @@ Module.prototype.updateStats = function () {
131
132
  }
132
133
 
133
134
  await stats
134
- .set({
135
- users: {
136
- online: onlineCount
137
- }
138
- }, { merge: true })
135
+ .set(update, { merge: true })
139
136
  .catch(function (e) {
140
- return reject(`Failed getting stats: ${e}`);
137
+ return reject(new Error(`Failed getting stats: ${e}`));
141
138
  })
142
139
 
143
140
  return resolve();
@@ -156,7 +153,7 @@ Module.prototype.getAllUsers = function () {
156
153
  });
157
154
  }
158
155
 
159
- Module.prototype.getAllSubscriptions = function () {
156
+ Module.prototype.getAllNotifications = function () {
160
157
  const self = this;
161
158
  return new Promise(async function(resolve, reject) {
162
159
  await self.libraries.admin.firestore().collection('notifications/subscriptions/all')
@@ -170,6 +167,33 @@ Module.prototype.getAllSubscriptions = function () {
170
167
  });
171
168
  }
172
169
 
170
+ Module.prototype.getAllSubscriptions = function () {
171
+ const self = this;
172
+ return new Promise(async function(resolve, reject) {
173
+ await self.libraries.admin.firestore().collection('users')
174
+ .where('plan.expires.timestampUNIX', '>=', new Date().getTime() / 1000)
175
+ .get()
176
+ .then(function(snapshot) {
177
+ let count = 0;
178
+
179
+ snapshot
180
+ .forEach((doc, i) => {
181
+ const data = doc.data();
182
+ const planId = _.get(data, 'plan.id', 'basic');
183
+ if (!['', 'basic', 'free'].includes(planId)) {
184
+ count++;
185
+ }
186
+ });
187
+
188
+ return resolve(count);
189
+ })
190
+ .catch(function(e) {
191
+ return reject(e)
192
+ });
193
+
194
+ });
195
+ }
196
+
173
197
  function getUsersBatch(self, nextPageToken) {
174
198
  return new Promise(async function(resolve, reject) {
175
199
  self.libraries.admin.auth().listUsers(1000, nextPageToken)
@@ -23,11 +23,11 @@
23
23
  || (auth == null && (data.child('uid').val() == ''))
24
24
  ",
25
25
  ".write": "
26
- // Allowed if the user is signed in AND is the owner of the existing doc
26
+ // Allowed if the user is authenticated AND is the owner of the existing doc
27
27
  (auth != null && auth.uid == data.child('uid').val())
28
- // Allowed if the user is signed in AND is the owner of the new doc
28
+ // Allowed if the user is authenticated AND is the owner of the new doc
29
29
  || (auth != null && auth.uid == newData.child('uid').val())
30
- // Allowed if the user is signed in AND is the owner of the existing doc
30
+ // Allowed if the user is authenticated AND is the owner of the existing doc
31
31
  || (auth != null && auth.uid == data.child('uid').val())
32
32
  // Allowed if uid is equal to the doc id [LEGACY FOR SOMIIBO]
33
33
  || (auth != null && auth.uid == $id)