@tellescope/sdk 0.0.7 → 0.0.11

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 (45) hide show
  1. package/lib/cjs/enduser.d.ts +207 -7
  2. package/lib/cjs/enduser.d.ts.map +1 -1
  3. package/lib/cjs/enduser.js +18 -5
  4. package/lib/cjs/enduser.js.map +1 -1
  5. package/lib/cjs/sdk.d.ts +157 -11
  6. package/lib/cjs/sdk.d.ts.map +1 -1
  7. package/lib/cjs/sdk.js +32 -9
  8. package/lib/cjs/sdk.js.map +1 -1
  9. package/lib/cjs/session.d.ts +5 -4
  10. package/lib/cjs/session.d.ts.map +1 -1
  11. package/lib/cjs/session.js +34 -0
  12. package/lib/cjs/session.js.map +1 -1
  13. package/lib/cjs/tests/public_endpoint_tests.js +3 -3
  14. package/lib/cjs/tests/public_endpoint_tests.js.map +1 -1
  15. package/lib/cjs/tests/socket_tests.js +47 -10
  16. package/lib/cjs/tests/socket_tests.js.map +1 -1
  17. package/lib/cjs/tests/tests.js +149 -44
  18. package/lib/cjs/tests/tests.js.map +1 -1
  19. package/lib/esm/enduser.d.ts +207 -7
  20. package/lib/esm/enduser.d.ts.map +1 -1
  21. package/lib/esm/enduser.js +18 -5
  22. package/lib/esm/enduser.js.map +1 -1
  23. package/lib/esm/sdk.d.ts +157 -11
  24. package/lib/esm/sdk.d.ts.map +1 -1
  25. package/lib/esm/sdk.js +32 -9
  26. package/lib/esm/sdk.js.map +1 -1
  27. package/lib/esm/session.d.ts +5 -4
  28. package/lib/esm/session.d.ts.map +1 -1
  29. package/lib/esm/session.js +34 -0
  30. package/lib/esm/session.js.map +1 -1
  31. package/lib/esm/tests/public_endpoint_tests.js +3 -3
  32. package/lib/esm/tests/public_endpoint_tests.js.map +1 -1
  33. package/lib/esm/tests/socket_tests.js +47 -10
  34. package/lib/esm/tests/socket_tests.js.map +1 -1
  35. package/lib/esm/tests/tests.js +144 -44
  36. package/lib/esm/tests/tests.js.map +1 -1
  37. package/lib/tsconfig.tsbuildinfo +1 -1
  38. package/package.json +11 -11
  39. package/src/enduser.ts +30 -6
  40. package/src/sdk.ts +38 -11
  41. package/src/session.ts +38 -3
  42. package/src/tests/public_endpoint_tests.ts +3 -3
  43. package/src/tests/socket_tests.ts +38 -8
  44. package/src/tests/tests.ts +101 -14
  45. package/tsconfig.json +0 -1
@@ -32,14 +32,14 @@ const enduser_login_tests = async () => {
32
32
  )
33
33
  await async_test(
34
34
  'setPassword',
35
- () => sdk.api.endusers.setPassword(e.id, password),
35
+ () => sdk.api.endusers.setPassword({ id: e.id, password }),
36
36
  { onResult: _ => true }
37
37
  )
38
38
 
39
39
  let authToken = 'placeholder'
40
40
  await async_test(
41
41
  'isAuthenticated (no)',
42
- () => sdk.api.endusers.isAuthenticated(e.id, authToken),
42
+ () => sdk.api.endusers.isAuthenticated({ id: e.id, authToken }),
43
43
  { onResult: ({ isAuthenticated, enduser }) => isAuthenticated === false && enduser === null }
44
44
  )
45
45
  await async_test(
@@ -49,7 +49,7 @@ const enduser_login_tests = async () => {
49
49
  )
50
50
  await async_test(
51
51
  'isAuthenticated (yes)',
52
- () => sdk.api.endusers.isAuthenticated(e.id, authToken),
52
+ () => sdk.api.endusers.isAuthenticated({ id: e.id, authToken }),
53
53
  { onResult: ({ isAuthenticated, enduser }) => isAuthenticated === true && enduser?.id === e.id }
54
54
  )
55
55
  }
@@ -7,8 +7,15 @@ import {
7
7
  import {
8
8
  objects_equivalent,
9
9
  } from "@tellescope/utilities"
10
+ import {
11
+ ChatMessage,
12
+ } from "@tellescope/types-client"
13
+ import {
14
+ Indexable,
15
+ } from "@tellescope/types-utilities"
10
16
  import { EnduserSession } from "../enduser"
11
17
  import { Session, /* APIQuery */ } from "../sdk"
18
+ import { PLACEHOLDER_ID } from "@tellescope/constants"
12
19
 
13
20
 
14
21
  const host = process.env.TEST_URL || 'http://localhost:8080'
@@ -56,14 +63,14 @@ const access_tests = async () => {
56
63
  // const user2Deletions: string[] = []
57
64
 
58
65
  user1.handle_events({
59
- 'created-chat-rooms': rs => user1Events.push(...rs),
60
- 'updated-chat-rooms': rs => user1Events.push(...rs),
61
- 'deleted-chat-rooms': rs => user1Events.push(...rs),
66
+ 'created-chat_rooms': rs => user1Events.push(...rs),
67
+ // 'updated-chat_rooms': rs => user1Events.push(...rs), sent message will create update event as cached messagepreview/sender updated
68
+ // 'deleted-chat_rooms': rs => user1Events.push(...rs),
62
69
  })
63
70
  user2.handle_events({
64
- 'created-chat-rooms': rs => user2Events.push(...rs),
65
- 'updated-chat-rooms': rs => user2Events.push(...rs),
66
- 'deleted-chat-rooms': rs => user2Events.push(...rs),
71
+ 'created-chat_rooms': rs => user2Events.push(...rs),
72
+ // 'updated-chat_rooms': rs => user2Events.push(...rs),
73
+ // 'deleted-chat_rooms': rs => user2Events.push(...rs),
67
74
  })
68
75
 
69
76
  const user1Id = user1.userInfo.id
@@ -78,6 +85,13 @@ const access_tests = async () => {
78
85
  assert(user1Events.length === 0, 'bad event distribution for filter', 'verify filter socket no self')
79
86
  assert(user2Events.length === 1 && sharedRoom.id === user2Events[0].id, 'bad event distribution for filter', 'verify filter socket push')
80
87
 
88
+ user1.removeAllSocketListeners('created-chat_rooms')
89
+ user1.removeAllSocketListeners('update-chat_rooms')
90
+ user1.removeAllSocketListeners('deleted-chat_rooms')
91
+ user2.removeAllSocketListeners('created-chat_rooms')
92
+ user2.removeAllSocketListeners('update-chat_rooms')
93
+ user2.removeAllSocketListeners('deleted-chat_rooms')
94
+
81
95
  user1.subscribe({ [room.id]: 'chats' })
82
96
  await wait(undefined, 10)
83
97
  user2.subscribe({ [room.id]: 'chats' }) // connection should be rejected
@@ -109,7 +123,7 @@ const access_tests = async () => {
109
123
  await wait(undefined, 25)
110
124
  const sharedChat2 = await user2.api.chats.createOne({ roomId: sharedRoom.id, message: "Hello there!", })
111
125
  await wait(undefined, 25)
112
-
126
+
113
127
  assert(user1Events.length === 1 && user1Events[0].id === sharedChat2.id, 'bad chats subscription', 'verify chat received')
114
128
  assert(
115
129
  user2Events.length === 2 && user2Events[1].id === sharedChat.id,
@@ -119,7 +133,7 @@ const access_tests = async () => {
119
133
 
120
134
  const enduser_tests = async () => {
121
135
  const enduser = await user1.api.endusers.createOne({ email: "enduser@tellescope.com" })
122
- await user1.api.endusers.setPassword(enduser.id, 'enduserPassword!')
136
+ await user1.api.endusers.setPassword({ id: enduser.id, password: 'enduserPassword!' })
123
137
 
124
138
  await enduserSDK.authenticate(enduser.email as string, 'enduserPassword!')
125
139
  await wait(undefined, 25)
@@ -135,15 +149,18 @@ const enduser_tests = async () => {
135
149
  await wait(undefined, 25)
136
150
 
137
151
  user1.subscribe({ [room.id]: 'chats' })
152
+ user1.subscribe({ tickets: 'tickets' })
138
153
  await wait(undefined, 10)
139
154
  enduserSDK.subscribe({ [room.id]: 'chats' })
140
155
  await wait(undefined, 10)
141
156
 
142
157
  user1.handle_events({
143
158
  'created-chats': rs => userEvents.push(...rs),
159
+ 'created-tickets': rs => userEvents.push(...rs),
144
160
  })
145
161
  enduserSDK.handle_events({
146
162
  'created-chats': rs => enduserEvents.push(...rs),
163
+ 'created-tickets': rs => enduserEvents.push(...rs),
147
164
  })
148
165
 
149
166
  const messageToEnduser = await user1.api.chats.createOne({ roomId: room.id, message: "Hello!" })
@@ -152,7 +169,20 @@ const enduser_tests = async () => {
152
169
 
153
170
  assert(objects_equivalent(userEvents[0], messageToUser), 'no message on socket', 'push message to user')
154
171
  assert(objects_equivalent(enduserEvents[0], messageToEnduser), 'no message on socket', 'push message to enduser')
172
+
173
+ const unusedTicket = await user1.api.tickets.createOne({ enduserId: PLACEHOLDER_ID, title: "For Noone" }) // should not get pushed to enduser
174
+ const ticketForEnduser = await user1.api.tickets.createOne({ enduserId: enduser.id, title: "For enduser" })
175
+ const ticketFromEnduser = await enduserSDK.api.tickets.createOne({ enduserId: enduser.id, title: "By enduser" })
176
+ await wait(undefined, 25)
177
+
178
+ assert(objects_equivalent(userEvents[1], ticketFromEnduser), 'no ticket on socket for user', 'push ticket to user')
179
+ assert(objects_equivalent(enduserEvents[1], ticketForEnduser), 'no ticket on socket for enduser', 'push ticket to enduser')
180
+ assert(enduserEvents[2] === undefined, 'enduser got an orgwide ticket', 'enduser does not receive org-wide ticket')
155
181
 
182
+ await user1.api.tickets.deleteOne(unusedTicket.id)
183
+ await user1.api.tickets.deleteOne(ticketForEnduser.id)
184
+ await user1.api.tickets.deleteOne(ticketFromEnduser.id)
185
+
156
186
  // test enduser logout
157
187
  await enduserSDK.api.endusers.logout()
158
188
  await async_test(
@@ -6,24 +6,30 @@ import {
6
6
  ClientModelForName,
7
7
  ClientModelForName_required,
8
8
  } from "@tellescope/types-client"
9
+ import {
10
+ Model as ClientModel,
11
+ ModelName,
12
+ } from "@tellescope/types-models"
9
13
 
10
-
11
- // import v from 'validator'
14
+ import {
15
+ Indexable,
16
+ Operation,
17
+ } from "@tellescope/types-utilities"
12
18
 
13
19
  import {
14
20
  fieldsToValidation,
15
21
  mongoIdValidator,
16
22
 
17
- // InputValidation,
23
+ InputValidation,
18
24
  } from "@tellescope/validation"
19
25
 
20
26
  import { Session, APIQuery, EnduserSession } from "../sdk"
21
- import { url_safe_path } from "@tellescope/utilities"
22
- import { DEFAULT_OPERATIONS } from "@tellescope/constants"
27
+ import { } from "@tellescope/utilities"
28
+ import { DEFAULT_OPERATIONS, PLACEHOLDER_ID } from "@tellescope/constants"
23
29
  import {
24
30
  schema,
25
- // Model,
26
- // ModelFields
31
+ Model,
32
+ ModelFields,
27
33
  } from "@tellescope/schema"
28
34
 
29
35
  import {
@@ -35,6 +41,7 @@ import {
35
41
 
36
42
  import {
37
43
  objects_equivalent,
44
+ url_safe_path,
38
45
  } from "@tellescope/utilities"
39
46
 
40
47
 
@@ -183,6 +190,48 @@ const threadKeyTests = async () => {
183
190
  ])
184
191
  }
185
192
 
193
+ const filterTests = async () => {
194
+ const enduser = await sdk.api.endusers.createOne({ email: 'filtertests@tellescope.com', fname: 'test' })
195
+ const otherEnduser = await sdk.api.endusers.createOne({ email: 'other@tellescope.com' })
196
+ await async_test(
197
+ `find enduser by filter`,
198
+ () => sdk.api.endusers.getSome({ filter: { email: 'filtertests@tellescope.com' }}),
199
+ { onResult: es => es.length === 1 && es[0].id === enduser.id },
200
+ )
201
+ await async_test(
202
+ `find enduser by compound filter`,
203
+ () => sdk.api.endusers.getSome({ filter: { email: 'filtertests@tellescope.com', fname: 'test' }}),
204
+ { onResult: es => es.length === 1 && es[0].id === enduser.id },
205
+ )
206
+ await async_test(
207
+ `nothing found by invalid compound filter`,
208
+ () => sdk.api.endusers.getSome({ filter: { email: 'filtertests@tellescope.com', fname: 'nottest' }}),
209
+ { onResult: es => es.length === 0 },
210
+ )
211
+ await async_test(
212
+ `nothing found for invalid filter`,
213
+ () => sdk.api.endusers.getSome({ filter: { email: 'doesnotexist@tellescope.com' }}),
214
+ { onResult: es => es.length === 0 },
215
+ )
216
+
217
+ await sdk.api.endusers.deleteOne(enduser.id)
218
+ await sdk.api.endusers.deleteOne(otherEnduser.id)
219
+ }
220
+
221
+ const updatesTests = async () => {
222
+ const enduser = await sdk.api.endusers.createOne({ email: 'test@tellescope.com', phone: '+15555555555' })
223
+ await sdk.api.endusers.updateOne(enduser.id, { phone: '+15555555552' }) // update to new phone number
224
+ await sdk.api.endusers.updateOne(enduser.id, { phone: '+15555555552' }) // update to same phone number
225
+ assert(!!enduser, '', 'Updated phone number')
226
+
227
+ const task = await sdk.api.tasks.createOne({ text: "do the thing", completed: false })
228
+ await sdk.api.tasks.updateOne(task.id, { completed: false }) // test setting false (falsey value) on update
229
+ assert(!!task, '', 'Set completed false')
230
+
231
+ await sdk.api.tasks.deleteOne(task.id)
232
+ await sdk.api.endusers.deleteOne(enduser.id)
233
+ }
234
+
186
235
  const instanceForModel = <N extends ModelName, T=ClientModelForName[N], REQ=ClientModelForName_required[N]>(model: Model<T, N>, i=0) => {
187
236
  const instance = {} as Indexable
188
237
  const updates = {} as Indexable
@@ -304,7 +353,7 @@ const run_generated_tests = async <N extends ModelName>({ queries, model, name,
304
353
  await async_test(
305
354
  `update-${singularName}`,
306
355
  () => queries.updateOne(_id, updates, { replaceObjectFields: true }),
307
- passOnVoid
356
+ { onResult: u => typeof u === 'object' && u.id === _id }
308
357
  )
309
358
  }
310
359
  await async_test(
@@ -550,7 +599,7 @@ const journey_tests = async (queries=sdk.api.journeys) => {
550
599
 
551
600
  await async_test(
552
601
  `journey-updateState`,
553
- () => queries.updateState(journey.id, 'Added', { name: 'Updated', priority: 'N/A' }),
602
+ () => queries.updateState({ id: journey.id, name: 'Added', updates: { name: 'Updated', priority: 'N/A' }}),
554
603
  passOnVoid,
555
604
  )
556
605
  await wait(undefined, 25) // wait for side effects to update endusers
@@ -813,7 +862,7 @@ const enduserAccessTests = async () => {
813
862
  const password = 'testpassword'
814
863
 
815
864
  const enduser = await sdk.api.endusers.createOne({ email })
816
- await sdk.api.endusers.setPassword(enduser.id, password).catch(console.error)
865
+ await sdk.api.endusers.setPassword({ id: enduser.id, password }).catch(console.error)
817
866
  await enduserSDK.authenticate(email, password).catch(console.error)
818
867
 
819
868
  for (const n in schema) {
@@ -864,6 +913,31 @@ const enduserAccessTests = async () => {
864
913
  }
865
914
  }
866
915
 
916
+ const ticketAccessible = await sdk.api.tickets.createOne({ enduserId: enduser.id, title: "Accessible ticket" })
917
+ const ticketInaccessible = await sdk.api.tickets.createOne({ enduserId: PLACEHOLDER_ID, title: "Inaccessible ticket" })
918
+ await async_test(
919
+ `enduser cannot create ticket for another enduser`,
920
+ () => enduserSDK.api.tickets.createOne({ enduserId: sdk.userInfo.id, title: "Error on Creation" }),
921
+ { shouldError: true, onError: e => e.message === "enduserId does not match creator id for enduser session" }
922
+ )
923
+ await async_test(
924
+ `enduser-access default, no access constraints, matching enduserId`,
925
+ () => enduserSDK.api.tickets.getOne(ticketAccessible.id),
926
+ { onResult: t => t.id === ticketAccessible.id }
927
+ )
928
+ await async_test(
929
+ `no-enduser-access default, no access constraints, non-matching enduserId`,
930
+ () => enduserSDK.api.tickets.getOne(ticketInaccessible.id),
931
+ { shouldError: true, onError: e => e.message.startsWith("Could not find")}
932
+ )
933
+ await async_test(
934
+ `no-enduser-access default, no access constraints, get many`,
935
+ () => enduserSDK.api.tickets.getSome(),
936
+ { onResult: ts => ts.length === 1 }
937
+ )
938
+
939
+ await sdk.api.tickets.deleteOne(ticketAccessible.id)
940
+ await sdk.api.tickets.deleteOne(ticketInaccessible.id)
867
941
  await sdk.api.endusers.deleteOne(enduser.id)
868
942
  }
869
943
 
@@ -879,16 +953,29 @@ const tests: { [K in keyof ClientModelForName]: () => void } = {
879
953
  chat_rooms: chat_room_tests,
880
954
  users: () => {},
881
955
  templates: () => {},
956
+ files: () => {},
957
+ tickets: () => {},
958
+ meetings: () => {},
882
959
  };
883
960
 
884
961
  (async () => {
885
962
  await sdk.authenticate(email, password, host)
886
963
 
887
964
  log_header("API")
888
- await setup_tests()
889
- await enduserAccessTests()
890
- await multi_tenant_tests() // should come right after setup tests
891
- await threadKeyTests()
965
+
966
+ try {
967
+ await setup_tests()
968
+ await multi_tenant_tests() // should come right after setup tests
969
+ await filterTests()
970
+ await updatesTests()
971
+ await threadKeyTests()
972
+ await enduserAccessTests()
973
+ } catch(err) {
974
+ console.error("Failed during custom test")
975
+ console.error(err)
976
+ process.exit()
977
+ }
978
+
892
979
 
893
980
  let n: keyof typeof schema;
894
981
  for (n in schema) {
package/tsconfig.json CHANGED
@@ -10,7 +10,6 @@
10
10
  { "path": "../constants" },
11
11
  { "path": "../schema" },
12
12
  { "path": "../testing" },
13
- { "path": "../types" },
14
13
  { "path": "../types-client" },
15
14
  { "path": "../utilities" },
16
15
  { "path": "../validation" }