backend-manager 2.5.22 → 2.5.24

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 CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "backend-manager",
3
- "version": "2.5.22",
3
+ "version": "2.5.24",
4
4
  "description": "Quick tools for developing Firebase functions",
5
5
  "main": "src/manager/index.js",
6
6
  "bin": {
@@ -61,4 +61,4 @@
61
61
  "wonderful-fetch": "^0.0.14",
62
62
  "yargs": "^17.5.1"
63
63
  }
64
- }
64
+ }
@@ -0,0 +1,5 @@
1
+ module.exports = function () {
2
+ return {
3
+
4
+ }
5
+ }
@@ -0,0 +1,101 @@
1
+ const path = require('path');
2
+ const fetch = require('wonderful-fetch');
3
+ const {get,set,merge} = require('lodash');
4
+
5
+ function Module() {
6
+
7
+ }
8
+
9
+ Module.prototype.main = function () {
10
+ const self = this;
11
+ const Manager = self.Manager;
12
+ const Api = self.Api;
13
+ const assistant = self.assistant;
14
+ const payload = self.payload;
15
+
16
+ return new Promise(async function(resolve, reject) {
17
+ payload.data.payload.id = payload.data.payload.id
18
+ payload.data.payload.email = payload.data.payload.email
19
+
20
+ const DEFAULT = {
21
+ spamFilter: {
22
+ ip: 3,
23
+ email: 3,
24
+ }
25
+ }
26
+
27
+ if (!payload.data.payload.id) {
28
+ return reject(assistant.errorManager(`Parameter {id} is required.`, {code: 400, sentry: false, send: false, log: false}).error)
29
+ } else if (!payload.data.payload.email) {
30
+ return reject(assistant.errorManager(`Parameter {email} is required.`, {code: 400, sentry: false, send: false, log: false}).error)
31
+ }
32
+
33
+ let emailPayload
34
+ try {
35
+ emailPayload = merge({}, DEFAULT, require(path.join(__dirname, 'emails', `${payload.data.payload.id}.js`))());
36
+ } catch (e) {
37
+ return reject(assistant.errorManager(`${payload.data.payload.id} is not a valid email ID.`, {code: 400, sentry: false, send: false, log: false}).error)
38
+ }
39
+
40
+ const storage = Manager.storage({temporary: true});
41
+ const ipPath = ['api:general:send-email', 'ips', assistant.request.ip];
42
+ const emailPath = ['api:general:send-email', 'emails', payload.data.payload.email];
43
+
44
+ const ipData = storage.get(ipPath).value() || {};
45
+ const emailData = storage.get(emailPath).value() || {};
46
+
47
+ ipData.count = (ipData.count || 0) + 1;
48
+ ipData.firstRequestTime = ipData.firstRequestTime ? ipData.firstRequestTime : new Date().toISOString();
49
+ ipData.lastRequestTime = new Date().toISOString();
50
+
51
+ emailData.count = (emailData.count || 0) + 1;
52
+ emailData.firstRequestTime = emailData.firstRequestTime ? emailData.firstRequestTime : new Date().toISOString();
53
+ emailData.lastRequestTime = new Date().toISOString();
54
+
55
+ storage.set(ipPath, ipData).write();
56
+ storage.set(emailPath, emailData).write();
57
+
58
+ assistant.log('Storage', storage.getState()['api:general:send-email']);
59
+
60
+ if (ipData.count >= emailPayload.spamFilter.ip || emailData.count >= emailPayload.spamFilter.email) {
61
+ self.assistant.errorManager(`Spam filter triggered ip=${ipData.count}, email=${emailData.count}`, {code: 429, sentry: false, send: false, log: true})
62
+ return resolve({data: {success: true}});
63
+ }
64
+
65
+ console.log('---emailPayload', emailPayload);
66
+
67
+ fetch('https://us-central1-itw-creative-works.cloudfunctions.net/wrapper', {
68
+ method: 'post',
69
+ timeout: 30000,
70
+ tries: 1,
71
+ response: 'json',
72
+ body: {
73
+ service: 'sendgrid',
74
+ command: `client/v4/zones/${doc.cloudflare.zone}/purge_cache`,
75
+ method: 'post',
76
+ body: {
77
+ purge_everything: true
78
+ },
79
+ },
80
+ })
81
+ .then(result => {
82
+ console.log('---result', result);
83
+ return resolve({
84
+ data: {
85
+ success: true,
86
+ }
87
+ });
88
+ })
89
+ .catch(e => {
90
+ return reject(assistant.errorManager(`Error sending email: ${e}`, {code: 500, sentry: true, send: false, log: false}).error)
91
+ })
92
+
93
+ // return resolve({data: {success: true}});
94
+
95
+ });
96
+
97
+ };
98
+
99
+
100
+ module.exports = Module;
101
+
@@ -169,7 +169,7 @@ Module.prototype.processState_tokenize = function (newUrl) {
169
169
  return new Promise(async function(resolve, reject) {
170
170
  const finalUrl = newUrl.toString();
171
171
 
172
- assistant.log('Running processState_tokenize()', {environment: 'development'});
172
+ assistant.log('Running processState_tokenize()', {environment: 'production'});
173
173
 
174
174
  const body = {
175
175
  client_id: _.get(Manager.config, `oauth2.${payload.data.payload.provider}.client_id`),
@@ -180,7 +180,7 @@ Module.prototype.processState_tokenize = function (newUrl) {
180
180
  // scope: '',
181
181
  };
182
182
 
183
- assistant.log('body', body, {environment: 'development'});
183
+ assistant.log('body', body, {environment: 'production'});
184
184
 
185
185
  const tokenizeResponse = await fetch(finalUrl, {
186
186
  method: 'POST',
@@ -197,7 +197,7 @@ Module.prototype.processState_tokenize = function (newUrl) {
197
197
  .then(json => json)
198
198
  .catch(e => e)
199
199
 
200
- assistant.log('tokenizeResponse', tokenizeResponse, {environment: 'development'});
200
+ assistant.log('tokenizeResponse', tokenizeResponse, {environment: 'production'});
201
201
 
202
202
  if (tokenizeResponse instanceof Error) {
203
203
  return reject(tokenizeResponse);
@@ -208,7 +208,7 @@ Module.prototype.processState_tokenize = function (newUrl) {
208
208
  .then(identity => identity)
209
209
  .catch(e => e);
210
210
 
211
- assistant.log('verifiedIdentity', verifiedIdentity, {environment: 'development'});
211
+ assistant.log('verifiedIdentity', verifiedIdentity, {environment: 'production'});
212
212
 
213
213
  if (verifiedIdentity instanceof Error) {
214
214
  return reject(verifiedIdentity);
@@ -234,7 +234,7 @@ Module.prototype.processState_tokenize = function (newUrl) {
234
234
  .then(r => r)
235
235
  .catch(e => e)
236
236
 
237
- assistant.log('storeResponse', storeResponse, {environment: 'development'});
237
+ assistant.log('storeResponse', storeResponse, {environment: 'production'});
238
238
 
239
239
  if (storeResponse instanceof Error) {
240
240
  return reject(storeResponse);