2021-02-19 14:52:58 +00:00
|
|
|
import {
|
|
|
|
DuplicateContactException,
|
|
|
|
ExceptionWithMessage,
|
|
|
|
} from 'shared/helpers/CustomErrors';
|
2020-11-10 09:55:26 +00:00
|
|
|
import types from '../../mutation-types';
|
|
|
|
import ContactAPI from '../../../api/contacts';
|
2021-10-13 13:05:13 +00:00
|
|
|
import AccountActionsAPI from '../../../api/accountActions';
|
2020-11-10 09:55:26 +00:00
|
|
|
|
2022-07-12 08:03:16 +00:00
|
|
|
const buildContactFormData = contactParams => {
|
|
|
|
const formData = new FormData();
|
|
|
|
const { additional_attributes = {}, ...contactProperties } = contactParams;
|
|
|
|
Object.keys(contactProperties).forEach(key => {
|
|
|
|
if (contactProperties[key]) {
|
|
|
|
formData.append(key, contactProperties[key]);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
const {
|
|
|
|
social_profiles,
|
|
|
|
...additionalAttributesProperties
|
|
|
|
} = additional_attributes;
|
|
|
|
Object.keys(additionalAttributesProperties).forEach(key => {
|
|
|
|
formData.append(
|
|
|
|
`additional_attributes[${key}]`,
|
|
|
|
additionalAttributesProperties[key]
|
|
|
|
);
|
|
|
|
});
|
|
|
|
Object.keys(social_profiles).forEach(key => {
|
|
|
|
formData.append(
|
|
|
|
`additional_attributes[social_profiles][${key}]`,
|
|
|
|
social_profiles[key]
|
|
|
|
);
|
|
|
|
});
|
|
|
|
return formData;
|
|
|
|
};
|
|
|
|
|
2020-11-10 09:55:26 +00:00
|
|
|
export const actions = {
|
2021-06-18 14:38:58 +00:00
|
|
|
search: async ({ commit }, { search, page, sortAttr, label }) => {
|
2020-11-10 09:55:26 +00:00
|
|
|
commit(types.SET_CONTACT_UI_FLAG, { isFetching: true });
|
|
|
|
try {
|
|
|
|
const {
|
|
|
|
data: { payload, meta },
|
2021-06-18 14:38:58 +00:00
|
|
|
} = await ContactAPI.search(search, page, sortAttr, label);
|
2020-11-10 09:55:26 +00:00
|
|
|
commit(types.CLEAR_CONTACTS);
|
|
|
|
commit(types.SET_CONTACTS, payload);
|
|
|
|
commit(types.SET_CONTACT_META, meta);
|
|
|
|
commit(types.SET_CONTACT_UI_FLAG, { isFetching: false });
|
|
|
|
} catch (error) {
|
|
|
|
commit(types.SET_CONTACT_UI_FLAG, { isFetching: false });
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2021-06-18 14:38:58 +00:00
|
|
|
get: async ({ commit }, { page = 1, sortAttr, label } = {}) => {
|
2020-11-10 09:55:26 +00:00
|
|
|
commit(types.SET_CONTACT_UI_FLAG, { isFetching: true });
|
|
|
|
try {
|
|
|
|
const {
|
|
|
|
data: { payload, meta },
|
2021-06-18 14:38:58 +00:00
|
|
|
} = await ContactAPI.get(page, sortAttr, label);
|
2020-11-10 09:55:26 +00:00
|
|
|
commit(types.CLEAR_CONTACTS);
|
|
|
|
commit(types.SET_CONTACTS, payload);
|
|
|
|
commit(types.SET_CONTACT_META, meta);
|
|
|
|
commit(types.SET_CONTACT_UI_FLAG, { isFetching: false });
|
|
|
|
} catch (error) {
|
|
|
|
commit(types.SET_CONTACT_UI_FLAG, { isFetching: false });
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
show: async ({ commit }, { id }) => {
|
|
|
|
commit(types.SET_CONTACT_UI_FLAG, { isFetchingItem: true });
|
|
|
|
try {
|
|
|
|
const response = await ContactAPI.show(id);
|
|
|
|
commit(types.SET_CONTACT_ITEM, response.data.payload);
|
|
|
|
commit(types.SET_CONTACT_UI_FLAG, {
|
|
|
|
isFetchingItem: false,
|
|
|
|
});
|
|
|
|
} catch (error) {
|
|
|
|
commit(types.SET_CONTACT_UI_FLAG, {
|
|
|
|
isFetchingItem: false,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2022-07-12 08:03:16 +00:00
|
|
|
update: async ({ commit }, { id, isFormData = false, ...contactParams }) => {
|
2020-11-10 09:55:26 +00:00
|
|
|
commit(types.SET_CONTACT_UI_FLAG, { isUpdating: true });
|
|
|
|
try {
|
2022-07-12 08:03:16 +00:00
|
|
|
const response = await ContactAPI.update(
|
|
|
|
id,
|
|
|
|
isFormData ? buildContactFormData(contactParams) : contactParams
|
|
|
|
);
|
2020-11-10 09:55:26 +00:00
|
|
|
commit(types.EDIT_CONTACT, response.data.payload);
|
|
|
|
commit(types.SET_CONTACT_UI_FLAG, { isUpdating: false });
|
|
|
|
} catch (error) {
|
|
|
|
commit(types.SET_CONTACT_UI_FLAG, { isUpdating: false });
|
2021-12-03 07:49:11 +00:00
|
|
|
if (error.response?.status === 422) {
|
|
|
|
throw new DuplicateContactException(error.response.data.attributes);
|
2020-11-10 09:55:26 +00:00
|
|
|
} else {
|
|
|
|
throw new Error(error);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2022-07-12 08:03:16 +00:00
|
|
|
create: async ({ commit }, { isFormData = false, ...contactParams }) => {
|
2021-02-19 14:52:58 +00:00
|
|
|
commit(types.SET_CONTACT_UI_FLAG, { isCreating: true });
|
|
|
|
try {
|
2022-07-12 08:03:16 +00:00
|
|
|
const response = await ContactAPI.create(
|
|
|
|
isFormData ? buildContactFormData(contactParams) : contactParams
|
|
|
|
);
|
2021-02-19 14:52:58 +00:00
|
|
|
commit(types.SET_CONTACT_ITEM, response.data.payload.contact);
|
|
|
|
commit(types.SET_CONTACT_UI_FLAG, { isCreating: false });
|
|
|
|
} catch (error) {
|
|
|
|
commit(types.SET_CONTACT_UI_FLAG, { isCreating: false });
|
|
|
|
if (error.response?.data?.message) {
|
|
|
|
throw new ExceptionWithMessage(error.response.data.message);
|
|
|
|
} else {
|
|
|
|
throw new Error(error);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
2022-07-12 08:03:16 +00:00
|
|
|
|
2021-09-29 06:31:58 +00:00
|
|
|
import: async ({ commit }, file) => {
|
|
|
|
commit(types.SET_CONTACT_UI_FLAG, { isCreating: true });
|
|
|
|
try {
|
|
|
|
await ContactAPI.importContacts(file);
|
|
|
|
commit(types.SET_CONTACT_UI_FLAG, { isCreating: false });
|
|
|
|
} catch (error) {
|
|
|
|
commit(types.SET_CONTACT_UI_FLAG, { isCreating: false });
|
|
|
|
if (error.response?.data?.message) {
|
|
|
|
throw new ExceptionWithMessage(error.response.data.message);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
2022-07-12 08:03:16 +00:00
|
|
|
|
2021-09-23 07:22:49 +00:00
|
|
|
delete: async ({ commit }, id) => {
|
|
|
|
commit(types.SET_CONTACT_UI_FLAG, { isDeleting: true });
|
|
|
|
try {
|
|
|
|
await ContactAPI.delete(id);
|
|
|
|
commit(types.SET_CONTACT_UI_FLAG, { isDeleting: false });
|
|
|
|
} catch (error) {
|
|
|
|
commit(types.SET_CONTACT_UI_FLAG, { isDeleting: false });
|
|
|
|
if (error.response?.data?.message) {
|
|
|
|
throw new Error(error.response.data.message);
|
|
|
|
} else {
|
|
|
|
throw new Error(error);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2021-11-11 09:53:33 +00:00
|
|
|
deleteCustomAttributes: async ({ commit }, { id, customAttributes }) => {
|
|
|
|
try {
|
|
|
|
const response = await ContactAPI.destroyCustomAttributes(
|
|
|
|
id,
|
|
|
|
customAttributes
|
|
|
|
);
|
|
|
|
commit(types.EDIT_CONTACT, response.data.payload);
|
|
|
|
} catch (error) {
|
|
|
|
throw new Error(error);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2022-07-12 08:03:16 +00:00
|
|
|
deleteAvatar: async ({ commit }, id) => {
|
|
|
|
try {
|
|
|
|
const response = await ContactAPI.destroyAvatar(id);
|
|
|
|
commit(types.EDIT_CONTACT, response.data.payload);
|
|
|
|
} catch (error) {
|
|
|
|
throw new Error(error);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2021-04-16 15:01:07 +00:00
|
|
|
fetchContactableInbox: async ({ commit }, id) => {
|
|
|
|
commit(types.SET_CONTACT_UI_FLAG, { isFetchingInboxes: true });
|
|
|
|
try {
|
|
|
|
const response = await ContactAPI.getContactableInboxes(id);
|
|
|
|
const contact = {
|
|
|
|
id,
|
|
|
|
contactableInboxes: response.data.payload,
|
|
|
|
};
|
|
|
|
commit(types.SET_CONTACT_ITEM, contact);
|
|
|
|
} catch (error) {
|
|
|
|
if (error.response?.data?.message) {
|
|
|
|
throw new ExceptionWithMessage(error.response.data.message);
|
|
|
|
} else {
|
|
|
|
throw new Error(error);
|
|
|
|
}
|
|
|
|
} finally {
|
|
|
|
commit(types.SET_CONTACT_UI_FLAG, { isFetchingInboxes: false });
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2020-11-10 09:55:26 +00:00
|
|
|
updatePresence: ({ commit }, data) => {
|
|
|
|
commit(types.UPDATE_CONTACTS_PRESENCE, data);
|
|
|
|
},
|
|
|
|
|
|
|
|
setContact({ commit }, data) {
|
|
|
|
commit(types.SET_CONTACT_ITEM, data);
|
|
|
|
},
|
2021-09-23 07:22:49 +00:00
|
|
|
|
2021-10-13 13:05:13 +00:00
|
|
|
merge: async ({ commit }, { childId, parentId }) => {
|
|
|
|
commit(types.SET_CONTACT_UI_FLAG, { isMerging: true });
|
|
|
|
try {
|
|
|
|
const response = await AccountActionsAPI.merge(parentId, childId);
|
|
|
|
commit(types.SET_CONTACT_ITEM, response.data);
|
|
|
|
} catch (error) {
|
|
|
|
throw new Error(error);
|
|
|
|
} finally {
|
|
|
|
commit(types.SET_CONTACT_UI_FLAG, { isMerging: false });
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2021-09-23 07:22:49 +00:00
|
|
|
deleteContactThroughConversations: ({ commit }, id) => {
|
|
|
|
commit(types.DELETE_CONTACT, id);
|
|
|
|
commit(types.CLEAR_CONTACT_CONVERSATIONS, id, { root: true });
|
|
|
|
commit(`contactConversations/${types.DELETE_CONTACT_CONVERSATION}`, id, {
|
|
|
|
root: true,
|
|
|
|
});
|
|
|
|
},
|
2021-11-15 09:26:35 +00:00
|
|
|
|
|
|
|
updateContact: async ({ commit }, updateObj) => {
|
|
|
|
commit(types.SET_CONTACT_UI_FLAG, { isUpdating: true });
|
|
|
|
try {
|
|
|
|
commit(types.EDIT_CONTACT, updateObj);
|
|
|
|
commit(types.SET_CONTACT_UI_FLAG, { isUpdating: false });
|
|
|
|
} catch (error) {
|
|
|
|
commit(types.SET_CONTACT_UI_FLAG, { isUpdating: false });
|
|
|
|
}
|
|
|
|
},
|
2021-12-03 03:12:44 +00:00
|
|
|
|
|
|
|
filter: async ({ commit }, { page = 1, sortAttr, queryPayload } = {}) => {
|
|
|
|
commit(types.SET_CONTACT_UI_FLAG, { isFetching: true });
|
|
|
|
try {
|
|
|
|
const {
|
|
|
|
data: { payload, meta },
|
|
|
|
} = await ContactAPI.filter(page, sortAttr, queryPayload);
|
|
|
|
commit(types.CLEAR_CONTACTS);
|
|
|
|
commit(types.SET_CONTACTS, payload);
|
|
|
|
commit(types.SET_CONTACT_META, meta);
|
|
|
|
commit(types.SET_CONTACT_UI_FLAG, { isFetching: false });
|
|
|
|
} catch (error) {
|
|
|
|
commit(types.SET_CONTACT_UI_FLAG, { isFetching: false });
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
setContactFilters({ commit }, data) {
|
|
|
|
commit(types.SET_CONTACT_FILTERS, data);
|
|
|
|
},
|
|
|
|
|
|
|
|
clearContactFilters({ commit }) {
|
|
|
|
commit(types.CLEAR_CONTACT_FILTERS);
|
|
|
|
},
|
2020-11-10 09:55:26 +00:00
|
|
|
};
|