groupcore-utils 1.0.1 → 1.2.3

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 (3) hide show
  1. package/Utils.js +58 -1
  2. package/Utils.spec.js +32 -0
  3. package/package.json +8 -8
package/Utils.js CHANGED
@@ -1,16 +1,30 @@
1
1
  const axios = require('axios');
2
2
  const _isEmpty = require('lodash').isEmpty;
3
+ const _isUndefined = require('lodash').isUndefined;
3
4
 
4
5
  class Utils {
6
+ /**
7
+ * The project-specific details when creating an instance. All details can be gotten from your Core account
8
+ * @param {number} projectId - The Core project ID
9
+ * @param {string} apiToken - The Core API token to authenticate and authorize access
10
+ * @param {string} apiUrl - The Core API URL provided during Core setup
11
+ */
5
12
  constructor({ projectId, apiToken, apiUrl }) {
6
13
  this.project = projectId;
7
14
  this.token = apiToken;
8
15
  this.apiUrl = apiUrl;
9
16
  }
10
17
 
18
+ /**
19
+ * Submit a form to the Core Lead Management module
20
+ * @param {object} formData - Key-value pair of all form fields to be submitted
21
+ * @param {number} formId - The ID of the Core form for the submission
22
+ * @param {string} email - The email address of the submitter
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
25
+ */
11
26
  submitForm({ formData, formId, email, leadId = 0 }) {
12
27
  if (_isEmpty(formData) || _isEmpty(String(formId)) || _isEmpty(email)) {
13
- console.log('got here');
14
28
  return { error: 'Missing some required params' };
15
29
  }
16
30
 
@@ -29,6 +43,16 @@ class Utils {
29
43
  });
30
44
  }
31
45
 
46
+ /**
47
+ * Send an email message from your Core account.
48
+ * @param {string} recipientEmail - Email address of recipient
49
+ * @param {string} subject - Subject of the email
50
+ * @param {string} htmlBody - HTML body of the email message
51
+ * @param {boolean} [hasAttachment] - Specify if an attachment will be sent with the email
52
+ * @param {string} [attachmentFilename] - Choose the filename of the attachment. Optional if hasAttachment is false
53
+ * @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
55
+ */
32
56
  sendMail({ recipientEmail, subject, htmlBody, hasAttachment = false, attachmentFilename, attachmentUrl }) {
33
57
  if (hasAttachment) {
34
58
  if (_isEmpty(attachmentFilename) || _isEmpty(attachmentUrl)) {
@@ -60,6 +84,14 @@ class Utils {
60
84
  });
61
85
  }
62
86
 
87
+ /**
88
+ * Add a lead to the Core Lead Management module
89
+ * @param {string} firstname - The first name of the lead
90
+ * @param {string} lastname - The last name of the lead
91
+ * @param {string} phone - The phone number of the lead
92
+ * @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
94
+ */
63
95
  addLead({ firstname, lastname, phone = null, email }) {
64
96
  if (_isEmpty(firstname) || _isEmpty(lastname) || _isEmpty(email)) {
65
97
  return { error: 'Missing some required params' };
@@ -80,6 +112,31 @@ class Utils {
80
112
  },
81
113
  });
82
114
  }
115
+
116
+ /**
117
+ * get inventory by group
118
+ * @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
120
+ */
121
+ async getGroupInventory({ groupId }) {
122
+ if (_isUndefined(groupId)) {
123
+ return { error: 'Missing some required params' };
124
+ }
125
+
126
+ try {
127
+ const response = await axios({
128
+ method: 'GET',
129
+ url: `${this.apiUrl}/inv/groups?id=${groupId}`,
130
+ headers: {
131
+ token: this.token,
132
+ },
133
+ });
134
+
135
+ return response.data[0];
136
+ } catch (e) {
137
+ return { error: 'API error' };
138
+ }
139
+ }
83
140
  }
84
141
 
85
142
  module.exports = Utils;
package/Utils.spec.js CHANGED
@@ -89,3 +89,35 @@ describe('Testing Utils.addLead()', () => {
89
89
  expect(response).toEqual(error);
90
90
  });
91
91
  });
92
+
93
+ describe('Testing Utils.getGroupInventory', () => {
94
+ it('Should return the right response', async () => {
95
+ axios.mockResolvedValue([
96
+ {
97
+ id: 13,
98
+ item: '{"item":"test","price":12.34}',
99
+ inventoryGroup: 6,
100
+ status: 0,
101
+ dateAdded: 0,
102
+ cost: 8,
103
+ data: {
104
+ test: 'test',
105
+ },
106
+ },
107
+ ]);
108
+
109
+ const response = await util.getGroupInventory({ groupId: 6 });
110
+ expect(response.id).toEqual(13);
111
+ });
112
+
113
+ it('Should validate the group ID param', async () => {
114
+ const response = await util.getGroupInventory({ groups: 6 });
115
+ expect(response).toEqual({ error: 'Missing some required params' });
116
+ });
117
+
118
+ it('Should catch any errors during API call', async () => {
119
+ axios.mockRejectedValue();
120
+ const response = await util.getGroupInventory({ groupId: 6 });
121
+ expect(response).toEqual({ error: 'API error' });
122
+ });
123
+ });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "groupcore-utils",
3
- "version": "1.0.1",
3
+ "version": "1.2.3",
4
4
  "description": "Utilities for working with some Core features",
5
5
  "main": "Utils.js",
6
6
  "scripts": {
@@ -8,20 +8,20 @@
8
8
  },
9
9
  "repository": {
10
10
  "type": "git",
11
- "url": "git+https://github.com/TheGroupC/groupcore-utils.git"
11
+ "url": "https://bitbucket.org/thegroupc/groupcore-utils/src/master"
12
12
  },
13
- "author": "Ken Sawyerr",
13
+ "author": "Ken",
14
14
  "license": "ISC",
15
15
  "bugs": {
16
- "url": "https://github.com/TheGroupC/groupcore-utils/issues"
16
+ "url": "https://bitbucket.org/thegroupc/groupcore-utils/src/master"
17
17
  },
18
- "homepage": "https://github.com/TheGroupC/groupcore-utils#readme",
18
+ "homepage": "https://bitbucket.org/thegroupc/groupcore-utils/src/master",
19
19
  "dependencies": {
20
20
  "axios": "^0.23.0",
21
- "lodash": "^4.17.21",
22
- "prettier": "^2.4.1"
21
+ "lodash": "^4.17.21"
23
22
  },
24
23
  "devDependencies": {
25
- "jest": "^27.2.5"
24
+ "jest": "^27.2.5",
25
+ "prettier": "^2.4.1"
26
26
  }
27
27
  }