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.
- package/package.json +19 -20
- package/src/cli/cli.js +22 -7
- package/src/manager/functions/core/actions/api/admin/create-post.js +160 -0
- package/src/manager/functions/core/actions/api/admin/firestore-query.js +172 -0
- package/src/manager/functions/core/actions/api/admin/firestore-read.js +54 -0
- package/src/manager/functions/core/actions/api/admin/firestore-write.js +47 -0
- package/src/manager/functions/core/actions/api/admin/get-stats.js +206 -0
- package/src/manager/functions/core/actions/api/admin/payment-processor.js +56 -0
- package/src/manager/functions/core/actions/api/admin/send-notification.js +189 -0
- package/src/manager/functions/core/actions/api/general/generate-uuid.js +49 -0
- package/src/manager/functions/core/actions/api/handler/create-post.js +118 -0
- package/src/manager/functions/core/actions/api/template.js +34 -0
- package/src/manager/functions/core/actions/api/test/authenticate.js +31 -0
- package/src/manager/functions/core/actions/api/test/create-test-accounts.js +36 -0
- package/src/manager/functions/core/actions/api/test/webhook.js +35 -0
- package/src/manager/functions/core/actions/api/user/create-custom-token.js +63 -0
- package/src/manager/functions/core/actions/api/user/delete.js +73 -0
- package/src/manager/functions/core/actions/api/user/get-subscription-info.js +63 -0
- package/src/manager/functions/core/actions/api/user/sign-out-all-sessions.js +86 -0
- package/src/manager/functions/core/actions/api/user/sign-up.js +241 -0
- package/src/manager/functions/core/actions/api.js +117 -323
- package/src/manager/functions/core/actions/old/api-2.js +414 -0
- package/src/manager/functions/core/admin/create-post.js +1 -1
- package/src/manager/index.js +1 -18
|
@@ -1,337 +1,131 @@
|
|
|
1
|
-
|
|
2
|
-
const _ = require('lodash');
|
|
1
|
+
const path = require('path');
|
|
3
2
|
|
|
4
|
-
|
|
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
|
-
|
|
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
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
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
|
-
|
|
154
|
-
|
|
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
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
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
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
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
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
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
|
-
|
|
52
|
+
response.status = e.code || 500;
|
|
53
|
+
response.error = e || new Error('Unknown error occured');
|
|
249
54
|
})
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
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
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
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;
|