backend-manager 3.2.19 → 3.2.21

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.19",
3
+ "version": "3.2.21",
4
4
  "description": "Quick tools for developing Firebase functions",
5
5
  "main": "src/manager/index.js",
6
6
  "bin": {
@@ -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
 
@@ -122,17 +127,23 @@ Module.prototype.sendRateEmail = function (user) {
122
127
  template: 'd-b7f8da3c98ad49a2ad1e187f3a67b546',
123
128
  group: 25927,
124
129
  data: {
125
- title: 'Account deleted',
126
- message: `
127
- Your account at ${Manager.config.brand.name} has been deleted because you have signed up for too many accounts.
128
- <br>
129
- If you believe this is a mistake, please contact us at ${Manager.config.brand.email}.
130
- <br>
131
- <br>
132
- <strong>User Record</strong>:
133
- <br>
134
- ${JSON.stringify(user, null, 2)}
135
- `
130
+ email: {
131
+ preview: `You have signed up for too many accounts at ${Manager.config.brand.name}! Your account has been deleted.`,
132
+ },
133
+ body: {
134
+ title: `Account deleted`,
135
+ message: `
136
+ Your account at <strong>${Manager.config.brand.name}</strong> has been <strong>deleted</strong> because you have signed up for too many accounts.
137
+ <br>
138
+ <br>
139
+ If you believe this is a mistake, please contact us at ${Manager.config.brand.email}.
140
+ <br>
141
+ <br>
142
+ <strong>User Record</strong>:
143
+ <br>
144
+ <pre><code>${JSON.stringify(user, null, 2)}</code></pre>
145
+ `,
146
+ },
136
147
  },
137
148
  },
138
149
  })
@@ -147,53 +158,72 @@ Module.prototype.sendRateEmail = function (user) {
147
158
  });
148
159
  }
149
160
 
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
- // }
161
+ Module.prototype.sendWelcomeEmail = function (user) {
162
+ const self = this;
163
+
164
+ return new Promise(async function(resolve, reject) {
165
+ const Manager = self.Manager;
166
+ const assistant = self.assistant;
167
+
168
+ // Send email
169
+ fetch(`https://us-central1-itw-creative-works.cloudfunctions.net/sendEmail`, {
170
+ method: 'post',
171
+ response: 'json',
172
+ log: true,
173
+ body: {
174
+ backendManagerKey: Manager.config.backend_manager.key,
175
+ app: Manager.config.app.id,
176
+ to: {
177
+ email: user.auth.email,
178
+ },
179
+ categories: [`account/welcome-1`],
180
+ subject: `Welcome to ${Manager.config.brand.name}!`,
181
+ template: 'd-b7f8da3c98ad49a2ad1e187f3a67b546',
182
+ group: 25928,
183
+ data: {
184
+ email: {
185
+ preview: `Welcome to ${Manager.config.brand.name}! I'm Ian, the CEO. I'm here to help you get started.`,
186
+ },
187
+ body: {
188
+ title: `Welcome to ${Manager.config.brand.name}!`,
189
+ message: `
190
+ Hey there,
191
+ <br>
192
+ <br>
193
+ I'm Ian, the CEO of <strong>${Manager.config.brand.name}</strong>. I'm here to help you get started.
194
+ <br>
195
+ <br>
196
+ I'm thrilled to welcome you to <strong>${Manager.config.brand.name}</strong>! Your journey with us starts now, and we're eager to <strong>support you every step of the way</strong>.
197
+ <br>
198
+ <br>
199
+ As the founder and CEO, <strong>I'm personally committed to ensuring your experience with us is nothing short of exceptional</strong>. Should you need any assistance or have any questions, feel free to reply directly to this email. Our team and I are here to help.
200
+ <br>
201
+ <br>
202
+ Thanks for choosing <strong>${Manager.config.brand.name}</strong>. Here's to new beginnings!
203
+ <br>
204
+ <br>
205
+ `
206
+ },
207
+ personalization: {
208
+ type: 'personal',
209
+ image: null,
210
+ name: 'Ian Wiedenman, CEO',
211
+ url: 'https://ianwiedenman.com',
212
+ urlText: '@ianwieds',
213
+ },
214
+ },
215
+ },
216
+ })
217
+ .then(async (json) => {
218
+ assistant.log('sendEmail(): Success', json)
219
+ return resolve(json);
220
+ })
221
+ .catch(e => {
222
+ assistant.error('sendEmail(): Failed', e)
223
+ return resolve(e);
224
+ });
225
+ });
226
+ }
197
227
 
198
228
  Module.prototype.signUp = function (payload) {
199
229
  const self = this;
@@ -214,7 +244,7 @@ Module.prototype.signUp = function (payload) {
214
244
  assistant.log(`signUp(): payload`, payload)
215
245
 
216
246
  // Check if the user has a UID and email
217
- if (!_.get(payload, 'auth.uid', null) || !_.get(payload, 'auth.email', null)) {
247
+ if (!payload?.auth?.uid || !payload?.auth?.email) {
218
248
  return reject(new Error('Cannot create user without UID and email.'))
219
249
  }
220
250
 
@@ -245,7 +275,7 @@ Module.prototype.signUp = function (payload) {
245
275
  metadata: Manager.Metadata().set({tag: 'user:sign-up'}),
246
276
  }
247
277
 
248
- assistant.log(`signUp(): user`, user)
278
+ assistant.log(`signUp(): user`, user);
249
279
 
250
280
  // Set the user
251
281
  self.libraries.admin.firestore().doc(`users/${payload.auth.uid}`)
@@ -334,6 +364,78 @@ Module.prototype.updateReferral = function (payload) {
334
364
  });
335
365
  }
336
366
 
367
+ // addToSendGridList
368
+ Module.prototype.addToSendGridList = function (user) {
369
+ const self = this;
370
+
371
+ return new Promise(async function(resolve, reject) {
372
+ const Manager = self.Manager;
373
+ const assistant = self.assistant;
374
+ const Api = self.Api;
375
+
376
+ if (!user?.auth?.email) {
377
+ return reject(new Error('Cannot add user to SendGrid list without email.'))
378
+ }
379
+
380
+ // Add to SendGrid list
381
+ fetch('https://api.itwcreativeworks.com/wrapper', {
382
+ method: 'post',
383
+ response: 'json',
384
+ body: {
385
+ backendManagerKey: Manager.config.backend_manager.key,
386
+ service: 'sendgrid',
387
+ command: `/v3/marketing/contacts`,
388
+ method: `put`,
389
+ body: {
390
+ contacts: [
391
+ {
392
+ email: user?.auth?.email,
393
+ address_line_1: undefined,
394
+ address_line_2: undefined,
395
+ // alternate_emails: [],
396
+ city: user?.activity?.geolocation?.city,
397
+ country: user?.activity?.geolocation?.country,
398
+ first_name: undefined,
399
+ last_name: undefined,
400
+ postal_code: undefined,
401
+ state_province_region: user?.activity?.geolocation?.region,
402
+
403
+ custom_fields: {
404
+ app: Manager.config.app.id,
405
+ user: user?.auth?.uid,
406
+ },
407
+ },
408
+ ],
409
+ },
410
+ },
411
+ })
412
+ .then(function (res) {
413
+ assistant.log('Sucessfully added user to SendGrid list.')
414
+ return resolve(res);
415
+ })
416
+ .catch(function (e) {
417
+ assistant.log('Failed to add user to SendGrid list.', e)
418
+ return resolve(e);
419
+ })
420
+
421
+ // await self.libraries.sendgrid.request({
422
+ // method: 'post',
423
+ // url: `/v3/contactdb/recipients`,
424
+ // body: [{
425
+ // email: email,
426
+ // }],
427
+ // })
428
+ // .then(function (res) {
429
+ // assistant.log('Sucessfully added user to SendGrid list.')
430
+ // return resolve(res);
431
+ // })
432
+ // .catch(function (e) {
433
+ // assistant.log('Failed to add user to SendGrid list.', e)
434
+ // return resolve(e);
435
+ // })
436
+ });
437
+ }
438
+
337
439
  function addToMCList(key, listId, email) {
338
440
  return new Promise((resolve, reject) => {
339
441
  let datacenter = key.split('-')[1];