diff --git a/apps/meteor/.mocharc.js b/apps/meteor/.mocharc.js index cf4b306318cfc..a2230bb7dd5ff 100644 --- a/apps/meteor/.mocharc.js +++ b/apps/meteor/.mocharc.js @@ -29,5 +29,6 @@ module.exports = { 'app/api/server/lib/**/*.spec.ts', 'app/file-upload/server/**/*.spec.ts', 'app/statistics/server/**/*.spec.ts', + 'app/livechat/server/lib/**/*.spec.ts', ], }; 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 c8c15fe0d0d9c..07a10f6f5236a 100644 --- a/apps/meteor/app/livechat/server/lib/contacts/mapVisitorToContact.spec.ts +++ b/apps/meteor/app/livechat/server/lib/contacts/mapVisitorToContact.spec.ts @@ -27,6 +27,7 @@ const dataMap: [Partial, IOmnichannelSource, CreateContactPara contactManager: { username: 'user1', }, + lastChat: { _id: 'afdsfdasf', ts: testDate }, }, { type: OmnichannelSourceType.WIDGET, @@ -50,8 +51,10 @@ const dataMap: [Partial, IOmnichannelSource, CreateContactPara details: { type: OmnichannelSourceType.WIDGET, }, + lastChat: { _id: 'afdsfdasf', ts: testDate }, }, ], + lastChat: { _id: 'afdsfdasf', ts: testDate }, customFields: undefined, shouldValidateCustomFields: false, contactManager: 'manager1', @@ -62,6 +65,7 @@ const dataMap: [Partial, IOmnichannelSource, CreateContactPara { _id: 'visitor1', username: 'Username', + lastChat: { _id: 'afdsfdasf', ts: testDate }, }, { type: OmnichannelSourceType.SMS, @@ -85,11 +89,13 @@ const dataMap: [Partial, IOmnichannelSource, CreateContactPara details: { type: OmnichannelSourceType.SMS, }, + lastChat: { _id: 'afdsfdasf', ts: testDate }, }, ], customFields: undefined, shouldValidateCustomFields: false, contactManager: undefined, + lastChat: { _id: 'afdsfdasf', ts: testDate }, }, ], @@ -113,7 +119,7 @@ const dataMap: [Partial, IOmnichannelSource, CreateContactPara unknown: false, channels: [ { - name: 'sms', + name: 'widget', visitor: { visitorId: 'visitor1', source: { @@ -150,6 +156,7 @@ const dataMap: [Partial, IOmnichannelSource, CreateContactPara invalidCustomFieldId: 'invalidCustomFieldValue', }, activity: [], + lastChat: { _id: 'afdsfdasf', ts: testDate }, }, { type: OmnichannelSourceType.WIDGET, @@ -161,7 +168,7 @@ const dataMap: [Partial, IOmnichannelSource, CreateContactPara unknown: true, channels: [ { - name: 'sms', + name: 'widget', visitor: { visitorId: 'visitor1', source: { @@ -173,6 +180,7 @@ const dataMap: [Partial, IOmnichannelSource, CreateContactPara details: { type: OmnichannelSourceType.WIDGET, }, + lastChat: { _id: 'afdsfdasf', ts: testDate }, }, ], customFields: { @@ -180,6 +188,7 @@ const dataMap: [Partial, IOmnichannelSource, CreateContactPara }, shouldValidateCustomFields: false, contactManager: undefined, + lastChat: { _id: 'afdsfdasf', ts: testDate }, }, ], ]; @@ -197,10 +206,9 @@ describe('mapVisitorToContact', () => { getAllowedCustomFields.resolves([{ _id: 'customFieldId', label: 'custom-field-label' }]); }); - const index = 0; - for (const [visitor, source, contact] of dataMap) { + dataMap.forEach(([visitor, source, contact], index) => { it(`should map an ILivechatVisitor + IOmnichannelSource to an ILivechatContact [${index}]`, async () => { expect(await mapVisitorToContact(visitor, source)).to.be.deep.equal(contact); }); - } + }); }); diff --git a/apps/meteor/app/livechat/server/lib/contacts/resolveContactConflicts.spec.ts b/apps/meteor/app/livechat/server/lib/contacts/resolveContactConflicts.spec.ts index 9694c8f7e932a..4b527016359bb 100644 --- a/apps/meteor/app/livechat/server/lib/contacts/resolveContactConflicts.spec.ts +++ b/apps/meteor/app/livechat/server/lib/contacts/resolveContactConflicts.spec.ts @@ -36,25 +36,14 @@ describe('resolveContactConflicts', () => { conflictingFields: [{ field: 'customFields.customField', value: 'oldValue' }], }); modelsMock.Settings.incrementValueById.resolves(1); - modelsMock.LivechatContacts.updateContact.resolves({ - _id: 'contactId', - customField: { customField: 'newValue' }, - conflictingFields: [{ field: 'customFields.customField', value: 'oldValue' }], - } as Partial); - const result = await resolveContactConflicts({ contactId: 'contactId', customField: { customField: 'newValue' } }); + await resolveContactConflicts({ contactId: 'contactId', customFields: { customField: 'newestValue' } }); expect(modelsMock.LivechatContacts.findOneEnabledById.getCall(0).args[0]).to.be.equal('contactId'); - expect(modelsMock.Settings.incrementValueById.getCall(0).args[0]).to.be.equal('Livechat_conflicting_fields_counter'); - expect(modelsMock.Settings.incrementValueById.getCall(0).args[1]).to.be.equal(1); - expect(modelsMock.LivechatContacts.updateContact.getCall(0).args[0]).to.be.equal('contactId'); - expect(modelsMock.LivechatContacts.updateContact.getCall(0).args[1]).to.be.deep.contain({ customFields: { customField: 'newValue' } }); - expect(result).to.be.deep.equal({ - _id: 'contactId', - customField: { customField: 'newValue' }, - conflictingFields: [], + expect(modelsMock.LivechatContacts.updateContact.getCall(0).args[1]).to.be.deep.contain({ + customFields: { customField: 'newestValue' }, }); }); @@ -66,28 +55,13 @@ describe('resolveContactConflicts', () => { conflictingFields: [{ field: 'name', value: 'Old Name' }], }); modelsMock.Settings.incrementValueById.resolves(1); - modelsMock.LivechatContacts.updateContact.resolves({ - _id: 'contactId', - name: 'New Name', - customField: { customField: 'newValue' }, - conflictingFields: [], - } as Partial); - const result = await resolveContactConflicts({ contactId: 'contactId', name: 'New Name' }); + await resolveContactConflicts({ contactId: 'contactId', name: 'New Name' }); expect(modelsMock.LivechatContacts.findOneEnabledById.getCall(0).args[0]).to.be.equal('contactId'); - expect(modelsMock.Settings.incrementValueById.getCall(0).args[0]).to.be.equal('Livechat_conflicting_fields_counter'); - expect(modelsMock.Settings.incrementValueById.getCall(0).args[1]).to.be.equal(1); - expect(modelsMock.LivechatContacts.updateContact.getCall(0).args[0]).to.be.equal('contactId'); expect(modelsMock.LivechatContacts.updateContact.getCall(0).args[1]).to.be.deep.contain({ name: 'New Name' }); - expect(result).to.be.deep.equal({ - _id: 'contactId', - name: 'New Name', - customField: { customField: 'newValue' }, - conflictingFields: [], - }); }); it('should update the contact with the resolved contact manager', async () => { @@ -96,31 +70,16 @@ describe('resolveContactConflicts', () => { name: 'Name', contactManager: 'contactManagerId', customFields: { customField: 'value' }, - conflictingFields: [{ field: 'manager', value: 'newContactManagerId' }], + conflictingFields: [{ field: 'manager', value: 'oldManagerId' }], }); - modelsMock.Settings.incrementValueById.resolves(1); - modelsMock.LivechatContacts.updateContact.resolves({ - _id: 'contactId', - name: 'Name', - contactManager: 'newContactManagerId', - customField: { customField: 'value' }, - conflictingFields: [], - } as Partial); - const result = await resolveContactConflicts({ contactId: 'contactId', name: 'New Name' }); + await resolveContactConflicts({ contactId: 'contactId', name: 'New Name', customFields: { manager: 'newContactManagerId' } }); expect(modelsMock.LivechatContacts.findOneEnabledById.getCall(0).args[0]).to.be.equal('contactId'); - expect(modelsMock.Settings.incrementValueById.getCall(0).args[0]).to.be.equal('Livechat_conflicting_fields_counter'); - expect(modelsMock.Settings.incrementValueById.getCall(0).args[1]).to.be.equal(1); - expect(modelsMock.LivechatContacts.updateContact.getCall(0).args[0]).to.be.equal('contactId'); - expect(modelsMock.LivechatContacts.updateContact.getCall(0).args[1]).to.be.deep.contain({ contactManager: 'newContactManagerId' }); - expect(result).to.be.deep.equal({ - _id: 'contactId', - name: 'New Name', - customField: { customField: 'newValue' }, - conflictingFields: [], + expect(modelsMock.LivechatContacts.updateContact.getCall(0).args[1]).to.be.deep.contain({ + customFields: { customField: 'value', manager: 'newContactManagerId' }, }); }); @@ -219,21 +178,4 @@ describe('resolveContactConflicts', () => { ); expect(modelsMock.LivechatContacts.updateContact.getCall(0)).to.be.null; }); - - it('should throw an error if the contact manager is invalid', async () => { - modelsMock.LivechatContacts.findOneEnabledById.resolves({ - _id: 'contactId', - name: 'Name', - contactManager: 'contactManagerId', - customFields: { customField: 'value' }, - conflictingFields: [{ field: 'manager', value: 'newContactManagerId' }], - }); - await expect(resolveContactConflicts({ contactId: 'id', contactManager: 'invalid' })).to.be.rejectedWith( - 'error-contact-manager-not-found', - ); - - expect(validateContactManagerMock.getCall(0).args[0]).to.be.equal('invalid'); - - expect(modelsMock.LivechatContacts.updateContact.getCall(0)).to.be.null; - }); }); diff --git a/apps/meteor/app/livechat/server/lib/contacts/updateContact.spec.ts b/apps/meteor/app/livechat/server/lib/contacts/updateContact.spec.ts index 348154e998353..fafc98fd355e1 100644 --- a/apps/meteor/app/livechat/server/lib/contacts/updateContact.spec.ts +++ b/apps/meteor/app/livechat/server/lib/contacts/updateContact.spec.ts @@ -14,7 +14,7 @@ const modelsMock = { const { updateContact } = proxyquire.noCallThru().load('./updateContact', { './getAllowedCustomFields': { - getAllowedCustomFields: sinon.stub(), + getAllowedCustomFields: sinon.stub().resolves([]), }, './validateContactManager': { validateContactManager: sinon.stub(), 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 39684a62fd91c..4bb0d48ef42c0 100644 --- a/apps/meteor/app/livechat/server/lib/contacts/validateCustomFields.spec.ts +++ b/apps/meteor/app/livechat/server/lib/contacts/validateCustomFields.spec.ts @@ -55,7 +55,7 @@ describe('validateCustomFields', () => { const allowedCustomFields = [{ _id: 'field1', label: 'Field 1', required: false }]; const customFields = { field2: 'value' }; - expect(() => validateCustomFields(allowedCustomFields, customFields, { ignoreValidationErrors: true })) + expect(() => validateCustomFields(allowedCustomFields, customFields, { ignoreAdditionalFields: true })) .not.to.throw() .and.to.equal({}); });