From c42fc65a2d9bd13963e6acb9b33be7d20dba742b Mon Sep 17 00:00:00 2001 From: Nithin David <1277421+nithindavid@users.noreply.github.com> Date: Tue, 9 Nov 2021 18:57:51 +0530 Subject: [PATCH] feat: Refactors API for conversation and messages to support multiple conversations --- .../widget/api/conversationPublic.js | 51 +++++++++++++++++++ app/javascript/widget/api/messagePublic.js | 40 +++++++++++++++ 2 files changed, 91 insertions(+) create mode 100644 app/javascript/widget/api/conversationPublic.js create mode 100644 app/javascript/widget/api/messagePublic.js diff --git a/app/javascript/widget/api/conversationPublic.js b/app/javascript/widget/api/conversationPublic.js new file mode 100644 index 000000000..db0f9f83b --- /dev/null +++ b/app/javascript/widget/api/conversationPublic.js @@ -0,0 +1,51 @@ +import { API } from 'widget/helpers/axios'; + +const buildUrl = endPoint => + `/api/v1/widget/${endPoint}${window.location.search}`; + +/* + * Refer: https://www.chatwoot.com/developers/api#tag/Conversations-API + */ + +export default { + async createWithMessage(content, contact) { + const referrerURL = window.referrerURL || ''; + + return API.post(buildUrl('conversations'), { + contact: { + name: contact.fullName, + email: contact.emailAddress, + }, + message: { + content, + timestamp: new Date().toString(), + referer_url: referrerURL, + }, + }); + }, + + async get() { + return API.get(buildUrl('conversations')); + }, + + async toggleTypingIn({ typingStatus, conversationId }) { + return API.post(buildUrl(`conversations/${conversationId}/toggle_typing`), { + typing_status: typingStatus, + }); + }, + + async setUserLastSeenIn({ lastSeen, conversationId }) { + return API.post( + buildUrl(`conversations/${conversationId}/update_last_seen`), + { + contact_last_seen_at: lastSeen, + } + ); + }, + + async sendEmailTranscriptIn({ email, conversationId }) { + return API.post(buildUrl(`conversations/${conversationId}/transcript`), { + email, + }); + }, +}; diff --git a/app/javascript/widget/api/messagePublic.js b/app/javascript/widget/api/messagePublic.js new file mode 100644 index 000000000..cd47ad7ba --- /dev/null +++ b/app/javascript/widget/api/messagePublic.js @@ -0,0 +1,40 @@ +import { API } from 'widget/helpers/axios'; + +const buildUrl = endPoint => + `/api/v1/widget/${endPoint}${window.location.search}`; + +/* + * Refer: https://www.chatwoot.com/developers/api#tag/Messages-API + */ + +export default { + create(conversationId, content, echoId) { + return API.post(buildUrl(`conversations/${conversationId}/messages`), { + content, + echo_id: echoId, + }); + }, + + createAttachment(conversationId, attachmentParams) { + return API.post( + buildUrl(`conversations/${conversationId}/messages`), + attachmentParams + ); + }, + + get(conversationId, beforeId) { + return API.get(buildUrl(`conversations/${conversationId}/messages`), { + params: { before: beforeId }, + }); + }, + + update(conversationId, messageObject) { + const { id: messageId } = messageObject; + return API.patch( + buildUrl(`conversations/${conversationId}/messages/${messageId}`), + { + ...messageObject, + } + ); + }, +};