feat: Refactors API for conversation and messages to support multiple conversations
This commit is contained in:
parent
0c9b682329
commit
c42fc65a2d
2 changed files with 91 additions and 0 deletions
51
app/javascript/widget/api/conversationPublic.js
Normal file
51
app/javascript/widget/api/conversationPublic.js
Normal file
|
@ -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,
|
||||||
|
});
|
||||||
|
},
|
||||||
|
};
|
40
app/javascript/widget/api/messagePublic.js
Normal file
40
app/javascript/widget/api/messagePublic.js
Normal file
|
@ -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,
|
||||||
|
}
|
||||||
|
);
|
||||||
|
},
|
||||||
|
};
|
Loading…
Add table
Add a link
Reference in a new issue