@engage_so/core 1.3.0 → 1.4.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/index.js CHANGED
@@ -1,6 +1,7 @@
1
1
  const fetch = require('cross-fetch')
2
+ const Buffer = require('buffer/').Buffer
2
3
  const EngageError = require('./error')
3
- const root = 'https://api.engage.so'
4
+ const root = 'https://api.engage.so/v1'
4
5
  if (typeof btoa === 'undefined') {
5
6
  global.btoa = function (str) {
6
7
  return Buffer.from(str).toString('base64')
@@ -14,6 +15,9 @@ const options = {
14
15
 
15
16
  async function _request (url, params, method) {
16
17
  try {
18
+ if (!params) {
19
+ params = {}
20
+ }
17
21
  const response = await fetch(url, {
18
22
  method,
19
23
  headers: {
@@ -45,7 +49,7 @@ const request = (url, params, method) => {
45
49
 
46
50
  const init = (o) => {
47
51
  if (!o) {
48
- throw new EngageError('You need to pass in your API key')
52
+ throw new EngageError('You need to pass in your API key.')
49
53
  }
50
54
  if (typeof o === 'string') {
51
55
  options.key = o
@@ -53,7 +57,7 @@ const init = (o) => {
53
57
  }
54
58
 
55
59
  if (!o.key) {
56
- throw new EngageError('`key` missing in object')
60
+ throw new EngageError('`key` missing in object.')
57
61
  }
58
62
  if (o.key) {
59
63
  options.key = `${o.key}`
@@ -77,46 +81,46 @@ const identify = async (o) => {
77
81
  const params = {
78
82
  meta: {}
79
83
  }
80
- Object.keys(o).map(k => {
84
+ for (const k in o) {
81
85
  if (allowed.indexOf(k) !== -1) {
82
86
  params[k] = o[k]
83
87
  } else {
84
88
  params.meta[k] = o[k]
85
89
  }
86
- })
90
+ }
87
91
 
88
92
  return _request(`${root}/users/${o.id}`, params, 'PUT')
89
93
  }
90
94
 
91
95
  const addAttribute = async (uid, data) => {
92
96
  if (!uid) {
93
- throw new EngageError('User id missing')
97
+ throw new EngageError('User id missing.')
94
98
  }
95
99
  if (!data) {
96
- throw new EngageError('Attributes missing')
100
+ throw new EngageError('Attributes missing.')
97
101
  }
98
102
  if (!Object.keys(data).length) {
99
- throw new EngageError('Attributes missing')
103
+ throw new EngageError('Attributes missing.')
100
104
  }
101
105
  const notMeta = ['created_at', 'number', 'device_token', 'device_platform', 'email', 'first_name', 'last_name']
102
106
  const params = { meta: {} }
103
- Object.keys(data).map(k => {
107
+ for (const k in data) {
104
108
  if (notMeta.indexOf(k) === -1) {
105
109
  params.meta[k] = data[k]
106
110
  } else {
107
111
  params[k] = data[k]
108
112
  }
109
- })
113
+ }
110
114
 
111
115
  return _request(`${root}/users/${uid}`, params, 'PUT')
112
116
  }
113
117
 
114
118
  const track = async (uid, data) => {
115
119
  if (!uid) {
116
- throw new EngageError('User id missing')
120
+ throw new EngageError('User id missing.')
117
121
  }
118
122
  if (!data) {
119
- throw new EngageError('Attributes missing')
123
+ throw new EngageError('Attributes missing.')
120
124
  }
121
125
  if (typeof data === 'string') {
122
126
  data = {
@@ -125,17 +129,76 @@ const track = async (uid, data) => {
125
129
  }
126
130
  } else {
127
131
  if (!Object.keys(data).length) {
128
- throw new EngageError('Attributes missing')
132
+ throw new EngageError('Attributes missing.')
129
133
  }
130
134
  }
131
135
 
132
136
  return _request(`${root}/users/${uid}/events`, data, 'POST')
133
137
  }
134
138
 
139
+ const addToGroup = async (uid, gid, role) => {
140
+ if (!uid) {
141
+ throw new EngageError('User id missing.')
142
+ }
143
+ if (!gid) {
144
+ throw new EngageError('Group id missing.')
145
+ }
146
+ if (role && typeof role !== 'string') {
147
+ throw new EngageError('Role should be a text.')
148
+ }
149
+ const g = {
150
+ id: gid
151
+ }
152
+ if (role) {
153
+ g.role = role
154
+ }
155
+ return _request(`${root}/users/${uid}/groups`, { groups: [g] }, 'POST')
156
+ }
157
+ const removeFromGroup = async (uid, gid) => {
158
+ if (!uid) {
159
+ throw new EngageError('User id missing.')
160
+ }
161
+ if (!gid) {
162
+ throw new EngageError('Group id missing.')
163
+ }
164
+ return _request(`${root}/users/${uid}/groups/${gid}`, null, 'DELETE')
165
+ }
166
+
167
+ const changeGroupRole = async (uid, gid, role) => {
168
+ if (!uid) {
169
+ throw new EngageError('User id missing.')
170
+ }
171
+ if (!gid) {
172
+ throw new EngageError('Group id missing.')
173
+ }
174
+ if (!role) {
175
+ throw new EngageError('New role missing.')
176
+ }
177
+ return _request(`${root}/users/${uid}/groups/${gid}`, { role }, 'PUT')
178
+ }
179
+
180
+ const convertToUser = async (uid) => {
181
+ if (!uid) {
182
+ throw new EngageError('User id missing.')
183
+ }
184
+ return _request(`${root}/users/${uid}/convert`, { type: 'user' }, 'POST')
185
+ }
186
+ const convertToGroup = async (uid) => {
187
+ if (!uid) {
188
+ throw new EngageError('User id missing.')
189
+ }
190
+ return _request(`${root}/users/${uid}/convert`, { type: 'group' }, 'POST')
191
+ }
192
+
135
193
  module.exports = {
136
194
  init,
137
195
  identify,
138
196
  addAttribute,
139
197
  track,
140
- request
198
+ request,
199
+ addToGroup,
200
+ removeFromGroup,
201
+ changeGroupRole,
202
+ convertToUser,
203
+ convertToGroup
141
204
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@engage_so/core",
3
- "version": "1.3.0",
3
+ "version": "1.4.0",
4
4
  "description": "Event driven customer segmentation and targeted engagement.",
5
5
  "main": "index.js",
6
6
  "scripts": {
@@ -12,10 +12,11 @@
12
12
  "author": "Engage",
13
13
  "license": "MIT",
14
14
  "dependencies": {
15
- "cross-fetch": "^3.0.6"
15
+ "buffer": "^6.0.3",
16
+ "cross-fetch": "^3.1.5"
16
17
  },
17
18
  "devDependencies": {
18
19
  "jest": "^26.4.2"
19
20
  },
20
- "gitHead": "c6bcac60200eaf8bbe1da30f9888cf5e755724f4"
21
+ "gitHead": "243019a68a3906c5cc2faa5c5227e4d497ee8a50"
21
22
  }
@@ -1,120 +0,0 @@
1
- const Engage = require('../index')
2
- const id = '1234'
3
-
4
- describe('Init', () => {
5
- test('should throw if no parameters sent', () => {
6
- expect(() => {
7
- Engage.init()
8
- }).toThrowError('API')
9
- })
10
- test('should throw if parameter is empty object', () => {
11
- expect(() => {
12
- Engage.init({})
13
- }).toThrowError('key')
14
- })
15
- test('should throw if empty key sent', () => {
16
- expect(() => {
17
- Engage.init('')
18
- }).toThrowError()
19
- })
20
- test('should not throw if key is sent', () => {
21
- expect(() => {
22
- Engage.init(process.env.KEY)
23
- }).not.toThrowError()
24
- })
25
- test('should not throw if key/secret is sent', () => {
26
- expect(() => {
27
- Engage.init({
28
- key: process.env.KEY,
29
- secret: process.env.SECRET
30
- })
31
- }).not.toThrowError()
32
- })
33
- })
34
-
35
- describe('Identify', () => {
36
- test('should throw if no parameter passed', async () => {
37
- await expect(Engage.identify()).rejects.toThrowError('object')
38
- })
39
- test('should throw if empty string passed', async () => {
40
- await expect(Engage.identify('')).rejects.toThrowError('id')
41
- })
42
- test('should throw if empty object passed', async () => {
43
- await expect(Engage.identify({})).rejects.toThrowError(/id/i)
44
- })
45
- test('should throw if no email passed', async () => {
46
- await expect(Engage.identify({ id })).rejects.toThrowError(/email/i)
47
- })
48
- test('should throw if invalid email passed', async () => {
49
- await expect(Engage.identify({
50
- id,
51
- email: 'invalid'
52
- })).rejects.toThrowError(/email/i)
53
- })
54
- test('should work if id and email passed', async () => {
55
- await expect(Engage.identify({
56
- id,
57
- email: 'fickledreams@yahoo.com'
58
- })).resolves.toMatchObject({
59
- status: 'ok'
60
- })
61
- })
62
- })
63
-
64
- describe('Add attribute', () => {
65
- test('should throw if no parameter passed', async () => {
66
- await expect(Engage.addAttribute()).rejects.toThrowError('id')
67
- })
68
- test('should throw if no data attribute passed', async () => {
69
- await expect(Engage.addAttribute(id)).rejects.toThrowError(/attributes/i)
70
- })
71
- test('should throw if empty object passed', async () => {
72
- await expect(Engage.addAttribute(id, {})).rejects.toThrowError(/attributes/i)
73
- })
74
- test('should resolve if parameters passed', async () => {
75
- await expect(Engage.addAttribute(id, {
76
- first_name: 'Opeyemi',
77
- active: true,
78
- created_at: '2020-08-11'
79
- })).resolves.toMatchObject({
80
- status: 'ok'
81
- })
82
- })
83
- })
84
-
85
- describe('Track', () => {
86
- test('should throw if no parameter passed', async () => {
87
- await expect(Engage.track()).rejects.toThrowError('id')
88
- })
89
- test('should throw if no data attribute passed', async () => {
90
- await expect(Engage.track(id)).rejects.toThrowError(/attributes/i)
91
- })
92
- test('should throw if empty object passed', async () => {
93
- await expect(Engage.track(id, {})).rejects.toThrowError(/attributes/i)
94
- })
95
- test('should pass if string property passed', async () => {
96
- await expect(Engage.track(id, 'loggedin')).resolves.toMatchObject({
97
- status: 'ok'
98
- })
99
- })
100
- test('should pass if right parameters passed', async () => {
101
- await expect(Engage.track(id, {
102
- event: 'played',
103
- value: 'vid_133',
104
- timestamp: '2020-05-30T09:30:10Z'
105
- })).resolves.toMatchObject({
106
- status: 'ok'
107
- })
108
- })
109
- test('should resolve if other variant passed', async () => {
110
- await expect(Engage.track(id, {
111
- event: 'loggedin',
112
- properties: {
113
- ip: '127.0.0.1',
114
- referral: 'localhost'
115
- }
116
- })).resolves.toMatchObject({
117
- status: 'ok'
118
- })
119
- })
120
- })