backend-manager 2.0.2 → 2.0.5

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.0.2",
3
+ "version": "2.0.5",
4
4
  "description": "Quick tools for developing Firebase functions",
5
5
  "main": "src/manager/index.js",
6
6
  "bin": {
@@ -31,7 +31,7 @@
31
31
  "@google-cloud/storage": "^5.19.2",
32
32
  "@sendgrid/mail": "^7.6.2",
33
33
  "@sentry/node": "^6.19.6",
34
- "backend-assistant": "^0.0.58",
34
+ "backend-assistant": "^0.0.59",
35
35
  "busboy": "^0.3.1",
36
36
  "chalk": "^4.1.2",
37
37
  "cors": "^2.8.5",
@@ -64,4 +64,4 @@
64
64
  "src/",
65
65
  "templates/"
66
66
  ]
67
- }
67
+ }
@@ -9,6 +9,7 @@ function Module() {
9
9
 
10
10
  Module.prototype.init = async function (s, payload) {
11
11
  const self = this;
12
+ self.Api = s;
12
13
  self.Manager = s.Manager;
13
14
  self.libraries = s.Manager.libraries;
14
15
  self.assistant = s.assistant;
@@ -7,6 +7,7 @@ function Module() {
7
7
 
8
8
  Module.prototype.init = async function (s, payload) {
9
9
  const self = this;
10
+ self.Api = s;
10
11
  self.Manager = s.Manager;
11
12
  self.libraries = s.Manager.libraries;
12
13
  self.assistant = s.Manager.assistant;
@@ -4,6 +4,7 @@ function Module() {
4
4
 
5
5
  Module.prototype.init = async function (s, payload) {
6
6
  const self = this;
7
+ self.Api = s;
7
8
  self.Manager = s.Manager;
8
9
  self.libraries = s.Manager.libraries;
9
10
  self.assistant = s.Manager.assistant;
@@ -4,6 +4,7 @@ function Module() {
4
4
 
5
5
  Module.prototype.init = async function (s, payload) {
6
6
  const self = this;
7
+ self.Api = s;
7
8
  self.Manager = s.Manager;
8
9
  self.libraries = s.Manager.libraries;
9
10
  self.assistant = s.Manager.assistant;
@@ -4,6 +4,7 @@ function Module() {
4
4
 
5
5
  Module.prototype.init = async function (s, payload) {
6
6
  const self = this;
7
+ self.Api = s;
7
8
  self.Manager = s.Manager;
8
9
  self.libraries = s.Manager.libraries;
9
10
  self.assistant = s.Manager.assistant;
@@ -6,6 +6,7 @@ function Module() {
6
6
 
7
7
  Module.prototype.init = async function (s, payload) {
8
8
  const self = this;
9
+ self.Api = s;
9
10
  self.Manager = s.Manager;
10
11
  self.libraries = s.Manager.libraries;
11
12
  self.assistant = s.Manager.assistant;
@@ -8,6 +8,7 @@ function Module() {
8
8
 
9
9
  Module.prototype.init = async function (s, payload) {
10
10
  const self = this;
11
+ self.Api = s;
11
12
  self.Manager = s.Manager;
12
13
  self.libraries = s.Manager.libraries;
13
14
  self.assistant = s.assistant;
@@ -6,6 +6,7 @@ function Module() {
6
6
 
7
7
  Module.prototype.init = async function (s, payload) {
8
8
  const self = this;
9
+ self.Api = s;
9
10
  self.Manager = s.Manager;
10
11
  self.libraries = s.Manager.libraries;
11
12
  self.assistant = s.Manager.assistant;
@@ -39,7 +40,7 @@ Module.prototype.main = function () {
39
40
 
40
41
  assistant.log('UUID Generated', payload.data.payload, result, {environment: 'production'});
41
42
 
42
- return resolve({data: {uuid: result}});
43
+ return resolve({data: {uuid: result, timestamp: new Date().toISOString()}});
43
44
 
44
45
  });
45
46
 
@@ -7,6 +7,7 @@ function Module() {
7
7
 
8
8
  Module.prototype.init = async function (s, payload) {
9
9
  const self = this;
10
+ self.Api = s;
10
11
  self.Manager = s.Manager;
11
12
  self.libraries = s.Manager.libraries;
12
13
  self.assistant = s.Manager.assistant;
@@ -4,6 +4,7 @@ function Module() {
4
4
 
5
5
  Module.prototype.init = async function (s, payload) {
6
6
  const self = this;
7
+ self.Api = s;
7
8
  self.Manager = s.Manager;
8
9
  self.libraries = s.Manager.libraries;
9
10
  self.assistant = s.Manager.assistant;
@@ -24,6 +25,14 @@ Module.prototype.main = function () {
24
25
  return reject(assistant.errorManager(`Admin required.`, {code: 401, sentry: false, send: false, log: false}).error)
25
26
  }
26
27
 
28
+ // self.Api.resolveUser({adminRequired: false})
29
+ // .then(async (user) => {
30
+ //
31
+ // })
32
+ // .catch(e => {
33
+ // return reject(e);
34
+ // })
35
+
27
36
  return resolve({data: {success: true}});
28
37
 
29
38
  });
@@ -4,6 +4,7 @@ function Module() {
4
4
 
5
5
  Module.prototype.init = async function (s, payload) {
6
6
  const self = this;
7
+ self.Api = s;
7
8
  self.Manager = s.Manager;
8
9
  self.libraries = s.Manager.libraries;
9
10
  self.assistant = s.Manager.assistant;
@@ -4,6 +4,7 @@ function Module() {
4
4
 
5
5
  Module.prototype.init = async function (s, payload) {
6
6
  const self = this;
7
+ self.Api = s;
7
8
  self.Manager = s.Manager;
8
9
  self.libraries = s.Manager.libraries;
9
10
  self.assistant = s.Manager.assistant;
@@ -4,6 +4,7 @@ function Module() {
4
4
 
5
5
  Module.prototype.init = async function (s, payload) {
6
6
  const self = this;
7
+ self.Api = s;
7
8
  self.Manager = s.Manager;
8
9
  self.libraries = s.Manager.libraries;
9
10
  self.assistant = s.Manager.assistant;
@@ -6,6 +6,7 @@ function Module() {
6
6
 
7
7
  Module.prototype.init = async function (s, payload) {
8
8
  const self = this;
9
+ self.Api = s;
9
10
  self.Manager = s.Manager;
10
11
  self.libraries = s.Manager.libraries;
11
12
  self.assistant = s.Manager.assistant;
@@ -21,31 +22,8 @@ Module.prototype.main = function () {
21
22
  const payload = self.payload;
22
23
 
23
24
  return new Promise(async function(resolve, reject) {
24
- let user = null;
25
- if (payload.user.roles.admin && payload.data.payload.uid) {
26
- await self.libraries.admin.firestore().doc(`users/${payload.data.payload.uid}`)
27
- .get()
28
- .then(async function (doc) {
29
- const data = doc.data();
30
- if (data) {
31
- user = data;
32
- } else {
33
- throw new Error('User does not exist')
34
- }
35
- })
36
- .catch(function (e) {
37
- user = e;
38
- })
39
- } else if (payload.user.authenticated) {
40
- user = payload.user;
41
- }
42
-
43
- if (user instanceof Error) {
44
- return reject(assistant.errorManager(user, {code: 400, sentry: false, send: false, log: false}).error)
45
- } else if (!user) {
46
- return reject(assistant.errorManager(`Admin or authenticated user required.`, {code: 401, sentry: false, send: false, log: false}).error)
47
- } else {
48
-
25
+ self.Api.resolveUser({adminRequired: true})
26
+ .then(async (user) => {
49
27
  await self.libraries.admin.auth().createCustomToken(_.get(user, 'auth.uid', null))
50
28
  .then(token => {
51
29
  return resolve({data: {token: token}});
@@ -53,10 +31,11 @@ Module.prototype.main = function () {
53
31
  .catch(e => {
54
32
  return reject(assistant.errorManager(`Failed to create custom token: ${e}`, {code: 400, sentry: false, send: false, log: false}).error)
55
33
  })
56
- }
57
-
34
+ })
35
+ .catch(e => {
36
+ return reject(e);
37
+ })
58
38
  });
59
-
60
39
  };
61
40
 
62
41
 
@@ -6,6 +6,7 @@ function Module() {
6
6
 
7
7
  Module.prototype.init = async function (s, payload) {
8
8
  const self = this;
9
+ self.Api = s;
9
10
  self.Manager = s.Manager;
10
11
  self.libraries = s.Manager.libraries;
11
12
  self.assistant = s.Manager.assistant;
@@ -21,30 +22,8 @@ Module.prototype.main = function () {
21
22
  const payload = self.payload;
22
23
 
23
24
  return new Promise(async function(resolve, reject) {
24
- let user = null;
25
- if (payload.user.roles.admin && payload.data.payload.uid) {
26
- await self.libraries.admin.firestore().doc(`users/${payload.data.payload.uid}`)
27
- .get()
28
- .then(async function (doc) {
29
- const data = doc.data();
30
- if (data) {
31
- user = data;
32
- } else {
33
- throw new Error('User does not exist')
34
- }
35
- })
36
- .catch(function (e) {
37
- user = e;
38
- })
39
- } else if (payload.user.authenticated) {
40
- user = payload.user;
41
- }
42
-
43
- if (user instanceof Error) {
44
- return reject(assistant.errorManager(user, {code: 400, sentry: false, send: false, log: false}).error)
45
- } else if (!user) {
46
- return reject(assistant.errorManager(`Admin or authenticated user required.`, {code: 401, sentry: false, send: false, log: false}).error)
47
- } else {
25
+ self.Api.resolveUser({adminRequired: true})
26
+ .then(async (user) => {
48
27
  // const planExpireDate = new Date(_.get(payload.user, 'plan.expires.timestamp', 0));
49
28
  // if (planExpireDate >= new Date()) {
50
29
  // payload.response.status = 401;
@@ -63,8 +42,10 @@ Module.prototype.main = function () {
63
42
  .catch(e => {
64
43
  return reject(assistant.errorManager(`Failed to delete user: ${e}`, {code: 400, sentry: false, send: false, log: false}).error)
65
44
  })
66
- }
67
-
45
+ })
46
+ .catch(e => {
47
+ return reject(e);
48
+ })
68
49
  });
69
50
 
70
51
  };
@@ -6,6 +6,7 @@ function Module() {
6
6
 
7
7
  Module.prototype.init = async function (s, payload) {
8
8
  const self = this;
9
+ self.Api = s;
9
10
  self.Manager = s.Manager;
10
11
  self.libraries = s.Manager.libraries;
11
12
  self.assistant = s.Manager.assistant;
@@ -21,30 +22,8 @@ Module.prototype.main = function () {
21
22
  const payload = self.payload;
22
23
 
23
24
  return new Promise(async function(resolve, reject) {
24
- let user = null;
25
- if (payload.user.roles.admin && payload.data.payload.uid) {
26
- await self.libraries.admin.firestore().doc(`users/${payload.data.payload.uid}`)
27
- .get()
28
- .then(async function (doc) {
29
- const data = doc.data();
30
- if (data) {
31
- user = data;
32
- } else {
33
- throw new Error('User does not exist')
34
- }
35
- })
36
- .catch(function (e) {
37
- user = e;
38
- })
39
- } else if (payload.user.authenticated) {
40
- user = payload.user;
41
- }
42
-
43
- if (user instanceof Error) {
44
- return reject(assistant.errorManager(user, {code: 400, sentry: false, send: false, log: false}).error)
45
- } else if (!user) {
46
- return reject(assistant.errorManager(`Admin or authenticated user required.`, {code: 401, sentry: false, send: false, log: false}).error)
47
- } else {
25
+ self.Api.resolveUser({adminRequired: false})
26
+ .then(async (user) => {
48
27
  const result = {
49
28
  plan: {
50
29
  id: _.get(user, 'plan.id', 'unknown'),
@@ -54,9 +33,11 @@ Module.prototype.main = function () {
54
33
  }
55
34
  }
56
35
  return resolve({data: result});
57
- }
36
+ })
37
+ .catch(e => {
38
+ return reject(e);
39
+ })
58
40
  });
59
-
60
41
  };
61
42
 
62
43
 
@@ -6,6 +6,7 @@ function Module() {
6
6
 
7
7
  Module.prototype.init = async function (s, payload) {
8
8
  const self = this;
9
+ self.Api = s;
9
10
  self.Manager = s.Manager;
10
11
  self.libraries = s.Manager.libraries;
11
12
  self.assistant = s.Manager.assistant;
@@ -21,30 +22,9 @@ Module.prototype.main = function () {
21
22
  const payload = self.payload;
22
23
 
23
24
  return new Promise(async function(resolve, reject) {
24
- let user = null;
25
- if (payload.user.roles.admin && payload.data.payload.uid) {
26
- await self.libraries.admin.firestore().doc(`users/${payload.data.payload.uid}`)
27
- .get()
28
- .then(async function (doc) {
29
- const data = doc.data();
30
- if (data) {
31
- user = data;
32
- } else {
33
- throw new Error('User does not exist')
34
- }
35
- })
36
- .catch(function (e) {
37
- user = e;
38
- })
39
- } else if (payload.user.authenticated) {
40
- user = payload.user;
41
- }
42
25
 
43
- if (user instanceof Error) {
44
- return reject(assistant.errorManager(user, {code: 400, sentry: false, send: false, log: false}).error)
45
- } else if (!user) {
46
- return reject(assistant.errorManager(`Admin or authenticated user required.`, {code: 401, sentry: false, send: false, log: false}).error)
47
- } else {
26
+ self.Api.resolveUser({adminRequired: true})
27
+ .then(async (user) => {
48
28
  const uid = _.get(user, 'auth.uid', null);
49
29
 
50
30
  await self.libraries.admin.database().ref(`gatherings/online`)
@@ -75,9 +55,10 @@ Module.prototype.main = function () {
75
55
  .catch(e => {
76
56
  return reject(assistant.errorManager(`Failed to sign out of all sessions: ${e}`, {code: 500, sentry: false, send: false, log: false}).error)
77
57
  })
78
-
79
- }
80
-
58
+ })
59
+ .catch(e => {
60
+ return reject(e);
61
+ })
81
62
  });
82
63
 
83
64
  };
@@ -7,6 +7,7 @@ function Module() {
7
7
 
8
8
  Module.prototype.init = async function (s, payload) {
9
9
  const self = this;
10
+ self.Api = s;
10
11
  self.Manager = s.Manager;
11
12
  self.libraries = s.Manager.libraries;
12
13
  self.assistant = s.Manager.assistant;
@@ -22,61 +23,40 @@ Module.prototype.main = function () {
22
23
  const payload = self.payload;
23
24
 
24
25
  return new Promise(async function(resolve, reject) {
25
- let user = null;
26
- if (payload.user.roles.admin && payload.data.payload.uid) {
27
- await self.libraries.admin.firestore().doc(`users/${payload.data.payload.uid}`)
28
- .get()
29
- .then(async function (doc) {
30
- const data = doc.data();
31
- if (data) {
32
- user = data;
33
- } else {
34
- throw new Error('User does not exist')
35
- }
36
- })
37
- .catch(function (e) {
38
- user = e;
39
- })
40
- } else if (payload.user.authenticated) {
41
- user = payload.user;
42
- }
43
-
44
- if (user instanceof Error) {
45
- return reject(assistant.errorManager(user, {code: 400, sentry: false, send: false, log: false}).error)
46
- } else if (!user) {
47
- return reject(assistant.errorManager(`Admin or authenticated user required.`, {code: 401, sentry: false, send: false, log: false}).error)
48
- } else {
49
-
50
- await self.signUp({
51
- auth: {
52
- uid: _.get(user, 'auth.uid', null),
53
- email: _.get(user, 'auth.email', null),
54
- },
55
- affiliate: {
56
- referrer: _.get(payload.data.payload, 'affiliateCode', null),
57
- },
58
- })
59
- .then(async function (result) {
60
- if (_.get(payload.data.payload, 'newsletterSignUp', false)) {
61
- await addToMCList(
62
- _.get(Manager.config, 'mailchimp.key'),
63
- _.get(Manager.config, 'mailchimp.list_id'),
64
- _.get(user, 'auth.email', null),
65
- )
66
- .then(function (res) {
67
- assistant.log('Sucessfully added user to MC list.')
68
- })
69
- .catch(function (e) {
70
- assistant.log('Failed to add user to MC list.', e)
71
- })
72
- }
73
- return resolve({data: result});
74
- })
75
- .catch(function (e) {
76
- return reject(assistant.errorManager(`Failed to sign up: ${e}`, {code: 500, sentry: false, send: false, log: false}).error)
77
- })
78
-
79
- }
26
+ self.Api.resolveUser({adminRequired: true})
27
+ .then(async (user) => {
28
+ await self.signUp({
29
+ auth: {
30
+ uid: _.get(user, 'auth.uid', null),
31
+ email: _.get(user, 'auth.email', null),
32
+ },
33
+ affiliate: {
34
+ referrer: _.get(payload.data.payload, 'affiliateCode', null),
35
+ },
36
+ })
37
+ .then(async function (result) {
38
+ if (_.get(payload.data.payload, 'newsletterSignUp', false)) {
39
+ await addToMCList(
40
+ _.get(Manager.config, 'mailchimp.key'),
41
+ _.get(Manager.config, 'mailchimp.list_id'),
42
+ _.get(user, 'auth.email', null),
43
+ )
44
+ .then(function (res) {
45
+ assistant.log('Sucessfully added user to MC list.')
46
+ })
47
+ .catch(function (e) {
48
+ assistant.log('Failed to add user to MC list.', e)
49
+ })
50
+ }
51
+ return resolve({data: result});
52
+ })
53
+ .catch(function (e) {
54
+ return reject(assistant.errorManager(`Failed to sign up: ${e}`, {code: 500, sentry: false, send: false, log: false}).error)
55
+ })
56
+ })
57
+ .catch(e => {
58
+ return reject(e);
59
+ })
80
60
 
81
61
  });
82
62
 
@@ -1,4 +1,5 @@
1
1
  const path = require('path');
2
+ const _ = require('lodash');
2
3
 
3
4
  function Module() {
4
5
 
@@ -11,6 +12,15 @@ Module.prototype.init = function (Manager, data) {
11
12
  self.assistant = Manager.Assistant({req: data.req, res: data.res})
12
13
  self.req = data.req;
13
14
  self.res = data.res;
15
+ self.payload = {
16
+ response: {
17
+ status: 200,
18
+ data: {},
19
+ error: null,
20
+ },
21
+ data: {},
22
+ user: {},
23
+ };
14
24
 
15
25
  return self;
16
26
  }
@@ -23,56 +33,50 @@ Module.prototype.main = function() {
23
33
  const res = self.res;
24
34
 
25
35
  return libraries.cors(req, res, async () => {
26
- const response = {
27
- status: 200,
28
- data: {},
29
- error: null,
30
- };
31
- const user = await assistant.authenticate();
32
- const command = resolveCommand(assistant.request.data.command);
36
+ self.payload.data = assistant.request.data;
37
+ self.payload.user = await assistant.authenticate();
38
+
39
+ const command = self.resolveCommand(self.payload.data.command);
33
40
  const commandPath = './' + path.join('./api/', `${command.replace(/\.\.\//g, '').replace(/\:/, '/')}.js`);
34
- const payload = {
35
- response: response,
36
- data: assistant.request.data,
37
- user: user,
38
- }
39
41
 
40
- self.assistant.log(`Executing: ${command}`, payload, JSON.stringify(payload), {environment: 'production'})
42
+ self.assistant.log(`Executing: ${command}`, self.payload, JSON.stringify(self.payload), {environment: 'production'})
41
43
 
42
44
  try {
43
45
  const lib = new (require(commandPath))();
44
46
  try {
45
- await lib.init(self, payload);
47
+ await lib.init(self, self.payload);
46
48
  await lib.main()
47
49
  .then(r => {
48
- response.status = r.status || 200;
49
- response.data = r.data || {};
50
+ self.payload.response.status = r.status || 200;
51
+ self.payload.response.data = r.data || {};
50
52
  })
51
53
  .catch(e => {
52
- response.status = e.code || 500;
53
- response.error = e || new Error('Unknown error occured');
54
+ self.payload.response.status = e.code || 500;
55
+ self.payload.response.error = e || new Error('Unknown error occured');
54
56
  })
55
57
  } catch (e) {
56
- response.status = 500;
57
- response.error = e || new Error('Unknown error occured');
58
+ self.payload.response.status = 500;
59
+ self.payload.response.error = e || new Error('Unknown error occured');
58
60
  }
59
61
  } catch (e) {
60
- response.status = 400;
61
- response.error = new Error(`Improper command supplied: ${command}`);
62
+ self.payload.response.status = 400;
63
+ self.payload.response.error = new Error(`Improper command supplied: ${command}`);
62
64
  assistant.log('Dev error log', e)
63
65
  }
64
66
 
65
- if (response.status === 200) {
66
- return res.status(response.status).json(response.data);
67
+ if (self.payload.response.status === 200) {
68
+ return res.status(self.payload.response.status).json(self.payload.response.data);
67
69
  } else {
68
- console.error(`Error executing ${command} @ ${commandPath}`, response.error)
69
- // return res.status(response.status).send(response.error.message);
70
- return res.status(response.status).send(`${response.error}`);
70
+ console.error(`Error executing ${command} @ ${commandPath}`, self.payload.response.error)
71
+ // return res.status(self.payload.response.status).send(self.payload.response.error.message);
72
+ return res.status(self.payload.response.status).send(`${self.payload.response.error}`);
71
73
  }
72
74
  });
73
75
  }
74
76
 
75
- function resolveCommand(command) {
77
+ Module.prototype.resolveCommand = function (command) {
78
+ const self = this;
79
+
76
80
  // Start
77
81
  if (false) {
78
82
 
@@ -128,4 +132,47 @@ function resolveCommand(command) {
128
132
  return command;
129
133
  }
130
134
 
135
+ Module.prototype.resolveUser = function (options) {
136
+ const self = this;
137
+ return new Promise(async function(resolve, reject) {
138
+ let user = null;
139
+
140
+ options = options || {};
141
+ options.uid = typeof options.uid !== 'undefined' ? options.uid : self.payload.data.payload.uid;
142
+ options.admin = typeof options.admin !== 'undefined' ? options.admin : self.payload.user.roles.admin;
143
+ options.adminRequired = typeof options.adminRequired !== 'undefined' ? options.adminRequired : true;
144
+
145
+ if (options.uid) {
146
+ if (options.adminRequired && !options.admin) {
147
+ user = self.assistant.errorManager('Admin required', {code: 401, sentry: false, send: false, log: false}).error;
148
+ } else {
149
+ await self.libraries.admin.firestore().doc(`users/${options.uid}`)
150
+ .get()
151
+ .then(async function (doc) {
152
+ const data = doc.data();
153
+ if (data) {
154
+ user = data;
155
+ } else {
156
+ user = self.assistant.errorManager(`User does not exist: ${options.uid}`, {code: 400, sentry: false, send: false, log: false}).error;
157
+ }
158
+ })
159
+ .catch(function (e) {
160
+ user = self.assistant.errorManager(e, {code: 500, sentry: false, send: false, log: false}).error;
161
+ })
162
+ }
163
+ } else if (self.payload.user.authenticated) {
164
+ user = self.payload.user;
165
+ }
166
+
167
+ if (user instanceof Error) {
168
+ return reject(user);
169
+ } else if (!user) {
170
+ return reject(self.assistant.errorManager('Unable to resolve user', {code: 500, sentry: false, send: false, log: false}).error);
171
+ } else {
172
+ return resolve(user);
173
+ }
174
+
175
+ });
176
+ };
177
+
131
178
  module.exports = Module;