fix: Retry message not working if the conversation has an external issue (#8529)

This commit is contained in:
Sivin Varghese
2023-12-13 15:46:10 +05:30
committed by GitHub
parent 60a312ace5
commit 3adaa2d602
6 changed files with 96 additions and 4 deletions

View File

@@ -11,6 +11,19 @@ import {
} from './helpers/actionHelpers';
import messageReadActions from './actions/messageReadActions';
import messageTranslateActions from './actions/messageTranslateActions';
export const hasMessageFailedWithExternalError = pendingMessage => {
// This helper is used to check if the message has failed with an external error.
// We have two cases
// 1. Messages that fail from the UI itself (due to large attachments or a failed network):
// In this case, the message will have a status of failed but no external error. So we need to create that message again
// 2. Messages sent from Chatwoot but failed to deliver to the customer for some reason (user blocking or client system down):
// In this case, the message will have a status of failed and an external error. So we need to retry that message
const { content_attributes: contentAttributes, status } = pendingMessage;
const externalError = contentAttributes?.external_error ?? '';
return status === MESSAGE_STATUS.FAILED && externalError !== '';
};
// actions
const actions = {
getConversation: async ({ commit }, conversationId) => {
@@ -242,12 +255,15 @@ const actions = {
},
sendMessageWithData: async ({ commit }, pendingMessage) => {
const { conversation_id: conversationId, id } = pendingMessage;
try {
commit(types.ADD_MESSAGE, {
...pendingMessage,
status: MESSAGE_STATUS.PROGRESS,
});
const response = await MessageApi.create(pendingMessage);
const response = hasMessageFailedWithExternalError(pendingMessage)
? await MessageApi.retry(conversationId, id)
: await MessageApi.create(pendingMessage);
commit(types.ADD_MESSAGE, {
...response.data,
status: MESSAGE_STATUS.SENT,

View File

@@ -1,5 +1,7 @@
import axios from 'axios';
import actions from '../../conversations/actions';
import actions, {
hasMessageFailedWithExternalError,
} from '../../conversations/actions';
import types from '../../../mutation-types';
const dataToSend = {
payload: [
@@ -18,6 +20,41 @@ const dispatch = jest.fn();
global.axios = axios;
jest.mock('axios');
describe('#hasMessageFailedWithExternalError', () => {
it('returns false if message is sent', () => {
const pendingMessage = {
status: 'sent',
content_attributes: {},
};
expect(hasMessageFailedWithExternalError(pendingMessage)).toBe(false);
});
it('returns false if status is not failed', () => {
const pendingMessage = {
status: 'progress',
content_attributes: {},
};
expect(hasMessageFailedWithExternalError(pendingMessage)).toBe(false);
});
it('returns false if status is failed but no external error', () => {
const pendingMessage = {
status: 'failed',
content_attributes: {},
};
expect(hasMessageFailedWithExternalError(pendingMessage)).toBe(false);
});
it('returns true if status is failed and has external error', () => {
const pendingMessage = {
status: 'failed',
content_attributes: {
external_error: 'error',
},
};
expect(hasMessageFailedWithExternalError(pendingMessage)).toBe(true);
});
});
describe('#actions', () => {
describe('#getConversation', () => {
it('sends correct actions if API is success', async () => {