From 621ca340a9919414216739015293d63cfc0b6f03 Mon Sep 17 00:00:00 2001 From: murtaza98 Date: Tue, 14 Feb 2023 09:58:37 +0530 Subject: [PATCH 1/5] [FIX] Custom fields does not appear on the registration form --- .../app/livechat/server/api/lib/livechat.ts | 2 +- .../app/livechat/server/api/v1/config.ts | 2 +- .../tests/data/livechat/custom-fields.ts | 11 +++++-- .../end-to-end/api/livechat/11-livechat.ts | 29 +++++++++++++++++++ 4 files changed, 40 insertions(+), 4 deletions(-) diff --git a/apps/meteor/app/livechat/server/api/lib/livechat.ts b/apps/meteor/app/livechat/server/api/lib/livechat.ts index 789455d6723f2..5228c2ca53bd9 100644 --- a/apps/meteor/app/livechat/server/api/lib/livechat.ts +++ b/apps/meteor/app/livechat/server/api/lib/livechat.ts @@ -209,7 +209,7 @@ export async function settings({ businessUnit = '' }: { businessUnit?: string } }; } -export async function getExtraConfigInfo(room: IOmnichannelRoom): Promise { +export async function getExtraConfigInfo(room?: IOmnichannelRoom): Promise { return callbacks.run('livechat.onLoadConfigApi', { room }); } diff --git a/apps/meteor/app/livechat/server/api/v1/config.ts b/apps/meteor/app/livechat/server/api/v1/config.ts index 13c6b0be8f725..be95fd61cc25d 100644 --- a/apps/meteor/app/livechat/server/api/v1/config.ts +++ b/apps/meteor/app/livechat/server/api/v1/config.ts @@ -28,7 +28,7 @@ API.v1.addRoute( const room = guest && findOpenRoom(token); const agent = guest && room && room.servedBy && findAgent(room.servedBy._id); - const extra = room && (await getExtraConfigInfo(room)); + const extra = await getExtraConfigInfo(room); return API.v1.success({ config: { ...config, online: status, ...extra, ...(guest && { guest }), ...(room && { room }), ...(agent && { agent }) }, }); diff --git a/apps/meteor/tests/data/livechat/custom-fields.ts b/apps/meteor/tests/data/livechat/custom-fields.ts index 2bbb6c732881a..0d5dafa9113fe 100644 --- a/apps/meteor/tests/data/livechat/custom-fields.ts +++ b/apps/meteor/tests/data/livechat/custom-fields.ts @@ -39,8 +39,15 @@ export const createCustomField = (customField: ILivechatCustomField) => new Prom export const deleteCustomField = (customFieldID: string) => new Promise((resolve, reject) => { request - .post(methodCall('livechat:saveCustomField')) - .send(customFieldID) + .post(methodCall('livechat:removeCustomField')) + .send({ + message: JSON.stringify({ + method: 'livechat:removeCustomField', + params: [customFieldID], + id: 'id', + msg: 'method', + }), + }) .set(credentials) .end((err: Error, res: Response): void => { if (err) { diff --git a/apps/meteor/tests/end-to-end/api/livechat/11-livechat.ts b/apps/meteor/tests/end-to-end/api/livechat/11-livechat.ts index f6f059e5b42ec..94cb6ecdffca0 100644 --- a/apps/meteor/tests/end-to-end/api/livechat/11-livechat.ts +++ b/apps/meteor/tests/end-to-end/api/livechat/11-livechat.ts @@ -1,8 +1,10 @@ /* eslint-env mocha */ +import type { ILivechatCustomField } from '@rocket.chat/core-typings'; import { expect } from 'chai'; import { getCredentials, api, request, credentials } from '../../../data/api-data'; +import { createCustomField, deleteCustomField } from '../../../data/livechat/custom-fields'; import { createVisitor, createLivechatRoom } from '../../../data/livechat/rooms'; import { updateSetting } from '../../../data/permissions.helper'; @@ -75,6 +77,33 @@ describe('LIVECHAT - Utils', function () { expect(body.config).to.have.property('settings'); expect(body.config).to.have.property('departments').that.is.an('array'); }); + it('should have custom fields data', async () => { + const customFieldName = `new_custom_field_${Date.now()}`; + await createCustomField({ + searchable: true, + field: customFieldName, + label: customFieldName, + defaultValue: 'test_default_address', + scope: 'visitor', + visibility: 'visible', + regexp: '', + public: true, + required: false, + + options: '', + } as unknown as ILivechatCustomField & { field: string }); + + const { body } = await request.get(api('livechat/config')).set(credentials); + + expect(body).to.have.property('config'); + expect(body.config).to.have.property('customFields').that.is.an('array'); + expect(body.config.customFields).to.have.length.greaterThan(0); + const customField = body.config.customFields.find((field: any) => field._id === customFieldName); + expect(customField).to.be.an('object'); + expect(customField).to.have.property('label', customFieldName); + + await deleteCustomField(customFieldName); + }); }); describe('livechat/page.visited', () => { From 233705e8539a10596c40acf1cf138f6211fd0f4b Mon Sep 17 00:00:00 2001 From: murtaza98 Date: Tue, 14 Feb 2023 11:51:00 +0530 Subject: [PATCH 2/5] fix types --- apps/meteor/app/livechat/server/api/v1/config.ts | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/apps/meteor/app/livechat/server/api/v1/config.ts b/apps/meteor/app/livechat/server/api/v1/config.ts index be95fd61cc25d..74148c4f92c04 100644 --- a/apps/meteor/app/livechat/server/api/v1/config.ts +++ b/apps/meteor/app/livechat/server/api/v1/config.ts @@ -1,5 +1,6 @@ import mem from 'mem'; import { isGETLivechatConfigParams } from '@rocket.chat/rest-typings'; +import type { ILivechatVisitor, IOmnichannelRoom } from '@rocket.chat/core-typings'; import { API } from '../../../../api/server'; import { Livechat } from '../../lib/Livechat'; @@ -18,14 +19,14 @@ API.v1.addRoute( return API.v1.success({ config: { enabled: false } }); } - const { token, department, businessUnit } = this.queryParams; + const { token, department, businessUnit } = this.queryParams as { token?: string; department?: string; businessUnit?: string }; const config = await cachedSettings({ businessUnit }); const status = Livechat.online(department); - const guest = token && (await Livechat.findGuest(token)); + const guest: ILivechatVisitor | null = token ? await Livechat.findGuest(token) : null; - const room = guest && findOpenRoom(token); + const room: IOmnichannelRoom | undefined = guest && token ? findOpenRoom(token) : undefined; const agent = guest && room && room.servedBy && findAgent(room.servedBy._id); const extra = await getExtraConfigInfo(room); From ab623cb7a6da5be60bb98963a4d917cf067863b0 Mon Sep 17 00:00:00 2001 From: murtaza98 Date: Tue, 14 Feb 2023 19:01:37 +0530 Subject: [PATCH 3/5] remove unwanted type override --- apps/meteor/app/livechat/server/api/v1/config.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/apps/meteor/app/livechat/server/api/v1/config.ts b/apps/meteor/app/livechat/server/api/v1/config.ts index 74148c4f92c04..d79110b5d9764 100644 --- a/apps/meteor/app/livechat/server/api/v1/config.ts +++ b/apps/meteor/app/livechat/server/api/v1/config.ts @@ -19,7 +19,7 @@ API.v1.addRoute( return API.v1.success({ config: { enabled: false } }); } - const { token, department, businessUnit } = this.queryParams as { token?: string; department?: string; businessUnit?: string }; + const { token, department, businessUnit } = this.queryParams; const config = await cachedSettings({ businessUnit }); From be131af5166d0861525a9a8bf3f422afb15fd745 Mon Sep 17 00:00:00 2001 From: murtaza98 Date: Tue, 14 Feb 2023 19:57:40 +0530 Subject: [PATCH 4/5] CRS --- apps/meteor/app/livechat/server/api/v1/config.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/apps/meteor/app/livechat/server/api/v1/config.ts b/apps/meteor/app/livechat/server/api/v1/config.ts index d79110b5d9764..4a655c4c2b2eb 100644 --- a/apps/meteor/app/livechat/server/api/v1/config.ts +++ b/apps/meteor/app/livechat/server/api/v1/config.ts @@ -26,7 +26,7 @@ API.v1.addRoute( const status = Livechat.online(department); const guest: ILivechatVisitor | null = token ? await Livechat.findGuest(token) : null; - const room: IOmnichannelRoom | undefined = guest && token ? findOpenRoom(token) : undefined; + const room: IOmnichannelRoom | undefined = guest ? findOpenRoom(guest.token) : undefined; const agent = guest && room && room.servedBy && findAgent(room.servedBy._id); const extra = await getExtraConfigInfo(room); From a6e29730d02e8146a29c457cb156b883f5232d0b Mon Sep 17 00:00:00 2001 From: Kevin Aleman Date: Wed, 15 Feb 2023 13:53:26 -0600 Subject: [PATCH 5/5] ts be happy --- .../app/livechat/server/api/v1/config.ts | 5 +- .../tests/data/livechat/custom-fields.ts | 51 ++++++++++--------- .../tests/end-to-end/api/livechat/00-rooms.ts | 11 +--- .../api/livechat/03-custom-fields.ts | 5 +- .../end-to-end/api/livechat/09-visitors.ts | 6 +-- .../end-to-end/api/livechat/11-livechat.ts | 4 +- 6 files changed, 36 insertions(+), 46 deletions(-) diff --git a/apps/meteor/app/livechat/server/api/v1/config.ts b/apps/meteor/app/livechat/server/api/v1/config.ts index 4a655c4c2b2eb..9cba2db92368b 100644 --- a/apps/meteor/app/livechat/server/api/v1/config.ts +++ b/apps/meteor/app/livechat/server/api/v1/config.ts @@ -1,6 +1,5 @@ import mem from 'mem'; import { isGETLivechatConfigParams } from '@rocket.chat/rest-typings'; -import type { ILivechatVisitor, IOmnichannelRoom } from '@rocket.chat/core-typings'; import { API } from '../../../../api/server'; import { Livechat } from '../../lib/Livechat'; @@ -24,9 +23,9 @@ API.v1.addRoute( const config = await cachedSettings({ businessUnit }); const status = Livechat.online(department); - const guest: ILivechatVisitor | null = token ? await Livechat.findGuest(token) : null; + const guest = token ? await Livechat.findGuest(token) : null; - const room: IOmnichannelRoom | undefined = guest ? findOpenRoom(guest.token) : undefined; + const room = guest ? findOpenRoom(guest.token) : undefined; const agent = guest && room && room.servedBy && findAgent(room.servedBy._id); const extra = await getExtraConfigInfo(room); diff --git a/apps/meteor/tests/data/livechat/custom-fields.ts b/apps/meteor/tests/data/livechat/custom-fields.ts index 0d5dafa9113fe..f41fcf41115f3 100644 --- a/apps/meteor/tests/data/livechat/custom-fields.ts +++ b/apps/meteor/tests/data/livechat/custom-fields.ts @@ -2,36 +2,37 @@ import type { Response } from 'supertest'; import type { ILivechatCustomField } from '@rocket.chat/core-typings'; import { credentials, request, methodCall, api } from './../api-data'; -export const createCustomField = (customField: ILivechatCustomField) => new Promise((resolve, reject) => { +type ExtendedCustomField = Omit & { field: string }; + +export const createCustomField = (customField: ExtendedCustomField): Promise => new Promise((resolve, reject) => { request - .get(api('livechat/custom-fields/'+customField.label)) + .get(api(`livechat/custom-fields/${customField.label}`)) .set(credentials) .send() - .end((err: Error, res:Response) => { + .end((err: Error, res: Response) => { if (err) { - reject(err); + return reject(err); + } + if (res.body.customField != null && res.body.customField != undefined) { + resolve(res.body.customField); } else { - if (res.body.customField != null && res.body.customField != undefined) { - resolve(res.body.customField); - }else{ - request - .post(methodCall('livechat:saveCustomField')) - .send({ - message: JSON.stringify({ - method: 'livechat:saveCustomField', - params: [null,customField], - id: 'id', - msg: 'method', - }), - }) - .set(credentials) - .end((err: Error, res: Response): void => { - if (err) { - return reject(err); - } - resolve(res.body); - }); - } + request + .post(methodCall('livechat:saveCustomField')) + .send({ + message: JSON.stringify({ + method: 'livechat:saveCustomField', + params: [null, customField], + id: 'id', + msg: 'method', + }), + }) + .set(credentials) + .end((err: Error, res: Response): void => { + if (err) { + return reject(err); + } + resolve(res.body); + }); } }); diff --git a/apps/meteor/tests/end-to-end/api/livechat/00-rooms.ts b/apps/meteor/tests/end-to-end/api/livechat/00-rooms.ts index fb511c3e64fed..138129384051b 100644 --- a/apps/meteor/tests/end-to-end/api/livechat/00-rooms.ts +++ b/apps/meteor/tests/end-to-end/api/livechat/00-rooms.ts @@ -4,14 +4,7 @@ import fs from 'fs'; import path from 'path'; import { expect } from 'chai'; -import type { - IOmnichannelRoom, - ILivechatVisitor, - IUser, - IOmnichannelSystemMessage, - ILivechatCustomField, - ILivechatPriority, -} from '@rocket.chat/core-typings'; +import type { IOmnichannelRoom, ILivechatVisitor, IUser, IOmnichannelSystemMessage, ILivechatPriority } from '@rocket.chat/core-typings'; import { LivechatPriorityWeight } from '@rocket.chat/core-typings'; import type { Response } from 'supertest'; import faker from '@faker-js/faker'; @@ -1359,7 +1352,7 @@ describe('LIVECHAT - rooms', function () { scope: 'room', visibility: 'visible', regexp: '', - } as unknown as ILivechatCustomField & { field: string }); + }); const newVisitor = await createVisitor(); const newRoom = await createLivechatRoom(newVisitor.token); diff --git a/apps/meteor/tests/end-to-end/api/livechat/03-custom-fields.ts b/apps/meteor/tests/end-to-end/api/livechat/03-custom-fields.ts index 8681d04d88ca6..6e81751538db6 100644 --- a/apps/meteor/tests/end-to-end/api/livechat/03-custom-fields.ts +++ b/apps/meteor/tests/end-to-end/api/livechat/03-custom-fields.ts @@ -1,6 +1,5 @@ /* eslint-env mocha */ -import type { ILivechatCustomField } from '@rocket.chat/core-typings'; import { expect } from 'chai'; import type { Response } from 'supertest'; @@ -117,7 +116,7 @@ describe('LIVECHAT - custom fields', function () { scope: 'visitor', visibility: 'public', regexp: '', - } as unknown as ILivechatCustomField & { field: string }); + }); const { body } = await request .post(api('livechat/custom.field')) @@ -176,7 +175,7 @@ describe('LIVECHAT - custom fields', function () { scope: 'visitor', visibility: 'public', regexp: '', - } as unknown as ILivechatCustomField & { field: string }); + }); const { body } = await request .post(api('livechat/custom.fields')) diff --git a/apps/meteor/tests/end-to-end/api/livechat/09-visitors.ts b/apps/meteor/tests/end-to-end/api/livechat/09-visitors.ts index cc32e2c3b7868..e91e9140d051a 100644 --- a/apps/meteor/tests/end-to-end/api/livechat/09-visitors.ts +++ b/apps/meteor/tests/end-to-end/api/livechat/09-visitors.ts @@ -1,7 +1,7 @@ /* eslint-env mocha */ import { expect } from 'chai'; -import type { ILivechatAgent, ILivechatCustomField, ILivechatVisitor, IOmnichannelRoom } from '@rocket.chat/core-typings'; +import type { ILivechatAgent, ILivechatVisitor, IOmnichannelRoom } from '@rocket.chat/core-typings'; import type { Response } from 'supertest'; import { getCredentials, api, request, credentials } from '../../../data/api-data'; @@ -69,7 +69,7 @@ describe('LIVECHAT - visitors', function () { scope: 'visitor', visibility: 'public', regexp: '', - } as unknown as ILivechatCustomField & { field: string }); + }); const { body } = await request.post(api('livechat/visitor')).send({ visitor: { token, @@ -693,7 +693,7 @@ describe('LIVECHAT - visitors', function () { scope: 'visitor', visibility: 'public', regexp: '', - } as unknown as ILivechatCustomField & { field: string }) + }) .then((cf) => { if (!cf) { throw new Error('Custom field not created'); diff --git a/apps/meteor/tests/end-to-end/api/livechat/11-livechat.ts b/apps/meteor/tests/end-to-end/api/livechat/11-livechat.ts index 94cb6ecdffca0..73b42ba86b6d9 100644 --- a/apps/meteor/tests/end-to-end/api/livechat/11-livechat.ts +++ b/apps/meteor/tests/end-to-end/api/livechat/11-livechat.ts @@ -1,6 +1,5 @@ /* eslint-env mocha */ -import type { ILivechatCustomField } from '@rocket.chat/core-typings'; import { expect } from 'chai'; import { getCredentials, api, request, credentials } from '../../../data/api-data'; @@ -89,9 +88,8 @@ describe('LIVECHAT - Utils', function () { regexp: '', public: true, required: false, - options: '', - } as unknown as ILivechatCustomField & { field: string }); + }); const { body } = await request.get(api('livechat/config')).set(credentials);