groupcore-utils 1.3.0 → 1.3.1

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 (4) hide show
  1. package/README.md +92 -0
  2. package/Utils.js +116 -81
  3. package/Utils.spec.js +118 -61
  4. package/package.json +1 -1
package/README.md ADDED
@@ -0,0 +1,92 @@
1
+ ##Group Core Util Package
2
+ ###Description
3
+ This package provides methods to work with parts of your Core account to enable you build solutions around the Core features.
4
+
5
+ ###Installation
6
+ ```
7
+ npm install groupcore-utils
8
+ ```
9
+
10
+ ###Basic Usage
11
+ ```
12
+ import CoreUtils from 'groupcore-utils'
13
+
14
+ ...
15
+ const projectId = 'your Core project ID'
16
+ const apiToken = 'your Core project API token'
17
+ const apiUrl = 'your Core API URL'
18
+
19
+ const coreUtil = new CoreUtils({ projectId, apiToken, apiUrl })
20
+
21
+ // now you can call any of the available methods
22
+ // for example, to get the inventory items of a particular inveentory group with ID of 1
23
+ coreUtil.getGroupInventory({groupId: 1}).then((response)=>{
24
+ ....
25
+ })
26
+
27
+ ```
28
+
29
+ ###Available Methods
30
+ These are the methods you can call with this package. This list will continually be updated as needs arise.
31
+
32
+ ####submitForm()
33
+ ```
34
+ /**
35
+ * Submit a form to the Core Lead Management module
36
+ * @param {object} formData - Key-value pair of all form fields to be submitted
37
+ * @param {number} formId - The ID of the Core form for the submission
38
+ * @param {string} email - The email address of the submitter
39
+ * @param {number} [leadId=null] - The lead ID, if the addLead method is called before submission
40
+ * @returns {Promise}
41
+ */
42
+ ```
43
+
44
+
45
+ ####sendMail()
46
+ ```
47
+ /**
48
+ * Send an email message from your Core account.
49
+ * @param {string} recipientEmail - Email address of recipient
50
+ * @param {string} subject - Subject of the email
51
+ * @param {string} htmlBody - HTML body of the email message
52
+ * @param {boolean} [hasAttachment] - Specify if an attachment will be sent with the email
53
+ * @param {string} [attachmentFilename] - Choose the filename of the attachment. Optional if hasAttachment is false
54
+ * @param {string} [attachmentUrl] - The location of the attachment. Optional if hasAttachment is false
55
+ * @returns {Promise}
56
+ */
57
+ ```
58
+
59
+ ####addLead()
60
+ ```
61
+ /**
62
+ * Add a lead to the Core Lead Management module
63
+ * @param {string} firstname - The first name of the lead
64
+ * @param {string} lastname - The last name of the lead
65
+ * @param {string} phone - The phone number of the lead
66
+ * @param {string} email - The email address of the lead
67
+ * @returns {Promise}
68
+ */
69
+ ```
70
+
71
+ ####getGroupInventory()
72
+ ```
73
+ /**
74
+ * get inventory by group
75
+ * @param {number} groupId - The ID of the Core inventory group
76
+ * @returns {Promise}
77
+ */
78
+ ```
79
+
80
+ ####getSectionContent()
81
+ ```
82
+ /**
83
+ * Get content from the Core content management system, by section
84
+ * @param {number} groupId - The ID of the Core inventory group
85
+ * @returns {Promise}
86
+ */
87
+ ```
88
+
89
+ ###Testing
90
+ ```
91
+ npm run test
92
+ ```
package/Utils.js CHANGED
@@ -21,25 +21,34 @@ class Utils {
21
21
  * @param {number} formId - The ID of the Core form for the submission
22
22
  * @param {string} email - The email address of the submitter
23
23
  * @param {number} [leadId=null] - The lead ID, if the addLead method is called before submission
24
- * @returns {Object.<{error: string}>|Promise.<Object.<{insertId: number}>>} - Returns an error or a promise that resolves to an object
24
+ * @returns {Promise}
25
25
  */
26
26
  submitForm({ formData, formId, email, leadId = 0 }) {
27
- if (_isEmpty(formData) || _isEmpty(String(formId)) || _isEmpty(email)) {
28
- return { error: 'Missing some required params' };
29
- }
27
+ return new Promise((resolve, reject) => {
28
+ if (_isEmpty(formData) || _isEmpty(String(formId)) || _isEmpty(email)) {
29
+ reject({ error: 'Missing some required params' });
30
+ return;
31
+ }
30
32
 
31
- return axios({
32
- method: 'POST',
33
- url: `${this.apiUrl}/com/submissions`,
34
- data: {
35
- formData,
36
- formId,
37
- email,
38
- leadId,
39
- },
40
- headers: {
41
- token: this.token,
42
- },
33
+ axios({
34
+ method: 'POST',
35
+ url: `${this.apiUrl}/com/submissions`,
36
+ data: {
37
+ formData,
38
+ formId,
39
+ email,
40
+ leadId,
41
+ },
42
+ headers: {
43
+ token: this.token,
44
+ },
45
+ })
46
+ .then((response) => {
47
+ resolve(response);
48
+ })
49
+ .catch(() => {
50
+ reject({ error: 'API error' });
51
+ });
43
52
  });
44
53
  }
45
54
 
@@ -51,36 +60,47 @@ class Utils {
51
60
  * @param {boolean} [hasAttachment] - Specify if an attachment will be sent with the email
52
61
  * @param {string} [attachmentFilename] - Choose the filename of the attachment. Optional if hasAttachment is false
53
62
  * @param {string} [attachmentUrl] - The location of the attachment. Optional if hasAttachment is false
54
- * @returns {Object.<{error: string}>|Promise.<Object.<{insertId: number}>>} - Returns an error or a promise that resolves to an object
63
+ * @returns {Promise}
55
64
  */
56
65
  sendMail({ recipientEmail, subject, htmlBody, hasAttachment = false, attachmentFilename, attachmentUrl }) {
57
- if (hasAttachment) {
58
- if (_isEmpty(attachmentFilename) || _isEmpty(attachmentUrl)) {
59
- return { error: 'Some missing params. If hasAttachment is true, both attachmentFilename and attachmentUrl are required' };
66
+ return new Promise((resolve, reject) => {
67
+ if (hasAttachment) {
68
+ if (_isEmpty(attachmentFilename) || _isEmpty(attachmentUrl)) {
69
+ reject({ error: 'Some missing params. If hasAttachment is true, both attachmentFilename and attachmentUrl are required' });
70
+ return;
71
+ }
60
72
  }
61
- }
62
73
 
63
- if (!hasAttachment && (attachmentFilename || attachmentUrl)) {
64
- return { error: 'If hasAttachment is false, both attachmentFilename and attachmentUrl cannot be accepted' };
65
- }
74
+ if (!hasAttachment && (attachmentFilename || attachmentUrl)) {
75
+ reject({ error: 'If hasAttachment is false, both attachmentFilename and attachmentUrl cannot be accepted' });
76
+ return;
77
+ }
66
78
 
67
- if (_isEmpty(recipientEmail) || _isEmpty(subject) || _isEmpty(htmlBody)) {
68
- return { error: 'Missing some required params' };
69
- }
79
+ if (_isEmpty(recipientEmail) || _isEmpty(subject) || _isEmpty(htmlBody)) {
80
+ reject({ error: 'Missing some required params' });
81
+ return;
82
+ }
70
83
 
71
- return axios({
72
- method: 'POST',
73
- url: `${this.apiUrl}/com/mail`,
74
- data: {
75
- project: this.project,
76
- email: recipientEmail,
77
- message: htmlBody,
78
- subject,
79
- attachments: hasAttachment ? [{ filename: attachmentFilename, path: attachmentUrl }] : null,
80
- },
81
- headers: {
82
- token: this.token,
83
- },
84
+ axios({
85
+ method: 'POST',
86
+ url: `${this.apiUrl}/com/mail`,
87
+ data: {
88
+ project: this.project,
89
+ email: recipientEmail,
90
+ message: htmlBody,
91
+ subject,
92
+ attachments: hasAttachment ? [{ filename: attachmentFilename, path: attachmentUrl }] : null,
93
+ },
94
+ headers: {
95
+ token: this.token,
96
+ },
97
+ })
98
+ .then((response) => {
99
+ resolve(response);
100
+ })
101
+ .catch(() => {
102
+ reject({ error: 'API error' });
103
+ });
84
104
  });
85
105
  }
86
106
 
@@ -90,77 +110,92 @@ class Utils {
90
110
  * @param {string} lastname - The last name of the lead
91
111
  * @param {string} phone - The phone number of the lead
92
112
  * @param {string} email - The email address of the lead
93
- * @returns {Object.<{error: string}>|Promise.<Object.<{insertId: number}>>} - Returns an error or a promise that resolves to an object
113
+ * @returns {Promise}
94
114
  */
95
115
  addLead({ firstname, lastname, phone = null, email }) {
96
- if (_isEmpty(firstname) || _isEmpty(lastname) || _isEmpty(email)) {
97
- return { error: 'Missing some required params' };
98
- }
116
+ return new Promise((resolve, reject) => {
117
+ if (_isEmpty(firstname) || _isEmpty(lastname) || _isEmpty(email)) {
118
+ reject({ error: 'Missing some required params' });
119
+ return;
120
+ }
99
121
 
100
- return axios({
101
- method: 'POST',
102
- url: `${this.apiUrl}/com/leads`,
103
- data: {
104
- project: this.project,
105
- firstname,
106
- lastname,
107
- email,
108
- phone,
109
- },
110
- headers: {
111
- token: this.token,
112
- },
122
+ axios({
123
+ method: 'POST',
124
+ url: `${this.apiUrl}/com/leads`,
125
+ data: {
126
+ project: this.project,
127
+ firstname,
128
+ lastname,
129
+ email,
130
+ phone,
131
+ },
132
+ headers: {
133
+ token: this.token,
134
+ },
135
+ })
136
+ .then((response) => {
137
+ resolve(response);
138
+ })
139
+ .catch(() => {
140
+ reject({ error: 'API error' });
141
+ });
113
142
  });
114
143
  }
115
144
 
116
145
  /**
117
146
  * get inventory by group
118
147
  * @param {number} groupId - The ID of the Core inventory group
119
- * @returns {Object.<{error: string}>|Promise.<Object>} - Returns an error or a promise that resolves to an object
148
+ * @returns {Promise}
120
149
  */
121
150
  async getGroupInventory({ groupId }) {
122
- if (_isUndefined(groupId)) {
123
- return { error: 'Missing some required params' };
124
- }
151
+ return new Promise((resolve, reject) => {
152
+ if (_isUndefined(groupId)) {
153
+ reject({ error: 'Missing some required params' });
154
+ return;
155
+ }
125
156
 
126
- try {
127
- const response = await axios({
157
+ axios({
128
158
  method: 'GET',
129
159
  url: `${this.apiUrl}/inv/groups?id=${groupId}`,
130
160
  headers: {
131
161
  token: this.token,
132
162
  },
133
- });
134
-
135
- return response.data[0];
136
- } catch (e) {
137
- return { error: 'API error' };
138
- }
163
+ })
164
+ .then((response) => {
165
+ resolve(response.data[0]);
166
+ })
167
+ .catch(() => {
168
+ reject({ error: 'API error' });
169
+ });
170
+ });
139
171
  }
140
172
 
141
173
  /**
142
174
  * Get content from the Core content management system, by section
143
175
  * @param {number} groupId - The ID of the Core inventory group
144
- * @returns {Object.<{error: string}>|Promise.<Object>} - Returns an error or a promise that resolves to an object
176
+ * @returns {Promise}
145
177
  */
146
178
  async getSectionContent({ groupId }) {
147
- if (_isUndefined(groupId)) {
148
- return { error: 'Missing some required params' };
149
- }
179
+ return new Promise((resolve, reject) => {
180
+ if (_isUndefined(groupId)) {
181
+ reject({ error: 'Missing some required params' });
182
+ return;
183
+ }
150
184
 
151
- try {
152
- const response = await axios({
185
+ axios({
153
186
  method: 'GET',
154
187
  url: `${this.apiUrl}/cms/sections?group=${groupId}`,
155
188
  headers: {
156
189
  token: this.token,
157
190
  },
158
- });
159
-
160
- return response.data;
161
- } catch (e) {
162
- return { error: 'API error' };
163
- }
191
+ })
192
+ .then((response) => {
193
+ resolve(response.data);
194
+ })
195
+ .catch(() => {
196
+ reject({ error: 'API error' });
197
+ });
198
+ });
164
199
  }
165
200
  }
166
201
 
package/Utils.spec.js CHANGED
@@ -5,104 +5,148 @@ const axios = require('axios');
5
5
  jest.mock('axios');
6
6
 
7
7
  describe('Testing Utils.submitForm()', () => {
8
- test('It should get a status 204 as response', async () => {
8
+ it('Should get a status 204 as response', async () => {
9
9
  axios.mockResolvedValue({ status: 200 });
10
- const response = await util.submitForm({ formData: { name: 'test' }, formId: 300, email: 'test@email.com' });
10
+ const response = await util.submitForm({ formData: { name: 'abcd' }, formId: 300, email: '1234' });
11
11
  expect(response.status).toEqual(200);
12
12
  });
13
13
 
14
- test('It should return error if missing params', async () => {
15
- const response = await util.submitForm({ formId: 10, email: 'test@email.com', leadId: 10 });
16
- const error = { error: 'Missing some required params' };
17
- expect(response).toEqual(error);
14
+ it('Should return error if missing params', async () => {
15
+ try {
16
+ await util.submitForm({ formId: 1234, email: '1234', leadId: 1234 });
17
+ } catch (error) {
18
+ expect(error).toEqual({ error: 'Missing some required params' });
19
+ }
20
+ });
21
+
22
+ it('Should catch any errors during API call', async () => {
23
+ axios.mockRejectedValue();
24
+
25
+ try {
26
+ await util.submitForm({ formData: { name: 'abcd' }, formId: 1234, email: '1234' });
27
+ } catch (error) {
28
+ expect(error).toEqual({ error: 'API error' });
29
+ }
18
30
  });
19
31
  });
20
32
 
21
33
  describe('Testing Utils.sendMail()', () => {
22
- test('It should return right response', async () => {
34
+ it('It should return right response', async () => {
23
35
  axios.mockResolvedValue({ status: 204 });
24
36
  const response = await util.sendMail({
25
- recipientEmail: 'test@email.com',
26
- subject: 'Test subject',
27
- htmlBody: '<p>Html body</p>',
37
+ recipientEmail: '1234',
38
+ subject: 'abcd',
39
+ htmlBody: '<p>abcd</p>',
28
40
  });
29
41
  expect(response.status).toEqual(204);
30
42
  });
31
43
 
32
- test('It should return right response with attachment', async () => {
44
+ it('It should return right response with attachment', async () => {
33
45
  axios.mockResolvedValue({ status: 204 });
34
46
  const response = await util.sendMail({
35
- recipientEmail: 'test@email.com',
36
- subject: 'Test subject',
37
- htmlBody: '<p>Html body</p>',
47
+ recipientEmail: 'abcd',
48
+ subject: 'abcd',
49
+ htmlBody: '<p>abcd</p>',
38
50
  hasAttachment: true,
39
- attachmentFilename: 'file',
40
- attachmentUrl: 'url',
51
+ attachmentFilename: 'abcd',
52
+ attachmentUrl: 'abcd',
41
53
  });
42
54
  expect(response.status).toEqual(204);
43
55
  });
44
56
 
45
- test('Should require attachmentFilename and attachmentUrl if hasAttachment is true', async () => {
46
- const response = await util.sendMail({
47
- recipientEmail: 'test@email.com',
48
- subject: 'Test subject',
49
- htmlBody: '<p>Html body</p>',
50
- hasAttachment: true,
51
- attachmentFilename: 'testfilename',
52
- });
53
- const error = { error: 'Some missing params. If hasAttachment is true, both attachmentFilename and attachmentUrl are required' };
54
- expect(response).toEqual(error);
57
+ it('Should require attachmentFilename and attachmentUrl if hasAttachment is true', async () => {
58
+ try {
59
+ await util.sendMail({
60
+ recipientEmail: 'abcd',
61
+ subject: 'abcd',
62
+ htmlBody: '<p>abcd</p>',
63
+ hasAttachment: true,
64
+ attachmentFilename: 'abcd',
65
+ });
66
+ } catch (error) {
67
+ expect(error).toEqual({ error: 'Some missing params. If hasAttachment is true, both attachmentFilename and attachmentUrl are required' });
68
+ }
55
69
  });
56
70
 
57
- test('Should return error if hasAttachment is false or undefined but attachmentUrl or attachmentFilename are not empty', async () => {
58
- const response = await util.sendMail({
59
- recipientEmail: 'test@email.com',
60
- subject: 'Test subject',
61
- htmlBody: '<p>Html body</p>',
62
- attachmentFilename: 'testfilename',
63
- });
64
- const error = { error: 'If hasAttachment is false, both attachmentFilename and attachmentUrl cannot be accepted' };
65
- expect(response).toEqual(error);
71
+ it('Should return error if hasAttachment is false or undefined but attachmentUrl or attachmentFilename are not empty', async () => {
72
+ try {
73
+ await util.sendMail({
74
+ recipientEmail: 'abcd',
75
+ subject: 'abcd',
76
+ htmlBody: '<p>abcd</p>',
77
+ attachmentFilename: 'abcd',
78
+ });
79
+ } catch (error) {
80
+ expect(error).toEqual({ error: 'If hasAttachment is false, both attachmentFilename and attachmentUrl cannot be accepted' });
81
+ }
66
82
  });
67
83
 
68
- test('Should return error if missing required params', async () => {
69
- const response = await util.sendMail({
70
- recipientEmail: 'test@email.com',
71
- });
72
- const error = { error: 'Missing some required params' };
73
- expect(response).toEqual(error);
84
+ it('Should return error if missing required params', async () => {
85
+ try {
86
+ await util.sendMail({
87
+ recipientEmail: '1234',
88
+ });
89
+ } catch (error) {
90
+ expect(error).toEqual({ error: 'Missing some required params' });
91
+ }
92
+ });
93
+
94
+ it('Should catch any errors during API call', async () => {
95
+ axios.mockRejectedValue();
96
+
97
+ try {
98
+ await util.sendMail({
99
+ recipientEmail: '1234',
100
+ subject: 'abcd',
101
+ htmlBody: '<p>abcd</p>',
102
+ });
103
+ } catch (error) {
104
+ expect(error).toEqual({ error: 'API error' });
105
+ }
74
106
  });
75
107
  });
76
108
 
77
109
  describe('Testing Utils.addLead()', () => {
78
- test('It should return right response', async () => {
110
+ it('It should return right response', async () => {
79
111
  axios.mockResolvedValue({ data: { insertId: 3 } });
80
- const response = await util.addLead({ firstname: 'test', lastname: 'test', email: 'test@email.com', phone: '123456' });
112
+ const response = await util.addLead({ firstname: 'test', lastname: 'test', email: 'abcd', phone: '123456' });
81
113
  expect(response.data.insertId).toEqual(3);
82
114
  });
83
115
 
84
- test('Should return error if missing required params', async () => {
85
- const response = await util.addLead({
86
- firstname: 'test@email.com',
87
- });
88
- const error = { error: 'Missing some required params' };
89
- expect(response).toEqual(error);
116
+ it('Should return error if missing required params', async () => {
117
+ try {
118
+ await util.addLead({
119
+ firstname: '1234',
120
+ });
121
+ } catch (error) {
122
+ expect(error).toEqual({ error: 'Missing some required params' });
123
+ }
124
+ });
125
+
126
+ it('Should catch any errors during API call', async () => {
127
+ axios.mockRejectedValue();
128
+
129
+ try {
130
+ await util.addLead({ firstname: 'test', lastname: 'test', email: 'abcd', phone: '123456' });
131
+ } catch (error) {
132
+ expect(error).toEqual({ error: 'API error' });
133
+ }
90
134
  });
91
135
  });
92
136
 
93
- describe('Testing Utils.getGroupInventory', () => {
137
+ describe('Testing Utils.getGroupInventory()', () => {
94
138
  it('Should return the right response', async () => {
95
139
  axios.mockResolvedValue({
96
140
  data: [
97
141
  {
98
142
  id: 13,
99
- item: '{"item":"test","price":12.34}',
143
+ item: '{"item":"abcd","price":12.34}',
100
144
  inventoryGroup: 6,
101
145
  status: 0,
102
146
  dateAdded: 0,
103
147
  cost: 8,
104
148
  data: {
105
- test: 'test',
149
+ test: 'abcd',
106
150
  },
107
151
  },
108
152
  ],
@@ -113,18 +157,24 @@ describe('Testing Utils.getGroupInventory', () => {
113
157
  });
114
158
 
115
159
  it('Should validate the group ID param', async () => {
116
- const response = await util.getGroupInventory({ groups: 6 });
117
- expect(response).toEqual({ error: 'Missing some required params' });
160
+ try {
161
+ await util.getGroupInventory({ groups: 6 });
162
+ } catch (error) {
163
+ expect(error).toEqual({ error: 'Missing some required params' });
164
+ }
118
165
  });
119
166
 
120
167
  it('Should catch any errors during API call', async () => {
121
168
  axios.mockRejectedValue();
122
- const response = await util.getGroupInventory({ groupId: 6 });
123
- expect(response).toEqual({ error: 'API error' });
169
+ try {
170
+ await util.getGroupInventory({ groupId: 6 });
171
+ } catch (error) {
172
+ expect(error).toEqual({ error: 'API error' });
173
+ }
124
174
  });
125
175
  });
126
176
 
127
- describe('Testing Utils.getSectionContent', () => {
177
+ describe('Testing Utils.getSectionContent()', () => {
128
178
  it('Should return the right response', async () => {
129
179
  axios.mockResolvedValue({
130
180
  data: {
@@ -137,13 +187,20 @@ describe('Testing Utils.getSectionContent', () => {
137
187
  });
138
188
 
139
189
  it('Should validate the group ID param', async () => {
140
- const response = await util.getSectionContent({ groups: 6 });
141
- expect(response).toEqual({ error: 'Missing some required params' });
190
+ try {
191
+ await util.getSectionContent({ groups: 6 });
192
+ } catch (error) {
193
+ expect(error).toEqual({ error: 'Missing some required params' });
194
+ }
142
195
  });
143
196
 
144
197
  it('Should catch any errors during API call', async () => {
145
198
  axios.mockRejectedValue();
146
- const response = await util.getSectionContent({ groupId: 6 });
147
- expect(response).toEqual({ error: 'API error' });
199
+
200
+ try {
201
+ await util.getSectionContent({ groupId: 6 });
202
+ } catch (error) {
203
+ expect(error).toEqual({ error: 'API error' });
204
+ }
148
205
  });
149
206
  });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "groupcore-utils",
3
- "version": "1.3.0",
3
+ "version": "1.3.1",
4
4
  "description": "Utilities for working with some Core features",
5
5
  "main": "Utils.js",
6
6
  "scripts": {