import { apiSrvHttpClient } from './httpClient.js'; import Attachment from 'models/Attachment'; import WabaTemplate from 'models/WabaTemplate'; const AMO_ACCOUNT_ID = AMOCRM.constant('account').amojo_id; const AMO_ACCOUNT_DOMAIN = AMOCRM.widgets.system.domain; const AMO_ENDPOINT = '/api/v1/amo'; const getIntegrationConfig = () => { return apiSrvHttpClient.get(`${AMO_ENDPOINT}/integrations/${AMO_ACCOUNT_ID}`) }; const createIntegrationConfig = (tbApiToken) => { return apiSrvHttpClient.post( `${AMO_ENDPOINT}/integrations/`, { accountId: AMO_ACCOUNT_ID, referer: AMO_ACCOUNT_DOMAIN, }, { headers: { 'Authorization': `Bearer ${tbApiToken}` } } ); }; const deleteIntegrationConfig = () => { return apiSrvHttpClient.post(`${AMO_ENDPOINT}/integrations/delete/${AMO_ACCOUNT_ID}`); }; const addWidgetAsSource = (widgetCode) => { return apiSrvHttpClient.post(`${AMO_ENDPOINT}/source/${AMO_ACCOUNT_ID}`, { widgetCode }); }; const getWabaTemplates = () => { return apiSrvHttpClient.get(`${AMO_ENDPOINT}/templates/whatsapp/${AMO_ACCOUNT_ID}`) .then((initialTemplates) => { const collator = new Intl.Collator('ru'); return initialTemplates .map((template) => new WabaTemplate(template)) .sort(({ name: a }, { name: b }) => collator.compare(a, b)) }); }; const getChannels = () => { return apiSrvHttpClient.get(`${AMO_ENDPOINT}/channels/waba`, { params: { accountId: AMO_ACCOUNT_ID }, }); }; const sendWabaTemplateMessage = ({ contactId, phone, channelId, templateId, attachment, parameters, amojoId, name }) => { return apiSrvHttpClient.post(`${AMO_ENDPOINT}/notification`, { contactId, phone, channelId, templateId, attachment, substitutions: parameters, amojoId, name, accountId: AMO_ACCOUNT_ID, }); }; const getWabaLastInboundMessageTime = (phone) => { return apiSrvHttpClient.get(`${AMO_ENDPOINT}/lastInboundMessageTime/whatsappb`, { headers: { 'Content-Type': 'multipart/form-data', }, params: { phone, accountId: AMO_ACCOUNT_ID, }, }) }; const uploadFile = async (file) => { const formData = new FormData(); formData.append('file', file); const { publicUri: publicUrl } = await apiSrvHttpClient.post(`api/upload/fromWidget`, formData, { headers: { 'Content-Type': 'multipart/form-data', }, params: { accountId: AMO_ACCOUNT_ID } }); return new Attachment({ publicUrl, channelFileId: publicUrl, fileName: file.name, fileSize: file.size, }); }; export default { getIntegrationConfig, createIntegrationConfig, deleteIntegrationConfig, addWidgetAsSource, getWabaTemplates, getChannels, sendWabaTemplateMessage, getWabaLastInboundMessageTime, uploadFile, };