backend-manager 5.0.70 → 5.0.72

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/README.md CHANGED
@@ -204,10 +204,7 @@ Route.prototype.main = async function (assistant) {
204
204
  }
205
205
 
206
206
  // Track analytics
207
- analytics.event({
208
- name: 'my_event',
209
- params: { action: 'test' },
210
- });
207
+ analytics.event('my_event', { action: 'test' });
211
208
 
212
209
  // Validate usage limits
213
210
  await usage.validate('requests');
@@ -495,13 +492,10 @@ const analytics = Manager.Analytics({
495
492
  uuid: user.auth.uid,
496
493
  });
497
494
 
498
- analytics.event({
499
- name: 'purchase',
500
- params: {
501
- item_id: 'product-123',
502
- value: 29.99,
503
- currency: 'USD',
504
- },
495
+ analytics.event('purchase', {
496
+ item_id: 'product-123',
497
+ value: 29.99,
498
+ currency: 'USD',
505
499
  });
506
500
  ```
507
501
 
@@ -59,3 +59,8 @@ Firestore trigger for payments-webhooks/{id}
59
59
  * process the webhook data and update the user's subscription data in both their user doc and their subscription doc
60
60
  * various checks like
61
61
  * if status === completed, do nothing
62
+
63
+ THen, we need to plan an effective way to test all of these scenarios in our emualted testing environment which you can explore here: /Users/ian/Developer/Repositories/ITW-Creative-Works/backend-manager/src/test
64
+ * we should be able to START with certain subscripton levels (basic/free, premium etc) and then see how events influence and change the subscription status and data in the user doc, subscription doc, webhook event doc, etc
65
+
66
+ So webhook comes in --> save immediateyl to return 200 to the payment provider --> process the webhook in a separate function trigger to update subscription data and user access (reprocess if something goes wrong, etc)
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "backend-manager",
3
- "version": "5.0.70",
3
+ "version": "5.0.72",
4
4
  "description": "Quick tools for developing Firebase functions",
5
5
  "main": "src/manager/index.js",
6
6
  "bin": {
@@ -62,10 +62,7 @@ module.exports = async ({ Manager, assistant, user, context, libraries }) => {
62
62
  Manager.Analytics({
63
63
  assistant: assistant,
64
64
  uuid: user.uid,
65
- }).event({
66
- name: 'user_delete',
67
- params: {},
68
- });
65
+ }).event('user_delete', {});
69
66
 
70
67
  assistant.log(`onDelete: Completed for ${user.uid} (${Date.now() - startTime}ms)`);
71
68
  };
@@ -42,10 +42,7 @@ module.exports = async ({ Manager, assistant, change, context, libraries }) => {
42
42
  Manager.Analytics({
43
43
  assistant: assistant,
44
44
  uuid: dataBefore?.owner?.uid,
45
- }).event({
46
- name: 'notification-unsubscribe',
47
- params: {},
48
- });
45
+ }).event('notification-unsubscribe', {});
49
46
 
50
47
  assistant.log('Notification subscription deleted:', dataBefore);
51
48
 
@@ -67,10 +64,7 @@ module.exports = async ({ Manager, assistant, change, context, libraries }) => {
67
64
  Manager.Analytics({
68
65
  assistant: assistant,
69
66
  uuid: dataAfter?.owner?.uid,
70
- }).event({
71
- name: 'notification-subscribe',
72
- params: {},
73
- });
67
+ }).event('notification-subscribe', {});
74
68
 
75
69
  assistant.log('Notification subscription created:', dataAfter);
76
70
 
@@ -212,7 +212,7 @@ Analytics.prototype.generateId = function (id) {
212
212
  : undefined;
213
213
  };
214
214
 
215
- Analytics.prototype.event = function (payload) {
215
+ Analytics.prototype.event = function (payload, params) {
216
216
  const self = this;
217
217
  const Manager = self.Manager;
218
218
  const assistant = self.assistant;
@@ -225,6 +225,10 @@ Analytics.prototype.event = function (payload) {
225
225
  // https://support.google.com/analytics/answer/13316687?hl=en#zippy=%2Cweb
226
226
  // https://support.google.com/analytics/answer/9268042?sjid=4476481583372132143-NC
227
227
  // https://developers.google.com/analytics/devguides/collection/protocol/ga4/reference/events#screen_view
228
+ // Support both: event({ name, params }) and event('name', params)
229
+ if (typeof payload === 'string') {
230
+ payload = { name: payload, params: params || {} };
231
+ }
228
232
  payload = payload || {};
229
233
 
230
234
  // Fix event name
@@ -314,12 +314,7 @@ Manager.prototype.init = function (exporter, options) {
314
314
  assistant: self.assistant,
315
315
  uuid: self.SERVER_UUID,
316
316
  })
317
- .event({
318
- name: 'admin/initialized',
319
- params: {
320
- // screen_class: 'MainActivity',
321
- },
322
- });
317
+ .event('admin/initialized', {});
323
318
 
324
319
  // Return
325
320
  return self;
@@ -22,10 +22,7 @@ module.exports = async ({ assistant, settings, analytics }) => {
22
22
  : uuid.v4();
23
23
 
24
24
  // Send analytics event
25
- analytics.event({
26
- name: 'general/uuid',
27
- params: { version },
28
- });
25
+ analytics.event('general/uuid', { version });
29
26
 
30
27
  // Log and respond
31
28
  assistant.log('UUID Generated', { name, version, namespace, result });
@@ -3,10 +3,7 @@ const jetpack = require('fs-jetpack');
3
3
  module.exports = async ({ assistant, user, analytics }) => {
4
4
 
5
5
  // Send analytics event
6
- analytics.event({
7
- name: 'restart',
8
- params: {},
9
- });
6
+ analytics.event('restart', {});
10
7
 
11
8
  // Require authentication
12
9
  if (!user.authenticated) {
@@ -1,10 +1,7 @@
1
1
  module.exports = async ({ assistant, analytics }) => {
2
2
 
3
3
  // Send analytics event
4
- analytics.event({
5
- name: 'test',
6
- params: {},
7
- });
4
+ analytics.event('test', {});
8
5
 
9
6
  // Log
10
7
  assistant.log('Running test');
@@ -0,0 +1,56 @@
1
+ const fetch = require('wonderful-fetch');
2
+
3
+ module.exports = {
4
+ provider: 'spotify',
5
+ name: 'Spotify',
6
+ urls: {
7
+ authorize: 'https://accounts.spotify.com/authorize',
8
+ tokenize: 'https://accounts.spotify.com/api/token',
9
+ refresh: 'https://accounts.spotify.com/api/token',
10
+ revoke: '',
11
+ status: '',
12
+ removeAccess: 'https://www.spotify.com/account/apps/',
13
+ },
14
+ scope: ['user-read-email', 'user-read-private'],
15
+
16
+ // Spotify doesn't need special auth params
17
+ authParams: {},
18
+
19
+ // Spotify does not support token revocation
20
+ async revokeToken(token, context) {
21
+ const { assistant } = context;
22
+
23
+ assistant.log('Spotify does not support token revocation');
24
+
25
+ return { revoked: false, reason: 'Spotify does not support token revocation' };
26
+ },
27
+
28
+ async verifyIdentity(tokenizeResult, Manager, assistant) {
29
+ assistant.log('verifyIdentity(): tokenizeResult', tokenizeResult);
30
+
31
+ // Get identity from Spotify API
32
+ const identityResponse = await fetch('https://api.spotify.com/v1/me', {
33
+ timeout: 60000,
34
+ response: 'json',
35
+ tries: 1,
36
+ log: true,
37
+ cacheBreaker: false,
38
+ headers: {
39
+ authorization: `${tokenizeResult.token_type} ${tokenizeResult.access_token}`,
40
+ },
41
+ });
42
+
43
+ assistant.log('verifyIdentity(): identityResponse', identityResponse);
44
+
45
+ // Check if exists
46
+ const snap = await Manager.libraries.admin.firestore().collection('users')
47
+ .where('oauth2.spotify.identity.id', '==', identityResponse.id)
48
+ .get();
49
+
50
+ if (snap.size > 0) {
51
+ throw new Error(`This Spotify account is already connected to a ${Manager.config.brand.name} account`);
52
+ }
53
+
54
+ return identityResponse;
55
+ },
56
+ };