1
+ import { expect , test } from '@jest/globals' ;
2
+ import { DataAccessor , UserQueryBuilder } from '../../../src' ;
3
+ import { DataValueFactory , MultiCurrency , Multilingual , MultilingualDataValue , Tracker , UserFactory } from '@relewise/client' ;
4
+ import { randomUUID } from 'crypto' ;
5
+ const { npm_config_API_KEY : API_KEY , npm_config_DATASET_ID : DATASET_ID , npm_config_SERVER_URL : SERVER_URL } = process . env ;
6
+
7
+ const dataAccessor = new DataAccessor ( DATASET_ID ! , API_KEY ! , { serverUrl : SERVER_URL } ) ;
8
+ const tracker = new Tracker ( DATASET_ID ! , API_KEY ! , { serverUrl : SERVER_URL } ) ;
9
+
10
+ test ( 'Query Users when no user found' , async ( ) => {
11
+ const randomId = randomUUID ( ) ;
12
+
13
+ const query = new UserQueryBuilder ( )
14
+ . criteria ( c => c . byAuthenticatedId ( randomId ) )
15
+ . build ( ) ;
16
+
17
+ const result = await dataAccessor . queryUsers ( query ) ;
18
+
19
+ expect ( result ?. results ) . toBeDefined ( ) ;
20
+ expect ( result ?. results ) . toHaveLength ( 1 ) ;
21
+ expect ( result ?. results ! [ 0 ] ) . toHaveLength ( 0 ) ;
22
+ } ) ;
23
+
24
+ test ( 'Query Users when user found by authenticated id' , async ( ) => {
25
+ const authenticatedId = "some authenticated id" ;
26
+
27
+ await tracker . trackProductView ( { productId : "SomeProduct" , user : UserFactory . byAuthenticatedId ( authenticatedId ) } ) ;
28
+
29
+ const query = new UserQueryBuilder ( )
30
+ . criteria ( c => c . byAuthenticatedId ( authenticatedId ) )
31
+ . build ( ) ;
32
+
33
+ const result = await dataAccessor . queryUsers ( query ) ;
34
+
35
+ expect ( result ?. results ) . toBeDefined ( ) ;
36
+ expect ( result ?. results ) . toHaveLength ( 1 ) ;
37
+ expect ( result ?. results ! [ 0 ] ) . toHaveLength ( 1 ) ;
38
+ expect ( result ?. results ! [ 0 ] [ 0 ] . authenticatedId ) . toBe ( authenticatedId ) ;
39
+ } ) ;
40
+
41
+ test ( 'Query Users when user found by temporary id' , async ( ) => {
42
+ const temporaryId = "some temporary id" ;
43
+
44
+ await tracker . trackProductView ( { productId : "SomeProduct" , user : UserFactory . byTemporaryId ( temporaryId ) } ) ;
45
+
46
+ const query = new UserQueryBuilder ( )
47
+ . criteria ( c => c . byTemporaryId ( temporaryId ) )
48
+ . build ( ) ;
49
+
50
+ const result = await dataAccessor . queryUsers ( query ) ;
51
+
52
+ expect ( result ?. results ) . toBeDefined ( ) ;
53
+ expect ( result ?. results ) . toHaveLength ( 1 ) ;
54
+ expect ( result ?. results ! [ 0 ] ) . toHaveLength ( 1 ) ;
55
+ expect ( result ?. results ! [ 0 ] [ 0 ] . temporaryId ) . toBe ( temporaryId ) ;
56
+ } ) ;
57
+
58
+ test ( 'Query Users when user found by email' , async ( ) => {
59
+ const email = "some@email.com" ;
60
+
61
+ await tracker . trackProductView ( { productId : "SomeProduct" , user : UserFactory . byEmail ( email ) } ) ;
62
+
63
+ const query = new UserQueryBuilder ( )
64
+ . criteria ( c => c . byEmail ( email ) )
65
+ . build ( ) ;
66
+
67
+ const result = await dataAccessor . queryUsers ( query ) ;
68
+
69
+ expect ( result ?. results ) . toBeDefined ( ) ;
70
+ expect ( result ?. results ) . toHaveLength ( 1 ) ;
71
+ expect ( result ?. results ! [ 0 ] ) . toHaveLength ( 1 ) ;
72
+ expect ( result ?. results ! [ 0 ] [ 0 ] . email ) . toBe ( email ) ;
73
+ } ) ;
74
+
75
+ test ( 'Query Users when user found by identifier' , async ( ) => {
76
+ const key = "SomeKey" ;
77
+ const value = "SomeValue" ;
78
+
79
+ await tracker . trackProductView ( { productId : "SomeProduct" , user : UserFactory . byIdentifier ( key , value ) } ) ;
80
+
81
+ const query = new UserQueryBuilder ( )
82
+ . criteria ( c => c . byIdentifier ( key , value ) )
83
+ . build ( ) ;
84
+
85
+ const result = await dataAccessor . queryUsers ( query ) ;
86
+
87
+ expect ( result ?. results ) . toBeDefined ( ) ;
88
+ expect ( result ?. results ) . toHaveLength ( 1 ) ;
89
+ expect ( result ?. results ! [ 0 ] ) . toHaveLength ( 1 ) ;
90
+ expect ( result ?. results ! [ 0 ] [ 0 ] . identifiers ) . toBeDefined ( ) ;
91
+
92
+ // The API always to lower cases indentifer keys
93
+ expect ( result ?. results ! [ 0 ] [ 0 ] . identifiers ! [ key . toLowerCase ( ) ] ) . toBe ( value ) ;
94
+ } ) ;
95
+
96
+ test ( 'Query Users when user found by identifier' , async ( ) => {
97
+ const key = "SomeKey" ;
98
+ const value = "SomeValue" ;
99
+
100
+ const key2 = "SomeKey2" ;
101
+ const value2 = "SomeValue2" ;
102
+
103
+ await tracker . trackProductView ( { productId : "SomeProduct" , user : UserFactory . byIdentifiers ( { [ key ] : value , [ key2 ] : value2 } ) } ) ;
104
+
105
+ const query = new UserQueryBuilder ( )
106
+ . criteria ( c => c . byIdentifiers ( { [ key ] : value , [ key2 ] : value2 } ) )
107
+ . build ( ) ;
108
+
109
+ const result = await dataAccessor . queryUsers ( query ) ;
110
+
111
+ expect ( result ?. results ) . toBeDefined ( ) ;
112
+ expect ( result ?. results ) . toHaveLength ( 1 ) ;
113
+ expect ( result ?. results ! [ 0 ] ) . toHaveLength ( 1 ) ;
114
+ expect ( result ?. results ! [ 0 ] [ 0 ] . identifiers ) . toBeDefined ( ) ;
115
+
116
+ // The API always to lower cases indentifer keys
117
+ expect ( result ?. results ! [ 0 ] [ 0 ] . identifiers ! [ key . toLowerCase ( ) ] ) . toBe ( value ) ;
118
+ expect ( result ?. results ! [ 0 ] [ 0 ] . identifiers ! [ key2 . toLowerCase ( ) ] ) . toBe ( value2 ) ;
119
+ } ) ;
120
+
121
+ test ( 'Query Users when user found by authenticated id' , async ( ) => {
122
+ const authenticatedId = "some authenticated id" ;
123
+ const temporaryId = "some temporary id" ;
124
+
125
+ await tracker . trackProductView ( { productId : "SomeProduct" , user : UserFactory . byAuthenticatedId ( authenticatedId ) } ) ;
126
+ await tracker . trackProductView ( { productId : "SomeProduct" , user : UserFactory . byTemporaryId ( temporaryId ) } ) ;
127
+
128
+ const query = new UserQueryBuilder ( )
129
+ . criteria ( c => c . byAuthenticatedId ( authenticatedId ) )
130
+ . criteria ( c => c . byTemporaryId ( temporaryId ) )
131
+ . build ( ) ;
132
+
133
+ const result = await dataAccessor . queryUsers ( query ) ;
134
+
135
+ expect ( result ?. results ) . toBeDefined ( ) ;
136
+ expect ( result ?. results ) . toHaveLength ( 2 ) ;
137
+ expect ( result ?. results ! [ 0 ] ) . toHaveLength ( 1 ) ;
138
+ expect ( result ?. results ! [ 0 ] [ 0 ] . authenticatedId ) . toBe ( authenticatedId ) ;
139
+ expect ( result ?. results ! [ 1 ] ) . toHaveLength ( 1 ) ;
140
+ expect ( result ?. results ! [ 1 ] [ 0 ] . temporaryId ) . toBe ( temporaryId ) ;
141
+ } ) ;
142
+
143
+ test ( 'Query Users when no language or currency provided' , async ( ) => {
144
+ const authenticatedId = "some authenticated id" ;
145
+
146
+ const user = UserFactory . byAuthenticatedId ( authenticatedId ) ;
147
+
148
+ const multilingualKey = "multilingual" ;
149
+ const multiCurrencyKey = "multiCurrency" ;
150
+
151
+ user . data = { } ;
152
+ user . data [ multilingualKey ] = DataValueFactory . multilingual ( [ { language : "da" , value : "123" } , { language : "en" , value : "456" } ] ) ;
153
+ user . data [ multiCurrencyKey ] = DataValueFactory . multiCurrency ( [ { currency : "DKK" , amount : 123 } , { currency : "EUR" , amount : 123 } ] ) ;
154
+
155
+ await tracker . trackProductView ( { productId : "SomeProduct" , user : user } ) ;
156
+
157
+ const query = new UserQueryBuilder ( )
158
+ . criteria ( c => c . byAuthenticatedId ( authenticatedId ) )
159
+ . build ( ) ;
160
+
161
+ const result = await dataAccessor . queryUsers ( query ) ;
162
+
163
+ expect ( result ?. results ) . toBeDefined ( ) ;
164
+ expect ( result ?. results ) . toHaveLength ( 1 ) ;
165
+ expect ( result ?. results ! [ 0 ] ) . toHaveLength ( 1 ) ;
166
+ expect ( ( result ?. results ! [ 0 ] [ 0 ] . data ! [ multilingualKey ] . value as Multilingual ) . values ) . toHaveLength ( 2 )
167
+ expect ( ( result ?. results ! [ 0 ] [ 0 ] . data ! [ multiCurrencyKey ] . value as MultiCurrency ) . values ) . toHaveLength ( 2 )
168
+ } ) ;
169
+
170
+ test ( 'Query Users when a language is provided' , async ( ) => {
171
+ const authenticatedId = "some authenticated id" ;
172
+
173
+ const user = UserFactory . byAuthenticatedId ( authenticatedId ) ;
174
+
175
+ const multilingualKey = "multilingual" ;
176
+
177
+ const da = "da" ;
178
+ const en = "en" ;
179
+
180
+ user . data = { } ;
181
+ user . data [ multilingualKey ] = DataValueFactory . multilingual ( [ { language : da , value : "123" } , { language : en , value : "456" } ] ) ;
182
+
183
+ await tracker . trackProductView ( { productId : "SomeProduct" , user : user } ) ;
184
+
185
+ const query = new UserQueryBuilder ( )
186
+ . criteria ( c => c . byAuthenticatedId ( authenticatedId ) )
187
+ . language ( da )
188
+ . build ( ) ;
189
+
190
+ const result = await dataAccessor . queryUsers ( query ) ;
191
+
192
+ expect ( result ?. results ) . toBeDefined ( ) ;
193
+ expect ( result ?. results ) . toHaveLength ( 1 ) ;
194
+ expect ( result ?. results ! [ 0 ] ) . toHaveLength ( 1 ) ;
195
+ expect ( ( result ?. results ! [ 0 ] [ 0 ] . data ! [ multilingualKey ] . value as Multilingual ) . values ) . toHaveLength ( 1 )
196
+ expect ( ( result ?. results ! [ 0 ] [ 0 ] . data ! [ multilingualKey ] . value as Multilingual ) . values ! [ 0 ] . language . value ) . toBe ( da )
197
+ } ) ;
0 commit comments