backend-manager 2.0.3 → 2.0.6
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 +2 -2
- package/src/cli/cli.js +13 -8
- package/src/manager/functions/core/actions/api/admin/create-post.js +1 -0
- package/src/manager/functions/core/actions/api/admin/firestore-query.js +1 -0
- package/src/manager/functions/core/actions/api/admin/firestore-read.js +1 -0
- package/src/manager/functions/core/actions/api/admin/firestore-write.js +1 -0
- package/src/manager/functions/core/actions/api/admin/get-stats.js +1 -0
- package/src/manager/functions/core/actions/api/admin/payment-processor.js +1 -0
- package/src/manager/functions/core/actions/api/admin/send-notification.js +1 -0
- package/src/manager/functions/core/actions/api/general/generate-uuid.js +2 -1
- package/src/manager/functions/core/actions/api/handler/create-post.js +1 -0
- package/src/manager/functions/core/actions/api/template.js +9 -0
- package/src/manager/functions/core/actions/api/test/authenticate.js +1 -0
- package/src/manager/functions/core/actions/api/test/create-test-accounts.js +1 -0
- package/src/manager/functions/core/actions/api/test/webhook.js +1 -0
- package/src/manager/functions/core/actions/api/user/create-custom-token.js +7 -28
- package/src/manager/functions/core/actions/api/user/delete.js +7 -26
- package/src/manager/functions/core/actions/api/user/get-subscription-info.js +7 -28
- package/src/manager/functions/core/actions/api/user/sign-out-all-sessions.js +7 -26
- package/src/manager/functions/core/actions/api/user/sign-up.js +35 -55
- package/src/manager/functions/core/actions/api.js +75 -28
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "backend-manager",
|
|
3
|
-
"version": "2.0.
|
|
3
|
+
"version": "2.0.6",
|
|
4
4
|
"description": "Quick tools for developing Firebase functions",
|
|
5
5
|
"main": "src/manager/index.js",
|
|
6
6
|
"bin": {
|
|
@@ -31,7 +31,7 @@
|
|
|
31
31
|
"@google-cloud/storage": "^5.19.2",
|
|
32
32
|
"@sendgrid/mail": "^7.6.2",
|
|
33
33
|
"@sentry/node": "^6.19.6",
|
|
34
|
-
"backend-assistant": "^0.0.
|
|
34
|
+
"backend-assistant": "^0.0.59",
|
|
35
35
|
"busboy": "^0.3.1",
|
|
36
36
|
"chalk": "^4.1.2",
|
|
37
37
|
"cors": "^2.8.5",
|
package/src/cli/cli.js
CHANGED
|
@@ -241,6 +241,11 @@ Main.prototype.setup = async function () {
|
|
|
241
241
|
this.projectUrl = `https://console.firebase.google.com/project/${this.projectName}`;
|
|
242
242
|
log(chalk.black(`Id: `, chalk.bold(`${this.projectName}`)));
|
|
243
243
|
log(chalk.black(`Url:`, chalk.bold(`${this.projectUrl}`)));
|
|
244
|
+
|
|
245
|
+
if (!self.package || !self.package.engines || !self.package.engines.node) {
|
|
246
|
+
throw new Error('Missing <engines.node> in package.json')
|
|
247
|
+
}
|
|
248
|
+
|
|
244
249
|
await this.test('is a firebase project', async function () {
|
|
245
250
|
let exists = fs.exists(`${self.firebaseProjectPath}/firebase.json`);
|
|
246
251
|
return exists;
|
|
@@ -263,17 +268,17 @@ Main.prototype.setup = async function () {
|
|
|
263
268
|
return self.package.engines.node.toString() === CLI_CONFIG.node && processMajor >= engineMajor;
|
|
264
269
|
}, fix_nodeVersion);
|
|
265
270
|
|
|
266
|
-
await this.test('project level package.json exists', async function () {
|
|
267
|
-
|
|
268
|
-
}, fix_projpackage);
|
|
271
|
+
// await this.test('project level package.json exists', async function () {
|
|
272
|
+
// return !!(self.projectPackage && self.projectPackage.version && self.projectPackage.name);
|
|
273
|
+
// }, fix_projpackage);
|
|
269
274
|
|
|
270
275
|
await this.test('functions level package.json exists', async function () {
|
|
271
|
-
return !!self.package.dependencies && !!self.package.devDependencies;
|
|
276
|
+
return !!self.package && !!self.package.dependencies && !!self.package.devDependencies;
|
|
272
277
|
}, fix_deps);
|
|
273
278
|
|
|
274
|
-
await this.test('functions level package.json has updated version', async function () {
|
|
275
|
-
|
|
276
|
-
}, fix_packageversion);
|
|
279
|
+
// await this.test('functions level package.json has updated version', async function () {
|
|
280
|
+
// return self.package.version === self.projectPackage.version;
|
|
281
|
+
// }, fix_packageversion);
|
|
277
282
|
|
|
278
283
|
await this.test('using updated firebase-admin', async function () {
|
|
279
284
|
let pkg = 'firebase-admin';
|
|
@@ -656,7 +661,7 @@ async function fix_serviceAccount(self) {
|
|
|
656
661
|
|
|
657
662
|
function fix_nodeVersion(self) {
|
|
658
663
|
return new Promise(function(resolve, reject) {
|
|
659
|
-
_.set(self.package, 'engines.node',
|
|
664
|
+
_.set(self.package, 'engines.node', CLI_CONFIG.node)
|
|
660
665
|
|
|
661
666
|
fs.write(`${self.firebaseProjectPath}/functions/package.json`, JSON.stringify(self.package, null, 2) );
|
|
662
667
|
resolve();
|
|
@@ -6,6 +6,7 @@ function Module() {
|
|
|
6
6
|
|
|
7
7
|
Module.prototype.init = async function (s, payload) {
|
|
8
8
|
const self = this;
|
|
9
|
+
self.Api = s;
|
|
9
10
|
self.Manager = s.Manager;
|
|
10
11
|
self.libraries = s.Manager.libraries;
|
|
11
12
|
self.assistant = s.Manager.assistant;
|
|
@@ -39,7 +40,7 @@ Module.prototype.main = function () {
|
|
|
39
40
|
|
|
40
41
|
assistant.log('UUID Generated', payload.data.payload, result, {environment: 'production'});
|
|
41
42
|
|
|
42
|
-
return resolve({data: {uuid: result}});
|
|
43
|
+
return resolve({data: {uuid: result, timestamp: new Date().toISOString()}});
|
|
43
44
|
|
|
44
45
|
});
|
|
45
46
|
|
|
@@ -4,6 +4,7 @@ function Module() {
|
|
|
4
4
|
|
|
5
5
|
Module.prototype.init = async function (s, payload) {
|
|
6
6
|
const self = this;
|
|
7
|
+
self.Api = s;
|
|
7
8
|
self.Manager = s.Manager;
|
|
8
9
|
self.libraries = s.Manager.libraries;
|
|
9
10
|
self.assistant = s.Manager.assistant;
|
|
@@ -24,6 +25,14 @@ Module.prototype.main = function () {
|
|
|
24
25
|
return reject(assistant.errorManager(`Admin required.`, {code: 401, sentry: false, send: false, log: false}).error)
|
|
25
26
|
}
|
|
26
27
|
|
|
28
|
+
// self.Api.resolveUser({adminRequired: false})
|
|
29
|
+
// .then(async (user) => {
|
|
30
|
+
//
|
|
31
|
+
// })
|
|
32
|
+
// .catch(e => {
|
|
33
|
+
// return reject(e);
|
|
34
|
+
// })
|
|
35
|
+
|
|
27
36
|
return resolve({data: {success: true}});
|
|
28
37
|
|
|
29
38
|
});
|
|
@@ -6,6 +6,7 @@ function Module() {
|
|
|
6
6
|
|
|
7
7
|
Module.prototype.init = async function (s, payload) {
|
|
8
8
|
const self = this;
|
|
9
|
+
self.Api = s;
|
|
9
10
|
self.Manager = s.Manager;
|
|
10
11
|
self.libraries = s.Manager.libraries;
|
|
11
12
|
self.assistant = s.Manager.assistant;
|
|
@@ -21,31 +22,8 @@ Module.prototype.main = function () {
|
|
|
21
22
|
const payload = self.payload;
|
|
22
23
|
|
|
23
24
|
return new Promise(async function(resolve, reject) {
|
|
24
|
-
|
|
25
|
-
|
|
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
|
-
|
|
25
|
+
self.Api.resolveUser({adminRequired: true})
|
|
26
|
+
.then(async (user) => {
|
|
49
27
|
await self.libraries.admin.auth().createCustomToken(_.get(user, 'auth.uid', null))
|
|
50
28
|
.then(token => {
|
|
51
29
|
return resolve({data: {token: token}});
|
|
@@ -53,10 +31,11 @@ Module.prototype.main = function () {
|
|
|
53
31
|
.catch(e => {
|
|
54
32
|
return reject(assistant.errorManager(`Failed to create custom token: ${e}`, {code: 400, sentry: false, send: false, log: false}).error)
|
|
55
33
|
})
|
|
56
|
-
}
|
|
57
|
-
|
|
34
|
+
})
|
|
35
|
+
.catch(e => {
|
|
36
|
+
return reject(e);
|
|
37
|
+
})
|
|
58
38
|
});
|
|
59
|
-
|
|
60
39
|
};
|
|
61
40
|
|
|
62
41
|
|
|
@@ -6,6 +6,7 @@ function Module() {
|
|
|
6
6
|
|
|
7
7
|
Module.prototype.init = async function (s, payload) {
|
|
8
8
|
const self = this;
|
|
9
|
+
self.Api = s;
|
|
9
10
|
self.Manager = s.Manager;
|
|
10
11
|
self.libraries = s.Manager.libraries;
|
|
11
12
|
self.assistant = s.Manager.assistant;
|
|
@@ -21,30 +22,8 @@ Module.prototype.main = function () {
|
|
|
21
22
|
const payload = self.payload;
|
|
22
23
|
|
|
23
24
|
return new Promise(async function(resolve, reject) {
|
|
24
|
-
|
|
25
|
-
|
|
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 {
|
|
25
|
+
self.Api.resolveUser({adminRequired: true})
|
|
26
|
+
.then(async (user) => {
|
|
48
27
|
// const planExpireDate = new Date(_.get(payload.user, 'plan.expires.timestamp', 0));
|
|
49
28
|
// if (planExpireDate >= new Date()) {
|
|
50
29
|
// payload.response.status = 401;
|
|
@@ -63,8 +42,10 @@ Module.prototype.main = function () {
|
|
|
63
42
|
.catch(e => {
|
|
64
43
|
return reject(assistant.errorManager(`Failed to delete user: ${e}`, {code: 400, sentry: false, send: false, log: false}).error)
|
|
65
44
|
})
|
|
66
|
-
}
|
|
67
|
-
|
|
45
|
+
})
|
|
46
|
+
.catch(e => {
|
|
47
|
+
return reject(e);
|
|
48
|
+
})
|
|
68
49
|
});
|
|
69
50
|
|
|
70
51
|
};
|
|
@@ -6,6 +6,7 @@ function Module() {
|
|
|
6
6
|
|
|
7
7
|
Module.prototype.init = async function (s, payload) {
|
|
8
8
|
const self = this;
|
|
9
|
+
self.Api = s;
|
|
9
10
|
self.Manager = s.Manager;
|
|
10
11
|
self.libraries = s.Manager.libraries;
|
|
11
12
|
self.assistant = s.Manager.assistant;
|
|
@@ -21,32 +22,8 @@ Module.prototype.main = function () {
|
|
|
21
22
|
const payload = self.payload;
|
|
22
23
|
|
|
23
24
|
return new Promise(async function(resolve, reject) {
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
// if (payload.user.roles.admin && payload.data.payload.uid) {
|
|
27
|
-
if (payload.data.payload.uid) {
|
|
28
|
-
await self.libraries.admin.firestore().doc(`users/${payload.data.payload.uid}`)
|
|
29
|
-
.get()
|
|
30
|
-
.then(async function (doc) {
|
|
31
|
-
const data = doc.data();
|
|
32
|
-
if (data) {
|
|
33
|
-
user = data;
|
|
34
|
-
} else {
|
|
35
|
-
throw new Error('User does not exist')
|
|
36
|
-
}
|
|
37
|
-
})
|
|
38
|
-
.catch(function (e) {
|
|
39
|
-
user = e;
|
|
40
|
-
})
|
|
41
|
-
} else if (payload.user.authenticated) {
|
|
42
|
-
user = payload.user;
|
|
43
|
-
}
|
|
44
|
-
|
|
45
|
-
if (user instanceof Error) {
|
|
46
|
-
return reject(assistant.errorManager(user, {code: 400, sentry: false, send: false, log: false}).error)
|
|
47
|
-
} else if (!user) {
|
|
48
|
-
return reject(assistant.errorManager(`Admin or authenticated user required.`, {code: 401, sentry: false, send: false, log: false}).error)
|
|
49
|
-
} else {
|
|
25
|
+
self.Api.resolveUser({adminRequired: false})
|
|
26
|
+
.then(async (user) => {
|
|
50
27
|
const result = {
|
|
51
28
|
plan: {
|
|
52
29
|
id: _.get(user, 'plan.id', 'unknown'),
|
|
@@ -56,9 +33,11 @@ Module.prototype.main = function () {
|
|
|
56
33
|
}
|
|
57
34
|
}
|
|
58
35
|
return resolve({data: result});
|
|
59
|
-
}
|
|
36
|
+
})
|
|
37
|
+
.catch(e => {
|
|
38
|
+
return reject(e);
|
|
39
|
+
})
|
|
60
40
|
});
|
|
61
|
-
|
|
62
41
|
};
|
|
63
42
|
|
|
64
43
|
|
|
@@ -6,6 +6,7 @@ function Module() {
|
|
|
6
6
|
|
|
7
7
|
Module.prototype.init = async function (s, payload) {
|
|
8
8
|
const self = this;
|
|
9
|
+
self.Api = s;
|
|
9
10
|
self.Manager = s.Manager;
|
|
10
11
|
self.libraries = s.Manager.libraries;
|
|
11
12
|
self.assistant = s.Manager.assistant;
|
|
@@ -21,30 +22,9 @@ Module.prototype.main = function () {
|
|
|
21
22
|
const payload = self.payload;
|
|
22
23
|
|
|
23
24
|
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
25
|
|
|
43
|
-
|
|
44
|
-
|
|
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 {
|
|
26
|
+
self.Api.resolveUser({adminRequired: true})
|
|
27
|
+
.then(async (user) => {
|
|
48
28
|
const uid = _.get(user, 'auth.uid', null);
|
|
49
29
|
|
|
50
30
|
await self.libraries.admin.database().ref(`gatherings/online`)
|
|
@@ -75,9 +55,10 @@ Module.prototype.main = function () {
|
|
|
75
55
|
.catch(e => {
|
|
76
56
|
return reject(assistant.errorManager(`Failed to sign out of all sessions: ${e}`, {code: 500, sentry: false, send: false, log: false}).error)
|
|
77
57
|
})
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
58
|
+
})
|
|
59
|
+
.catch(e => {
|
|
60
|
+
return reject(e);
|
|
61
|
+
})
|
|
81
62
|
});
|
|
82
63
|
|
|
83
64
|
};
|
|
@@ -7,6 +7,7 @@ function Module() {
|
|
|
7
7
|
|
|
8
8
|
Module.prototype.init = async function (s, payload) {
|
|
9
9
|
const self = this;
|
|
10
|
+
self.Api = s;
|
|
10
11
|
self.Manager = s.Manager;
|
|
11
12
|
self.libraries = s.Manager.libraries;
|
|
12
13
|
self.assistant = s.Manager.assistant;
|
|
@@ -22,61 +23,40 @@ Module.prototype.main = function () {
|
|
|
22
23
|
const payload = self.payload;
|
|
23
24
|
|
|
24
25
|
return new Promise(async function(resolve, reject) {
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
}
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
}
|
|
55
|
-
|
|
56
|
-
|
|
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
|
-
}
|
|
26
|
+
self.Api.resolveUser({adminRequired: true})
|
|
27
|
+
.then(async (user) => {
|
|
28
|
+
await self.signUp({
|
|
29
|
+
auth: {
|
|
30
|
+
uid: _.get(user, 'auth.uid', null),
|
|
31
|
+
email: _.get(user, 'auth.email', null),
|
|
32
|
+
},
|
|
33
|
+
affiliate: {
|
|
34
|
+
referrer: _.get(payload.data.payload, 'affiliateCode', null),
|
|
35
|
+
},
|
|
36
|
+
})
|
|
37
|
+
.then(async function (result) {
|
|
38
|
+
if (_.get(payload.data.payload, 'newsletterSignUp', false)) {
|
|
39
|
+
await addToMCList(
|
|
40
|
+
_.get(Manager.config, 'mailchimp.key'),
|
|
41
|
+
_.get(Manager.config, 'mailchimp.list_id'),
|
|
42
|
+
_.get(user, 'auth.email', null),
|
|
43
|
+
)
|
|
44
|
+
.then(function (res) {
|
|
45
|
+
assistant.log('Sucessfully added user to MC list.')
|
|
46
|
+
})
|
|
47
|
+
.catch(function (e) {
|
|
48
|
+
assistant.log('Failed to add user to MC list.', e)
|
|
49
|
+
})
|
|
50
|
+
}
|
|
51
|
+
return resolve({data: result});
|
|
52
|
+
})
|
|
53
|
+
.catch(function (e) {
|
|
54
|
+
return reject(assistant.errorManager(`Failed to sign up: ${e}`, {code: 500, sentry: false, send: false, log: false}).error)
|
|
55
|
+
})
|
|
56
|
+
})
|
|
57
|
+
.catch(e => {
|
|
58
|
+
return reject(e);
|
|
59
|
+
})
|
|
80
60
|
|
|
81
61
|
});
|
|
82
62
|
|
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
const path = require('path');
|
|
2
|
+
const _ = require('lodash');
|
|
2
3
|
|
|
3
4
|
function Module() {
|
|
4
5
|
|
|
@@ -11,6 +12,15 @@ Module.prototype.init = function (Manager, data) {
|
|
|
11
12
|
self.assistant = Manager.Assistant({req: data.req, res: data.res})
|
|
12
13
|
self.req = data.req;
|
|
13
14
|
self.res = data.res;
|
|
15
|
+
self.payload = {
|
|
16
|
+
response: {
|
|
17
|
+
status: 200,
|
|
18
|
+
data: {},
|
|
19
|
+
error: null,
|
|
20
|
+
},
|
|
21
|
+
data: {},
|
|
22
|
+
user: {},
|
|
23
|
+
};
|
|
14
24
|
|
|
15
25
|
return self;
|
|
16
26
|
}
|
|
@@ -23,56 +33,50 @@ Module.prototype.main = function() {
|
|
|
23
33
|
const res = self.res;
|
|
24
34
|
|
|
25
35
|
return libraries.cors(req, res, async () => {
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
};
|
|
31
|
-
const user = await assistant.authenticate();
|
|
32
|
-
const command = resolveCommand(assistant.request.data.command);
|
|
36
|
+
self.payload.data = assistant.request.data;
|
|
37
|
+
self.payload.user = await assistant.authenticate();
|
|
38
|
+
|
|
39
|
+
const command = self.resolveCommand(self.payload.data.command);
|
|
33
40
|
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
41
|
|
|
40
|
-
self.assistant.log(`Executing: ${command}`, payload, JSON.stringify(payload), {environment: 'production'})
|
|
42
|
+
self.assistant.log(`Executing: ${command}`, self.payload, JSON.stringify(self.payload), {environment: 'production'})
|
|
41
43
|
|
|
42
44
|
try {
|
|
43
45
|
const lib = new (require(commandPath))();
|
|
44
46
|
try {
|
|
45
|
-
await lib.init(self, payload);
|
|
47
|
+
await lib.init(self, self.payload);
|
|
46
48
|
await lib.main()
|
|
47
49
|
.then(r => {
|
|
48
|
-
response.status = r.status || 200;
|
|
49
|
-
response.data = r.data || {};
|
|
50
|
+
self.payload.response.status = r.status || 200;
|
|
51
|
+
self.payload.response.data = r.data || {};
|
|
50
52
|
})
|
|
51
53
|
.catch(e => {
|
|
52
|
-
response.status = e.code || 500;
|
|
53
|
-
response.error = e || new Error('Unknown error occured');
|
|
54
|
+
self.payload.response.status = e.code || 500;
|
|
55
|
+
self.payload.response.error = e || new Error('Unknown error occured');
|
|
54
56
|
})
|
|
55
57
|
} catch (e) {
|
|
56
|
-
response.status = 500;
|
|
57
|
-
response.error = e || new Error('Unknown error occured');
|
|
58
|
+
self.payload.response.status = 500;
|
|
59
|
+
self.payload.response.error = e || new Error('Unknown error occured');
|
|
58
60
|
}
|
|
59
61
|
} catch (e) {
|
|
60
|
-
response.status = 400;
|
|
61
|
-
response.error = new Error(`Improper command supplied: ${command}`);
|
|
62
|
+
self.payload.response.status = 400;
|
|
63
|
+
self.payload.response.error = new Error(`Improper command supplied: ${command}`);
|
|
62
64
|
assistant.log('Dev error log', e)
|
|
63
65
|
}
|
|
64
66
|
|
|
65
|
-
if (response.status === 200) {
|
|
66
|
-
return res.status(response.status).json(response.data);
|
|
67
|
+
if (self.payload.response.status === 200) {
|
|
68
|
+
return res.status(self.payload.response.status).json(self.payload.response.data);
|
|
67
69
|
} 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}`);
|
|
70
|
+
console.error(`Error executing ${command} @ ${commandPath}`, self.payload.response.error)
|
|
71
|
+
// return res.status(self.payload.response.status).send(self.payload.response.error.message);
|
|
72
|
+
return res.status(self.payload.response.status).send(`${self.payload.response.error}`);
|
|
71
73
|
}
|
|
72
74
|
});
|
|
73
75
|
}
|
|
74
76
|
|
|
75
|
-
function
|
|
77
|
+
Module.prototype.resolveCommand = function (command) {
|
|
78
|
+
const self = this;
|
|
79
|
+
|
|
76
80
|
// Start
|
|
77
81
|
if (false) {
|
|
78
82
|
|
|
@@ -128,4 +132,47 @@ function resolveCommand(command) {
|
|
|
128
132
|
return command;
|
|
129
133
|
}
|
|
130
134
|
|
|
135
|
+
Module.prototype.resolveUser = function (options) {
|
|
136
|
+
const self = this;
|
|
137
|
+
return new Promise(async function(resolve, reject) {
|
|
138
|
+
let user = null;
|
|
139
|
+
|
|
140
|
+
options = options || {};
|
|
141
|
+
options.uid = typeof options.uid !== 'undefined' ? options.uid : self.payload.data.payload.uid;
|
|
142
|
+
options.admin = typeof options.admin !== 'undefined' ? options.admin : self.payload.user.roles.admin;
|
|
143
|
+
options.adminRequired = typeof options.adminRequired !== 'undefined' ? options.adminRequired : true;
|
|
144
|
+
|
|
145
|
+
if (options.uid) {
|
|
146
|
+
if (options.adminRequired && !options.admin) {
|
|
147
|
+
user = self.assistant.errorManager('Admin required', {code: 401, sentry: false, send: false, log: false}).error;
|
|
148
|
+
} else {
|
|
149
|
+
await self.libraries.admin.firestore().doc(`users/${options.uid}`)
|
|
150
|
+
.get()
|
|
151
|
+
.then(async function (doc) {
|
|
152
|
+
const data = doc.data();
|
|
153
|
+
if (data) {
|
|
154
|
+
user = data;
|
|
155
|
+
} else {
|
|
156
|
+
user = self.assistant.errorManager(`User does not exist: ${options.uid}`, {code: 400, sentry: false, send: false, log: false}).error;
|
|
157
|
+
}
|
|
158
|
+
})
|
|
159
|
+
.catch(function (e) {
|
|
160
|
+
user = self.assistant.errorManager(e, {code: 500, sentry: false, send: false, log: false}).error;
|
|
161
|
+
})
|
|
162
|
+
}
|
|
163
|
+
} else if (self.payload.user.authenticated) {
|
|
164
|
+
user = self.payload.user;
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
if (user instanceof Error) {
|
|
168
|
+
return reject(user);
|
|
169
|
+
} else if (!user) {
|
|
170
|
+
return reject(self.assistant.errorManager('Unable to resolve user', {code: 500, sentry: false, send: false, log: false}).error);
|
|
171
|
+
} else {
|
|
172
|
+
return resolve(user);
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
});
|
|
176
|
+
};
|
|
177
|
+
|
|
131
178
|
module.exports = Module;
|