/* global axios */ import ApiClient from './ApiClient'; export const buildContactParams = (page, sortAttr, label, search) => ({ include_contact_inboxes: false, page, sort: sortAttr, ...(search ? { q: search } : {}), ...(label ? { labels: [label] } : {}), }); class ContactAPI extends ApiClient { constructor() { super('contacts', { accountScoped: true }); } get(page, sortAttr = 'name', label = '') { return axios.get(this.url, { params: buildContactParams(page, sortAttr, label, ''), }); } show(id) { return axios.get(`${this.url}/${id}?include_contact_inboxes=false`); } update(id, data) { return axios.patch(`${this.url}/${id}?include_contact_inboxes=false`, data); } getConversations(contactId, { inboxId } = {}) { const params = inboxId ? { inbox_id: inboxId } : {}; return axios.get(`${this.url}/${contactId}/conversations`, { params }); } getAttachments(contactId, page = 1) { return axios.get(`${this.url}/${contactId}/attachments`, { params: { page }, }); } getContactableInboxes(contactId) { return axios.get(`${this.url}/${contactId}/contactable_inboxes`); } getContactLabels(contactId) { return axios.get(`${this.url}/${contactId}/labels`); } initiateCall(contactId, inboxId, conversationId = null) { return axios.post(`${this.url}/${contactId}/call`, { inbox_id: inboxId, conversation_id: conversationId, }); } updateContactLabels(contactId, labels) { return axios.post(`${this.url}/${contactId}/labels`, { labels }); } search(search = '', page = 1, sortAttr = 'name', label = '', options = {}) { return axios.get(`${this.url}/search`, { params: buildContactParams(page, sortAttr, label, search), signal: options.signal, }); } active(page = 1, sortAttr = 'name') { return axios.get(`${this.url}/active`, { params: buildContactParams(page, sortAttr), }); } // eslint-disable-next-line default-param-last filter(page = 1, sortAttr = 'name', queryPayload) { return axios.post(`${this.url}/filter`, queryPayload, { params: buildContactParams(page, sortAttr), }); } importContacts(file) { const formData = new FormData(); formData.append('import_file', file); return axios.post(`${this.url}/import`, formData, { headers: { 'Content-Type': 'multipart/form-data' }, }); } destroyCustomAttributes(contactId, customAttributes) { return axios.post(`${this.url}/${contactId}/destroy_custom_attributes`, { custom_attributes: customAttributes, }); } destroyAvatar(contactId) { return axios.delete(`${this.url}/${contactId}/avatar`); } exportContacts(queryPayload) { return axios.post(`${this.url}/export`, queryPayload); } } export default new ContactAPI();