backend-manager 3.2.170 → 3.2.171

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.
Files changed (88) hide show
  1. package/LICENSE +1 -1
  2. package/README.md +1 -1
  3. package/dist/cli/cli.js +1534 -0
  4. package/dist/manager/functions/core/actions/api/admin/backup.js +338 -0
  5. package/dist/manager/functions/core/actions/api/admin/create-post.js +388 -0
  6. package/dist/manager/functions/core/actions/api/admin/cron.js +37 -0
  7. package/dist/manager/functions/core/actions/api/admin/database-read.js +35 -0
  8. package/dist/manager/functions/core/actions/api/admin/database-write.js +39 -0
  9. package/dist/manager/functions/core/actions/api/admin/edit-post.js +158 -0
  10. package/dist/manager/functions/core/actions/api/admin/firestore-query.js +165 -0
  11. package/dist/manager/functions/core/actions/api/admin/firestore-read.js +38 -0
  12. package/dist/manager/functions/core/actions/api/admin/firestore-write.js +54 -0
  13. package/dist/manager/functions/core/actions/api/admin/get-stats.js +269 -0
  14. package/dist/manager/functions/core/actions/api/admin/payment-processor.js +57 -0
  15. package/dist/manager/functions/core/actions/api/admin/run-hook.js +95 -0
  16. package/dist/manager/functions/core/actions/api/admin/send-notification.js +197 -0
  17. package/dist/manager/functions/core/actions/api/admin/sync-users.js +125 -0
  18. package/dist/manager/functions/core/actions/api/admin/templates/post.html +16 -0
  19. package/dist/manager/functions/core/actions/api/firebase/get-providers.js +102 -0
  20. package/dist/manager/functions/core/actions/api/general/emails/general:download-app-link.js +21 -0
  21. package/dist/manager/functions/core/actions/api/general/fetch-post.js +99 -0
  22. package/dist/manager/functions/core/actions/api/general/generate-uuid.js +41 -0
  23. package/dist/manager/functions/core/actions/api/general/send-email.js +112 -0
  24. package/dist/manager/functions/core/actions/api/handler/create-post.js +146 -0
  25. package/dist/manager/functions/core/actions/api/special/setup-electron-manager-client.js +103 -0
  26. package/dist/manager/functions/core/actions/api/template.js +33 -0
  27. package/dist/manager/functions/core/actions/api/test/authenticate.js +22 -0
  28. package/dist/manager/functions/core/actions/api/test/create-test-accounts.js +27 -0
  29. package/dist/manager/functions/core/actions/api/test/lab.js +55 -0
  30. package/dist/manager/functions/core/actions/api/test/redirect.js +26 -0
  31. package/dist/manager/functions/core/actions/api/test/webhook.js +30 -0
  32. package/dist/manager/functions/core/actions/api/user/create-custom-token.js +32 -0
  33. package/dist/manager/functions/core/actions/api/user/delete.js +68 -0
  34. package/dist/manager/functions/core/actions/api/user/get-active-sessions.js +45 -0
  35. package/dist/manager/functions/core/actions/api/user/get-subscription-info.js +49 -0
  36. package/dist/manager/functions/core/actions/api/user/oauth2/discord.js +114 -0
  37. package/dist/manager/functions/core/actions/api/user/oauth2/google.js +99 -0
  38. package/dist/manager/functions/core/actions/api/user/oauth2.js +476 -0
  39. package/dist/manager/functions/core/actions/api/user/regenerate-api-keys.js +54 -0
  40. package/dist/manager/functions/core/actions/api/user/resolve.js +32 -0
  41. package/dist/manager/functions/core/actions/api/user/sign-out-all-sessions.js +118 -0
  42. package/dist/manager/functions/core/actions/api/user/sign-up copy.js +544 -0
  43. package/dist/manager/functions/core/actions/api/user/sign-up.js +99 -0
  44. package/dist/manager/functions/core/actions/api/user/submit-feedback.js +96 -0
  45. package/dist/manager/functions/core/actions/api/user/validate-settings.js +86 -0
  46. package/dist/manager/functions/core/actions/api.js +354 -0
  47. package/dist/manager/functions/core/actions/create-post-handler.js +184 -0
  48. package/dist/manager/functions/core/actions/generate-uuid.js +62 -0
  49. package/dist/manager/functions/core/actions/sign-up-handler.js +205 -0
  50. package/dist/manager/functions/core/admin/create-post.js +206 -0
  51. package/dist/manager/functions/core/admin/firestore-write.js +72 -0
  52. package/dist/manager/functions/core/admin/get-stats.js +218 -0
  53. package/dist/manager/functions/core/admin/query.js +198 -0
  54. package/dist/manager/functions/core/admin/send-notification.js +206 -0
  55. package/dist/manager/functions/core/cron/daily/ghostii-auto-publisher.js +377 -0
  56. package/dist/manager/functions/core/cron/daily/reset-usage.js +197 -0
  57. package/dist/manager/functions/core/cron/daily.js +114 -0
  58. package/dist/manager/functions/core/events/auth/before-create.js +124 -0
  59. package/dist/manager/functions/core/events/auth/before-signin.js +62 -0
  60. package/dist/manager/functions/core/events/auth/on-create copy.js +121 -0
  61. package/dist/manager/functions/core/events/auth/on-create.js +564 -0
  62. package/dist/manager/functions/core/events/auth/on-delete.js +72 -0
  63. package/dist/manager/functions/core/events/firestore/on-subscription.js +107 -0
  64. package/dist/manager/functions/test/authenticate.js +38 -0
  65. package/dist/manager/functions/test/create-test-accounts.js +144 -0
  66. package/dist/manager/functions/test/webhook.js +37 -0
  67. package/dist/manager/functions/wrappers/mailchimp/addToList.js +25 -0
  68. package/dist/manager/helpers/analytics copy.js +217 -0
  69. package/dist/manager/helpers/analytics.js +467 -0
  70. package/dist/manager/helpers/api-manager.js +324 -0
  71. package/dist/manager/helpers/assistant.js +1043 -0
  72. package/dist/manager/helpers/metadata.js +32 -0
  73. package/dist/manager/helpers/middleware.js +154 -0
  74. package/dist/manager/helpers/roles.js +69 -0
  75. package/dist/manager/helpers/settings.js +158 -0
  76. package/dist/manager/helpers/subscription-resolver-new.js +828 -0
  77. package/dist/manager/helpers/subscription-resolver.js +842 -0
  78. package/dist/manager/helpers/usage.js +381 -0
  79. package/dist/manager/helpers/user.js +198 -0
  80. package/dist/manager/helpers/utilities.js +292 -0
  81. package/dist/manager/index.js +1076 -0
  82. package/dist/manager/libraries/openai.js +460 -0
  83. package/dist/manager/routes/restart/index.js +52 -0
  84. package/dist/manager/routes/test/index.js +43 -0
  85. package/dist/manager/schemas/restart.js +13 -0
  86. package/dist/manager/schemas/test.js +13 -0
  87. package/dist/require.js +3 -0
  88. package/package.json +18 -8
@@ -0,0 +1,112 @@
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
+ payload.data.payload.name = payload.data.payload.name;
20
+
21
+ const DEFAULT = {
22
+
23
+ spamFilter: {
24
+ ip: 3,
25
+ email: 3,
26
+ },
27
+ delay: 1,
28
+ payload: {
29
+ backendManagerKey: Manager.config.backend_manager.key,
30
+ app: Manager.config.app.id,
31
+ },
32
+ }
33
+
34
+ if (!payload.data.payload.id) {
35
+ return reject(assistant.errorify(`Parameter {id} is required.`, {code: 400}));
36
+ } else if (!payload.data.payload.email) {
37
+ return reject(assistant.errorify(`Parameter {email} is required.`, {code: 400}));
38
+ }
39
+
40
+ let emailPayload
41
+ try {
42
+ const script = require(path.join(__dirname, 'emails', `${payload.data.payload.id}.js`))
43
+ emailPayload = merge(
44
+ {},
45
+ DEFAULT,
46
+ script(payload.data.payload, Manager.config),
47
+ );
48
+ } catch (e) {
49
+ return reject(assistant.errorify(`${payload.data.payload.id} is not a valid email ID.`, {code: 400}));
50
+ }
51
+
52
+ const storage = Manager.storage({temporary: true});
53
+ const ipPath = ['api:general:send-email', 'ips', assistant.request.geolocation.ip];
54
+ const emailPath = ['api:general:send-email', 'emails', payload.data.payload.email];
55
+
56
+ const ipData = storage.get(ipPath).value() || {};
57
+ const emailData = storage.get(emailPath).value() || {};
58
+
59
+ ipData.count = (ipData.count || 0) + 1;
60
+ ipData.firstRequestTime = ipData.firstRequestTime ? ipData.firstRequestTime : new Date().toISOString();
61
+ ipData.lastRequestTime = new Date().toISOString();
62
+
63
+ emailData.count = (emailData.count || 0) + 1;
64
+ emailData.firstRequestTime = emailData.firstRequestTime ? emailData.firstRequestTime : new Date().toISOString();
65
+ emailData.lastRequestTime = new Date().toISOString();
66
+
67
+ storage.set(ipPath, ipData).write();
68
+ storage.set(emailPath, emailData).write();
69
+
70
+ assistant.log('Storage:', storage.getState()['api:general:send-email']);
71
+
72
+ if (ipData.count >= emailPayload.spamFilter.ip || emailData.count >= emailPayload.spamFilter.email) {
73
+ self.assistant.errorify(`Spam filter triggered ip=${ipData.count}, email=${emailData.count}`, {code: 429, log: true});
74
+
75
+ return resolve({data: {success: true}});
76
+ }
77
+
78
+ if (emailPayload.delay) {
79
+ // emailPayload.payload.sendAt = new Date(new Date().getTime() + (emailPayload.delay * 1000)).toISOString();
80
+ emailPayload.payload.sendAt = Math.round((new Date().getTime() + emailPayload.delay) / 1000);
81
+ }
82
+
83
+ // Log the email payload
84
+ assistant.log('Email payload:', emailPayload);
85
+
86
+ // Send the email
87
+ await fetch(`https://us-central1-itw-creative-works.cloudfunctions.net/sendEmail`, {
88
+ method: 'post',
89
+ response: 'json',
90
+ log: true,
91
+ body: emailPayload.payload,
92
+ })
93
+ .then(async (json) => {
94
+ assistant.log('Response:', json);
95
+
96
+ return resolve({
97
+ data: {
98
+ success: true,
99
+ }
100
+ });
101
+ })
102
+ .catch(e => {
103
+ return reject(assistant.errorify(`Error sending email: ${e}`, {code: 500, sentry: true}));
104
+ });
105
+
106
+ });
107
+
108
+ };
109
+
110
+
111
+ module.exports = Module;
112
+
@@ -0,0 +1,146 @@
1
+ const fetch = require('wonderful-fetch');
2
+ const _ = require('lodash')
3
+
4
+ function Module() {
5
+
6
+ }
7
+
8
+ Module.prototype.main = function () {
9
+ const self = this;
10
+ const Manager = self.Manager;
11
+ const Api = self.Api;
12
+ const assistant = self.assistant;
13
+ const payload = self.payload;
14
+
15
+ return new Promise(async function(resolve, reject) {
16
+
17
+ if (!payload.user.roles.admin) {
18
+ return reject(assistant.errorify(`Admin required.`, {code: 401}));
19
+ }
20
+
21
+ payload.response.data = {
22
+ invoice: {
23
+ success: false,
24
+ data: {},
25
+ },
26
+ notification: {
27
+ success: false,
28
+ data: {},
29
+ }
30
+ }
31
+
32
+ const postSlug = `/blog/${payload.data.payload.url}`;
33
+ const postUrl = `${Manager.config.brand.url}${postSlug}`;
34
+ const invoiceNote = `GP to ${Manager.config.brand.name} \nSlug: ${postSlug} \n\n${payload.data.payload.invoiceNote || ''}`
35
+
36
+ if (payload.data.payload.invoiceEmail && payload.data.payload.invoicePrice) {
37
+ // Create invoice
38
+ const createdInvoice = await fetch('https://us-central1-itw-creative-works.cloudfunctions.net/wrapper', {
39
+ method: 'POST',
40
+ response: 'json',
41
+ body: {
42
+ authenticationToken: Manager.config.backend_manager.key,
43
+ method: 'post',
44
+ service: 'paypal',
45
+ command: 'v2/invoicing/invoices',
46
+ body: {
47
+ detail: {
48
+ currency_code: 'USD',
49
+ // note: `Post to ${Manager.config.brand.name} \n ${payload.data.payload.invoiceNote || ''}`,
50
+ // note: `GP to ${Manager.config.brand.name} \n\n${payload.data.payload.invoiceNote || '' }`,
51
+ // memo: `GP to ${Manager.config.brand.name} \nSlug: ${postSlug}`,
52
+ note: invoiceNote,
53
+ memo: invoiceNote,
54
+ },
55
+ primary_recipients: [
56
+ {
57
+ billing_info: {
58
+ email_address: payload.data.payload.invoiceEmail,
59
+ },
60
+ }
61
+ ],
62
+ items: [
63
+ {
64
+ // name: `Guest Post`,
65
+ name: `GP`,
66
+ // description: `Post URL: ${postUrl}`,
67
+ description: `Slug: ${postSlug}`,
68
+ quantity: '1',
69
+ unit_amount: {
70
+ currency_code: 'USD',
71
+ value: `${payload.data.payload.invoicePrice}`
72
+ },
73
+ // discount: {
74
+ // percent: '5'
75
+ // },
76
+ unit_of_measure: 'QUANTITY',
77
+ },
78
+ ],
79
+ }
80
+ },
81
+ })
82
+ .then(response => response)
83
+ .catch(e => e);
84
+
85
+ if (createdInvoice instanceof Error) {
86
+ return reject(assistant.errorify(createdInvoice, {code: 400}));
87
+ }
88
+
89
+ // Send invoice
90
+ const createdInvoiceId = _.get(createdInvoice, 'href', '').split('/').pop();
91
+ const sentInvoice = await fetch('https://us-central1-itw-creative-works.cloudfunctions.net/wrapper', {
92
+ method: 'POST',
93
+ response: 'json',
94
+ body: {
95
+ authenticationToken: Manager.config.backend_manager.key,
96
+ service: 'paypal',
97
+ command: `v2/invoicing/invoices/${createdInvoiceId}/send`,
98
+ method: 'post',
99
+ body: {
100
+ }
101
+ },
102
+ })
103
+ .then(response => response)
104
+ .catch(e => e);
105
+
106
+ if (sentInvoice instanceof Error) {
107
+ return reject(assistant.errorify(sentInvoice, {code: 500}));
108
+ }
109
+
110
+ payload.response.data.invoice = {
111
+ success: true,
112
+ data: sentInvoice,
113
+ }
114
+
115
+ }
116
+
117
+ // Send notification
118
+ if (payload.data.payload.sendNotification !== false) {
119
+ const sentNotification = fetch(`https://us-central1-${Manager.project.projectId}.cloudfunctions.net/bm_api`, {
120
+ method: 'POST',
121
+ response: 'json',
122
+ body: {
123
+ authenticationToken: Manager.config.backend_manager.key,
124
+ command: `admin:send-notification`,
125
+ payload: {
126
+ title: payload.data.payload.title,
127
+ body: `"${payload.data.payload.title}" was just published on our blog. It's a great read and we think you'll enjoy the content!`,
128
+ // click_action: `${Manager.config.brand.url}/${postUrl}`,
129
+ click_action: `${Manager.config.brand.url}/blog`,
130
+ icon: Manager.config.brand.brandmark,
131
+ }
132
+ },
133
+ })
134
+ .then(response => response)
135
+ .catch(e => e);
136
+ }
137
+
138
+ return resolve({data: payload.response.data})
139
+
140
+
141
+ });
142
+
143
+ };
144
+
145
+
146
+ module.exports = Module;
@@ -0,0 +1,103 @@
1
+ function Module() {
2
+
3
+ }
4
+
5
+ Module.prototype.main = function () {
6
+ const self = this;
7
+ const Manager = self.Manager;
8
+ const Api = self.Api;
9
+ const assistant = self.assistant;
10
+ const payload = self.payload;
11
+
12
+ return new Promise(async function(resolve, reject) {
13
+
14
+ const fetch = Manager.require('wonderful-fetch');
15
+ const _ = Manager.require('lodash');
16
+
17
+ let uid = payload.data.payload.uid;
18
+ const app = payload.data.payload.appId || payload.data.payload.app || Manager.config.app.id;
19
+ let config = payload.data.payload.config || {};
20
+
21
+ let uuid = null;
22
+ let error;
23
+
24
+ let signInToken = null;
25
+
26
+ if (payload.data.authenticationToken || payload.data.backendManagerKey) {
27
+ await self.Api.resolveUser({adminRequired: true})
28
+ .then(async (user) => {
29
+ uid = _.get(user, 'auth.uid', null);
30
+ await self.libraries.admin.auth().createCustomToken(uid)
31
+ .then(token => {
32
+ signInToken = token;
33
+ })
34
+ .catch(e => {
35
+ error = assistant.errorify(`Failed to create custom token: ${e}`, {code: 500});
36
+ })
37
+ })
38
+ .catch(e => {
39
+ assistant.errorify(`Failed to resolve user: ${e}`, {code: 500, log: true});
40
+ })
41
+
42
+ if (error) {
43
+ return reject(error)
44
+ }
45
+ }
46
+
47
+ // Generate uuid
48
+ if (uid) {
49
+ await Api.import('general:generate-uuid', {version: 5, name: uid})
50
+ .then(library => {
51
+
52
+ library.main()
53
+ .then(result => {
54
+ uuid = result.data.uuid;
55
+ })
56
+ .catch(e => {
57
+ error = e;
58
+ })
59
+ })
60
+ }
61
+
62
+ if (error) {
63
+ return reject(error)
64
+ }
65
+
66
+ if (config.backendManagerKey === Manager.config.backend_manager.key && Manager.config.backend_manager.key) {
67
+ assistant.log('Validated config', config)
68
+ } else {
69
+ config = {};
70
+ }
71
+
72
+ // Fetch app details
73
+ await fetch('https://us-central1-itw-creative-works.cloudfunctions.net/getApp', {
74
+ method: 'post',
75
+ timeout: 30000,
76
+ tries: 3,
77
+ response: 'json',
78
+ body: {
79
+ id: app,
80
+ },
81
+ })
82
+ .then(result => {
83
+ return resolve({
84
+ data: {
85
+ uuid: uuid,
86
+ signInToken: signInToken,
87
+ timestamp: new Date().toISOString(),
88
+ ip: assistant.request.geolocation.ip,
89
+ country: assistant.request.geolocation.country,
90
+ app: result,
91
+ config: config,
92
+ }
93
+ });
94
+ })
95
+ .catch(e => {
96
+ return reject(assistant.errorify(`Error fetching app details: ${e}`, {code: 500}));
97
+ })
98
+
99
+ });
100
+
101
+ };
102
+
103
+ module.exports = Module;
@@ -0,0 +1,33 @@
1
+ function Module() {
2
+
3
+ }
4
+
5
+ Module.prototype.main = function () {
6
+ const self = this;
7
+ const Manager = self.Manager;
8
+ const Api = self.Api;
9
+ const assistant = self.assistant;
10
+ const payload = self.payload;
11
+
12
+ return new Promise(async function(resolve, reject) {
13
+
14
+ if (!payload.user.roles.admin) {
15
+ return reject(assistant.errorify(`Admin required.`, {code: 401}));
16
+ }
17
+
18
+ // self.Api.resolveUser({adminRequired: false})
19
+ // .then(async (user) => {
20
+ //
21
+ // })
22
+ // .catch(e => {
23
+ // return reject(e);
24
+ // })
25
+
26
+ return resolve({data: {success: true}});
27
+
28
+ });
29
+
30
+ };
31
+
32
+
33
+ module.exports = Module;
@@ -0,0 +1,22 @@
1
+ function Module() {
2
+
3
+ }
4
+
5
+ Module.prototype.main = function () {
6
+ const self = this;
7
+ const Manager = self.Manager;
8
+ const Api = self.Api;
9
+ const assistant = self.assistant;
10
+ const payload = self.payload;
11
+
12
+ return new Promise(async function(resolve, reject) {
13
+
14
+ assistant.log('User:', payload.user);
15
+
16
+ return resolve({data: {user: payload.user}});
17
+ });
18
+
19
+ };
20
+
21
+
22
+ module.exports = Module;
@@ -0,0 +1,27 @@
1
+ function Module() {
2
+
3
+ }
4
+
5
+ Module.prototype.main = function () {
6
+ const self = this;
7
+ const Manager = self.Manager;
8
+ const Api = self.Api;
9
+ const assistant = self.assistant;
10
+ const payload = self.payload;
11
+
12
+ return new Promise(async function(resolve, reject) {
13
+
14
+ if (!payload.user.roles.admin) {
15
+ return reject(assistant.errorify(`Admin required.`, {code: 401}));
16
+ }
17
+
18
+ assistant.log('User:', payload.user);
19
+
20
+ return resolve({data: {user: user}});
21
+
22
+ });
23
+
24
+ };
25
+
26
+
27
+ module.exports = Module;
@@ -0,0 +1,55 @@
1
+ const fetch = require('node-fetch');
2
+ const _ = require('lodash')
3
+
4
+ function Module() {
5
+
6
+ }
7
+
8
+ Module.prototype.main = function () {
9
+ const self = this;
10
+ const Manager = self.Manager;
11
+ const Api = self.Api;
12
+ const assistant = self.assistant;
13
+ const payload = self.payload;
14
+
15
+ return new Promise(async function(resolve, reject) {
16
+
17
+ if (!payload.user.roles.admin) {
18
+ self.assistant.log('User is not admin')
19
+ }
20
+
21
+ const url = 'https://itwcreativeworks.com';
22
+ const title = 'https://itwcreativeworks.com';
23
+ const icon = 'https://cdn.itwcreativeworks.com/assets/itw-creative-works/images/socials/itw-creative-works-brandmark-square-black-1024x1024.png?cb=1651834176';
24
+
25
+
26
+ await Api.import('admin:send-notification', {
27
+ title: 'New blog post!',
28
+ click_action: url,
29
+ body: `"${title}" was just published on our blog. It's a great read and we think you'll enjoy the content!`,
30
+ icon: icon,
31
+ })
32
+ .then(library => {
33
+
34
+ console.log('----');
35
+
36
+ library.main()
37
+ .then(result => {
38
+ return resolve({data: {success: true}});
39
+ })
40
+ .catch(e => {
41
+ console.error('Error', e);
42
+ })
43
+
44
+ })
45
+
46
+ // if (error) {
47
+ //
48
+ // }
49
+
50
+ });
51
+
52
+ };
53
+
54
+
55
+ module.exports = Module;
@@ -0,0 +1,26 @@
1
+ const uuid = require('uuid');
2
+
3
+ function Module() {
4
+
5
+ }
6
+
7
+ Module.prototype.main = function () {
8
+ const self = this;
9
+ const Manager = self.Manager;
10
+ const Api = self.Api;
11
+ const assistant = self.assistant;
12
+ const payload = self.payload;
13
+
14
+ return new Promise(async function(resolve, reject) {
15
+
16
+ payload.data.payload.url = payload.data.payload.url || 'https://itwcreativeworks.com'
17
+
18
+ assistant.log('Redirecting', payload.data.payload.url);
19
+
20
+ return resolve({redirect: payload.data.payload.url});
21
+ });
22
+
23
+ };
24
+
25
+
26
+ module.exports = Module;
@@ -0,0 +1,30 @@
1
+ function Module() {
2
+
3
+ }
4
+
5
+ Module.prototype.main = function () {
6
+ const self = this;
7
+ const Manager = self.Manager;
8
+ const Api = self.Api;
9
+ const assistant = self.assistant;
10
+ const payload = self.payload;
11
+ const powertools = Manager.require('node-powertools');
12
+
13
+ return new Promise(async function(resolve, reject) {
14
+
15
+ if (payload.data.payload.delay > 0) {
16
+ await powertools.wait(payload.data.payload.delay)
17
+ }
18
+
19
+ if (payload.data.payload.status >= 200 && payload.data.payload.status <= 299) {
20
+ return resolve({data: payload.data.payload.response, status: payload.data.payload.status});
21
+ } else if (payload.data.payload.status >= 400 && payload.data.payload.status <= 599) {
22
+ return reject(assistant.errorify(payload.data.payload.response || 'Unknown error message provided', {code: payload.data.payload.status}));
23
+ }
24
+
25
+ });
26
+
27
+ };
28
+
29
+
30
+ module.exports = Module;
@@ -0,0 +1,32 @@
1
+ const _ = require('lodash')
2
+
3
+ function Module() {
4
+
5
+ }
6
+
7
+ Module.prototype.main = function () {
8
+ const self = this;
9
+ const Manager = self.Manager;
10
+ const Api = self.Api;
11
+ const assistant = self.assistant;
12
+ const payload = self.payload;
13
+
14
+ return new Promise(async function(resolve, reject) {
15
+ Api.resolveUser({adminRequired: true})
16
+ .then(async (user) => {
17
+ await self.libraries.admin.auth().createCustomToken(_.get(user, 'auth.uid', null))
18
+ .then(token => {
19
+ return resolve({data: {token: token}});
20
+ })
21
+ .catch(e => {
22
+ return reject(assistant.errorify(`Failed to create custom token: ${e}`, {code: 400}));
23
+ })
24
+ })
25
+ .catch(e => {
26
+ return reject(e);
27
+ })
28
+ });
29
+ };
30
+
31
+
32
+ module.exports = Module;
@@ -0,0 +1,68 @@
1
+ const _ = require('lodash');
2
+ const fetch = require('wonderful-fetch');
3
+
4
+ function Module() {
5
+
6
+ }
7
+
8
+ Module.prototype.main = function () {
9
+ const self = this;
10
+ const Manager = self.Manager;
11
+ const Api = self.Api;
12
+ const assistant = self.assistant;
13
+ const payload = self.payload;
14
+
15
+ return new Promise(async function(resolve, reject) {
16
+ Api.resolveUser({adminRequired: true})
17
+ .then(async (user) => {
18
+ const uid = user?.auth?.uid;
19
+
20
+ // Disallow deleting users with subscriptions in any state other than cancelled or active payments
21
+ if (
22
+ (user?.plan?.status && user?.plan?.status !== 'cancelled')
23
+ || user?.plan?.payment?.active
24
+ ) {
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}));
26
+ }
27
+
28
+ // Signout of all sessions
29
+ assistant.log(`Signout of all sessions...`);
30
+ await fetch(`https://us-central1-${self.Manager.project.projectId}.cloudfunctions.net/bm_api`, {
31
+ method: 'post',
32
+ timeout: 30000,
33
+ response: 'json',
34
+ tries: 2,
35
+ log: true,
36
+ body: {
37
+ backendManagerKey: self.Manager.config.backend_manager.key,
38
+ command: 'user:sign-out-all-sessions',
39
+ payload: {
40
+ uid: uid,
41
+ }
42
+ },
43
+ })
44
+ .then((json) => {
45
+ assistant.log(`Signout of all sessions success`, json);
46
+ })
47
+ .catch(e => {
48
+ assistant.error(`Signout of all sessions failed`, e);
49
+ })
50
+
51
+ // Perform the delete
52
+ await self.libraries.admin.auth().deleteUser(uid)
53
+ .then(() => {
54
+ return resolve({data: {success: true}});
55
+ })
56
+ .catch(e => {
57
+ return reject(assistant.errorify(`Failed to delete user: ${e}`, {code: 400}));
58
+ })
59
+ })
60
+ .catch(e => {
61
+ return reject(e);
62
+ })
63
+ });
64
+
65
+ };
66
+
67
+
68
+ module.exports = Module;