1- import type { ILivechatVisitor } from '@rocket.chat/core-typings' ;
1+ import type { ILivechatVisitor , IOmnichannelRoom } from '@rocket.chat/core-typings' ;
22import { expect } from 'chai' ;
33import { after , before , describe , it } from 'mocha' ;
44import type { Response } from 'supertest' ;
55
66import { getCredentials , api , request , credentials } from '../../../data/api-data' ;
77import { createCustomField , deleteCustomField } from '../../../data/livechat/custom-fields' ;
8- import { createVisitor , deleteVisitor } from '../../../data/livechat/rooms' ;
8+ import { closeOmnichannelRoom , createLivechatRoom , createVisitor , deleteVisitor } from '../../../data/livechat/rooms' ;
99import { updatePermission , updateSetting } from '../../../data/permissions.helper' ;
1010
1111describe ( 'LIVECHAT - custom fields' , ( ) => {
@@ -118,6 +118,52 @@ describe('LIVECHAT - custom fields', () => {
118118 } ) ;
119119
120120 describe ( 'livechat/custom.fields' , ( ) => {
121+ const customFieldName = `new_custom_field_${ Date . now ( ) } _1` ;
122+ const customFieldName2 = `new_custom_field_${ Date . now ( ) } _2` ;
123+ const customFieldName3 = `new_custom_field_${ Date . now ( ) } _3` ;
124+ let visitor : ILivechatVisitor ;
125+ let visitorRoom : IOmnichannelRoom ;
126+
127+ before ( async ( ) => {
128+ await createCustomField ( {
129+ searchable : true ,
130+ field : customFieldName ,
131+ label : customFieldName ,
132+ defaultValue : 'test_default_address' ,
133+ scope : 'visitor' ,
134+ visibility : 'public' ,
135+ regexp : '' ,
136+ } ) ;
137+ await createCustomField ( {
138+ searchable : true ,
139+ field : customFieldName2 ,
140+ label : customFieldName2 ,
141+ defaultValue : 'test_default_address' ,
142+ scope : 'visitor' ,
143+ visibility : 'public' ,
144+ regexp : '' ,
145+ } ) ;
146+ await createCustomField ( {
147+ searchable : true ,
148+ field : customFieldName3 ,
149+ label : customFieldName3 ,
150+ defaultValue : 'test_default_address' ,
151+ scope : 'visitor' ,
152+ visibility : 'public' ,
153+ regexp : '' ,
154+ } ) ;
155+ visitor = await createVisitor ( ) ;
156+ // start a room for visitor2
157+ visitorRoom = await createLivechatRoom ( visitor . token ) ;
158+ } ) ;
159+ after ( async ( ) => {
160+ await Promise . all ( [
161+ deleteCustomField ( customFieldName ) ,
162+ deleteCustomField ( customFieldName2 ) ,
163+ deleteCustomField ( customFieldName3 ) ,
164+ closeOmnichannelRoom ( visitorRoom . _id ) ,
165+ ] ) ;
166+ } ) ;
121167 it ( 'should fail when token is not on body params' , async ( ) => {
122168 await request . post ( api ( 'livechat/custom.fields' ) ) . expect ( 400 ) ;
123169 } ) ;
@@ -163,16 +209,6 @@ describe('LIVECHAT - custom fields', () => {
163209 } ) ;
164210 it ( 'should save a custom field on visitor' , async ( ) => {
165211 const visitor = await createVisitor ( ) ;
166- const customFieldName = `new_custom_field_${ Date . now ( ) } ` ;
167- await createCustomField ( {
168- searchable : true ,
169- field : customFieldName ,
170- label : customFieldName ,
171- defaultValue : 'test_default_address' ,
172- scope : 'visitor' ,
173- visibility : 'public' ,
174- regexp : '' ,
175- } ) ;
176212
177213 const { body } = await request
178214 . post ( api ( 'livechat/custom.fields' ) )
@@ -188,6 +224,122 @@ describe('LIVECHAT - custom fields', () => {
188224 expect ( body . fields ) . to . have . lengthOf ( 1 ) ;
189225 expect ( body . fields [ 0 ] ) . to . have . property ( 'value' , 'test_address' ) ;
190226 } ) ;
227+ it ( 'should save multiple custom fields on a visitor' , async ( ) => {
228+ const visitor = await createVisitor ( ) ;
229+
230+ const { body } = await request
231+ . post ( api ( 'livechat/custom.fields' ) )
232+ . send ( {
233+ token : visitor . token ,
234+ customFields : [
235+ { key : customFieldName , value : 'test_address' , overwrite : true } ,
236+ { key : customFieldName2 , value : 'test_address2' , overwrite : true } ,
237+ { key : customFieldName3 , value : 'test_address3' , overwrite : true } ,
238+ ] ,
239+ } )
240+ . expect ( 200 ) ;
241+
242+ expect ( body ) . to . have . property ( 'success' , true ) ;
243+ expect ( body ) . to . have . property ( 'fields' ) ;
244+ expect ( body . fields ) . to . be . an ( 'array' ) ;
245+ expect ( body . fields ) . to . have . lengthOf ( 3 ) ;
246+ expect ( body . fields [ 0 ] ) . to . have . property ( 'value' , 'test_address' ) ;
247+ expect ( body . fields [ 1 ] ) . to . have . property ( 'value' , 'test_address2' ) ;
248+ expect ( body . fields [ 2 ] ) . to . have . property ( 'value' , 'test_address3' ) ;
249+ } ) ;
250+ it ( 'should save multiple custom fields on contact when visitor already has custom fields and an update with multiple fields is issued' , async ( ) => {
251+ const { body } = await request
252+ . post ( api ( 'livechat/custom.fields' ) )
253+ . send ( {
254+ token : visitor . token ,
255+ customFields : [ { key : customFieldName , value : 'test_address' , overwrite : true } ] ,
256+ } )
257+ . expect ( 200 ) ;
258+
259+ expect ( body ) . to . have . property ( 'success' , true ) ;
260+ expect ( body ) . to . have . property ( 'fields' ) ;
261+ expect ( body . fields ) . to . be . an ( 'array' ) ;
262+ expect ( body . fields ) . to . have . lengthOf ( 1 ) ;
263+ expect ( body . fields [ 0 ] ) . to . have . property ( 'value' , 'test_address' ) ;
264+
265+ await request
266+ . post ( api ( 'livechat/custom.fields' ) )
267+ . send ( {
268+ token : visitor . token ,
269+ customFields : [
270+ { key : customFieldName2 , value : 'test_address2' , overwrite : true } ,
271+ { key : customFieldName3 , value : 'test_address3' , overwrite : true } ,
272+ ] ,
273+ } )
274+ . expect ( 200 ) ;
275+
276+ await request
277+ . get ( api ( `omnichannel/contacts.get` ) )
278+ . set ( credentials )
279+ . query ( { contactId : visitorRoom . contactId } )
280+ . expect ( 200 )
281+ . expect ( ( res ) => {
282+ expect ( res . body ) . to . have . property ( 'contact' ) ;
283+ expect ( res . body . contact ) . to . have . property ( 'customFields' ) ;
284+ expect ( res . body . contact . customFields ) . to . have . property ( customFieldName , 'test_address' ) ;
285+ expect ( res . body . contact . customFields ) . to . have . property ( customFieldName2 , 'test_address2' ) ;
286+ expect ( res . body . contact . customFields ) . to . have . property ( customFieldName3 , 'test_address3' ) ;
287+ } ) ;
288+ } ) ;
289+ it ( 'should mark a conflict on a contact custom fields when overwrite is true and visitor already has the custom field set' , async ( ) => {
290+ await request
291+ . post ( api ( 'livechat/custom.fields' ) )
292+ . send ( {
293+ token : visitor . token ,
294+ customFields : [ { key : customFieldName , value : 'test_address_conflict' , overwrite : false } ] ,
295+ } )
296+ . expect ( 200 ) ;
297+
298+ await request
299+ . get ( api ( `omnichannel/contacts.get` ) )
300+ . set ( credentials )
301+ . query ( { contactId : visitorRoom . contactId } )
302+ . expect ( 200 )
303+ . expect ( ( res ) => {
304+ expect ( res . body ) . to . have . property ( 'contact' ) ;
305+ expect ( res . body . contact ) . to . have . property ( 'customFields' ) ;
306+ expect ( res . body . contact . customFields ) . to . have . property ( customFieldName , 'test_address' ) ;
307+ expect ( res . body . contact . customFields ) . to . have . property ( customFieldName2 , 'test_address2' ) ;
308+ expect ( res . body . contact . customFields ) . to . have . property ( customFieldName3 , 'test_address3' ) ;
309+ expect ( res . body . contact ) . to . have . property ( 'conflictingFields' ) . that . is . an ( 'array' ) ;
310+ expect ( res . body . contact . conflictingFields [ 0 ] ) . to . deep . equal ( {
311+ field : `customFields.${ customFieldName } ` ,
312+ value : 'test_address_conflict' ,
313+ } ) ;
314+ } ) ;
315+ } ) ;
316+ it ( 'should overwrite the contact custom field when overwrite is true' , async ( ) => {
317+ await request
318+ . post ( api ( 'livechat/custom.fields' ) )
319+ . send ( {
320+ token : visitor . token ,
321+ customFields : [ { key : customFieldName2 , value : 'test_new_add' , overwrite : true } ] ,
322+ } )
323+ . expect ( 200 ) ;
324+
325+ await request
326+ . get ( api ( `omnichannel/contacts.get` ) )
327+ . set ( credentials )
328+ . query ( { contactId : visitorRoom . contactId } )
329+ . expect ( 200 )
330+ . expect ( ( res ) => {
331+ expect ( res . body ) . to . have . property ( 'contact' ) ;
332+ expect ( res . body . contact ) . to . have . property ( 'customFields' ) ;
333+ expect ( res . body . contact . customFields ) . to . have . property ( customFieldName , 'test_address' ) ;
334+ expect ( res . body . contact . customFields ) . to . have . property ( customFieldName2 , 'test_new_add' ) ;
335+ expect ( res . body . contact . customFields ) . to . have . property ( customFieldName3 , 'test_address3' ) ;
336+ expect ( res . body . contact ) . to . have . property ( 'conflictingFields' ) . that . is . an ( 'array' ) ;
337+ expect ( res . body . contact . conflictingFields [ 0 ] ) . to . deep . equal ( {
338+ field : `customFields.${ customFieldName } ` ,
339+ value : 'test_address_conflict' ,
340+ } ) ;
341+ } ) ;
342+ } ) ;
191343 } ) ;
192344
193345 describe ( 'livechat/custom.field [with Contacts]' , ( ) => {
0 commit comments