groupcore-utils 1.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 ADDED
@@ -0,0 +1,25 @@
1
+ {
2
+ "env": {
3
+ "browser": true,
4
+ "commonjs": true,
5
+ "es2021": true
6
+ },
7
+ "extends": ["eslint:recommended", "airbnb", "prettier", "plugin:jest/all"],
8
+ "parserOptions": {
9
+ "ecmaVersion": 12
10
+ },
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"
24
+ }
25
+ }
@@ -0,0 +1,5 @@
1
+ {
2
+ "trailingComma": "es5",
3
+ "singleQuote": true,
4
+ "printWidth":200
5
+ }
package/Utils.js ADDED
@@ -0,0 +1,85 @@
1
+ const axios = require('axios');
2
+ const _isEmpty = require('lodash').isEmpty;
3
+
4
+ class Utils {
5
+ constructor({ projectId, apiToken, apiUrl }) {
6
+ this.project = projectId;
7
+ this.token = apiToken;
8
+ this.apiUrl = apiUrl;
9
+ }
10
+
11
+ submitForm({ formData, formId, email, leadId = 0 }) {
12
+ if (_isEmpty(formData) || _isEmpty(String(formId)) || _isEmpty(email)) {
13
+ console.log('got here');
14
+ return { error: 'Missing some required params' };
15
+ }
16
+
17
+ return axios({
18
+ method: 'POST',
19
+ url: `${this.apiUrl}/com/submissions`,
20
+ data: {
21
+ formData,
22
+ formId,
23
+ email,
24
+ leadId,
25
+ },
26
+ headers: {
27
+ token: this.token,
28
+ },
29
+ });
30
+ }
31
+
32
+ sendMail({ recipientEmail, subject, htmlBody, hasAttachment = false, attachmentFilename, attachmentUrl }) {
33
+ if (hasAttachment) {
34
+ if (_isEmpty(attachmentFilename) || _isEmpty(attachmentUrl)) {
35
+ return { error: 'Some missing params. If hasAttachment is true, both attachmentFilename and attachmentUrl are required' };
36
+ }
37
+ }
38
+
39
+ if (!hasAttachment && (attachmentFilename || attachmentUrl)) {
40
+ return { error: 'If hasAttachment is false, both attachmentFilename and attachmentUrl cannot be accepted' };
41
+ }
42
+
43
+ if (_isEmpty(recipientEmail) || _isEmpty(subject) || _isEmpty(htmlBody)) {
44
+ return { error: 'Missing some required params' };
45
+ }
46
+
47
+ return axios({
48
+ method: 'POST',
49
+ url: `${this.apiUrl}/mail`,
50
+ data: {
51
+ project: this.project,
52
+ email: recipientEmail,
53
+ message: htmlBody,
54
+ subject,
55
+ attachments: hasAttachment ? [{ filename: attachmentFilename, path: attachmentUrl }] : null,
56
+ },
57
+ headers: {
58
+ token: this.token,
59
+ },
60
+ });
61
+ }
62
+
63
+ addLead({ firstname, lastname, phone = null, email }) {
64
+ if (_isEmpty(firstname) || _isEmpty(lastname) || _isEmpty(email)) {
65
+ return { error: 'Missing some required params' };
66
+ }
67
+
68
+ return axios({
69
+ method: 'POST',
70
+ url: `${this.apiUrl}/com/leads`,
71
+ data: {
72
+ project: this.project,
73
+ firstname,
74
+ lastname,
75
+ email,
76
+ phone,
77
+ },
78
+ headers: {
79
+ token: this.token,
80
+ },
81
+ });
82
+ }
83
+ }
84
+
85
+ module.exports = Utils;
package/Utils.spec.js ADDED
@@ -0,0 +1,91 @@
1
+ const Utils = require('./Utils');
2
+ const util = new Utils({ projectId: 10, apiToken: 'sampletoken', apiUrl: 'https://' });
3
+ const axios = require('axios');
4
+
5
+ jest.mock('axios');
6
+
7
+ describe('Testing Utils.submitForm()', () => {
8
+ test('It should get a status 204 as response', async () => {
9
+ axios.mockResolvedValue({ status: 200 });
10
+ const response = await util.submitForm({ formData: { name: 'test' }, formId: 300, email: 'test@email.com' });
11
+ expect(response.status).toEqual(200);
12
+ });
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);
18
+ });
19
+ });
20
+
21
+ describe('Testing Utils.sendMail()', () => {
22
+ test('It should return right response', async () => {
23
+ axios.mockResolvedValue({ status: 204 });
24
+ const response = await util.sendMail({
25
+ recipientEmail: 'test@email.com',
26
+ subject: 'Test subject',
27
+ htmlBody: '<p>Html body</p>',
28
+ });
29
+ expect(response.status).toEqual(204);
30
+ });
31
+
32
+ test('It should return right response with attachment', async () => {
33
+ axios.mockResolvedValue({ status: 204 });
34
+ const response = await util.sendMail({
35
+ recipientEmail: 'test@email.com',
36
+ subject: 'Test subject',
37
+ htmlBody: '<p>Html body</p>',
38
+ hasAttachment: true,
39
+ attachmentFilename: 'file',
40
+ attachmentUrl: 'url',
41
+ });
42
+ expect(response.status).toEqual(204);
43
+ });
44
+
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);
55
+ });
56
+
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);
66
+ });
67
+
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);
74
+ });
75
+ });
76
+
77
+ describe('Testing Utils.addLead()', () => {
78
+ test('It should return right response', async () => {
79
+ axios.mockResolvedValue({ data: { insertId: 3 } });
80
+ const response = await util.addLead({ firstname: 'test', lastname: 'test', email: 'test@email.com', phone: '123456' });
81
+ expect(response.data.insertId).toEqual(3);
82
+ });
83
+
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);
90
+ });
91
+ });
package/package.json ADDED
@@ -0,0 +1,27 @@
1
+ {
2
+ "name": "groupcore-utils",
3
+ "version": "1.0.0",
4
+ "description": "Utilities for working with some Core features",
5
+ "main": "Utils.js",
6
+ "scripts": {
7
+ "test": "jest --coverage"
8
+ },
9
+ "repository": {
10
+ "type": "git",
11
+ "url": "git+https://github.com/TheGroupC/groupcore-utils.git"
12
+ },
13
+ "author": "Ken Sawyerr",
14
+ "license": "ISC",
15
+ "bugs": {
16
+ "url": "https://github.com/TheGroupC/groupcore-utils/issues"
17
+ },
18
+ "homepage": "https://github.com/TheGroupC/groupcore-utils#readme",
19
+ "dependencies": {
20
+ "axios": "^0.23.0",
21
+ "lodash": "^4.17.21",
22
+ "prettier": "^2.4.1"
23
+ },
24
+ "devDependencies": {
25
+ "jest": "^27.2.5"
26
+ }
27
+ }