Skip to content

Commit

Permalink
validate custom fields on mapVisitorToContact
Browse files Browse the repository at this point in the history
  • Loading branch information
matheusbsilva137 committed Nov 22, 2024
1 parent 0fd01ad commit 917720c
Show file tree
Hide file tree
Showing 4 changed files with 8 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -142,6 +144,7 @@ describe('mapVisitorToContact', () => {

return undefined;
});
getAllowedCustomFields.returns([]);
});

const index = 0;
Expand Down
Original file line number Diff line number Diff line change
@@ -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<CreateContactParams> {
return {
Expand All @@ -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)),
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {};
Expand All @@ -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({});
});
});

0 comments on commit 917720c

Please sign in to comment.