From 917720cb115768550b58074c58de1e6a84bfd411 Mon Sep 17 00:00:00 2001 From: matheusbsilva137 Date: Fri, 22 Nov 2024 16:17:19 -0300 Subject: [PATCH] validate custom fields on mapVisitorToContact --- .../server/lib/contacts/createContact.ts | 2 +- .../lib/contacts/mapVisitorToContact.spec.ts | 3 +++ .../lib/contacts/mapVisitorToContact.ts | 5 ++++- .../lib/contacts/validateCustomFields.spec.ts | 22 ------------------- 4 files changed, 8 insertions(+), 24 deletions(-) diff --git a/apps/meteor/app/livechat/server/lib/contacts/createContact.ts b/apps/meteor/app/livechat/server/lib/contacts/createContact.ts index cd6eb493bed2..efdcacdbf4ad 100644 --- a/apps/meteor/app/livechat/server/lib/contacts/createContact.ts +++ b/apps/meteor/app/livechat/server/lib/contacts/createContact.ts @@ -33,7 +33,7 @@ export async function createContact({ } const allowedCustomFields = await getAllowedCustomFields(); - const customFields = validateCustomFields(allowedCustomFields, receivedCustomFields, { onlyFilterFields: true }); + const customFields = validateCustomFields(allowedCustomFields, receivedCustomFields); return LivechatContacts.insertContact({ name, diff --git a/apps/meteor/app/livechat/server/lib/contacts/mapVisitorToContact.spec.ts b/apps/meteor/app/livechat/server/lib/contacts/mapVisitorToContact.spec.ts index 65f26edfb83c..81196e4b6421 100644 --- a/apps/meteor/app/livechat/server/lib/contacts/mapVisitorToContact.spec.ts +++ b/apps/meteor/app/livechat/server/lib/contacts/mapVisitorToContact.spec.ts @@ -6,11 +6,13 @@ import sinon from 'sinon'; import type { CreateContactParams } from './createContact'; const getContactManagerIdByUsername = sinon.stub(); +const getAllowedCustomFields = sinon.stub(); const { mapVisitorToContact } = proxyquire.noCallThru().load('./mapVisitorToContact', { './getContactManagerIdByUsername': { getContactManagerIdByUsername, }, + './getAllowedCustomFields': { getAllowedCustomFields }, }); const testDate = new Date(); @@ -142,6 +144,7 @@ describe('mapVisitorToContact', () => { return undefined; }); + getAllowedCustomFields.returns([]); }); const index = 0; diff --git a/apps/meteor/app/livechat/server/lib/contacts/mapVisitorToContact.ts b/apps/meteor/app/livechat/server/lib/contacts/mapVisitorToContact.ts index ad1ac994ce0f..961382171ac6 100644 --- a/apps/meteor/app/livechat/server/lib/contacts/mapVisitorToContact.ts +++ b/apps/meteor/app/livechat/server/lib/contacts/mapVisitorToContact.ts @@ -1,7 +1,9 @@ import type { ILivechatVisitor, IOmnichannelSource } from '@rocket.chat/core-typings'; import type { CreateContactParams } from './createContact'; +import { getAllowedCustomFields } from './getAllowedCustomFields'; import { getContactManagerIdByUsername } from './getContactManagerIdByUsername'; +import { validateCustomFields } from './validateCustomFields'; export async function mapVisitorToContact(visitor: ILivechatVisitor, source: IOmnichannelSource): Promise { return { @@ -24,7 +26,8 @@ export async function mapVisitorToContact(visitor: ILivechatVisitor, source: IOm details: source, }, ], - customFields: visitor.livechatData, + customFields: + visitor.livechatData && validateCustomFields(await getAllowedCustomFields(), visitor.livechatData, { onlyFilterFields: true }), lastChat: visitor.lastChat, contactManager: visitor.contactManager?.username && (await getContactManagerIdByUsername(visitor.contactManager.username)), }; diff --git a/apps/meteor/app/livechat/server/lib/contacts/validateCustomFields.spec.ts b/apps/meteor/app/livechat/server/lib/contacts/validateCustomFields.spec.ts index 82b967cd35f6..960067755b18 100644 --- a/apps/meteor/app/livechat/server/lib/contacts/validateCustomFields.spec.ts +++ b/apps/meteor/app/livechat/server/lib/contacts/validateCustomFields.spec.ts @@ -20,23 +20,10 @@ describe('validateCustomFields', () => { .and.to.equal({}); }); - it('should NOT throw an error when a non-required custom field is missing', () => { - const allowedCustomFields = [{ _id: 'field1', label: 'Field 1', required: false }]; - const customFields = {}; - - expect(() => validateCustomFields(allowedCustomFields, customFields)).not.to.throw(); - }); - it('should throw an error if a custom field value does not match the regexp', () => { expect(() => validateCustomFields(mockCustomFields, { cf1: 'invalid' })).to.throw(); }); - it('should not throw an error if a custom field value does not match the regexp, but the onlyFilterFields option is provided', () => { - expect(() => validateCustomFields(mockCustomFields, { cf1: 'invalid' }, { onlyFilterFields: true })) - .not.to.throw() - .and.to.equal({}); - }); - it('should handle an empty customFields input without throwing an error', () => { const allowedCustomFields = [{ _id: 'field1', label: 'Field 1', required: false }]; const customFields = {}; @@ -50,13 +37,4 @@ describe('validateCustomFields', () => { expect(() => validateCustomFields(allowedCustomFields, customFields)).to.throw(); }); - - it('should not throw an error if a extra custom field is passed, but the onlyFilterFields option is provided', () => { - const allowedCustomFields = [{ _id: 'field1', label: 'Field 1', required: false }]; - const customFields = { field2: 'value' }; - - expect(() => validateCustomFields(allowedCustomFields, customFields, { onlyFilterFields: true })) - .not.to.throw() - .and.to.equal({}); - }); });