backend-manager 1.1.103 → 2.0.0

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.
Files changed (24) hide show
  1. package/package.json +19 -20
  2. package/src/cli/cli.js +22 -7
  3. package/src/manager/functions/core/actions/api/admin/create-post.js +160 -0
  4. package/src/manager/functions/core/actions/api/admin/firestore-query.js +172 -0
  5. package/src/manager/functions/core/actions/api/admin/firestore-read.js +54 -0
  6. package/src/manager/functions/core/actions/api/admin/firestore-write.js +47 -0
  7. package/src/manager/functions/core/actions/api/admin/get-stats.js +206 -0
  8. package/src/manager/functions/core/actions/api/admin/payment-processor.js +56 -0
  9. package/src/manager/functions/core/actions/api/admin/send-notification.js +189 -0
  10. package/src/manager/functions/core/actions/api/general/generate-uuid.js +49 -0
  11. package/src/manager/functions/core/actions/api/handler/create-post.js +118 -0
  12. package/src/manager/functions/core/actions/api/template.js +34 -0
  13. package/src/manager/functions/core/actions/api/test/authenticate.js +31 -0
  14. package/src/manager/functions/core/actions/api/test/create-test-accounts.js +36 -0
  15. package/src/manager/functions/core/actions/api/test/webhook.js +35 -0
  16. package/src/manager/functions/core/actions/api/user/create-custom-token.js +63 -0
  17. package/src/manager/functions/core/actions/api/user/delete.js +73 -0
  18. package/src/manager/functions/core/actions/api/user/get-subscription-info.js +63 -0
  19. package/src/manager/functions/core/actions/api/user/sign-out-all-sessions.js +86 -0
  20. package/src/manager/functions/core/actions/api/user/sign-up.js +241 -0
  21. package/src/manager/functions/core/actions/api.js +117 -323
  22. package/src/manager/functions/core/actions/old/api-2.js +414 -0
  23. package/src/manager/functions/core/admin/create-post.js +1 -1
  24. package/src/manager/index.js +1 -18
@@ -0,0 +1,36 @@
1
+ function Module() {
2
+
3
+ }
4
+
5
+ Module.prototype.init = async function (s, payload) {
6
+ const self = this;
7
+ self.Manager = s.Manager;
8
+ self.libraries = s.Manager.libraries;
9
+ self.assistant = s.Manager.assistant;
10
+ self.payload = payload;
11
+
12
+ return self;
13
+ };
14
+
15
+ Module.prototype.main = function () {
16
+ const self = this;
17
+ const Manager = self.Manager;
18
+ const assistant = self.assistant;
19
+ const payload = self.payload;
20
+
21
+ return new Promise(async function(resolve, reject) {
22
+
23
+ if (!payload.user.roles.admin) {
24
+ return reject(assistant.errorManager(`Admin required.`, {code: 401, sentry: false, send: false, log: false}).error)
25
+ }
26
+
27
+ assistant.log('User:', payload.user);
28
+
29
+ return resolve({data: {user: user}});
30
+
31
+ });
32
+
33
+ };
34
+
35
+
36
+ module.exports = Module;
@@ -0,0 +1,35 @@
1
+ function Module() {
2
+
3
+ }
4
+
5
+ Module.prototype.init = async function (s, payload) {
6
+ const self = this;
7
+ self.Manager = s.Manager;
8
+ self.libraries = s.Manager.libraries;
9
+ self.assistant = s.Manager.assistant;
10
+ self.payload = payload;
11
+
12
+ return self;
13
+ };
14
+
15
+ Module.prototype.main = function () {
16
+ const self = this;
17
+ const Manager = self.Manager;
18
+ const assistant = self.assistant;
19
+ const payload = self.payload;
20
+
21
+ return new Promise(async function(resolve, reject) {
22
+
23
+
24
+ if (payload.data.payload.status >= 200 && payload.data.payload.status <= 299) {
25
+ return resolve({data: payload.data.payload.response, status: payload.data.payload.status});
26
+ } else if (payload.data.payload.status >= 400 && payload.data.payload.status <= 599) {
27
+ return reject(assistant.errorManager(payload.data.payload.response || 'Unknown error message provided', {code: payload.data.payload.status, sentry: false, send: false, log: false}).error)
28
+ }
29
+
30
+ });
31
+
32
+ };
33
+
34
+
35
+ module.exports = Module;
@@ -0,0 +1,63 @@
1
+ const _ = require('lodash')
2
+
3
+ function Module() {
4
+
5
+ }
6
+
7
+ Module.prototype.init = async function (s, payload) {
8
+ const self = this;
9
+ self.Manager = s.Manager;
10
+ self.libraries = s.Manager.libraries;
11
+ self.assistant = s.Manager.assistant;
12
+ self.payload = payload;
13
+
14
+ return self;
15
+ };
16
+
17
+ Module.prototype.main = function () {
18
+ const self = this;
19
+ const Manager = self.Manager;
20
+ const assistant = self.assistant;
21
+ const payload = self.payload;
22
+
23
+ return new Promise(async function(resolve, reject) {
24
+ let user = null;
25
+ if (payload.user.roles.admin && payload.data.payload.uid) {
26
+ await self.libraries.admin.firestore().doc(`users/${payload.data.payload.uid}`)
27
+ .get()
28
+ .then(async function (doc) {
29
+ const data = doc.data();
30
+ if (data) {
31
+ user = data;
32
+ } else {
33
+ throw new Error('User does not exist')
34
+ }
35
+ })
36
+ .catch(function (e) {
37
+ user = e;
38
+ })
39
+ } else if (payload.user.authenticated) {
40
+ user = payload.user;
41
+ }
42
+
43
+ if (user instanceof Error) {
44
+ return reject(assistant.errorManager(user, {code: 400, sentry: false, send: false, log: false}).error)
45
+ } else if (!user) {
46
+ return reject(assistant.errorManager(`Admin or authenticated user required.`, {code: 401, sentry: false, send: false, log: false}).error)
47
+ } else {
48
+
49
+ await self.libraries.admin.auth().createCustomToken(_.get(user, 'auth.uid', null))
50
+ .then(token => {
51
+ return resolve({data: {token: token}});
52
+ })
53
+ .catch(e => {
54
+ return reject(assistant.errorManager(`Failed to create custom token: ${e}`, {code: 400, sentry: false, send: false, log: false}).error)
55
+ })
56
+ }
57
+
58
+ });
59
+
60
+ };
61
+
62
+
63
+ module.exports = Module;
@@ -0,0 +1,73 @@
1
+ const _ = require('lodash')
2
+
3
+ function Module() {
4
+
5
+ }
6
+
7
+ Module.prototype.init = async function (s, payload) {
8
+ const self = this;
9
+ self.Manager = s.Manager;
10
+ self.libraries = s.Manager.libraries;
11
+ self.assistant = s.Manager.assistant;
12
+ self.payload = payload;
13
+
14
+ return self;
15
+ };
16
+
17
+ Module.prototype.main = function () {
18
+ const self = this;
19
+ const Manager = self.Manager;
20
+ const assistant = self.assistant;
21
+ const payload = self.payload;
22
+
23
+ return new Promise(async function(resolve, reject) {
24
+ let user = null;
25
+ if (payload.user.roles.admin && payload.data.payload.uid) {
26
+ await self.libraries.admin.firestore().doc(`users/${payload.data.payload.uid}`)
27
+ .get()
28
+ .then(async function (doc) {
29
+ const data = doc.data();
30
+ if (data) {
31
+ user = data;
32
+ } else {
33
+ throw new Error('User does not exist')
34
+ }
35
+ })
36
+ .catch(function (e) {
37
+ user = e;
38
+ })
39
+ } else if (payload.user.authenticated) {
40
+ user = payload.user;
41
+ }
42
+
43
+ if (user instanceof Error) {
44
+ return reject(assistant.errorManager(user, {code: 400, sentry: false, send: false, log: false}).error)
45
+ } else if (!user) {
46
+ return reject(assistant.errorManager(`Admin or authenticated user required.`, {code: 401, sentry: false, send: false, log: false}).error)
47
+ } else {
48
+ // const planExpireDate = new Date(_.get(payload.user, 'plan.expires.timestamp', 0));
49
+ // if (planExpireDate >= new Date()) {
50
+ // payload.response.status = 401;
51
+ // payload.response.error = new Error(`Failed to delete user: There is an active paid subscription on this account. Please cancel it first and then try deleting the account again.`);
52
+ // return reject(payload.response.error);
53
+ // }
54
+ const isPlanActive = _.get(user, 'plan.payment.active', null);
55
+ if (isPlanActive === true) {
56
+ return reject(assistant.errorManager(`Failed to delete user: There is an active paid subscription on this account. Please cancel it first and then try deleting the account again.`, {code: 400, sentry: false, send: false, log: false}).error)
57
+ }
58
+
59
+ await self.libraries.admin.auth().deleteUser(_.get(user, 'auth.uid', null))
60
+ .then(() => {
61
+ return resolve({data: {success: true}});
62
+ })
63
+ .catch(e => {
64
+ return reject(assistant.errorManager(`Failed to delete user: ${e}`, {code: 400, sentry: false, send: false, log: false}).error)
65
+ })
66
+ }
67
+
68
+ });
69
+
70
+ };
71
+
72
+
73
+ module.exports = Module;
@@ -0,0 +1,63 @@
1
+ const _ = require('lodash')
2
+
3
+ function Module() {
4
+
5
+ }
6
+
7
+ Module.prototype.init = async function (s, payload) {
8
+ const self = this;
9
+ self.Manager = s.Manager;
10
+ self.libraries = s.Manager.libraries;
11
+ self.assistant = s.Manager.assistant;
12
+ self.payload = payload;
13
+
14
+ return self;
15
+ };
16
+
17
+ Module.prototype.main = function () {
18
+ const self = this;
19
+ const Manager = self.Manager;
20
+ const assistant = self.assistant;
21
+ const payload = self.payload;
22
+
23
+ return new Promise(async function(resolve, reject) {
24
+ let user = null;
25
+ if (payload.user.roles.admin && payload.data.payload.uid) {
26
+ await self.libraries.admin.firestore().doc(`users/${payload.data.payload.uid}`)
27
+ .get()
28
+ .then(async function (doc) {
29
+ const data = doc.data();
30
+ if (data) {
31
+ user = data;
32
+ } else {
33
+ throw new Error('User does not exist')
34
+ }
35
+ })
36
+ .catch(function (e) {
37
+ user = e;
38
+ })
39
+ } else if (payload.user.authenticated) {
40
+ user = payload.user;
41
+ }
42
+
43
+ if (user instanceof Error) {
44
+ return reject(assistant.errorManager(user, {code: 400, sentry: false, send: false, log: false}).error)
45
+ } else if (!user) {
46
+ return reject(assistant.errorManager(`Admin or authenticated user required.`, {code: 401, sentry: false, send: false, log: false}).error)
47
+ } else {
48
+ const result = {
49
+ plan: {
50
+ id: _.get(user, 'plan.id', 'unknown'),
51
+ payment: {
52
+ active: _.get(user, 'plan.payment.active', false),
53
+ },
54
+ }
55
+ }
56
+ return resolve({data: result});
57
+ }
58
+ });
59
+
60
+ };
61
+
62
+
63
+ module.exports = Module;
@@ -0,0 +1,86 @@
1
+ const _ = require('lodash')
2
+
3
+ function Module() {
4
+
5
+ }
6
+
7
+ Module.prototype.init = async function (s, payload) {
8
+ const self = this;
9
+ self.Manager = s.Manager;
10
+ self.libraries = s.Manager.libraries;
11
+ self.assistant = s.Manager.assistant;
12
+ self.payload = payload;
13
+
14
+ return self;
15
+ };
16
+
17
+ Module.prototype.main = function () {
18
+ const self = this;
19
+ const Manager = self.Manager;
20
+ const assistant = self.assistant;
21
+ const payload = self.payload;
22
+
23
+ return new Promise(async function(resolve, reject) {
24
+ let user = null;
25
+ if (payload.user.roles.admin && payload.data.payload.uid) {
26
+ await self.libraries.admin.firestore().doc(`users/${payload.data.payload.uid}`)
27
+ .get()
28
+ .then(async function (doc) {
29
+ const data = doc.data();
30
+ if (data) {
31
+ user = data;
32
+ } else {
33
+ throw new Error('User does not exist')
34
+ }
35
+ })
36
+ .catch(function (e) {
37
+ user = e;
38
+ })
39
+ } else if (payload.user.authenticated) {
40
+ user = payload.user;
41
+ }
42
+
43
+ if (user instanceof Error) {
44
+ return reject(assistant.errorManager(user, {code: 400, sentry: false, send: false, log: false}).error)
45
+ } else if (!user) {
46
+ return reject(assistant.errorManager(`Admin or authenticated user required.`, {code: 401, sentry: false, send: false, log: false}).error)
47
+ } else {
48
+ const uid = _.get(user, 'auth.uid', null);
49
+
50
+ await self.libraries.admin.database().ref(`gatherings/online`)
51
+ .orderByChild('uid')
52
+ .equalTo(uid)
53
+ .once('value')
54
+ .then(async snap => {
55
+ const data = snap.val();
56
+ const keys = Object.keys(data || {});
57
+ for (var i = 0; i < keys.length; i++) {
58
+ const key = keys[i];
59
+ self.assistant.log(`Signing out: ${key}`, {environment: 'production'});
60
+ await self.libraries.admin.database().ref(`gatherings/online/${key}/command`).set('signout').catch(e => self.assistant.error(`Failed to signout ${key}`, e))
61
+ await powertools.wait(3000);
62
+ await self.libraries.admin.database().ref(`gatherings/online/${key}`).remove().catch(e => self.assistant.error(`Failed to delete ${key}`, e))
63
+ }
64
+ })
65
+ .catch(e => {
66
+ console.error('Gathering query error', e);
67
+ })
68
+
69
+ await self.libraries.admin
70
+ .auth()
71
+ .revokeRefreshTokens(uid)
72
+ .then(() => {
73
+ return resolve({data: {message: `Successfully signed ${uid} out of all sessions`}});
74
+ })
75
+ .catch(e => {
76
+ return reject(assistant.errorManager(`Failed to sign out of all sessions: ${e}`, {code: 500, sentry: false, send: false, log: false}).error)
77
+ })
78
+
79
+ }
80
+
81
+ });
82
+
83
+ };
84
+
85
+
86
+ module.exports = Module;
@@ -0,0 +1,241 @@
1
+ const _ = require('lodash')
2
+ const fetch = require('node-fetch');
3
+
4
+ function Module() {
5
+
6
+ }
7
+
8
+ Module.prototype.init = async function (s, payload) {
9
+ const self = this;
10
+ self.Manager = s.Manager;
11
+ self.libraries = s.Manager.libraries;
12
+ self.assistant = s.Manager.assistant;
13
+ self.payload = payload;
14
+
15
+ return self;
16
+ };
17
+
18
+ Module.prototype.main = function () {
19
+ const self = this;
20
+ const Manager = self.Manager;
21
+ const assistant = self.assistant;
22
+ const payload = self.payload;
23
+
24
+ return new Promise(async function(resolve, reject) {
25
+ let user = null;
26
+ if (payload.user.roles.admin && payload.data.payload.uid) {
27
+ await self.libraries.admin.firestore().doc(`users/${payload.data.payload.uid}`)
28
+ .get()
29
+ .then(async function (doc) {
30
+ const data = doc.data();
31
+ if (data) {
32
+ user = data;
33
+ } else {
34
+ throw new Error('User does not exist')
35
+ }
36
+ })
37
+ .catch(function (e) {
38
+ user = e;
39
+ })
40
+ } else if (payload.user.authenticated) {
41
+ user = payload.user;
42
+ }
43
+
44
+ if (user instanceof Error) {
45
+ return reject(assistant.errorManager(user, {code: 400, sentry: false, send: false, log: false}).error)
46
+ } else if (!user) {
47
+ return reject(assistant.errorManager(`Admin or authenticated user required.`, {code: 401, sentry: false, send: false, log: false}).error)
48
+ } else {
49
+
50
+ await self.signUp({
51
+ auth: {
52
+ uid: _.get(user, 'auth.uid', null),
53
+ email: _.get(user, 'auth.email', null),
54
+ },
55
+ affiliate: {
56
+ referrer: _.get(payload.data.payload, 'affiliateCode', null),
57
+ },
58
+ })
59
+ .then(async function (result) {
60
+ if (_.get(payload.data.payload, 'newsletterSignUp', false)) {
61
+ await addToMCList(
62
+ _.get(Manager.config, 'mailchimp.key'),
63
+ _.get(Manager.config, 'mailchimp.list_id'),
64
+ _.get(user, 'auth.email', null),
65
+ )
66
+ .then(function (res) {
67
+ assistant.log('Sucessfully added user to MC list.')
68
+ })
69
+ .catch(function (e) {
70
+ assistant.log('Failed to add user to MC list.', e)
71
+ })
72
+ }
73
+ return resolve({data: result});
74
+ })
75
+ .catch(function (e) {
76
+ return reject(assistant.errorManager(`Failed to sign up: ${e}`, {code: 500, sentry: false, send: false, log: false}).error)
77
+ })
78
+
79
+ }
80
+
81
+ });
82
+
83
+ };
84
+
85
+ Module.prototype.signUp = function (payload) {
86
+ const self = this;
87
+ const result = {
88
+ signedUp: false,
89
+ referrerUid: undefined,
90
+ // updatedReferral: true,
91
+ };
92
+ let error;
93
+ payload = payload || {};
94
+
95
+ return new Promise(async function(resolve, reject) {
96
+ let existingUser = {};
97
+ let finalPayload = {};
98
+
99
+ if (!_.get(payload, 'auth.uid', null) || !_.get(payload, 'auth.email', null)) {
100
+ return reject(new Error('Cannot create user without UID and email.'))
101
+ }
102
+
103
+ await self.updateReferral({
104
+ affiliateCode: _.get(payload, 'affiliate.referrer', null),
105
+ uid: payload.auth.uid,
106
+ })
107
+ .then(r => {
108
+ payload.affiliate.referrer = r.referrerUid;
109
+ result.referrerUid = payload.affiliate.referrer;
110
+ })
111
+ .catch(function (e) {
112
+ payload.affiliate.referrer = undefined;
113
+ console.error('Failed to update affiliate code', e)
114
+ })
115
+
116
+ // payload.affiliate.referrer = undefined;
117
+
118
+ await self.libraries.admin.firestore().doc(`users/${payload.auth.uid}`)
119
+ .get()
120
+ .then(async function (doc) {
121
+ existingUser = doc.data() || {};
122
+ })
123
+ .catch(function (e) {
124
+ error = e;
125
+ })
126
+
127
+ if (error) {
128
+ return reject(error);
129
+ }
130
+
131
+ const user = self.Manager.User(payload);
132
+
133
+ // Merge the payload and the default user object
134
+ finalPayload = _.merge({}, existingUser, user.properties)
135
+
136
+ self.libraries.admin.firestore().doc(`users/${payload.auth.uid}`)
137
+ .set(finalPayload, { merge: true })
138
+ .then(function(data) {
139
+ result.signedUp = true;
140
+ return resolve(result);
141
+ })
142
+ .catch(function(e) {
143
+ return reject(e);
144
+ })
145
+
146
+ });
147
+ },
148
+
149
+ Module.prototype.updateReferral = function (payload) {
150
+ const self = this;
151
+ const result = {
152
+ count: 0,
153
+ updatedReferral: false,
154
+ referrerUid: undefined,
155
+ }
156
+ payload = payload || {};
157
+
158
+ return new Promise(function(resolve, reject) {
159
+ self.libraries.admin.firestore().collection('users')
160
+ .where('affiliate.code', '==', payload.affiliateCode)
161
+ .get()
162
+ .then(async (snapshot) => {
163
+ if (snapshot.empty) {
164
+ return resolve(result)
165
+ }
166
+ let count = 0;
167
+ let found = false;
168
+ let error = null;
169
+
170
+ for (var i = 0; i < snapshot.size; i++) {
171
+ const doc = snapshot.docs[i];
172
+ if (!found) {
173
+ let data = doc.data() || {};
174
+
175
+ let referrals = data.affiliate && data.affiliate.referrals ? data.affiliate.referrals : [];
176
+ referrals = Array.isArray(referrals) ? referrals : [];
177
+ count = referrals.length;
178
+ referrals = referrals.concat({
179
+ uid: payload.uid,
180
+ timestamp: self.assistant.meta.startTime.timestamp,
181
+ })
182
+
183
+ await self.libraries.admin.firestore().doc(`users/${doc.id}`)
184
+ .set({
185
+ affiliate: {
186
+ referrals: referrals
187
+ }
188
+ }, {merge: true})
189
+ .catch(e => {
190
+ console.error('Error updating referral', e);
191
+ error = e;
192
+ })
193
+
194
+ result.count = count;
195
+ result.updatedReferral = true;
196
+ result.referrerUid = doc.id
197
+ found = true
198
+ }
199
+ }
200
+ if (error) {
201
+ return reject(error);
202
+ }
203
+ return resolve(result)
204
+ })
205
+ .catch(e => {
206
+ return reject(e);
207
+ });
208
+ });
209
+ }
210
+
211
+ function addToMCList(key, listId, email) {
212
+ return new Promise((resolve, reject) => {
213
+ let datacenter = key.split('-')[1];
214
+ fetch(`https://${datacenter}.api.mailchimp.com/3.0/lists/${listId}/members`, {
215
+ method: 'post',
216
+ body: JSON.stringify({
217
+ email_address: email,
218
+ status: 'subscribed',
219
+ }),
220
+ timeout: 10000,
221
+ headers: {
222
+ 'Content-Type': 'application/json',
223
+ 'Authorization': `Basic ${key}`,
224
+ },
225
+ })
226
+ .then(res => res.json())
227
+ .then(json => {
228
+ if (json.status !== 'subscribed') {
229
+ return reject(new Error(json.status));
230
+ }
231
+ return resolve(json);
232
+ })
233
+ .catch(e => {
234
+ return reject(e);
235
+ })
236
+
237
+ });
238
+ }
239
+
240
+
241
+ module.exports = Module;