chore: Enhance contact merge action for identified users (#4886)
- Discard conflicting keys - Do not merge if there is already an identified contact Co-authored-by: Pranav Raj S <pranav@chatwoot.com>
This commit is contained in:
@@ -1,11 +1,23 @@
|
||||
import { IFrameHelper } from 'widget/helpers/utils';
|
||||
import { sendMessage } from 'widget/helpers/utils';
|
||||
import ContactsAPI from '../../api/contacts';
|
||||
import { SET_USER_ERROR } from '../../constants/errorTypes';
|
||||
import { setHeader } from '../../helpers/axios';
|
||||
const state = {
|
||||
currentUser: {},
|
||||
};
|
||||
|
||||
const SET_CURRENT_USER = 'SET_CURRENT_USER';
|
||||
const parseErrorData = error =>
|
||||
error && error.response && error.response.data ? error.response.data : error;
|
||||
export const updateWidgetAuthToken = widgetAuthToken => {
|
||||
if (widgetAuthToken) {
|
||||
setHeader(widgetAuthToken);
|
||||
sendMessage({
|
||||
event: 'setAuthCookie',
|
||||
data: { widgetAuthToken },
|
||||
});
|
||||
}
|
||||
};
|
||||
|
||||
export const getters = {
|
||||
getCurrentUser(_state) {
|
||||
@@ -22,13 +34,21 @@ export const actions = {
|
||||
// Ignore error
|
||||
}
|
||||
},
|
||||
update: async ({ dispatch }, { identifier, user: userObject }) => {
|
||||
update: async ({ dispatch }, { user }) => {
|
||||
try {
|
||||
await ContactsAPI.update(user);
|
||||
dispatch('get');
|
||||
} catch (error) {
|
||||
// Ignore error
|
||||
}
|
||||
},
|
||||
setUser: async ({ dispatch }, { identifier, user: userObject }) => {
|
||||
try {
|
||||
const {
|
||||
email,
|
||||
name,
|
||||
avatar_url,
|
||||
identifier_hash,
|
||||
identifier_hash: identifierHash,
|
||||
phone_number,
|
||||
company_name,
|
||||
city,
|
||||
@@ -41,7 +61,7 @@ export const actions = {
|
||||
email,
|
||||
name,
|
||||
avatar_url,
|
||||
identifier_hash,
|
||||
identifier_hash: identifierHash,
|
||||
phone_number,
|
||||
additional_attributes: {
|
||||
company_name,
|
||||
@@ -52,24 +72,19 @@ export const actions = {
|
||||
},
|
||||
custom_attributes,
|
||||
};
|
||||
await ContactsAPI.update(identifier, user);
|
||||
|
||||
const {
|
||||
data: { widget_auth_token: widgetAuthToken },
|
||||
} = await ContactsAPI.setUser(identifier, user);
|
||||
updateWidgetAuthToken(widgetAuthToken);
|
||||
dispatch('get');
|
||||
if (identifier_hash) {
|
||||
if (identifierHash || widgetAuthToken) {
|
||||
dispatch('conversation/clearConversations', {}, { root: true });
|
||||
dispatch('conversation/fetchOldConversations', {}, { root: true });
|
||||
dispatch('conversationAttributes/getAttributes', {}, { root: true });
|
||||
}
|
||||
} catch (error) {
|
||||
const data =
|
||||
error && error.response && error.response.data
|
||||
? error.response.data
|
||||
: error;
|
||||
IFrameHelper.sendMessage({
|
||||
event: 'error',
|
||||
errorType: SET_USER_ERROR,
|
||||
data,
|
||||
});
|
||||
const data = parseErrorData(error);
|
||||
sendMessage({ event: 'error', errorType: SET_USER_ERROR, data });
|
||||
}
|
||||
},
|
||||
setCustomAttributes: async (_, customAttributes = {}) => {
|
||||
|
||||
@@ -1,21 +1,26 @@
|
||||
import { API } from 'widget/helpers/axios';
|
||||
import { sendMessage } from 'widget/helpers/utils';
|
||||
import { actions } from '../../contacts';
|
||||
|
||||
const commit = jest.fn();
|
||||
const dispatch = jest.fn();
|
||||
jest.mock('widget/helpers/axios');
|
||||
jest.mock('widget/helpers/utils', () => ({
|
||||
sendMessage: jest.fn(),
|
||||
}));
|
||||
|
||||
describe('#actions', () => {
|
||||
describe('#update', () => {
|
||||
it('sends correct actions', async () => {
|
||||
describe('#setUser', () => {
|
||||
it('sends correct actions if contact object is refreshed ', async () => {
|
||||
const user = {
|
||||
email: 'thoma@sphadikam.com',
|
||||
name: 'Adu Thoma',
|
||||
avatar_url: '',
|
||||
identifier_hash: 'random_hex_identifier_hash',
|
||||
};
|
||||
API.patch.mockResolvedValue({ data: { pubsub_token: 'token' } });
|
||||
await actions.update({ commit, dispatch }, { identifier: 1, user });
|
||||
API.patch.mockResolvedValue({ data: { widget_auth_token: 'token' } });
|
||||
await actions.setUser({ commit, dispatch }, { identifier: 1, user });
|
||||
expect(sendMessage.mock.calls).toEqual([
|
||||
[{ data: { widgetAuthToken: 'token' }, event: 'setAuthCookie' }],
|
||||
]);
|
||||
expect(commit.mock.calls).toEqual([]);
|
||||
expect(dispatch.mock.calls).toEqual([
|
||||
['get'],
|
||||
@@ -24,5 +29,52 @@ describe('#actions', () => {
|
||||
['conversationAttributes/getAttributes', {}, { root: true }],
|
||||
]);
|
||||
});
|
||||
|
||||
it('sends correct actions if identifierHash is passed ', async () => {
|
||||
const user = {
|
||||
email: 'thoma@sphadikam.com',
|
||||
name: 'Adu Thoma',
|
||||
avatar_url: '',
|
||||
identifier_hash: '12345',
|
||||
};
|
||||
API.patch.mockResolvedValue({ data: { id: 1 } });
|
||||
await actions.setUser({ commit, dispatch }, { identifier: 1, user });
|
||||
expect(sendMessage.mock.calls).toEqual([]);
|
||||
expect(commit.mock.calls).toEqual([]);
|
||||
expect(dispatch.mock.calls).toEqual([
|
||||
['get'],
|
||||
['conversation/clearConversations', {}, { root: true }],
|
||||
['conversation/fetchOldConversations', {}, { root: true }],
|
||||
['conversationAttributes/getAttributes', {}, { root: true }],
|
||||
]);
|
||||
});
|
||||
|
||||
it('does not call sendMessage if contact object is not refreshed ', async () => {
|
||||
const user = {
|
||||
email: 'thoma@sphadikam.com',
|
||||
name: 'Adu Thoma',
|
||||
avatar_url: '',
|
||||
};
|
||||
API.patch.mockResolvedValue({ data: { id: 1 } });
|
||||
await actions.setUser({ commit, dispatch }, { identifier: 1, user });
|
||||
expect(sendMessage.mock.calls).toEqual([]);
|
||||
expect(commit.mock.calls).toEqual([]);
|
||||
expect(dispatch.mock.calls).toEqual([['get']]);
|
||||
});
|
||||
});
|
||||
|
||||
describe('#update', () => {
|
||||
it('sends correct actions', async () => {
|
||||
const user = {
|
||||
email: 'thoma@sphadikam.com',
|
||||
name: 'Adu Thoma',
|
||||
avatar_url: '',
|
||||
identifier_hash: 'random_hex_identifier_hash',
|
||||
};
|
||||
API.patch.mockResolvedValue({ data: { id: 1 } });
|
||||
await actions.update({ commit, dispatch }, { identifier: 1, user });
|
||||
expect(commit.mock.calls).toEqual([]);
|
||||
expect(dispatch.mock.calls).toEqual([['get']]);
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user