2019-08-14 09:48:44 +00:00
|
|
|
/* eslint no-console: 0 */
|
|
|
|
/* global axios */
|
2019-10-27 13:31:59 +00:00
|
|
|
import ApiClient from '../ApiClient';
|
2019-08-14 09:48:44 +00:00
|
|
|
|
2021-08-02 10:37:30 +00:00
|
|
|
export const buildCreatePayload = ({
|
|
|
|
message,
|
|
|
|
isPrivate,
|
|
|
|
contentAttributes,
|
|
|
|
echoId,
|
2022-01-25 02:46:58 +00:00
|
|
|
files,
|
2021-11-15 15:35:24 +00:00
|
|
|
ccEmails = '',
|
|
|
|
bccEmails = '',
|
2022-06-07 12:03:33 +00:00
|
|
|
templateParams,
|
2021-08-02 10:37:30 +00:00
|
|
|
}) => {
|
|
|
|
let payload;
|
2022-01-25 02:46:58 +00:00
|
|
|
if (files && files.length !== 0) {
|
2021-08-02 10:37:30 +00:00
|
|
|
payload = new FormData();
|
|
|
|
if (message) {
|
|
|
|
payload.append('content', message);
|
|
|
|
}
|
2022-01-27 10:27:22 +00:00
|
|
|
files.forEach(file => {
|
|
|
|
payload.append('attachments[]', file);
|
|
|
|
});
|
2021-08-02 10:37:30 +00:00
|
|
|
payload.append('private', isPrivate);
|
|
|
|
payload.append('echo_id', echoId);
|
2021-10-11 07:30:48 +00:00
|
|
|
payload.append('cc_emails', ccEmails);
|
|
|
|
payload.append('bcc_emails', bccEmails);
|
2021-08-02 10:37:30 +00:00
|
|
|
} else {
|
|
|
|
payload = {
|
|
|
|
content: message,
|
|
|
|
private: isPrivate,
|
|
|
|
echo_id: echoId,
|
|
|
|
content_attributes: contentAttributes,
|
2021-10-11 07:30:48 +00:00
|
|
|
cc_emails: ccEmails,
|
|
|
|
bcc_emails: bccEmails,
|
2022-06-07 12:03:33 +00:00
|
|
|
template_params: templateParams,
|
2021-08-02 10:37:30 +00:00
|
|
|
};
|
|
|
|
}
|
|
|
|
return payload;
|
|
|
|
};
|
|
|
|
|
2019-10-27 13:31:59 +00:00
|
|
|
class MessageApi extends ApiClient {
|
|
|
|
constructor() {
|
2020-03-09 17:57:10 +00:00
|
|
|
super('conversations', { accountScoped: true });
|
2019-10-27 13:31:59 +00:00
|
|
|
}
|
2019-08-14 09:48:44 +00:00
|
|
|
|
2020-12-25 07:45:01 +00:00
|
|
|
create({
|
|
|
|
conversationId,
|
|
|
|
message,
|
|
|
|
private: isPrivate,
|
|
|
|
contentAttributes,
|
|
|
|
echo_id: echoId,
|
2022-01-25 02:46:58 +00:00
|
|
|
files,
|
2021-11-15 15:35:24 +00:00
|
|
|
ccEmails = '',
|
|
|
|
bccEmails = '',
|
2022-06-07 12:03:33 +00:00
|
|
|
templateParams,
|
2020-12-25 07:45:01 +00:00
|
|
|
}) {
|
2020-03-22 10:24:36 +00:00
|
|
|
return axios({
|
|
|
|
method: 'post',
|
|
|
|
url: `${this.url}/${conversationId}/messages`,
|
2021-08-02 10:37:30 +00:00
|
|
|
data: buildCreatePayload({
|
|
|
|
message,
|
|
|
|
isPrivate,
|
|
|
|
contentAttributes,
|
|
|
|
echoId,
|
2022-01-25 02:46:58 +00:00
|
|
|
files,
|
2021-10-11 07:30:48 +00:00
|
|
|
ccEmails,
|
|
|
|
bccEmails,
|
2022-06-07 12:03:33 +00:00
|
|
|
templateParams,
|
2021-08-02 10:37:30 +00:00
|
|
|
}),
|
2020-03-22 10:24:36 +00:00
|
|
|
});
|
|
|
|
}
|
2021-01-06 12:26:29 +00:00
|
|
|
|
2021-06-29 05:49:08 +00:00
|
|
|
delete(conversationID, messageId) {
|
|
|
|
return axios.delete(`${this.url}/${conversationID}/messages/${messageId}`);
|
|
|
|
}
|
|
|
|
|
2021-01-06 12:26:29 +00:00
|
|
|
getPreviousMessages({ conversationId, before }) {
|
|
|
|
return axios.get(`${this.url}/${conversationId}/messages`, {
|
|
|
|
params: { before },
|
|
|
|
});
|
|
|
|
}
|
2019-10-27 13:31:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
export default new MessageApi();
|