|
|
|
|
@ -5,7 +5,7 @@ import sinon from 'sinon'; |
|
|
|
|
|
|
|
|
|
const modelsMock = { |
|
|
|
|
LivechatContacts: { |
|
|
|
|
findOneById: sinon.stub(), |
|
|
|
|
findOneEnabledById: sinon.stub(), |
|
|
|
|
updateContact: sinon.stub(), |
|
|
|
|
}, |
|
|
|
|
Settings: { |
|
|
|
|
@ -24,13 +24,13 @@ const { resolveContactConflicts } = proxyquire.noCallThru().load('./resolveConta |
|
|
|
|
|
|
|
|
|
describe('resolveContactConflicts', () => { |
|
|
|
|
beforeEach(() => { |
|
|
|
|
modelsMock.LivechatContacts.findOneById.reset(); |
|
|
|
|
modelsMock.LivechatContacts.findOneEnabledById.reset(); |
|
|
|
|
modelsMock.Settings.incrementValueById.reset(); |
|
|
|
|
modelsMock.LivechatContacts.updateContact.reset(); |
|
|
|
|
}); |
|
|
|
|
|
|
|
|
|
it('should update the contact with the resolved custom field', async () => { |
|
|
|
|
modelsMock.LivechatContacts.findOneById.resolves({ |
|
|
|
|
modelsMock.LivechatContacts.findOneEnabledById.resolves({ |
|
|
|
|
_id: 'contactId', |
|
|
|
|
customFields: { customField: 'newValue' }, |
|
|
|
|
conflictingFields: [{ field: 'customFields.customField', value: 'oldValue' }], |
|
|
|
|
@ -44,7 +44,7 @@ describe('resolveContactConflicts', () => { |
|
|
|
|
|
|
|
|
|
const result = await resolveContactConflicts({ contactId: 'contactId', customField: { customField: 'newValue' } }); |
|
|
|
|
|
|
|
|
|
expect(modelsMock.LivechatContacts.findOneById.getCall(0).args[0]).to.be.equal('contactId'); |
|
|
|
|
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); |
|
|
|
|
@ -59,7 +59,7 @@ describe('resolveContactConflicts', () => { |
|
|
|
|
}); |
|
|
|
|
|
|
|
|
|
it('should update the contact with the resolved name', async () => { |
|
|
|
|
modelsMock.LivechatContacts.findOneById.resolves({ |
|
|
|
|
modelsMock.LivechatContacts.findOneEnabledById.resolves({ |
|
|
|
|
_id: 'contactId', |
|
|
|
|
name: 'Old Name', |
|
|
|
|
customFields: { customField: 'newValue' }, |
|
|
|
|
@ -75,7 +75,7 @@ describe('resolveContactConflicts', () => { |
|
|
|
|
|
|
|
|
|
const result = await resolveContactConflicts({ contactId: 'contactId', name: 'New Name' }); |
|
|
|
|
|
|
|
|
|
expect(modelsMock.LivechatContacts.findOneById.getCall(0).args[0]).to.be.equal('contactId'); |
|
|
|
|
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); |
|
|
|
|
@ -91,7 +91,7 @@ describe('resolveContactConflicts', () => { |
|
|
|
|
}); |
|
|
|
|
|
|
|
|
|
it('should update the contact with the resolved contact manager', async () => { |
|
|
|
|
modelsMock.LivechatContacts.findOneById.resolves({ |
|
|
|
|
modelsMock.LivechatContacts.findOneEnabledById.resolves({ |
|
|
|
|
_id: 'contactId', |
|
|
|
|
name: 'Name', |
|
|
|
|
contactManager: 'contactManagerId', |
|
|
|
|
@ -109,7 +109,7 @@ describe('resolveContactConflicts', () => { |
|
|
|
|
|
|
|
|
|
const result = await resolveContactConflicts({ contactId: 'contactId', name: 'New Name' }); |
|
|
|
|
|
|
|
|
|
expect(modelsMock.LivechatContacts.findOneById.getCall(0).args[0]).to.be.equal('contactId'); |
|
|
|
|
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); |
|
|
|
|
@ -126,7 +126,7 @@ describe('resolveContactConflicts', () => { |
|
|
|
|
|
|
|
|
|
it('should wipe conflicts if wipeConflicts = true', async () => { |
|
|
|
|
it('should update the contact with the resolved name', async () => { |
|
|
|
|
modelsMock.LivechatContacts.findOneById.resolves({ |
|
|
|
|
modelsMock.LivechatContacts.findOneEnabledById.resolves({ |
|
|
|
|
_id: 'contactId', |
|
|
|
|
name: 'Name', |
|
|
|
|
customFields: { customField: 'newValue' }, |
|
|
|
|
@ -145,7 +145,7 @@ describe('resolveContactConflicts', () => { |
|
|
|
|
|
|
|
|
|
const result = await resolveContactConflicts({ contactId: 'contactId', name: 'New Name', wipeConflicts: true }); |
|
|
|
|
|
|
|
|
|
expect(modelsMock.LivechatContacts.findOneById.getCall(0).args[0]).to.be.equal('contactId'); |
|
|
|
|
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(2); |
|
|
|
|
@ -163,7 +163,7 @@ describe('resolveContactConflicts', () => { |
|
|
|
|
|
|
|
|
|
it('should wipe conflicts if wipeConflicts = true', async () => { |
|
|
|
|
it('should update the contact with the resolved name', async () => { |
|
|
|
|
modelsMock.LivechatContacts.findOneById.resolves({ |
|
|
|
|
modelsMock.LivechatContacts.findOneEnabledById.resolves({ |
|
|
|
|
_id: 'contactId', |
|
|
|
|
name: 'Name', |
|
|
|
|
customFields: { customField: 'newValue' }, |
|
|
|
|
@ -182,7 +182,7 @@ describe('resolveContactConflicts', () => { |
|
|
|
|
|
|
|
|
|
const result = await resolveContactConflicts({ contactId: 'contactId', name: 'New Name', wipeConflicts: false }); |
|
|
|
|
|
|
|
|
|
expect(modelsMock.LivechatContacts.findOneById.getCall(0).args[0]).to.be.equal('contactId'); |
|
|
|
|
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); |
|
|
|
|
@ -199,7 +199,7 @@ describe('resolveContactConflicts', () => { |
|
|
|
|
}); |
|
|
|
|
|
|
|
|
|
it('should throw an error if the contact does not exist', async () => { |
|
|
|
|
modelsMock.LivechatContacts.findOneById.resolves(undefined); |
|
|
|
|
modelsMock.LivechatContacts.findOneEnabledById.resolves(undefined); |
|
|
|
|
await expect(resolveContactConflicts({ contactId: 'id', customField: { customField: 'newValue' } })).to.be.rejectedWith( |
|
|
|
|
'error-contact-not-found', |
|
|
|
|
); |
|
|
|
|
@ -207,7 +207,7 @@ describe('resolveContactConflicts', () => { |
|
|
|
|
}); |
|
|
|
|
|
|
|
|
|
it('should throw an error if the contact has no conflicting fields', async () => { |
|
|
|
|
modelsMock.LivechatContacts.findOneById.resolves({ |
|
|
|
|
modelsMock.LivechatContacts.findOneEnabledById.resolves({ |
|
|
|
|
_id: 'contactId', |
|
|
|
|
name: 'Name', |
|
|
|
|
contactManager: 'contactManagerId', |
|
|
|
|
@ -221,7 +221,7 @@ describe('resolveContactConflicts', () => { |
|
|
|
|
}); |
|
|
|
|
|
|
|
|
|
it('should throw an error if the contact manager is invalid', async () => { |
|
|
|
|
modelsMock.LivechatContacts.findOneById.resolves({ |
|
|
|
|
modelsMock.LivechatContacts.findOneEnabledById.resolves({ |
|
|
|
|
_id: 'contactId', |
|
|
|
|
name: 'Name', |
|
|
|
|
contactManager: 'contactManagerId', |
|
|
|
|
|