@engage_so/core 1.3.4 → 1.4.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.
package/index.js CHANGED
@@ -1,7 +1,7 @@
1
1
  const fetch = require('cross-fetch')
2
2
  const Buffer = require('buffer/').Buffer
3
3
  const EngageError = require('./error')
4
- const root = 'https://api.engage.so'
4
+ const root = 'https://api.engage.so/v1'
5
5
  if (typeof btoa === 'undefined') {
6
6
  global.btoa = function (str) {
7
7
  return Buffer.from(str).toString('base64')
@@ -15,6 +15,9 @@ const options = {
15
15
 
16
16
  async function _request (url, params, method) {
17
17
  try {
18
+ if (!params) {
19
+ params = {}
20
+ }
18
21
  const response = await fetch(url, {
19
22
  method,
20
23
  headers: {
@@ -46,7 +49,7 @@ const request = (url, params, method) => {
46
49
 
47
50
  const init = (o) => {
48
51
  if (!o) {
49
- throw new EngageError('You need to pass in your API key')
52
+ throw new EngageError('You need to pass in your API key.')
50
53
  }
51
54
  if (typeof o === 'string') {
52
55
  options.key = o
@@ -54,7 +57,7 @@ const init = (o) => {
54
57
  }
55
58
 
56
59
  if (!o.key) {
57
- throw new EngageError('`key` missing in object')
60
+ throw new EngageError('`key` missing in object.')
58
61
  }
59
62
  if (o.key) {
60
63
  options.key = `${o.key}`
@@ -74,50 +77,50 @@ const identify = async (o) => {
74
77
  if (o.email && !/^\S+@\S+$/.test(o.email)) {
75
78
  throw new EngageError('Email invalid.')
76
79
  }
77
- const allowed = ['id', 'email', 'number', 'created_at', 'device_token', 'device_platform', 'first_name', 'last_name']
80
+ const allowed = ['id', 'is_group', 'email', 'number', 'created_at', 'device_token', 'device_platform', 'first_name', 'last_name']
78
81
  const params = {
79
82
  meta: {}
80
83
  }
81
- Object.keys(o).map(k => {
82
- if (allowed.indexOf(k) !== -1) {
84
+ for (const k in o) {
85
+ if (allowed.includes(k)) {
83
86
  params[k] = o[k]
84
87
  } else {
85
88
  params.meta[k] = o[k]
86
89
  }
87
- })
90
+ }
88
91
 
89
92
  return _request(`${root}/users/${o.id}`, params, 'PUT')
90
93
  }
91
94
 
92
95
  const addAttribute = async (uid, data) => {
93
96
  if (!uid) {
94
- throw new EngageError('User id missing')
97
+ throw new EngageError('User id missing.')
95
98
  }
96
99
  if (!data) {
97
- throw new EngageError('Attributes missing')
100
+ throw new EngageError('Attributes missing.')
98
101
  }
99
102
  if (!Object.keys(data).length) {
100
- throw new EngageError('Attributes missing')
103
+ throw new EngageError('Attributes missing.')
101
104
  }
102
- const notMeta = ['created_at', 'number', 'device_token', 'device_platform', 'email', 'first_name', 'last_name']
105
+ const notMeta = ['created_at', 'is_group', 'number', 'device_token', 'device_platform', 'email', 'first_name', 'last_name']
103
106
  const params = { meta: {} }
104
- Object.keys(data).map(k => {
105
- if (notMeta.indexOf(k) === -1) {
106
- params.meta[k] = data[k]
107
- } else {
107
+ for (const k in data) {
108
+ if (notMeta.includes(k)) {
108
109
  params[k] = data[k]
110
+ } else {
111
+ params.meta[k] = data[k]
109
112
  }
110
- })
113
+ }
111
114
 
112
115
  return _request(`${root}/users/${uid}`, params, 'PUT')
113
116
  }
114
117
 
115
118
  const track = async (uid, data) => {
116
119
  if (!uid) {
117
- throw new EngageError('User id missing')
120
+ throw new EngageError('User id missing.')
118
121
  }
119
122
  if (!data) {
120
- throw new EngageError('Attributes missing')
123
+ throw new EngageError('Attributes missing.')
121
124
  }
122
125
  if (typeof data === 'string') {
123
126
  data = {
@@ -126,17 +129,76 @@ const track = async (uid, data) => {
126
129
  }
127
130
  } else {
128
131
  if (!Object.keys(data).length) {
129
- throw new EngageError('Attributes missing')
132
+ throw new EngageError('Attributes missing.')
130
133
  }
131
134
  }
132
135
 
133
136
  return _request(`${root}/users/${uid}/events`, data, 'POST')
134
137
  }
135
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
+
136
193
  module.exports = {
137
194
  init,
138
195
  identify,
139
196
  addAttribute,
140
197
  track,
141
- request
198
+ request,
199
+ addToGroup,
200
+ removeFromGroup,
201
+ changeGroupRole,
202
+ convertToUser,
203
+ convertToGroup
142
204
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@engage_so/core",
3
- "version": "1.3.4",
3
+ "version": "1.4.1",
4
4
  "description": "Event driven customer segmentation and targeted engagement.",
5
5
  "main": "index.js",
6
6
  "scripts": {
@@ -18,5 +18,5 @@
18
18
  "devDependencies": {
19
19
  "jest": "^26.4.2"
20
20
  },
21
- "gitHead": "b288998ee988a348358f0a39c3747d1049d37dbd"
21
+ "gitHead": "7fc22abf94ef02987964ecfb6c3871314e42f922"
22
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
- })