backend-manager 3.1.2 → 3.1.4
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/manager/functions/core/actions/api/admin/backup.js +3 -3
- package/src/manager/functions/core/actions/api/admin/create-post.js +3 -3
- package/src/manager/functions/core/actions/api/admin/cron.js +3 -3
- package/src/manager/functions/core/actions/api/admin/database-read.js +2 -2
- package/src/manager/functions/core/actions/api/admin/database-write.js +3 -3
- package/src/manager/functions/core/actions/api/admin/firestore-query.js +2 -2
- package/src/manager/functions/core/actions/api/admin/firestore-read.js +3 -3
- package/src/manager/functions/core/actions/api/admin/firestore-write.js +3 -3
- package/src/manager/functions/core/actions/api/admin/get-stats.js +4 -4
- package/src/manager/functions/core/actions/api/admin/payment-processor.js +3 -3
- package/src/manager/functions/core/actions/api/admin/send-notification.js +4 -4
- package/src/manager/functions/core/actions/api/admin/sync-users.js +2 -2
- package/src/manager/functions/core/actions/api/firebase/get-providers.js +4 -4
- package/src/manager/functions/core/actions/api/general/generate-uuid.js +2 -2
- package/src/manager/functions/core/actions/api/general/send-email.js +5 -5
- package/src/manager/functions/core/actions/api/handler/create-post.js +3 -3
- package/src/manager/functions/core/actions/api/special/setup-electron-manager-client.js +3 -3
- package/src/manager/functions/core/actions/api/template.js +1 -1
- package/src/manager/functions/core/actions/api/test/create-test-accounts.js +1 -1
- package/src/manager/functions/core/actions/api/test/webhook.js +1 -1
- package/src/manager/functions/core/actions/api/user/create-custom-token.js +1 -1
- package/src/manager/functions/core/actions/api/user/delete.js +2 -2
- package/src/manager/functions/core/actions/api/user/get-active-sessions.js +1 -1
- package/src/manager/functions/core/actions/api/user/regenerate-api-keys.js +1 -1
- package/src/manager/functions/core/actions/api/user/sign-out-all-sessions.js +3 -3
- package/src/manager/functions/core/actions/api/user/sign-up.js +4 -4
- package/src/manager/functions/core/actions/api/user/submit-feedback.js +2 -2
- package/src/manager/functions/core/actions/api/user/validate-settings.js +2 -2
- package/src/manager/functions/core/actions/api.js +4 -4
- package/src/manager/functions/core/cron/daily/reset-usage.js +4 -4
- package/src/manager/functions/core/cron/daily.js +1 -1
- package/src/manager/helpers/assistant.js +45 -26
- package/src/manager/helpers/middleware.js +19 -10
- package/src/manager/helpers/usage.js +10 -9
- package/src/manager/index.js +1 -1
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "backend-manager",
|
|
3
|
-
"version": "3.1.
|
|
3
|
+
"version": "3.1.4",
|
|
4
4
|
"description": "Quick tools for developing Firebase functions",
|
|
5
5
|
"main": "src/manager/index.js",
|
|
6
6
|
"bin": {
|
|
@@ -65,7 +65,7 @@
|
|
|
65
65
|
"uid-generator": "^2.0.0",
|
|
66
66
|
"ultimate-jekyll-poster": "^0.0.15",
|
|
67
67
|
"uuid": "^9.0.1",
|
|
68
|
-
"wonderful-fetch": "^1.0
|
|
68
|
+
"wonderful-fetch": "^1.1.0",
|
|
69
69
|
"wonderful-log": "^1.0.5",
|
|
70
70
|
"yargs": "^17.7.2"
|
|
71
71
|
}
|
|
@@ -19,7 +19,7 @@ Module.prototype.main = function () {
|
|
|
19
19
|
payload.data.payload.deletionRegex = payload.data.payload.deletionRegex ? powertools.regexify(payload.data.payload.deletionRegex) : payload.data.payload.deletionRegex;
|
|
20
20
|
|
|
21
21
|
if (!payload.user.roles.admin && assistant.meta.environment === 'production') {
|
|
22
|
-
return reject(assistant.
|
|
22
|
+
return reject(assistant.errorify(`Admin required.`, {code: 401, sentry: false, send: false, log: false}).error)
|
|
23
23
|
}
|
|
24
24
|
|
|
25
25
|
// https://googleapis.dev/nodejs/firestore/latest/v1.FirestoreAdminClient.html#exportDocuments
|
|
@@ -61,7 +61,7 @@ Module.prototype.main = function () {
|
|
|
61
61
|
})
|
|
62
62
|
.catch(async (e) => {
|
|
63
63
|
await self._setMetaStats(e);
|
|
64
|
-
return reject(assistant.
|
|
64
|
+
return reject(assistant.errorify(e, {code: 500, sentry: false, send: false, log: true}).error)
|
|
65
65
|
});
|
|
66
66
|
|
|
67
67
|
});
|
|
@@ -96,7 +96,7 @@ Module.prototype._setMetaStats = function (error, meta) {
|
|
|
96
96
|
metadata: Manager.Metadata().set({tag: 'admin:backup'}),
|
|
97
97
|
}, {merge: true})
|
|
98
98
|
.catch(e => {
|
|
99
|
-
assistant.
|
|
99
|
+
assistant.errorify(e, {code: 500, sentry: false, send: false, log: true});
|
|
100
100
|
})
|
|
101
101
|
|
|
102
102
|
return resolve();
|
|
@@ -18,7 +18,7 @@ Module.prototype.main = function () {
|
|
|
18
18
|
return new Promise(async function(resolve, reject) {
|
|
19
19
|
// Perform checks
|
|
20
20
|
if (!payload.user.roles.admin && !payload.user.roles.blogger) {
|
|
21
|
-
return reject(assistant.
|
|
21
|
+
return reject(assistant.errorify(`Admin required.`, {code: 401, sentry: false, send: false, log: false}).error)
|
|
22
22
|
}
|
|
23
23
|
|
|
24
24
|
const repoInfo = assistant.parseRepo(get(self.Manager.config, 'github.repo_website'));
|
|
@@ -45,7 +45,7 @@ Module.prototype.main = function () {
|
|
|
45
45
|
const finalPost = await poster.create(payload.data).catch(e => e);
|
|
46
46
|
|
|
47
47
|
if (finalPost instanceof Error) {
|
|
48
|
-
return reject(assistant.
|
|
48
|
+
return reject(assistant.errorify(`Failed to post: ${finalPost}`, {code: 500, sentry: false, send: false, log: false}).error)
|
|
49
49
|
}
|
|
50
50
|
|
|
51
51
|
// Request indexing
|
|
@@ -68,7 +68,7 @@ Module.prototype.main = function () {
|
|
|
68
68
|
return resolve({data: finalPost});
|
|
69
69
|
})
|
|
70
70
|
.catch((e) => {
|
|
71
|
-
return reject(assistant.
|
|
71
|
+
return reject(assistant.errorify(`Failed to post: ${e}`, {code: 500, sentry: false, send: false, log: false}).error)
|
|
72
72
|
})
|
|
73
73
|
|
|
74
74
|
});
|
|
@@ -13,12 +13,12 @@ Module.prototype.main = function () {
|
|
|
13
13
|
|
|
14
14
|
// Check if the user is an admin
|
|
15
15
|
if (!payload.user.roles.admin && assistant.meta.environment === 'production') {
|
|
16
|
-
return reject(assistant.
|
|
16
|
+
return reject(assistant.errorify(`Admin required.`, {code: 401, sentry: false, send: false, log: false}).error)
|
|
17
17
|
}
|
|
18
18
|
|
|
19
19
|
// Check if the ID is set
|
|
20
20
|
if (!payload.data.payload.id) {
|
|
21
|
-
return reject(assistant.
|
|
21
|
+
return reject(assistant.errorify(`Missing parameter {id}`, {code: 400, sentry: false, send: false, log: false}).error)
|
|
22
22
|
}
|
|
23
23
|
|
|
24
24
|
// Run the cron job
|
|
@@ -27,7 +27,7 @@ Module.prototype.main = function () {
|
|
|
27
27
|
return resolve({data: res});
|
|
28
28
|
})
|
|
29
29
|
.catch(e => {
|
|
30
|
-
return reject(assistant.
|
|
30
|
+
return reject(assistant.errorify(e, {code: 400, sentry: false, send: false, log: false}).error)
|
|
31
31
|
})
|
|
32
32
|
});
|
|
33
33
|
|
|
@@ -16,9 +16,9 @@ Module.prototype.main = function () {
|
|
|
16
16
|
|
|
17
17
|
// Perform checks
|
|
18
18
|
if (!payload.user.roles.admin) {
|
|
19
|
-
return reject(assistant.
|
|
19
|
+
return reject(assistant.errorify(`Admin required.`, {code: 401, sentry: false, send: false, log: false}).error)
|
|
20
20
|
} else if (!payload.data.payload.path) {
|
|
21
|
-
return reject(assistant.
|
|
21
|
+
return reject(assistant.errorify(`<path> parameter required`, {code: 400, sentry: false, send: false, log: false}).error)
|
|
22
22
|
}
|
|
23
23
|
|
|
24
24
|
// Read from Firestore
|
|
@@ -17,9 +17,9 @@ Module.prototype.main = function () {
|
|
|
17
17
|
|
|
18
18
|
// Perform checks
|
|
19
19
|
if (payload.user.roles.admin) {
|
|
20
|
-
return reject(assistant.
|
|
20
|
+
return reject(assistant.errorify(`Admin required.`, {code: 401, sentry: false, send: false, log: false}).error)
|
|
21
21
|
} else if (!payload.data.payload.path) {
|
|
22
|
-
return reject(assistant.
|
|
22
|
+
return reject(assistant.errorify(`<path> parameter required`, {code: 400, sentry: false, send: false, log: false}).error);
|
|
23
23
|
}
|
|
24
24
|
|
|
25
25
|
// Write to Firestore
|
|
@@ -29,7 +29,7 @@ Module.prototype.main = function () {
|
|
|
29
29
|
return resolve({data: payload.data.payload.document});
|
|
30
30
|
})
|
|
31
31
|
.catch((e) => {
|
|
32
|
-
return reject(assistant.
|
|
32
|
+
return reject(assistant.errorify(e, {code: 500, sentry: false, send: false, log: false}).error)
|
|
33
33
|
});
|
|
34
34
|
});
|
|
35
35
|
|
|
@@ -15,7 +15,7 @@ Module.prototype.main = function () {
|
|
|
15
15
|
return new Promise(async function(resolve, reject) {
|
|
16
16
|
// Perform checks
|
|
17
17
|
if (!payload.user.roles.admin) {
|
|
18
|
-
return reject(assistant.
|
|
18
|
+
return reject(assistant.errorify(`Admin required.`, {code: 401, sentry: false, send: false, log: false}).error)
|
|
19
19
|
}
|
|
20
20
|
|
|
21
21
|
// Run queries
|
|
@@ -35,7 +35,7 @@ Module.prototype.main = function () {
|
|
|
35
35
|
return resolve({data: self.docs});
|
|
36
36
|
})
|
|
37
37
|
.catch((e) => {
|
|
38
|
-
return reject(assistant.
|
|
38
|
+
return reject(assistant.errorify(e, {code: 500, sentry: false, send: false, log: false}).error)
|
|
39
39
|
})
|
|
40
40
|
|
|
41
41
|
});
|
|
@@ -16,9 +16,9 @@ Module.prototype.main = function () {
|
|
|
16
16
|
|
|
17
17
|
// Perform checks
|
|
18
18
|
if (!payload.user.roles.admin) {
|
|
19
|
-
return reject(assistant.
|
|
19
|
+
return reject(assistant.errorify(`Admin required.`, {code: 401, sentry: false, send: false, log: false}).error)
|
|
20
20
|
} else if (!payload.data.payload.path) {
|
|
21
|
-
return reject(assistant.
|
|
21
|
+
return reject(assistant.errorify(`<path> parameter required`, {code: 400, sentry: false, send: false, log: false}).error)
|
|
22
22
|
}
|
|
23
23
|
|
|
24
24
|
// Read from Firestore
|
|
@@ -28,7 +28,7 @@ Module.prototype.main = function () {
|
|
|
28
28
|
return resolve({data: doc.data()});
|
|
29
29
|
})
|
|
30
30
|
.catch(e => {
|
|
31
|
-
return reject(assistant.
|
|
31
|
+
return reject(assistant.errorify(e, {code: 500, sentry: false, send: false, log: false}).error)
|
|
32
32
|
})
|
|
33
33
|
});
|
|
34
34
|
|
|
@@ -19,9 +19,9 @@ Module.prototype.main = function () {
|
|
|
19
19
|
|
|
20
20
|
// Perform checks
|
|
21
21
|
if (!payload.user.roles.admin) {
|
|
22
|
-
return reject(assistant.
|
|
22
|
+
return reject(assistant.errorify(`Admin required.`, {code: 401, sentry: false, send: false, log: false}).error)
|
|
23
23
|
} else if (!payload.data.payload.path) {
|
|
24
|
-
return reject(assistant.
|
|
24
|
+
return reject(assistant.errorify(`Path parameter required.`, {code: 400, sentry: false, send: false, log: false}).error)
|
|
25
25
|
}
|
|
26
26
|
|
|
27
27
|
// Set metadata
|
|
@@ -37,7 +37,7 @@ Module.prototype.main = function () {
|
|
|
37
37
|
return resolve({data: {path: payload.data.payload.path}});
|
|
38
38
|
})
|
|
39
39
|
.catch(e => {
|
|
40
|
-
return reject(assistant.
|
|
40
|
+
return reject(assistant.errorify(e, {code: 500, sentry: false, send: false, log: false}).error)
|
|
41
41
|
})
|
|
42
42
|
});
|
|
43
43
|
|
|
@@ -17,7 +17,7 @@ Module.prototype.main = function () {
|
|
|
17
17
|
|
|
18
18
|
// Perform checks
|
|
19
19
|
if (!payload.user.roles.admin) {
|
|
20
|
-
return reject(assistant.
|
|
20
|
+
return reject(assistant.errorify(`Admin required.`, {code: 401, sentry: false, send: false, log: false}).error)
|
|
21
21
|
}
|
|
22
22
|
|
|
23
23
|
// Get stats
|
|
@@ -34,7 +34,7 @@ Module.prototype.main = function () {
|
|
|
34
34
|
}
|
|
35
35
|
|
|
36
36
|
if (data instanceof Error) {
|
|
37
|
-
return reject(assistant.
|
|
37
|
+
return reject(assistant.errorify(data, {code: 500, sentry: false, send: false, log: false}).error)
|
|
38
38
|
}
|
|
39
39
|
|
|
40
40
|
// Retrieve the stats again after updating
|
|
@@ -47,13 +47,13 @@ Module.prototype.main = function () {
|
|
|
47
47
|
|
|
48
48
|
|
|
49
49
|
if (data instanceof Error) {
|
|
50
|
-
return reject(assistant.
|
|
50
|
+
return reject(assistant.errorify(data, {code: 500, sentry: false, send: false, log: false}).error)
|
|
51
51
|
}
|
|
52
52
|
|
|
53
53
|
return resolve({data: data})
|
|
54
54
|
})
|
|
55
55
|
.catch(function (e) {
|
|
56
|
-
return reject(assistant.
|
|
56
|
+
return reject(assistant.errorify(`Failed to get: ${e}`, {code: 500, sentry: false, send: false, log: false}).error)
|
|
57
57
|
})
|
|
58
58
|
});
|
|
59
59
|
|
|
@@ -15,12 +15,12 @@ Module.prototype.main = function () {
|
|
|
15
15
|
return new Promise(async function(resolve, reject) {
|
|
16
16
|
|
|
17
17
|
if (!payload.user.roles.admin) {
|
|
18
|
-
return reject(assistant.
|
|
18
|
+
return reject(assistant.errorify(`Admin required.`, {code: 401, sentry: false, send: false, log: false}).error)
|
|
19
19
|
}
|
|
20
20
|
|
|
21
21
|
const productId = _.get(payload, 'data.payload.payload.details.productIdGlobal');
|
|
22
22
|
if (!productId) {
|
|
23
|
-
return reject(assistant.
|
|
23
|
+
return reject(assistant.errorify(`No productId`, {code: 400, sentry: false, send: false, log: false}).error)
|
|
24
24
|
}
|
|
25
25
|
const processorPath = `${process.cwd()}/payment-processors/${productId}.js`
|
|
26
26
|
let processor;
|
|
@@ -45,7 +45,7 @@ Module.prototype.main = function () {
|
|
|
45
45
|
return resolve({data: result});
|
|
46
46
|
})
|
|
47
47
|
.catch(e => {
|
|
48
|
-
return reject(assistant.
|
|
48
|
+
return reject(assistant.errorify(`Payment processor @ "${processorPath}" failed: ${e}`, {code: 400, sentry: true, send: false, log: false}).error)
|
|
49
49
|
})
|
|
50
50
|
});
|
|
51
51
|
|
|
@@ -37,17 +37,17 @@ Module.prototype.main = function () {
|
|
|
37
37
|
self._notificationPayload.notification.click_action.searchParams.set('cb', new Date().getTime())
|
|
38
38
|
self._notificationPayload.notification.click_action = self._notificationPayload.notification.click_action.toString()
|
|
39
39
|
} catch (e) {
|
|
40
|
-
assistant.
|
|
40
|
+
assistant.errorify(`Failed to add cb to URL: ${e}`, {code: 500, sentry: false, send: false, log: true})
|
|
41
41
|
}
|
|
42
42
|
|
|
43
43
|
assistant.log('Resolved notification payload', self._notificationPayload)
|
|
44
44
|
|
|
45
45
|
if (!payload.user.roles.admin) {
|
|
46
|
-
return reject(assistant.
|
|
46
|
+
return reject(assistant.errorify(`Admin required.`, {code: 401, sentry: false, send: false, log: false}).error)
|
|
47
47
|
}
|
|
48
48
|
|
|
49
49
|
if (!payload.data.payload.title || !payload.data.payload.body) {
|
|
50
|
-
return reject(assistant.
|
|
50
|
+
return reject(assistant.errorify(`Parameters <title> and <body> required`, {code: 400, sentry: true, send: false, log: false}).error)
|
|
51
51
|
}
|
|
52
52
|
|
|
53
53
|
await self.getTokens({tags: false})
|
|
@@ -55,7 +55,7 @@ Module.prototype.main = function () {
|
|
|
55
55
|
return resolve({data: payload.response.data})
|
|
56
56
|
})
|
|
57
57
|
.catch(e => {
|
|
58
|
-
return reject(assistant.
|
|
58
|
+
return reject(assistant.errorify(`Failed to send notification: ${e}`, {code: 400, sentry: true, send: false, log: false}).error)
|
|
59
59
|
})
|
|
60
60
|
});
|
|
61
61
|
|
|
@@ -15,7 +15,7 @@ Module.prototype.main = function () {
|
|
|
15
15
|
|
|
16
16
|
// If the user is not an admin, reject
|
|
17
17
|
if (!payload.user.roles.admin && assistant.meta.environment === 'production') {
|
|
18
|
-
return reject(assistant.
|
|
18
|
+
return reject(assistant.errorify(`Admin required.`, {code: 401, sentry: false, send: false, log: false}).error)
|
|
19
19
|
}
|
|
20
20
|
|
|
21
21
|
// Get lastPageToken from meta/stats
|
|
@@ -116,7 +116,7 @@ Module.prototype.main = function () {
|
|
|
116
116
|
return resolve();
|
|
117
117
|
})
|
|
118
118
|
.catch(e => {
|
|
119
|
-
return reject(assistant.
|
|
119
|
+
return reject(assistant.errorify(e, {code: 500, sentry: false, send: false, log: false}).error)
|
|
120
120
|
})
|
|
121
121
|
});
|
|
122
122
|
|
|
@@ -44,7 +44,7 @@ Module.prototype.main = function () {
|
|
|
44
44
|
payload.data.payload.firebaseApiKey = payload.data.payload.firebaseApiKey || _.get(Manager, 'config.firebaseConfig.apiKey') || false;
|
|
45
45
|
|
|
46
46
|
if (!payload.data.payload.firebaseApiKey) {
|
|
47
|
-
return reject(assistant.
|
|
47
|
+
return reject(assistant.errorify(`The firebaseApiKey parameter is required.`, {code: 400, sentry: false, send: false, log: false}).error)
|
|
48
48
|
}
|
|
49
49
|
|
|
50
50
|
// Default
|
|
@@ -91,7 +91,7 @@ Module.prototype.main = function () {
|
|
|
91
91
|
|
|
92
92
|
payload.response.data[provider.name] = result;
|
|
93
93
|
} catch (e) {
|
|
94
|
-
assistant.
|
|
94
|
+
assistant.errorify(`Error parsing error: ${e}`, {sentry: true, send: false, log: true})
|
|
95
95
|
payload.response.data[provider.name] = false;
|
|
96
96
|
}
|
|
97
97
|
})
|
|
@@ -136,7 +136,7 @@ Module.prototype.main = function () {
|
|
|
136
136
|
|
|
137
137
|
})
|
|
138
138
|
.catch(e => {
|
|
139
|
-
assistant.
|
|
139
|
+
assistant.errorify(`Error getting app data: ${e}`, {sentry: false, send: false, log: true})
|
|
140
140
|
})
|
|
141
141
|
.finally(r => {
|
|
142
142
|
return resolve({data: payload.response.data});
|
|
@@ -144,7 +144,7 @@ Module.prototype.main = function () {
|
|
|
144
144
|
|
|
145
145
|
})
|
|
146
146
|
.catch(e => {
|
|
147
|
-
return reject(assistant.
|
|
147
|
+
return reject(assistant.errorify(`Failed to check providers: ${e}`, {code: 500, sentry: false, send: false, log: false}).error)
|
|
148
148
|
})
|
|
149
149
|
|
|
150
150
|
});
|
|
@@ -20,14 +20,14 @@ Module.prototype.main = function () {
|
|
|
20
20
|
|
|
21
21
|
if (payload.data.payload.version === '5') {
|
|
22
22
|
if (!payload.data.payload.name) {
|
|
23
|
-
return reject(assistant.
|
|
23
|
+
return reject(assistant.errorify(`You must provide a name to hash for uuid v5.`, {code: 400, sentry: false, send: false, log: false}).error)
|
|
24
24
|
} else {
|
|
25
25
|
result = uuid.v5(payload.data.payload.name, payload.data.payload.namespace);
|
|
26
26
|
}
|
|
27
27
|
} else if (payload.data.payload.version === '4') {
|
|
28
28
|
result = uuid.v4();
|
|
29
29
|
} else {
|
|
30
|
-
return reject(assistant.
|
|
30
|
+
return reject(assistant.errorify(`v${payload.data.payload.version} is not a valid version.`, {code: 400, sentry: false, send: false, log: false}).error)
|
|
31
31
|
}
|
|
32
32
|
|
|
33
33
|
assistant.log('UUID Generated', payload.data.payload, result);
|
|
@@ -32,9 +32,9 @@ Module.prototype.main = function () {
|
|
|
32
32
|
}
|
|
33
33
|
|
|
34
34
|
if (!payload.data.payload.id) {
|
|
35
|
-
return reject(assistant.
|
|
35
|
+
return reject(assistant.errorify(`Parameter {id} is required.`, {code: 400, sentry: false, send: false, log: false}).error)
|
|
36
36
|
} else if (!payload.data.payload.email) {
|
|
37
|
-
return reject(assistant.
|
|
37
|
+
return reject(assistant.errorify(`Parameter {email} is required.`, {code: 400, sentry: false, send: false, log: false}).error)
|
|
38
38
|
}
|
|
39
39
|
|
|
40
40
|
let emailPayload
|
|
@@ -46,7 +46,7 @@ Module.prototype.main = function () {
|
|
|
46
46
|
script(payload.data.payload, Manager.config),
|
|
47
47
|
);
|
|
48
48
|
} catch (e) {
|
|
49
|
-
return reject(assistant.
|
|
49
|
+
return reject(assistant.errorify(`${payload.data.payload.id} is not a valid email ID.`, {code: 400, sentry: false, send: false, log: false}).error)
|
|
50
50
|
}
|
|
51
51
|
|
|
52
52
|
const storage = Manager.storage({temporary: true});
|
|
@@ -70,7 +70,7 @@ Module.prototype.main = function () {
|
|
|
70
70
|
assistant.log('Storage:', storage.getState()['api:general:send-email']);
|
|
71
71
|
|
|
72
72
|
if (ipData.count >= emailPayload.spamFilter.ip || emailData.count >= emailPayload.spamFilter.email) {
|
|
73
|
-
self.assistant.
|
|
73
|
+
self.assistant.errorify(`Spam filter triggered ip=${ipData.count}, email=${emailData.count}`, {code: 429, sentry: false, send: false, log: true})
|
|
74
74
|
return resolve({data: {success: true}});
|
|
75
75
|
}
|
|
76
76
|
|
|
@@ -99,7 +99,7 @@ Module.prototype.main = function () {
|
|
|
99
99
|
});
|
|
100
100
|
})
|
|
101
101
|
.catch(e => {
|
|
102
|
-
return reject(assistant.
|
|
102
|
+
return reject(assistant.errorify(`Error sending email: ${e}`, {code: 500, sentry: true, send: false, log: false}).error)
|
|
103
103
|
});
|
|
104
104
|
|
|
105
105
|
});
|
|
@@ -15,7 +15,7 @@ Module.prototype.main = function () {
|
|
|
15
15
|
return new Promise(async function(resolve, reject) {
|
|
16
16
|
|
|
17
17
|
if (!payload.user.roles.admin) {
|
|
18
|
-
return reject(assistant.
|
|
18
|
+
return reject(assistant.errorify(`Admin required.`, {code: 401, sentry: false, send: false, log: false}).error)
|
|
19
19
|
}
|
|
20
20
|
|
|
21
21
|
payload.response.data = {
|
|
@@ -83,7 +83,7 @@ Module.prototype.main = function () {
|
|
|
83
83
|
.catch(e => e);
|
|
84
84
|
|
|
85
85
|
if (createdInvoice instanceof Error) {
|
|
86
|
-
return reject(assistant.
|
|
86
|
+
return reject(assistant.errorify(createdInvoice, {code: 400, sentry: false, send: false, log: false}).error)
|
|
87
87
|
}
|
|
88
88
|
|
|
89
89
|
// Send invoice
|
|
@@ -104,7 +104,7 @@ Module.prototype.main = function () {
|
|
|
104
104
|
.catch(e => e);
|
|
105
105
|
|
|
106
106
|
if (sentInvoice instanceof Error) {
|
|
107
|
-
return reject(assistant.
|
|
107
|
+
return reject(assistant.errorify(sentInvoice, {code: 500, sentry: false, send: false, log: false}).error)
|
|
108
108
|
}
|
|
109
109
|
|
|
110
110
|
payload.response.data.invoice = {
|
|
@@ -32,11 +32,11 @@ Module.prototype.main = function () {
|
|
|
32
32
|
signInToken = token;
|
|
33
33
|
})
|
|
34
34
|
.catch(e => {
|
|
35
|
-
error = assistant.
|
|
35
|
+
error = assistant.errorify(`Failed to create custom token: ${e}`, {code: 500, sentry: false, send: false, log: false}).error
|
|
36
36
|
})
|
|
37
37
|
})
|
|
38
38
|
.catch(e => {
|
|
39
|
-
assistant.
|
|
39
|
+
assistant.errorify(`Failed to resolve user: ${e}`, {code: 500, sentry: false, send: false, log: true})
|
|
40
40
|
})
|
|
41
41
|
|
|
42
42
|
if (error) {
|
|
@@ -93,7 +93,7 @@ Module.prototype.main = function () {
|
|
|
93
93
|
});
|
|
94
94
|
})
|
|
95
95
|
.catch(e => {
|
|
96
|
-
return reject(assistant.
|
|
96
|
+
return reject(assistant.errorify(`Error fetching app details: ${e}`, {code: 500, sentry: false, send: false, log: false}).error)
|
|
97
97
|
})
|
|
98
98
|
|
|
99
99
|
|
|
@@ -12,7 +12,7 @@ Module.prototype.main = function () {
|
|
|
12
12
|
return new Promise(async function(resolve, reject) {
|
|
13
13
|
|
|
14
14
|
if (!payload.user.roles.admin) {
|
|
15
|
-
return reject(assistant.
|
|
15
|
+
return reject(assistant.errorify(`Admin required.`, {code: 401, sentry: false, send: false, log: false}).error)
|
|
16
16
|
}
|
|
17
17
|
|
|
18
18
|
// self.Api.resolveUser({adminRequired: false})
|
|
@@ -12,7 +12,7 @@ Module.prototype.main = function () {
|
|
|
12
12
|
return new Promise(async function(resolve, reject) {
|
|
13
13
|
|
|
14
14
|
if (!payload.user.roles.admin) {
|
|
15
|
-
return reject(assistant.
|
|
15
|
+
return reject(assistant.errorify(`Admin required.`, {code: 401, sentry: false, send: false, log: false}).error)
|
|
16
16
|
}
|
|
17
17
|
|
|
18
18
|
assistant.log('User:', payload.user);
|
|
@@ -19,7 +19,7 @@ Module.prototype.main = function () {
|
|
|
19
19
|
if (payload.data.payload.status >= 200 && payload.data.payload.status <= 299) {
|
|
20
20
|
return resolve({data: payload.data.payload.response, status: payload.data.payload.status});
|
|
21
21
|
} else if (payload.data.payload.status >= 400 && payload.data.payload.status <= 599) {
|
|
22
|
-
return reject(assistant.
|
|
22
|
+
return reject(assistant.errorify(payload.data.payload.response || 'Unknown error message provided', {code: payload.data.payload.status, sentry: false, send: false, log: false}).error)
|
|
23
23
|
}
|
|
24
24
|
|
|
25
25
|
});
|
|
@@ -19,7 +19,7 @@ Module.prototype.main = function () {
|
|
|
19
19
|
return resolve({data: {token: token}});
|
|
20
20
|
})
|
|
21
21
|
.catch(e => {
|
|
22
|
-
return reject(assistant.
|
|
22
|
+
return reject(assistant.errorify(`Failed to create custom token: ${e}`, {code: 400, sentry: false, send: false, log: false}).error)
|
|
23
23
|
})
|
|
24
24
|
})
|
|
25
25
|
.catch(e => {
|
|
@@ -22,7 +22,7 @@ Module.prototype.main = function () {
|
|
|
22
22
|
(user?.plan?.status && user?.plan?.status !== 'cancelled')
|
|
23
23
|
|| user?.plan?.payment?.active
|
|
24
24
|
) {
|
|
25
|
-
return reject(assistant.
|
|
25
|
+
return reject(assistant.errorify(`This account cannot be deleted because it has a paid subscription attached to it. In order to delete the account, you must first cancel the paid subscription.`, {code: 400, sentry: false, send: false, log: false}).error)
|
|
26
26
|
}
|
|
27
27
|
|
|
28
28
|
// Signout of all sessions
|
|
@@ -54,7 +54,7 @@ Module.prototype.main = function () {
|
|
|
54
54
|
return resolve({data: {success: true}});
|
|
55
55
|
})
|
|
56
56
|
.catch(e => {
|
|
57
|
-
return reject(assistant.
|
|
57
|
+
return reject(assistant.errorify(`Failed to delete user: ${e}`, {code: 400, sentry: false, send: false, log: false}).error)
|
|
58
58
|
})
|
|
59
59
|
})
|
|
60
60
|
.catch(e => {
|
|
@@ -30,7 +30,7 @@ Module.prototype.main = function () {
|
|
|
30
30
|
return resolve({data: data});
|
|
31
31
|
})
|
|
32
32
|
.catch(e => {
|
|
33
|
-
return reject(assistant.
|
|
33
|
+
return reject(assistant.errorify(`Session query error: ${e}`, {code: 500, sentry: false, send: false, log: false}).error)
|
|
34
34
|
})
|
|
35
35
|
|
|
36
36
|
})
|
|
@@ -39,7 +39,7 @@ Module.prototype.main = function () {
|
|
|
39
39
|
return resolve({data: newKeys});
|
|
40
40
|
})
|
|
41
41
|
.catch(e => {
|
|
42
|
-
return reject(assistant.
|
|
42
|
+
return reject(assistant.errorify(`Failed to generate keys: ${e}`, {code: 500, sentry: true, send: false, log: false}).error)
|
|
43
43
|
})
|
|
44
44
|
|
|
45
45
|
})
|
|
@@ -37,10 +37,10 @@ Module.prototype.main = function () {
|
|
|
37
37
|
return resolve({data: {sessions: count, message: `Successfully signed ${uid} out of all sessions`}});
|
|
38
38
|
})
|
|
39
39
|
.catch(e => {
|
|
40
|
-
return reject(assistant.
|
|
40
|
+
return reject(assistant.errorify(`Failed to sign out of all sessions: ${e}`, {code: 500, sentry: false, send: false, log: false}).error)
|
|
41
41
|
})
|
|
42
42
|
} catch (e) {
|
|
43
|
-
return reject(assistant.
|
|
43
|
+
return reject(assistant.errorify(`Failed to sign out of all sessions: ${e}`, {code: 500, sentry: false, send: false, log: false}).error)
|
|
44
44
|
}
|
|
45
45
|
})
|
|
46
46
|
.catch(e => {
|
|
@@ -108,7 +108,7 @@ Module.prototype.signOutOfSession = function (uid, session) {
|
|
|
108
108
|
|
|
109
109
|
})
|
|
110
110
|
.catch(e => {
|
|
111
|
-
assistant.
|
|
111
|
+
assistant.errorify(`Session query error for session ${session}: ${e}`, {code: 500, sentry: true, send: false, log: true})
|
|
112
112
|
|
|
113
113
|
return reject(e);
|
|
114
114
|
})
|
|
@@ -26,7 +26,7 @@ Module.prototype.main = function () {
|
|
|
26
26
|
const usage = await Manager.Usage().init(assistant, {log: true, key: ip});
|
|
27
27
|
|
|
28
28
|
if (authUser instanceof Error) {
|
|
29
|
-
return reject(assistant.
|
|
29
|
+
return reject(assistant.errorify(`Failed to get auth user: ${authUser}`, {code: 500, sentry: false, send: false, log: false}).error)
|
|
30
30
|
}
|
|
31
31
|
|
|
32
32
|
// Age in seconds
|
|
@@ -34,7 +34,7 @@ Module.prototype.main = function () {
|
|
|
34
34
|
|
|
35
35
|
// If the user is not new, reject
|
|
36
36
|
if (ageInSeconds >= MAX_AGE) {
|
|
37
|
-
return reject(assistant.
|
|
37
|
+
return reject(assistant.errorify(`User is not new.`, {code: 400, sentry: false, send: false, log: false}).error)
|
|
38
38
|
}
|
|
39
39
|
|
|
40
40
|
// Check if IP has signed up too many times
|
|
@@ -86,7 +86,7 @@ Module.prototype.main = function () {
|
|
|
86
86
|
});
|
|
87
87
|
|
|
88
88
|
// Reject
|
|
89
|
-
return reject(assistant.
|
|
89
|
+
return reject(assistant.errorify(`Too many signups from this IP (${ip}).`, {code: 429, sentry: false, send: false, log: false}).error)
|
|
90
90
|
}
|
|
91
91
|
|
|
92
92
|
// Increment signups
|
|
@@ -121,7 +121,7 @@ Module.prototype.main = function () {
|
|
|
121
121
|
return resolve({data: result});
|
|
122
122
|
})
|
|
123
123
|
.catch(function (e) {
|
|
124
|
-
return reject(assistant.
|
|
124
|
+
return reject(assistant.errorify(`Failed to sign up: ${e}`, {code: 500, sentry: false, send: false, log: false}).error)
|
|
125
125
|
})
|
|
126
126
|
})
|
|
127
127
|
.catch(e => {
|
|
@@ -77,11 +77,11 @@ Module.prototype.main = function () {
|
|
|
77
77
|
});
|
|
78
78
|
})
|
|
79
79
|
.catch((e) => {
|
|
80
|
-
return reject(assistant.
|
|
80
|
+
return reject(assistant.errorify(`Failed to save feedback: ${e.message}`, {code: 500, sentry: true, send: false, log: true}).error)
|
|
81
81
|
})
|
|
82
82
|
})
|
|
83
83
|
.catch((e) => {
|
|
84
|
-
return reject(assistant.
|
|
84
|
+
return reject(assistant.errorify(`Failed to get app: ${e.message}`, {code: 500, sentry: true, send: false, log: true}).error)
|
|
85
85
|
})
|
|
86
86
|
|
|
87
87
|
})
|
|
@@ -28,7 +28,7 @@ Module.prototype.main = function () {
|
|
|
28
28
|
|
|
29
29
|
// Check if the file exists
|
|
30
30
|
if (!jetpack.exists(resolvedPath)) {
|
|
31
|
-
return reject(assistant.
|
|
31
|
+
return reject(assistant.errorify(`Defaults file at ${resolvedPath} does not exist, please add it manually.`, {code: 500, sentry: true, send: false, log: true}).error);
|
|
32
32
|
}
|
|
33
33
|
|
|
34
34
|
// Load the file
|
|
@@ -40,7 +40,7 @@ Module.prototype.main = function () {
|
|
|
40
40
|
|
|
41
41
|
return resolve({data: powertools.defaults(settings, combined)});
|
|
42
42
|
} catch (e) {
|
|
43
|
-
return reject(assistant.
|
|
43
|
+
return reject(assistant.errorify(`Unable to load file at ${resolvedPath}: ${e}`, {code: 500, sentry: true, send: false, log: true}).error);
|
|
44
44
|
}
|
|
45
45
|
|
|
46
46
|
})
|
|
@@ -271,7 +271,7 @@ Module.prototype.resolveUser = function (options) {
|
|
|
271
271
|
|
|
272
272
|
if (options.uid) {
|
|
273
273
|
if (options.adminRequired && !options.admin) {
|
|
274
|
-
user = self.assistant.
|
|
274
|
+
user = self.assistant.errorify('Admin required', {code: 401, sentry: false, send: false, log: false}).error;
|
|
275
275
|
} else {
|
|
276
276
|
await self.libraries.admin.firestore().doc(`users/${options.uid}`)
|
|
277
277
|
.get()
|
|
@@ -280,11 +280,11 @@ Module.prototype.resolveUser = function (options) {
|
|
|
280
280
|
if (data) {
|
|
281
281
|
user = data;
|
|
282
282
|
} else {
|
|
283
|
-
user = self.assistant.
|
|
283
|
+
user = self.assistant.errorify(`User does not exist: ${options.uid}`, {code: 400, sentry: false, send: false, log: false}).error;
|
|
284
284
|
}
|
|
285
285
|
})
|
|
286
286
|
.catch(function (e) {
|
|
287
|
-
user = self.assistant.
|
|
287
|
+
user = self.assistant.errorify(e, {code: 500, sentry: false, send: false, log: false}).error;
|
|
288
288
|
})
|
|
289
289
|
}
|
|
290
290
|
} else if (self.payload.user.authenticated) {
|
|
@@ -296,7 +296,7 @@ Module.prototype.resolveUser = function (options) {
|
|
|
296
296
|
if (user instanceof Error) {
|
|
297
297
|
return reject(user);
|
|
298
298
|
} else if (!user) {
|
|
299
|
-
return reject(self.assistant.
|
|
299
|
+
return reject(self.assistant.errorify('Unable to resolve user', {code: 500, sentry: false, send: false, log: false}).error);
|
|
300
300
|
} else {
|
|
301
301
|
return resolve(user);
|
|
302
302
|
}
|
|
@@ -105,7 +105,7 @@ Module.prototype.clearFirestore = function() {
|
|
|
105
105
|
assistant.log(`cron/daily/reset-usage() [firestore]: Resetting metrics`, metrics);
|
|
106
106
|
|
|
107
107
|
if (metrics instanceof Error) {
|
|
108
|
-
return reject(assistant.
|
|
108
|
+
return reject(assistant.errorify(`Failed to check providers: ${metrics}`, {code: 500, sentry: false, send: false, log: false}).error)
|
|
109
109
|
}
|
|
110
110
|
|
|
111
111
|
// Reset all metrics with for loop of metrics
|
|
@@ -140,7 +140,7 @@ Module.prototype.clearFirestore = function() {
|
|
|
140
140
|
assistant.log(`cron/daily/reset-usage() [firestore]: Reset ${metric} for ${doc.id} (${original} -> 0)`);
|
|
141
141
|
})
|
|
142
142
|
.catch(e => {
|
|
143
|
-
assistant.
|
|
143
|
+
assistant.errorify(`Error resetting ${metric} for ${doc.id}: ${e}`, {sentry: false, send: false, log: true})
|
|
144
144
|
})
|
|
145
145
|
}
|
|
146
146
|
|
|
@@ -159,7 +159,7 @@ Module.prototype.clearFirestore = function() {
|
|
|
159
159
|
assistant.log(`cron/daily/reset-usage() [firestore]: Reset ${metric} for all users complete!`);
|
|
160
160
|
})
|
|
161
161
|
.catch(e => {
|
|
162
|
-
assistant.
|
|
162
|
+
assistant.errorify(`Error resetting ${metric} for all users: ${e}`, {sentry: false, send: false, log: true})
|
|
163
163
|
})
|
|
164
164
|
}
|
|
165
165
|
|
|
@@ -169,7 +169,7 @@ Module.prototype.clearFirestore = function() {
|
|
|
169
169
|
assistant.log(`cron/daily/reset-usage() [firestore]: Deleted temporary/usage`);
|
|
170
170
|
})
|
|
171
171
|
.catch(e => {
|
|
172
|
-
assistant.
|
|
172
|
+
assistant.errorify(`Error deleting temporary/usage: ${e}`, {sentry: false, send: false, log: true})
|
|
173
173
|
})
|
|
174
174
|
|
|
175
175
|
return resolve();
|
|
@@ -48,7 +48,7 @@ Module.prototype.main = function() {
|
|
|
48
48
|
assistant.log(`cron/daily(): Job ${jobName} completed...`);
|
|
49
49
|
})
|
|
50
50
|
.catch(e => {
|
|
51
|
-
assistant.
|
|
51
|
+
assistant.errorify(`Error executing ${jobName}: ${e}`, {sentry: true, send: false, log: true});
|
|
52
52
|
caught = e;
|
|
53
53
|
})
|
|
54
54
|
}
|
|
@@ -60,7 +60,7 @@ BackendAssistant.prototype.init = function (ref, options) {
|
|
|
60
60
|
|
|
61
61
|
// Set ID
|
|
62
62
|
try {
|
|
63
|
-
self.id = self.Manager.Utilities().randomId();
|
|
63
|
+
self.id = self.ref.req.headers['function-execution-id'] || self.Manager.Utilities().randomId();
|
|
64
64
|
} catch {
|
|
65
65
|
self.id = now.getTime();
|
|
66
66
|
}
|
|
@@ -344,8 +344,9 @@ BackendAssistant.prototype._log = function () {
|
|
|
344
344
|
}
|
|
345
345
|
}
|
|
346
346
|
|
|
347
|
-
BackendAssistant.prototype.
|
|
347
|
+
BackendAssistant.prototype.errorify = function (e, options) {
|
|
348
348
|
const self = this;
|
|
349
|
+
const res = self.ref.res;
|
|
349
350
|
|
|
350
351
|
// Set options
|
|
351
352
|
options = options || {};
|
|
@@ -362,7 +363,7 @@ BackendAssistant.prototype.errorManager = function (e, options) {
|
|
|
362
363
|
? true
|
|
363
364
|
: options.send;
|
|
364
365
|
options.stack = typeof options.stack === 'undefined'
|
|
365
|
-
?
|
|
366
|
+
? false
|
|
366
367
|
: options.stack;
|
|
367
368
|
|
|
368
369
|
// Construct error
|
|
@@ -370,16 +371,12 @@ BackendAssistant.prototype.errorManager = function (e, options) {
|
|
|
370
371
|
? e
|
|
371
372
|
: new Error(stringify(e));
|
|
372
373
|
|
|
374
|
+
// Fix code
|
|
373
375
|
options.code = newError.code || options.code;
|
|
376
|
+
options.code = isBetween(options.code, 400, 599) ? options.code : 500;
|
|
374
377
|
|
|
375
378
|
// Attach properties
|
|
376
|
-
|
|
377
|
-
.forEach((item, i) => {
|
|
378
|
-
Object.assign(newError, { [item]: options[item] });
|
|
379
|
-
});
|
|
380
|
-
|
|
381
|
-
// Attach properties
|
|
382
|
-
_attachHeaderProperties(self, options);
|
|
379
|
+
_attachHeaderProperties(self, options, newError);
|
|
383
380
|
|
|
384
381
|
// Log the error
|
|
385
382
|
if (options.log) {
|
|
@@ -392,18 +389,24 @@ BackendAssistant.prototype.errorManager = function (e, options) {
|
|
|
392
389
|
}
|
|
393
390
|
|
|
394
391
|
// Quit and respond to the request
|
|
395
|
-
if (options.send &&
|
|
396
|
-
|
|
392
|
+
if (options.send && res && res.status) {
|
|
393
|
+
let sendable = newError?.stack && options.stack
|
|
397
394
|
? newError?.stack
|
|
398
395
|
: newError?.message;
|
|
399
396
|
|
|
400
|
-
|
|
397
|
+
// Set error
|
|
398
|
+
sendable = `${sendable || newError || 'Unknown error'}`;
|
|
399
|
+
|
|
400
|
+
// Attach tag
|
|
401
|
+
if (newError.tag) {
|
|
402
|
+
// sendable = `(${newError.tag}) ${sendable}`;
|
|
403
|
+
sendable = `${sendable} (${newError.tag})`;
|
|
404
|
+
}
|
|
405
|
+
|
|
406
|
+
// Send response
|
|
407
|
+
res
|
|
401
408
|
.status(options.code)
|
|
402
|
-
.send(
|
|
403
|
-
sendable
|
|
404
|
-
? sendable
|
|
405
|
-
: newError
|
|
406
|
-
) || 'Unknown error');
|
|
409
|
+
.send(sendable);
|
|
407
410
|
}
|
|
408
411
|
|
|
409
412
|
return {
|
|
@@ -411,10 +414,11 @@ BackendAssistant.prototype.errorManager = function (e, options) {
|
|
|
411
414
|
}
|
|
412
415
|
}
|
|
413
416
|
|
|
414
|
-
BackendAssistant.prototype.
|
|
417
|
+
BackendAssistant.prototype.errorManager = BackendAssistant.prototype.errorify;
|
|
415
418
|
|
|
416
419
|
BackendAssistant.prototype.respond = function(response, options) {
|
|
417
420
|
const self = this;
|
|
421
|
+
const res = self.ref.res;
|
|
418
422
|
|
|
419
423
|
// Set options
|
|
420
424
|
options = options || {};
|
|
@@ -426,10 +430,12 @@ BackendAssistant.prototype.respond = function(response, options) {
|
|
|
426
430
|
: options.log;
|
|
427
431
|
|
|
428
432
|
// Handle error
|
|
433
|
+
const isErrorCode = isBetween(options.code, 400, 599);
|
|
429
434
|
if (
|
|
430
435
|
response instanceof Error
|
|
431
|
-
||
|
|
436
|
+
|| isErrorCode
|
|
432
437
|
) {
|
|
438
|
+
options.code = !isErrorCode ? undefined : options.code;
|
|
433
439
|
return self.errorify(response, options);
|
|
434
440
|
}
|
|
435
441
|
|
|
@@ -442,15 +448,20 @@ BackendAssistant.prototype.respond = function(response, options) {
|
|
|
442
448
|
}
|
|
443
449
|
|
|
444
450
|
// Send response
|
|
445
|
-
|
|
451
|
+
res.status(options.code);
|
|
446
452
|
|
|
447
|
-
|
|
448
|
-
|
|
453
|
+
// If it is an object, send as json
|
|
454
|
+
if (response && typeof response === 'object') {
|
|
455
|
+
res.json(response);
|
|
449
456
|
} else {
|
|
450
|
-
|
|
457
|
+
res.send(response);
|
|
451
458
|
}
|
|
452
459
|
}
|
|
453
460
|
|
|
461
|
+
function isBetween(value, min, max) {
|
|
462
|
+
return value >= min && value <= max;
|
|
463
|
+
}
|
|
464
|
+
|
|
454
465
|
function stringify(e) {
|
|
455
466
|
if (typeof e === 'string') {
|
|
456
467
|
return e;
|
|
@@ -459,7 +470,7 @@ function stringify(e) {
|
|
|
459
470
|
}
|
|
460
471
|
}
|
|
461
472
|
|
|
462
|
-
function _attachHeaderProperties(self, options) {
|
|
473
|
+
function _attachHeaderProperties(self, options, error) {
|
|
463
474
|
// Create headers
|
|
464
475
|
const headers = {
|
|
465
476
|
code: options.code,
|
|
@@ -473,6 +484,14 @@ function _attachHeaderProperties(self, options) {
|
|
|
473
484
|
|
|
474
485
|
// Attach properties
|
|
475
486
|
self.ref.res.header('bm-properties', JSON.stringify(headers));
|
|
487
|
+
|
|
488
|
+
// Attach properties
|
|
489
|
+
if (error) {
|
|
490
|
+
Object.keys(headers)
|
|
491
|
+
.forEach((item, i) => {
|
|
492
|
+
error[item] = headers[item];
|
|
493
|
+
});
|
|
494
|
+
}
|
|
476
495
|
}
|
|
477
496
|
|
|
478
497
|
BackendAssistant.prototype.authenticate = async function (options) {
|
|
@@ -504,7 +523,7 @@ BackendAssistant.prototype.authenticate = async function (options) {
|
|
|
504
523
|
}
|
|
505
524
|
}
|
|
506
525
|
|
|
507
|
-
if (req?.headers?.authorization?.startsWith('Bearer ')) {
|
|
526
|
+
if (req?.headers?.authorization && req?.headers?.authorization?.startsWith('Bearer ')) {
|
|
508
527
|
// Read the ID Token from the Authorization header.
|
|
509
528
|
idToken = req.headers.authorization.split('Bearer ')[1];
|
|
510
529
|
self.log('Found "Authorization" header', idToken, logOptions);
|
|
@@ -36,33 +36,42 @@ Middleware.prototype.run = function (library, req, res, options) {
|
|
|
36
36
|
library = path.resolve(process.cwd(), `${library}.js`);
|
|
37
37
|
library = new (require(library))();
|
|
38
38
|
} catch (e) {
|
|
39
|
-
assistant.
|
|
39
|
+
assistant.errorify(`Unable to load library @ (${library}): ${e.message}`, {sentry: true, send: true, log: true});
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
// Setup usage
|
|
43
|
+
if (options.setupUsage) {
|
|
44
|
+
assistant.usage = await Manager.Usage().init(assistant);
|
|
40
45
|
}
|
|
41
46
|
|
|
42
47
|
// Setup analytics
|
|
43
48
|
if (options.setupAnalytics) {
|
|
49
|
+
const uuid = assistant?.usage?.user?.auth?.uid
|
|
50
|
+
|| assistant.request.user.auth.uid
|
|
51
|
+
|| assistant.request.geolocation.ip
|
|
52
|
+
|
|
44
53
|
assistant.analytics = Manager.Analytics({
|
|
45
54
|
assistant: assistant,
|
|
46
|
-
uuid:
|
|
55
|
+
uuid: uuid,
|
|
47
56
|
})
|
|
48
57
|
}
|
|
49
58
|
|
|
50
|
-
// Setup usage
|
|
51
|
-
if (options.setupUsage) {
|
|
52
|
-
assistant.usage = await Manager.Usage().init(assistant);
|
|
53
|
-
}
|
|
54
|
-
|
|
55
59
|
// Process
|
|
56
60
|
try {
|
|
57
|
-
|
|
61
|
+
// Set properties
|
|
62
|
+
library.Manager = Manager;
|
|
63
|
+
library.assistant = assistant;
|
|
64
|
+
|
|
65
|
+
// Run library
|
|
66
|
+
library.main(assistant, req, res)
|
|
58
67
|
// .then(result => {
|
|
59
68
|
// return res.status(200).json(result);
|
|
60
69
|
// })
|
|
61
70
|
.catch(e => {
|
|
62
|
-
assistant.
|
|
71
|
+
assistant.errorify(e, {sentry: true, send: true, log: true});
|
|
63
72
|
});
|
|
64
73
|
} catch (e) {
|
|
65
|
-
assistant.
|
|
74
|
+
assistant.errorify(e, {sentry: true, send: true, log: true});
|
|
66
75
|
}
|
|
67
76
|
});
|
|
68
77
|
};
|
|
@@ -81,13 +81,14 @@ Usage.prototype.init = function (assistant, options) {
|
|
|
81
81
|
let foundUsage;
|
|
82
82
|
|
|
83
83
|
if (options.unauthenticatedMode === 'firestore') {
|
|
84
|
+
// TODO: Make it request using .where() query so it doesnt use a read if it doesnt have to
|
|
84
85
|
foundUsage = await Manager.libraries.admin.firestore().doc(`temporary/usage`)
|
|
85
86
|
.get()
|
|
86
87
|
.then((r) => {
|
|
87
88
|
return r.data()?.[`${self.key}`];
|
|
88
89
|
})
|
|
89
90
|
.catch((e) => {
|
|
90
|
-
assistant.
|
|
91
|
+
assistant.errorify(`Usage.init(): Error fetching usage data: ${e}`, {sentry: true, send: false, log: true});
|
|
91
92
|
});
|
|
92
93
|
} else {
|
|
93
94
|
foundUsage = self.storage.get(`${self.paths.user}.usage`, {}).value();
|
|
@@ -114,7 +115,7 @@ Usage.prototype.init = function (assistant, options) {
|
|
|
114
115
|
self.storage.set(`${self.paths.app}.lastFetched`, new Date().toISOString()).write();
|
|
115
116
|
})
|
|
116
117
|
.catch(e => {
|
|
117
|
-
assistant.
|
|
118
|
+
assistant.errorify(`Usage.init(): Error fetching app data: ${e}`, {sentry: true, send: false, log: true});
|
|
118
119
|
})
|
|
119
120
|
}
|
|
120
121
|
|
|
@@ -172,14 +173,14 @@ Usage.prototype.validate = function (path, options) {
|
|
|
172
173
|
// If the captcha is valid, resolve
|
|
173
174
|
if (!captchaResult || captchaResult instanceof Error || !captchaResult.success) {
|
|
174
175
|
return reject(
|
|
175
|
-
assistant.
|
|
176
|
+
assistant.errorify(`Captcha verification failed.`, {code: 400, sentry: false, send: false, log: false}).error
|
|
176
177
|
);
|
|
177
178
|
}
|
|
178
179
|
}
|
|
179
180
|
|
|
180
181
|
// Otherwise, they are over the limit, reject
|
|
181
182
|
return reject(
|
|
182
|
-
assistant.
|
|
183
|
+
assistant.errorify(`You have exceeded your ${path} usage limit of ${period}/${allowed}.`, {code: 429, sentry: false, send: false, log: false}).error
|
|
183
184
|
);
|
|
184
185
|
});
|
|
185
186
|
};
|
|
@@ -282,17 +283,17 @@ Usage.prototype.update = function () {
|
|
|
282
283
|
.then(() => {
|
|
283
284
|
self.log(`Usage.update(): Updated user.usage in firestore`, self.user.usage);
|
|
284
285
|
|
|
285
|
-
return resolve();
|
|
286
|
+
return resolve(self.user.usage);
|
|
286
287
|
})
|
|
287
288
|
.catch(e => {
|
|
288
|
-
return reject(assistant.
|
|
289
|
+
return reject(assistant.errorify(e, {sentry: true, send: false, log: false}));
|
|
289
290
|
});
|
|
290
291
|
} else {
|
|
291
292
|
self.storage.set(`${self.paths.user}.usage`, self.user.usage).write();
|
|
292
293
|
|
|
293
294
|
self.log(`Usage.update(): Updated user.usage in local storage`, self.user.usage);
|
|
294
295
|
|
|
295
|
-
return resolve();
|
|
296
|
+
return resolve(self.user.usage);
|
|
296
297
|
}
|
|
297
298
|
} else {
|
|
298
299
|
Manager.libraries.admin.firestore().doc(`users/${self.user.auth.uid}`)
|
|
@@ -302,10 +303,10 @@ Usage.prototype.update = function () {
|
|
|
302
303
|
.then(() => {
|
|
303
304
|
self.log(`Usage.update(): Updated user.usage in firestore`, self.user.usage);
|
|
304
305
|
|
|
305
|
-
return resolve();
|
|
306
|
+
return resolve(self.user.usage);
|
|
306
307
|
})
|
|
307
308
|
.catch(e => {
|
|
308
|
-
return reject(assistant.
|
|
309
|
+
return reject(assistant.errorify(e, {sentry: true, send: false, log: false}));
|
|
309
310
|
});
|
|
310
311
|
}
|
|
311
312
|
});
|
package/src/manager/index.js
CHANGED
|
@@ -466,7 +466,7 @@ Manager.prototype._process = function (mod) {
|
|
|
466
466
|
function _reject(e, log) {
|
|
467
467
|
if (log) {
|
|
468
468
|
// self.assistant.error(e);
|
|
469
|
-
mod.assistant.
|
|
469
|
+
mod.assistant.errorify(e, {code: 500, sentry: true, send: false, log: true});
|
|
470
470
|
}
|
|
471
471
|
// res.status(500).send(e.message);
|
|
472
472
|
return resolve()
|