Skip to content
Snippets Groups Projects
Unverified Commit 7e2facc9 authored by Abhinav Kumar's avatar Abhinav Kumar Committed by GitHub
Browse files

fix: customFields ignored in livechat room creation (#33047)

parent a40541b6
No related branches found
No related tags found
No related merge requests found
---
'@rocket.chat/meteor': patch
---
Fixed: Custom fields in extraData now correctly added to extraRoomInfo by livechat.beforeRoom callback during livechat room creation.
...@@ -2,6 +2,7 @@ import { OmnichannelServiceLevelAgreements } from '@rocket.chat/models'; ...@@ -2,6 +2,7 @@ import { OmnichannelServiceLevelAgreements } from '@rocket.chat/models';
import { Meteor } from 'meteor/meteor'; import { Meteor } from 'meteor/meteor';
import { callbacks } from '../../../../../lib/callbacks'; import { callbacks } from '../../../../../lib/callbacks';
import { isPlainObject } from '../../../../../lib/utils/isPlainObject';
callbacks.add( callbacks.add(
'livechat.beforeRoom', 'livechat.beforeRoom',
...@@ -10,9 +11,11 @@ callbacks.add( ...@@ -10,9 +11,11 @@ callbacks.add(
return roomInfo; return roomInfo;
} }
const { sla: searchTerm } = extraData; const { sla: searchTerm, customFields } = extraData;
const roomInfoWithExtraData = { ...roomInfo, ...(isPlainObject(customFields) && { customFields }) };
if (!searchTerm) { if (!searchTerm) {
return roomInfo; return roomInfoWithExtraData;
} }
const sla = await OmnichannelServiceLevelAgreements.findOneByIdOrName(searchTerm); const sla = await OmnichannelServiceLevelAgreements.findOneByIdOrName(searchTerm);
...@@ -23,7 +26,7 @@ callbacks.add( ...@@ -23,7 +26,7 @@ callbacks.add(
} }
const { _id: slaId } = sla; const { _id: slaId } = sla;
return { ...roomInfo, slaId }; return { ...roomInfoWithExtraData, slaId };
}, },
callbacks.priority.MEDIUM, callbacks.priority.MEDIUM,
'livechat-before-new-room', 'livechat-before-new-room',
......
export function isPlainObject(value: unknown) {
return value !== null && typeof value === 'object' && !Array.isArray(value);
}
import { expect } from 'chai';
import { describe, it, beforeEach } from 'mocha';
import proxyquire from 'proxyquire';
import sinon from 'sinon';
import { callbacks } from '../../../../../lib/callbacks';
const findStub = sinon.stub();
proxyquire.noCallThru().load('../../../../../ee/app/livechat-enterprise/server/hooks/beforeNewRoom.ts', {
'meteor/meteor': {
Meteor: {
Error,
},
},
'@rocket.chat/models': {
OmnichannelServiceLevelAgreements: {
findOneByIdOrName: findStub,
},
},
});
describe('livechat.beforeRoom', () => {
beforeEach(() => findStub.withArgs('high').resolves({ _id: 'high' }).withArgs('invalid').resolves(null));
it('should return roomInfo with customFields when provided', async () => {
const roomInfo = { name: 'test' };
const extraData = { customFields: { test: 'test' } };
const result = await callbacks.run('livechat.beforeRoom', roomInfo, extraData);
expect(result).to.deep.equal({ ...roomInfo, customFields: extraData.customFields });
});
it('should throw an error when provided with an invalid sla', async () => {
const roomInfo = { name: 'test' };
const extraData = { customFields: { test: 'test' }, sla: 'invalid' };
await expect(callbacks.run('livechat.beforeRoom', roomInfo, extraData)).to.be.rejectedWith(Error, 'error-invalid-sla');
});
it('should not include field in roomInfo when extraData has field other than customFields, sla', async () => {
const roomInfo = { name: 'test' };
const extraData = { customFields: { test: 'test' }, sla: 'high' };
const result = await callbacks.run('livechat.beforeRoom', roomInfo, extraData);
expect(result).to.deep.equal({ ...roomInfo, customFields: extraData.customFields, slaId: 'high' });
});
it('should return roomInfo with no customFields when customFields is not an object', async () => {
const roomInfo = { name: 'test' };
const extraData = { customFields: 'not an object' };
const result = await callbacks.run('livechat.beforeRoom', roomInfo, extraData);
expect(result).to.deep.equal({ ...roomInfo });
});
});
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment