backend-manager 3.2.18 → 3.2.20

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": "3.2.18",
3
+ "version": "3.2.20",
4
4
  "description": "Quick tools for developing Firebase functions",
5
5
  "main": "src/manager/index.js",
6
6
  "bin": {
@@ -50,7 +50,7 @@ Module.prototype.main = function () {
50
50
  await lib.main().catch(e => e);
51
51
  })
52
52
 
53
- await self.sendRateEmail(user);
53
+ await self.sendRateEmail(user).catch(e => e);
54
54
 
55
55
  // Reject
56
56
  return reject(assistant.errorify(`Too many signups from this IP (${ip}).`, {code: 429, sentry: false, send: false, log: false}));
@@ -62,36 +62,41 @@ Module.prototype.main = function () {
62
62
  // Update signups
63
63
  await usage.update();
64
64
 
65
+ // Send welcome email
66
+ await self.sendWelcomeEmail(user).catch(e => e);
67
+
65
68
  await self.signUp({
66
69
  auth: {
67
- uid: _.get(user, 'auth.uid', null),
68
- email: _.get(user, 'auth.email', null),
70
+ uid: user?.auth?.uid,
71
+ email: user?.auth?.email,
69
72
  },
70
73
  affiliate: {
71
- referrer: _.get(payload.data.payload, 'affiliateCode', null),
74
+ referrer: payload?.data?.payload?.affiliateCode || null,
72
75
  },
73
76
  })
74
77
  .then(async function (result) {
75
- if (_.get(payload.data.payload, 'newsletterSignUp', false)) {
76
- await addToMCList(
77
- _.get(Manager.config, 'mailchimp.key'),
78
- _.get(Manager.config, 'mailchimp.list_id'),
79
- _.get(user, 'auth.email', null),
80
- )
81
- .then(function (res) {
82
- assistant.log('Sucessfully added user to MC list.')
83
- })
84
- .catch(function (e) {
85
- assistant.log('Failed to add user to MC list.', e)
86
- })
78
+ // Skip if not a newsletter sign up
79
+ if (!payload?.data?.payload?.newsletterSignUp) {
80
+ return resolve({data: result});
87
81
  }
82
+
83
+ // Add to SendGrid list
84
+ await self.addToSendGridList(user)
85
+ .then((r) => {
86
+ assistant.log('addToSendGridList(): Success', r)
87
+ })
88
+ .catch((e) => {
89
+ assistant.log('Failed to add user to MC list.', e)
90
+ })
91
+
92
+ // Resolve
88
93
  return resolve({data: result});
89
94
  })
90
- .catch(function (e) {
95
+ .catch((e) => {
91
96
  return reject(assistant.errorify(`Failed to sign up: ${e}`, {code: 500, sentry: false, send: false, log: false}));
92
97
  })
93
98
  })
94
- .catch(e => {
99
+ .catch((e) => {
95
100
  return reject(e);
96
101
  })
97
102
 
@@ -121,6 +126,61 @@ Module.prototype.sendRateEmail = function (user) {
121
126
  subject: `Your ${Manager.config.brand.name} account has been deleted`,
122
127
  template: 'd-b7f8da3c98ad49a2ad1e187f3a67b546',
123
128
  group: 25927,
129
+ data: {
130
+ body: {
131
+ title: 'Account deleted',
132
+ message: `
133
+ Your account at <strong>${Manager.config.brand.name}</strong> has been <strong>deleted</strong> because you have signed up for too many accounts.
134
+ <br>
135
+ <br>
136
+ If you believe this is a mistake, please contact us at ${Manager.config.brand.email}.
137
+ <br>
138
+ <br>
139
+ <strong>User Record</strong>:
140
+ <br>
141
+ <pre>
142
+ <code>
143
+ ${JSON.stringify(user, null, 2)}
144
+ </code>
145
+ </pre>
146
+ `,
147
+ },
148
+ },
149
+ },
150
+ })
151
+ .then(async (json) => {
152
+ assistant.log('sendEmail(): Success', json)
153
+ return resolve(json);
154
+ })
155
+ .catch(e => {
156
+ assistant.error('sendEmail(): Failed', e)
157
+ return resolve(e);
158
+ });
159
+ });
160
+ }
161
+
162
+ Module.prototype.sendWelcomeEmail = function (user) {
163
+ const self = this;
164
+
165
+ return new Promise(async function(resolve, reject) {
166
+ const Manager = self.Manager;
167
+ const assistant = self.assistant;
168
+
169
+ // Send email
170
+ fetch(`https://us-central1-itw-creative-works.cloudfunctions.net/sendEmail`, {
171
+ method: 'post',
172
+ response: 'json',
173
+ log: true,
174
+ body: {
175
+ backendManagerKey: Manager.config.backend_manager.key,
176
+ app: Manager.config.app.id,
177
+ to: {
178
+ email: user.auth.email,
179
+ },
180
+ categories: [`account/welcome-1`],
181
+ subject: `Your ${Manager.config.brand.name} account has been deleted`,
182
+ template: 'd-b7f8da3c98ad49a2ad1e187f3a67b546',
183
+ group: 25928,
124
184
  data: {
125
185
  title: 'Account deleted',
126
186
  message: `
@@ -147,54 +207,6 @@ Module.prototype.sendRateEmail = function (user) {
147
207
  });
148
208
  }
149
209
 
150
- // Module.prototype.sendWelcomeEmail = function (user) {
151
- // const self = this;
152
-
153
- // return new Promise(async function(resolve, reject) {
154
- // const Manager = self.Manager;
155
- // const assistant = self.assistant;
156
-
157
- // // Send email
158
- // fetch(`https://us-central1-itw-creative-works.cloudfunctions.net/sendEmail`, {
159
- // method: 'post',
160
- // response: 'json',
161
- // log: true,
162
- // body: {
163
- // backendManagerKey: Manager.config.backend_manager.key,
164
- // app: Manager.config.app.id,
165
- // to: {
166
- // email: user.auth.email,
167
- // },
168
- // categories: [`account/too-many-signups`],
169
- // subject: `Your ${Manager.config.brand.name} account has been deleted`,
170
- // template: 'd-b7f8da3c98ad49a2ad1e187f3a67b546',
171
- // group: 25928,
172
- // data: {
173
- // title: 'Account deleted',
174
- // message: `
175
- // Your account at ${Manager.config.brand.name} has been deleted because you have signed up for too many accounts.
176
- // <br>
177
- // If you believe this is a mistake, please contact us at ${Manager.config.brand.email}.
178
- // <br>
179
- // <br>
180
- // <strong>User Record</strong>:
181
- // <br>
182
- // ${JSON.stringify(user, null, 2)}
183
- // `
184
- // },
185
- // },
186
- // })
187
- // .then(async (json) => {
188
- // assistant.log('sendEmail(): Success', json)
189
- // return resolve(json);
190
- // })
191
- // .catch(e => {
192
- // assistant.error('sendEmail(): Failed', e)
193
- // return resolve(e);
194
- // });
195
- // });
196
- // }
197
-
198
210
  Module.prototype.signUp = function (payload) {
199
211
  const self = this;
200
212
 
@@ -214,7 +226,7 @@ Module.prototype.signUp = function (payload) {
214
226
  assistant.log(`signUp(): payload`, payload)
215
227
 
216
228
  // Check if the user has a UID and email
217
- if (!_.get(payload, 'auth.uid', null) || !_.get(payload, 'auth.email', null)) {
229
+ if (!payload?.auth?.uid || !payload?.email?.uid) {
218
230
  return reject(new Error('Cannot create user without UID and email.'))
219
231
  }
220
232
 
@@ -334,6 +346,78 @@ Module.prototype.updateReferral = function (payload) {
334
346
  });
335
347
  }
336
348
 
349
+ // addToSendGridList
350
+ Module.prototype.addToSendGridList = function (user) {
351
+ const self = this;
352
+
353
+ return new Promise(async function(resolve, reject) {
354
+ const Manager = self.Manager;
355
+ const assistant = self.assistant;
356
+ const Api = self.Api;
357
+
358
+ if (!user?.auth?.email) {
359
+ return reject(new Error('Cannot add user to SendGrid list without email.'))
360
+ }
361
+
362
+ // Add to SendGrid list
363
+ fetch('https://api.itwcreativeworks.com/wrapper', {
364
+ method: 'post',
365
+ response: 'json',
366
+ body: {
367
+ backendManagerKey: Manager.config.backend_manager.key,
368
+ service: 'sendgrid',
369
+ command: `/v3/marketing/contacts`,
370
+ method: `put`,
371
+ body: {
372
+ contacts: [
373
+ {
374
+ email: user?.auth?.email,
375
+ address_line_1: undefined,
376
+ address_line_2: undefined,
377
+ // alternate_emails: [],
378
+ city: user?.activity?.geolocation?.city,
379
+ country: user?.activity?.geolocation?.country,
380
+ first_name: undefined,
381
+ last_name: undefined,
382
+ postal_code: undefined,
383
+ state_province_region: user?.activity?.geolocation?.region,
384
+
385
+ custom_fields: {
386
+ app: Manager.config.app.id,
387
+ user: user?.auth?.uid,
388
+ },
389
+ },
390
+ ],
391
+ },
392
+ },
393
+ })
394
+ .then(function (res) {
395
+ assistant.log('Sucessfully added user to SendGrid list.')
396
+ return resolve(res);
397
+ })
398
+ .catch(function (e) {
399
+ assistant.log('Failed to add user to SendGrid list.', e)
400
+ return resolve(e);
401
+ })
402
+
403
+ // await self.libraries.sendgrid.request({
404
+ // method: 'post',
405
+ // url: `/v3/contactdb/recipients`,
406
+ // body: [{
407
+ // email: email,
408
+ // }],
409
+ // })
410
+ // .then(function (res) {
411
+ // assistant.log('Sucessfully added user to SendGrid list.')
412
+ // return resolve(res);
413
+ // })
414
+ // .catch(function (e) {
415
+ // assistant.log('Failed to add user to SendGrid list.', e)
416
+ // return resolve(e);
417
+ // })
418
+ });
419
+ }
420
+
337
421
  function addToMCList(key, listId, email) {
338
422
  return new Promise((resolve, reject) => {
339
423
  let datacenter = key.split('-')[1];