groupcore-utils 3.0.4 → 4.0.0

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/.eslintrc.json CHANGED
@@ -9,17 +9,17 @@
9
9
  "ecmaVersion": 12
10
10
  },
11
11
  "rules": {
12
- "no-underscore-dangle": "off",
13
- "class-methods-use-this": "off",
14
- "no-console": "off",
15
- "jest/no-hooks": "off",
16
- "jest/no-test-return-statement": "off",
17
- "jest/prefer-strict-equal": "off",
18
- "consistent-return": "off",
19
- "array-callback-return": "off",
20
- "no-param-reassign": "off",
21
- "no-restricted-syntax": "off",
22
- "prefer-destructuring": "off",
23
- "no-case-declarations": "off"
12
+ // "no-underscore-dangle": "off",
13
+ // "class-methods-use-this": "off",
14
+ // "no-console": "off",
15
+ // "jest/no-hooks": "off",
16
+ // "jest/no-test-return-statement": "off",
17
+ // "jest/prefer-strict-equal": "off",
18
+ // "consistent-return": "off",
19
+ // "array-callback-return": "off",
20
+ // "no-param-reassign": "off",
21
+ // "no-restricted-syntax": "off",
22
+ // "prefer-destructuring": "off",
23
+ // "no-case-declarations": "off"
24
24
  }
25
25
  }
package/Utils.js CHANGED
@@ -1,6 +1,5 @@
1
1
  const axios = require('axios');
2
- const _isEmpty = require('lodash').isEmpty;
3
- const _isUndefined = require('lodash').isUndefined;
2
+ const { isEmpty, isUndefined } = require('lodash');
4
3
 
5
4
  class Utils {
6
5
  /**
@@ -9,7 +8,7 @@ class Utils {
9
8
  * @param {string} apiToken - The Core API token to authenticate and authorize access
10
9
  * @param {string} apiUrl - The Core API URL provided during Core setup
11
10
  */
12
- constructor({ projectId, apiToken, apiUrl }) {
11
+ constructor(projectId, apiToken, apiUrl) {
13
12
  this.project = projectId;
14
13
  this.token = apiToken;
15
14
  this.apiUrl = apiUrl;
@@ -23,10 +22,10 @@ class Utils {
23
22
  * @param {number} [leadId=null] - The lead ID, if the addLead method is called before submission
24
23
  * @returns {Promise}
25
24
  */
26
- submitForm({ formData, formId, email, leadId = 0 }) {
25
+ submitForm(formData, formId, email, leadId = 0) {
27
26
  return new Promise((resolve, reject) => {
28
- if (_isEmpty(formData) || _isEmpty(String(formId)) || _isEmpty(email)) {
29
- reject({ error: 'Missing some required params' });
27
+ if (isEmpty(formData) || isEmpty(String(formId)) || isEmpty(email)) {
28
+ reject(new Error('Missing some required params'));
30
29
  return;
31
30
  }
32
31
 
@@ -47,7 +46,7 @@ class Utils {
47
46
  resolve(response);
48
47
  })
49
48
  .catch(() => {
50
- reject({ error: 'API error' });
49
+ reject(new Error('API error'));
51
50
  });
52
51
  });
53
52
  }
@@ -62,22 +61,22 @@ class Utils {
62
61
  * @param {string} [attachmentUrl] - The location of the attachment. Optional if hasAttachment is false
63
62
  * @returns {Promise}
64
63
  */
65
- sendMail({ recipientEmail, subject, htmlBody, hasAttachment = false, attachmentFilename, attachmentUrl }) {
64
+ sendMail(recipientEmail, subject, htmlBody, attachmentFilename, attachmentUrl, hasAttachment = false) {
66
65
  return new Promise((resolve, reject) => {
67
66
  if (hasAttachment) {
68
- if (_isEmpty(attachmentFilename) || _isEmpty(attachmentUrl)) {
69
- reject({ error: 'Some missing params. If hasAttachment is true, both attachmentFilename and attachmentUrl are required' });
67
+ if (isEmpty(attachmentFilename) || isEmpty(attachmentUrl)) {
68
+ reject(new Error('Some missing params. If hasAttachment is true, both attachmentFilename and attachmentUrl are required'));
70
69
  return;
71
70
  }
72
71
  }
73
72
 
74
73
  if (!hasAttachment && (attachmentFilename || attachmentUrl)) {
75
- reject({ error: 'If hasAttachment is false, both attachmentFilename and attachmentUrl cannot be accepted' });
74
+ reject(new Error('If hasAttachment is false, both attachmentFilename and attachmentUrl cannot be accepted'));
76
75
  return;
77
76
  }
78
77
 
79
- if (_isEmpty(recipientEmail) || _isEmpty(subject) || _isEmpty(htmlBody)) {
80
- reject({ error: 'Missing some required params' });
78
+ if (isEmpty(recipientEmail) || isEmpty(subject) || isEmpty(htmlBody)) {
79
+ reject(new Error('Missing some required params'));
81
80
  return;
82
81
  }
83
82
 
@@ -99,7 +98,7 @@ class Utils {
99
98
  resolve(response);
100
99
  })
101
100
  .catch(() => {
102
- reject({ error: 'API error' });
101
+ reject(new Error('API error'));
103
102
  });
104
103
  });
105
104
  }
@@ -112,10 +111,10 @@ class Utils {
112
111
  * @param {string} email - The email address of the lead
113
112
  * @returns {Promise}
114
113
  */
115
- addLead({ firstname, lastname, phone = null, email }) {
114
+ addLead(firstname, lastname, email, phone) {
116
115
  return new Promise((resolve, reject) => {
117
- if (_isEmpty(firstname) || _isEmpty(lastname) || _isEmpty(email)) {
118
- reject({ error: 'Missing some required params' });
116
+ if (isEmpty(firstname) || isEmpty(lastname) || isEmpty(email)) {
117
+ reject(new Error('Missing some required params'));
119
118
  return;
120
119
  }
121
120
 
@@ -127,7 +126,7 @@ class Utils {
127
126
  firstname,
128
127
  lastname,
129
128
  email,
130
- phone,
129
+ phone: phone || null,
131
130
  },
132
131
  headers: {
133
132
  token: this.token,
@@ -137,7 +136,7 @@ class Utils {
137
136
  resolve(response);
138
137
  })
139
138
  .catch(() => {
140
- reject({ error: 'API error' });
139
+ reject(new Error('API error'));
141
140
  });
142
141
  });
143
142
  }
@@ -147,10 +146,10 @@ class Utils {
147
146
  * @param {number} groupId - The ID of the Core inventory group
148
147
  * @returns {Promise}
149
148
  */
150
- async getGroupInventory({ groupId }) {
149
+ async getGroupInventory(groupId) {
151
150
  return new Promise((resolve, reject) => {
152
- if (_isUndefined(groupId)) {
153
- reject({ error: 'Missing some required params' });
151
+ if (isUndefined(groupId)) {
152
+ reject(new Error('Missing some required params'));
154
153
  return;
155
154
  }
156
155
 
@@ -165,7 +164,7 @@ class Utils {
165
164
  resolve(response.data[0]);
166
165
  })
167
166
  .catch(() => {
168
- reject({ error: 'API error' });
167
+ reject(new Error('API error'));
169
168
  });
170
169
  });
171
170
  }
@@ -175,10 +174,10 @@ class Utils {
175
174
  * @param {number} groupId - The ID of the Core inventory group
176
175
  * @returns {Promise}
177
176
  */
178
- async getSectionContent({ groupId }) {
177
+ async getSectionContent(groupId) {
179
178
  return new Promise((resolve, reject) => {
180
- if (_isUndefined(groupId)) {
181
- reject({ error: 'Missing some required params' });
179
+ if (isUndefined(groupId)) {
180
+ reject(new Error('Missing some required params'));
182
181
  return;
183
182
  }
184
183
 
@@ -193,7 +192,7 @@ class Utils {
193
192
  resolve(response.data);
194
193
  })
195
194
  .catch(() => {
196
- reject({ error: 'API error' });
195
+ reject(new Error('API error'));
197
196
  });
198
197
  });
199
198
  }
@@ -1,4 +1,4 @@
1
- const nodemailer = require("nodemailer");
1
+ const nodemailer = require('nodemailer');
2
2
 
3
3
  module.exports = class {
4
4
  /**
@@ -8,14 +8,14 @@ module.exports = class {
8
8
  * @param username {string} - smtp username
9
9
  * @param password {password} - smtp password
10
10
  */
11
- constructor({ hostname, port, username, password }) {
11
+ constructor(hostname, port, username, password) {
12
12
  this.hostname = hostname;
13
13
  this.port = port;
14
14
  this.username = username;
15
15
  this.password = password;
16
16
  }
17
17
 
18
- async _init() {
18
+ async init() {
19
19
  this.transporter = nodemailer.createTransport({
20
20
  host: this.hostname,
21
21
  port: this.port,
@@ -40,15 +40,15 @@ module.exports = class {
40
40
  * @param from {string} - sender email address
41
41
  * @returns {Promise<*>}
42
42
  */
43
- async send({ email, subject, message, attachment = null, from }) {
44
- await this._init();
43
+ async send(email, subject, message, from, attachment) {
44
+ await this.init();
45
45
 
46
46
  return this.transporter.sendMail({
47
47
  subject,
48
48
  from,
49
49
  to: email,
50
50
  html: message,
51
- attachments: attachment,
51
+ attachments: attachment || null,
52
52
  });
53
53
  }
54
54
  };
package/database/crud.js CHANGED
@@ -7,7 +7,7 @@ module.exports = class {
7
7
  * @param {string} dbTable - name of the table
8
8
  * @param {object} db - the initialised db instance
9
9
  */
10
- constructor({ dbTable, db }) {
10
+ constructor(dbTable, db) {
11
11
  this.dbTable = dbTable;
12
12
  this.db = db;
13
13
  }
@@ -16,12 +16,10 @@ module.exports = class {
16
16
  * @method create()
17
17
  * @description Insert values into db
18
18
  * @param {Object} data - the key value pair of the data to be inserted into the db
19
- * @param {Boolean} unique - if set to true, ensure no duplication of data during creation
20
- * @param {Array} ignore - fields to ignore when checking for unique values
21
19
  * @returns {Promise | string | Object} - resolves if successfully and rejects if there's an error
22
20
  */
23
21
 
24
- async create({ data }) {
22
+ async create(data) {
25
23
  const fields = Object.keys(data).join(',');
26
24
 
27
25
  const rawValues = [];
@@ -44,9 +42,9 @@ module.exports = class {
44
42
  * @param {Array} orderBy - explicitly specify the order, either asc or desc
45
43
  * @returns {Promise}
46
44
  */
47
- read({ where, orderBy }) {
45
+ read(where = false, orderBy = []) {
48
46
  if (where && !_.has(where, 'fields')) {
49
- return this.readV1({ where, orderBy });
47
+ return this.readV1(where, orderBy);
50
48
  }
51
49
 
52
50
  let query;
@@ -79,7 +77,7 @@ module.exports = class {
79
77
  }
80
78
 
81
79
  // should be deprecated soon
82
- readV1({ where, orderBy }) {
80
+ readV1(where = false, orderBy = false) {
83
81
  let query;
84
82
 
85
83
  if (orderBy) {
@@ -104,7 +102,7 @@ module.exports = class {
104
102
  * @param {number} id - id in where clause
105
103
  * @returns {Promise}
106
104
  */
107
- update({ data, id }) {
105
+ update(data, id) {
108
106
  // variable to hold the dynamic part of the update sql query
109
107
  let updateQuery = '';
110
108
 
@@ -127,18 +125,18 @@ module.exports = class {
127
125
  * @param {number} id - id of data to deleted
128
126
  * @returns {Promise}
129
127
  */
130
- delete({ id }) {
128
+ delete(id) {
131
129
  return this.db.query(`delete from ${this.dbTable} where id = '${id}'`);
132
130
  }
133
131
 
134
132
  /**
135
133
  * @method query()
136
134
  * @description make a custom db query
137
- * @param {string} statement - SQL query
135
+ * @param {string} query - SQL query
138
136
  * @returns {Promise}
139
137
  */
140
138
  // eslint-disable-next-line class-methods-use-this
141
- query({ statement }) {
142
- return this.db.query(statement);
139
+ query(query) {
140
+ return this.db.query(query);
143
141
  }
144
142
  };
package/database/init.js CHANGED
@@ -1,4 +1,4 @@
1
- const mysql = require("mysql");
1
+ const mysql = require('mysql');
2
2
 
3
3
  module.exports = class {
4
4
  /**
@@ -9,7 +9,7 @@ module.exports = class {
9
9
  * @param {string} db - db name
10
10
  * @param {string} socket - path to a unix domain socket to connect to. when used host and port are ignored
11
11
  */
12
- constructor({ host, user, password, db, socket }) {
12
+ constructor(host, user, password, db, socket) {
13
13
  this.host = host;
14
14
  this.user = user;
15
15
  this.password = password;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "groupcore-utils",
3
- "version": "3.0.4",
3
+ "version": "4.0.0",
4
4
  "description": "Utilities for working with some core features",
5
5
  "main": "Utils.js",
6
6
  "scripts": {
package/rbac/index.js CHANGED
@@ -8,7 +8,7 @@ module.exports = class {
8
8
  * @param {number} compareId - id to be compared
9
9
  * @returns {Promise<void>}
10
10
  */
11
- async validateAction({ id, compareId }) {
11
+ static async validateAction(id, compareId) {
12
12
  const rbac = new EasyRbac({
13
13
  user: {
14
14
  can: [
package/rest/index.js CHANGED
@@ -6,19 +6,14 @@ module.exports = {
6
6
  * @description send a GET request
7
7
  * @param {string} url - api url
8
8
  * @param {object} headers - headers to be added to request
9
- * @returns {AxiosPromise<any>}
9
+ * @returns {Promise<any>}
10
10
  */
11
- async get({ url, headers }) {
12
- try {
13
- return axios({
14
- url,
15
- headers,
16
- method: 'get',
17
- });
18
- } catch (err) {
19
- console.log('Axios GET Error:', err.message);
20
- throw err;
21
- }
11
+ async get(url, headers = []) {
12
+ return axios({
13
+ url,
14
+ headers,
15
+ method: 'get',
16
+ });
22
17
  },
23
18
 
24
19
  /**
@@ -27,20 +22,15 @@ module.exports = {
27
22
  * @param {string} url - api url
28
23
  * @param {object} data - payload
29
24
  * @param {object} headers - headers to be added to request
30
- * @returns {AxiosPromise<any>}
25
+ * @returns {Promise<any>}
31
26
  */
32
- async post({ url, data, headers }) {
33
- try {
34
- return axios({
35
- url,
36
- data,
37
- headers,
38
- method: 'post',
39
- });
40
- } catch (err) {
41
- console.log('Axios POST Error:', err.message);
42
- throw err;
43
- }
27
+ async post(url, data, headers = []) {
28
+ return axios({
29
+ url,
30
+ data,
31
+ headers,
32
+ method: 'post',
33
+ });
44
34
  },
45
35
 
46
36
  /**
@@ -49,20 +39,15 @@ module.exports = {
49
39
  * @param {string} url - api url
50
40
  * @param {object} data - payload
51
41
  * @param {object} headers - headers to be added to request
52
- * @returns {AxiosPromise<any>}
42
+ * @returns {Promise<any>}
53
43
  */
54
- async put({ url, data, headers }) {
55
- try {
56
- return axios({
57
- url,
58
- data,
59
- headers,
60
- method: 'put',
61
- });
62
- } catch (err) {
63
- console.log('Axios PUT Error:', err.message);
64
- throw err;
65
- }
44
+ async put(url, data, headers = []) {
45
+ return axios({
46
+ url,
47
+ data,
48
+ headers,
49
+ method: 'put',
50
+ });
66
51
  },
67
52
 
68
53
  /**
@@ -71,39 +56,29 @@ module.exports = {
71
56
  * @param {string} url - api url
72
57
  * @param {object} data - payload
73
58
  * @param {object} headers - headers to be added to request
74
- * @returns {AxiosPromise<any>}
59
+ * @returns {Promise<any>}
75
60
  */
76
- async patch({ url, data, headers }) {
77
- try {
78
- return axios({
79
- url,
80
- data,
81
- headers,
82
- method: 'patch',
83
- });
84
- } catch (err) {
85
- console.log('Axios PUT Error:', err.message);
86
- throw err;
87
- }
61
+ async patch(url, data, headers = []) {
62
+ return axios({
63
+ url,
64
+ data,
65
+ headers,
66
+ method: 'patch',
67
+ });
88
68
  },
89
69
 
90
70
  /**
91
71
  * @method delete()
92
72
  * @description send DELETE request
93
- * @param {string} urls - api url
73
+ * @param {string} url - api url
94
74
  * @param {object} headers - headers to be added to request
95
- * @returns {AxiosPromise<any>}
75
+ * @returns {Promise<any>}
96
76
  */
97
- async delete({ url, headers }) {
98
- try {
99
- return axios({
100
- url,
101
- headers,
102
- method: 'delete',
103
- });
104
- } catch (err) {
105
- console.log('Axios DELETE Error:', err.message);
106
- throw err;
107
- }
77
+ async delete(url, headers = []) {
78
+ return axios({
79
+ url,
80
+ headers,
81
+ method: 'delete',
82
+ });
108
83
  },
109
84
  };
@@ -14,7 +14,7 @@ module.exports = class {
14
14
  * @param {string} text - text to be hashed
15
15
  * @returns {*}
16
16
  */
17
- hash({ text }) {
17
+ hash(text) {
18
18
  return bcrypt.hashSync(text, bcrypt.genSaltSync(this.saltRounds));
19
19
  }
20
20
 
@@ -25,7 +25,7 @@ module.exports = class {
25
25
  * @param {string} text - the text to be compared with the hash
26
26
  * @returns {*}
27
27
  */
28
- verify({ hash, text }) {
28
+ static verify(hash, text) {
29
29
  return bcrypt.compareSync(text, hash);
30
30
  }
31
31
  };
package/security/jwt.js CHANGED
@@ -3,10 +3,10 @@ const jwt = require('jsonwebtoken');
3
3
  module.exports = class {
4
4
  /**
5
5
  * @description constructor
6
- * @param {string} jwtKey - jwt key
6
+ * @param {string} key - jwt key
7
7
  */
8
- constructor({ jwtKey }) {
9
- this.jwtKey = jwtKey;
8
+ constructor(key) {
9
+ this.key = key;
10
10
  }
11
11
 
12
12
  /**
@@ -15,17 +15,17 @@ module.exports = class {
15
15
  * @param {object} payload - payload to be tokenized
16
16
  * @returns {*}
17
17
  */
18
- jwtSign({ payload }) {
19
- return jwt.sign(payload, this.jwtKey);
18
+ sign(payload) {
19
+ return jwt.sign(payload, this.key);
20
20
  }
21
21
 
22
22
  /**
23
23
  * @method jwtDecode()
24
24
  * @description decode a JWT token
25
25
  * @param {string} token - JWT token to be decoded
26
- * @returns {{payload: any, signature: *, header: *}|*|SourceMapPayload|TokenPayload}
26
+ * @returns {{payload: any, signature: *, header: *}}
27
27
  */
28
- jwtDecode({ token }) {
28
+ static decode(token) {
29
29
  return jwt.decode(token);
30
30
  }
31
31
 
@@ -35,7 +35,7 @@ module.exports = class {
35
35
  * @param {string} token - token to be verified
36
36
  * @returns {*}
37
37
  */
38
- jwtVerify({ token }) {
39
- return jwt.verify(token, this.jwtKey);
38
+ verify(token) {
39
+ return jwt.verify(token, this.key);
40
40
  }
41
41
  };
@@ -1,14 +1,14 @@
1
1
  module.exports = class {
2
2
  constructor() {
3
- jest.spyOn(this, "send").mockImplementation((body) => body);
4
- jest.spyOn(this, "status").mockImplementation(() => this);
3
+ jest.spyOn(this, 'send').mockImplementation((body) => body);
4
+ jest.spyOn(this, 'status').mockImplementation(() => this);
5
5
  }
6
6
 
7
- send() {
7
+ static send() {
8
8
  return true;
9
9
  }
10
10
 
11
- status() {
11
+ static status() {
12
12
  return true;
13
13
  }
14
14
  };
@@ -6,7 +6,7 @@ module.exports = class {
6
6
  * @description returns the present unix timestamp
7
7
  * @returns {number}
8
8
  */
9
- now() {
9
+ static now() {
10
10
  return moment().unix();
11
11
  }
12
12
 
@@ -17,7 +17,7 @@ module.exports = class {
17
17
  * @param {string} format - the format to use, default is 'MMMM DD, YYYY'
18
18
  * @returns {string}
19
19
  */
20
- unixToDate({ unix, format = 'MMMM DD, YYYY' }) {
20
+ static unixToDate(unix, format) {
21
21
  return moment.unix(unix).format(format);
22
22
  }
23
23
 
@@ -28,7 +28,7 @@ module.exports = class {
28
28
  * @param {string} format - the format of the date
29
29
  * @returns {number}
30
30
  */
31
- dateToUnix({ date, format }) {
31
+ static dateToUnix(date, format) {
32
32
  return moment(date, format).valueOf() / 1000;
33
33
  }
34
34
 
@@ -38,7 +38,7 @@ module.exports = class {
38
38
  * @param {string} unix - unix timestamp
39
39
  * @returns {number}
40
40
  */
41
- unixToStartOfDay(unix) {
41
+ static unixToStartOfDay(unix) {
42
42
  return new Date(unix * 1000).setHours(0, 0, 0, 0) / 1000;
43
43
  }
44
44
 
@@ -48,7 +48,7 @@ module.exports = class {
48
48
  * @param {string} unix - unix timestamp
49
49
  * @returns {number}
50
50
  */
51
- unixToEndOfDay(unix) {
51
+ static unixToEndOfDay(unix) {
52
52
  return new Date(unix * 1000).setHours(23, 59, 59, 999) / 1000;
53
53
  }
54
54
 
@@ -58,8 +58,8 @@ module.exports = class {
58
58
  * @param {string} time - time to be converted
59
59
  * @returns {number}
60
60
  */
61
- toSeconds(time) {
62
- time = time.split(':');
63
- return time[0] * 3600 + time[1] * 60;
61
+ static toSeconds(time) {
62
+ const splitTime = time.split(':');
63
+ return splitTime[0] * 3600 + splitTime[1] * 60;
64
64
  }
65
65
  };