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
@@ -1,337 +1,131 @@
1
- let fetch;
2
- const _ = require('lodash');
1
+ const path = require('path');
3
2
 
4
- let Module = {
5
- init: async function (Manager, data) {
6
- this.Manager = Manager;
7
- this.libraries = Manager.libraries;
8
- this.assistant = Manager.Assistant({req: data.req, res: data.res})
9
- this.req = data.req;
10
- this.res = data.res;
3
+ function Module() {
11
4
 
12
- return this;
13
- },
14
- main: async function() {
15
- let self = this;
16
- let libraries = self.libraries;
17
- let assistant = self.assistant;
18
- let req = self.req;
19
- let res = self.res;
20
-
21
- let response = {
22
- status: 200,
23
- data: {},
24
- };
25
-
26
- return libraries.cors(req, res, async () => {
27
- let user = await assistant.authenticate();
28
-
29
- const command = assistant.request.data.command;
30
- const payload = {
31
- response: response,
32
- data: assistant.request.data,
33
- user: user,
34
- }
35
-
36
- self.assistant.log('Executing', command, {environment: 'production'})
37
-
38
- if (command === 'create-custom-token') {
39
- await self.createCustomToken(payload).catch(e => {self.assistant.error(e, {environment: 'production'})});
40
- } else if (command === 'delete-user') {
41
- await self.deleteUser(payload).catch(e => {self.assistant.error(e, {environment: 'production'})});
42
- } else if (command === 'firestore-write') {
43
- await self.firestoreWrite(payload).catch(e => {self.assistant.error(e, {environment: 'production'})});
44
- } else if (command === 'firestore-read') {
45
- await self.firestoreRead(payload).catch(e => {self.assistant.error(e, {environment: 'production'})});
46
- } else if (command === 'payment-processor') {
47
- await self.paymentProcessor(payload).catch(e => {self.assistant.error(e, {environment: 'production'})});
48
- } else if (command === 'sign-out-all-sessions') {
49
- await self.signOutAllSessions(payload).catch(e => {self.assistant.error(e, {environment: 'production'})});
50
- } else if (command === 'get-user-subscription-info') {
51
- await self.getUserSubscriptionInfo(payload).catch(e => {self.assistant.error(e, {environment: 'production'})});
52
- } else {
53
- response.status = 401;
54
- response.error = new Error(`Improper command supplied: ${command}`);
55
- }
56
-
57
- self.assistant.log('Api payload', {object: payload, string: JSON.stringify(payload)}, {environment: 'production'})
58
-
59
- if (response.status === 200) {
60
- return res.status(response.status).json(response.data);
61
- } else {
62
- return res.status(response.status).send(response.error.message);
63
- }
64
- });
65
- },
66
- createCustomToken: async function (payload) {
67
- const self = this;
68
-
69
- return new Promise(async function(resolve, reject) {
70
- if (payload.user.authenticated || payload.user.roles.admin) {
71
- await self.libraries.admin.auth().createCustomToken(payload.user.auth.uid)
72
- .then(token => {
73
- payload.response.data.token = token;
74
- return resolve(payload);
75
- })
76
- .catch(e => {
77
- payload.response.status = 401;
78
- payload.response.error = new Error(`Failed to create custom token: ${e}`);
79
- return reject(payload.response.error);
80
- })
81
- } else {
82
- payload.response.status = 401;
83
- payload.response.error = new Error('User not authenticated.');
84
- return reject(payload.response.error);
85
- }
86
- });
87
- },
88
- firestoreRead: async function (payload) {
89
- const self = this;
90
-
91
- return new Promise(async function(resolve, reject) {
92
- if (payload.user.authenticated || payload.user.roles.admin) {
93
-
94
- // console.log('---payload.data.payload', payload.data.payload);
95
-
96
- payload.data.payload.path = `${payload.data.payload.path || ''}`;
97
- payload.data.payload.document = payload.data.payload.document || {};
98
- payload.data.payload.options = payload.data.payload.options || { merge: true };
99
-
100
-
101
- if (!payload.data.payload.path) {
102
- payload.response.status = 401;
103
- payload.response.error = new Error('Path parameter required');
104
- return reject(payload);
105
- } else {
106
- await self.libraries.admin.firestore().doc(payload.data.payload.path)
107
- .get()
108
- .then(doc => {
109
- payload.response.data = doc.data();
110
- return resolve(payload.response.data);
111
- })
112
- .catch(e => {
113
- payload.response.status = 500;
114
- payload.response.error = e;
115
- return reject(payload);
116
- })
117
- }
118
-
119
- } else {
120
- payload.response.status = 401;
121
- payload.response.error = new Error('User not authenticated.');
122
- return reject(payload.response.error);
123
- }
124
- });
125
- },
126
- firestoreWrite: async function (payload) {
127
- const self = this;
128
-
129
- return new Promise(async function(resolve, reject) {
130
- if (payload.user.authenticated || payload.user.roles.admin) {
131
-
132
- payload.data.payload.path = `${payload.data.payload.path || ''}`;
133
- payload.data.payload.document = payload.data.payload.document || {};
134
- payload.data.payload.options = payload.data.payload.options || { merge: true };
5
+ }
135
6
 
136
- if (!payload.data.payload.path) {
137
- payload.response.status = 401;
138
- payload.response.error = new Error('Path parameter required');
139
- return reject(payload);
140
- } else {
141
- await self.libraries.admin.firestore().doc(payload.data.payload.path)
142
- .set(payload.data.payload.document, payload.data.payload.options)
143
- .then(r => {
144
- return resolve(payload);
145
- })
146
- .catch(e => {
147
- payload.response.status = 500;
148
- payload.response.error = e;
149
- return reject(payload);
150
- })
151
- }
7
+ Module.prototype.init = function (Manager, data) {
8
+ const self = this;
9
+ self.Manager = Manager;
10
+ self.libraries = Manager.libraries;
11
+ self.assistant = Manager.Assistant({req: data.req, res: data.res})
12
+ self.req = data.req;
13
+ self.res = data.res;
152
14
 
153
- } else {
154
- payload.response.status = 401;
155
- payload.response.error = new Error('User not authenticated.');
156
- return reject(payload.response.error);
157
- }
158
- });
159
- },
160
- deleteUser: async function (payload) {
161
- const self = this;
15
+ return self;
16
+ }
162
17
 
163
- return new Promise(async function(resolve, reject) {
164
- if (payload.user.authenticated || payload.user.roles.admin) {
165
- // const planExpireDate = new Date(_.get(payload.user, 'plan.expires.timestamp', 0));
166
- // if (planExpireDate >= new Date()) {
167
- // payload.response.status = 401;
168
- // 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.`);
169
- // return reject(payload.response.error);
170
- // }
171
- const isPlanActive = _.get(payload.user, 'plan.payment.active', null);
172
- if (isPlanActive === true) {
173
- payload.response.status = 401;
174
- 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.`);
175
- return reject(payload.response.error);
176
- }
18
+ Module.prototype.main = function() {
19
+ const self = this;
20
+ const libraries = self.libraries;
21
+ const assistant = self.assistant;
22
+ const req = self.req;
23
+ const res = self.res;
177
24
 
178
- await self.libraries.admin.auth().deleteUser(payload.user.auth.uid)
179
- .then(() => {
180
- return resolve(payload);
181
- })
182
- .catch(e => {
183
- payload.response.status = 401;
184
- payload.response.error = new Error(`Failed to delete user: ${e}`);
185
- return reject(payload.response.error);
186
- })
187
- } else {
188
- payload.response.status = 401;
189
- payload.response.error = new Error('User not authenticated.');
190
- return reject(payload.response.error);
191
- }
192
- });
193
- },
194
- paymentProcessor: async function (payload) {
195
- const self = this;
196
-
197
- return new Promise(async function(resolve, reject) {
198
- const productId = _.get(payload, 'data.payload.payload.details.productIdGlobal');
199
- if (!productId) {
200
- return reject(new Error('No productId'))
201
- }
202
- const processorPath = `${process.cwd()}/payment-processors/${productId}.js`
203
- let processor;
204
- // console.log('---processorPath', processorPath);
25
+ return libraries.cors(req, res, async () => {
26
+ const response = {
27
+ status: 200,
28
+ data: {},
29
+ error: null,
30
+ };
31
+ const user = await assistant.authenticate();
32
+ const command = resolveCommand(assistant.request.data.command);
33
+ const commandPath = './' + path.join('./api/', `${command.replace(/\.\.\//g, '').replace(/\:/, '/')}.js`);
34
+ const payload = {
35
+ response: response,
36
+ data: assistant.request.data,
37
+ user: user,
38
+ }
39
+
40
+ self.assistant.log(`Executing: ${command}`, payload, JSON.stringify(payload), {environment: 'production'})
41
+
42
+ try {
43
+ const lib = new (require(commandPath))();
205
44
  try {
206
- processor = new (require(processorPath));
207
- processor.Manager = self.Manager;
208
- } catch (e) {
209
- self.assistant.error('Error loading processor', processorPath, e, {environment: 'production'})
210
- return resolve()
211
- }
212
-
213
- await processor.process(payload.data.payload)
214
- .then(result => {
215
- payload.response.data = result;
216
- return resolve(result);
217
- })
218
- .catch(e => {
219
- self.Manager.libraries.sentry.captureException(e);
220
- console.error(`Payment processor @ "${processorPath}" failed`, e);
221
- return reject(e);
222
- })
223
- });
224
- },
225
- signOutAllSessions: async function (payload) {
226
- const self = this;
227
- const powertools = self.Manager.require('node-powertools')
228
- return new Promise(async function(resolve, reject) {
229
- const uid = _.get(payload.user, 'auth.uid', null);
230
-
231
- if (payload.user.authenticated || payload.user.roles.admin && uid) {
232
- await self.libraries.admin.database().ref(`gatherings/online`)
233
- .orderByChild('uid')
234
- .equalTo(uid)
235
- .once('value')
236
- .then(async snap => {
237
- const data = snap.val();
238
- const keys = Object.keys(data || {});
239
- for (var i = 0; i < keys.length; i++) {
240
- const key = keys[i];
241
- self.assistant.log(`Signing out: ${key}`, {environment: 'production'});
242
- await self.libraries.admin.database().ref(`gatherings/online/${key}/command`).set('signout').catch(e => self.assistant.error(`Failed to signout ${key}`, e))
243
- await powertools.wait(3000);
244
- await self.libraries.admin.database().ref(`gatherings/online/${key}`).remove().catch(e => self.assistant.error(`Failed to delete ${key}`, e))
245
- }
45
+ await lib.init(self, payload);
46
+ await lib.main()
47
+ .then(r => {
48
+ response.status = r.status || 200;
49
+ response.data = r.data || {};
246
50
  })
247
51
  .catch(e => {
248
- console.error('Gathering query error', e);
52
+ response.status = e.code || 500;
53
+ response.error = e || new Error('Unknown error occured');
249
54
  })
250
-
251
- await self.libraries.admin
252
- .auth()
253
- .revokeRefreshTokens(uid)
254
- .then(() => {
255
- self.assistant.log('Signed user out of all sessions', payload.user.auth.uid, {environment: 'production'})
256
- payload.data = {message: `Successfully signed ${payload.user.auth.uid} out of all sessions`}
257
- return resolve(payload.data);
258
- })
259
- .catch(e => {
260
- payload.response.status = 500;
261
- payload.response.error = e;
262
- })
263
-
264
- if (payload.response.status >= 200 && payload.response.status < 300) {
265
- return resolve(payload.response.data);
266
- } else {
267
- return reject(payload.response.error);
268
- }
269
- } else {
270
- payload.response.status = 401;
271
- payload.response.error = new Error('User not authenticated.');
272
- return reject(payload.response.error);
273
- }
274
-
275
- });
276
- },
277
- getUserSubscriptionInfo: async function (payload) {
278
- const self = this;
279
- const uid = _.get(payload, 'data.payload.uid', null)
280
-
281
- return new Promise(async function(resolve, reject) {
282
- // console.log('----payload.data', payload.data);
283
-
284
- if (!uid) {
285
- payload.response.status = 401;
286
- payload.response.error = new Error(`Improper uid supplied: ${uid}`);
287
- return reject(payload);
55
+ } catch (e) {
56
+ response.status = 500;
57
+ response.error = e || new Error('Unknown error occured');
288
58
  }
59
+ } catch (e) {
60
+ response.status = 400;
61
+ response.error = new Error(`Improper command supplied: ${command}`);
62
+ assistant.log('Dev error log', e)
63
+ }
64
+
65
+ if (response.status === 200) {
66
+ return res.status(response.status).json(response.data);
67
+ } else {
68
+ console.error(`Error executing ${command} @ ${commandPath}`, response.error)
69
+ // return res.status(response.status).send(response.error.message);
70
+ return res.status(response.status).send(`${response.error}`);
71
+ }
72
+ });
73
+ }
289
74
 
290
- await self.libraries.admin.firestore().doc(`users/${uid}`)
291
- .get()
292
- .then(doc => {
293
- const data = doc.data();
294
- if (!data) {
295
- payload.response.status = 401;
296
- payload.response.error = new Error(`Cannot find user with uid: ${uid}`);
297
- return reject(payload.response.data);
298
- } else {
299
- payload.response.data = {
300
- plan: {
301
- id: data.plan.id,
302
- payment: {
303
- active: data.plan.payment.active,
304
- },
305
- }
306
- }
307
- return resolve(payload.response.data);
308
- }
309
-
310
- })
311
- .catch(e => {
312
- payload.response.status = 500;
313
- payload.response.error = e;
314
- return reject(payload);
315
- })
316
- //
317
- //
318
- // const isPlanActive = _.get(payload.user, 'plan.payment.active', null);
319
- // if (isPlanActive === true) {
320
- // payload.response.status = 401;
321
- // 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.`);
322
- // return reject(payload.response.error);
323
- // }
324
- //
325
- // await self.libraries.admin.auth().deleteUser(payload.user.auth.uid)
326
- // .then(() => {
327
- // return resolve(payload);
328
- // })
329
- // .catch(e => {
330
- // payload.response.status = 401;
331
- // payload.response.error = new Error(`Failed to delete user: ${e}`);
332
- // return reject(payload.response.error);
333
- // })
334
- });
335
- },
75
+ function resolveCommand(command) {
76
+ // Start
77
+ if (false) {
78
+
79
+ // General
80
+ } else if (command === 'general:generate-uuid' || command === 'generate-uuid') {
81
+ command = 'general:generate-uuid';
82
+
83
+ // User
84
+ } else if (command === 'user:create-custom-token' || command === 'create-custom-token') { // rename: user:create-custom-token
85
+ command = 'user:create-custom-token';
86
+ } else if (command === 'user:delete' || command === 'delete-user') { // rename: user:delete
87
+ command = 'user:delete';
88
+ } else if (command === 'user:sign-out-all-sessions' || command === 'sign-out-all-sessions') { // rename: user:sign-out-all-sessions
89
+ command = 'user:sign-out-all-sessions';
90
+ } else if (command === 'user:get-subscription-info' || command === 'get-user-subscription-info') { // rename: user:get-subscription-info
91
+ command = 'user:get-subscription-info';
92
+ } else if (command === 'user:sign-up' || command === 'sign-up') {
93
+ command = 'user:sign-up';
94
+
95
+ // Handler
96
+ } else if (command === 'handler:create-post') {
97
+ command = 'handler:create-post';
98
+
99
+ // Admin
100
+ } else if (command === 'admin:create-post') {
101
+ command = 'admin:create-post';
102
+ } else if (command === 'admin:get-stats') {
103
+ command = 'admin:get-stats';
104
+ } else if (command === 'admin:send-notification') {
105
+ command = 'admin:send-notification';
106
+ } else if (command === 'admin:firestore-read' || command === 'firestore-read') {
107
+ command = 'admin:firestore-read';
108
+ } else if (command === 'admin:firestore-write' || command === 'firestore-write') {
109
+ command = 'admin:firestore-write';
110
+ } else if (command === 'admin:firestore-query' || command === 'firestore-query') {
111
+ command = 'admin:firestore-query';
112
+ } else if (command === 'admin:payment-processor' || command === 'payment-processor') { // rename: admin:payment-processor
113
+ command = 'admin:payment-processor';
114
+
115
+ // Test
116
+ } else if (command === 'test:authenticate' || command === 'authenticate') {
117
+ command = 'test:authenticate';
118
+ } else if (command === 'test:create-test-accounts' || command === 'create-test-accounts') {
119
+ command = 'test:create-test-accounts';
120
+ } else if (command === 'test:webhook' || command === 'webhook') {
121
+ command = 'test:webhook';
122
+
123
+ // End
124
+ } else {
125
+ command = '';
126
+ }
127
+
128
+ return command;
336
129
  }
130
+
337
131
  module.exports = Module;