feat: Ability to send attachment in new conversation (#7698)
Co-authored-by: Muhsin Keloth <muhsinkeramam@gmail.com>
This commit is contained in:
@@ -3,6 +3,34 @@ import * as types from '../mutation-types';
|
||||
import ContactAPI from '../../api/contacts';
|
||||
import ConversationApi from '../../api/conversations';
|
||||
|
||||
export const createMessagePayload = (payload, message) => {
|
||||
const { content, cc_emails, bcc_emails } = message;
|
||||
payload.append('message[content]', content);
|
||||
if (cc_emails) payload.append('message[cc_emails]', cc_emails);
|
||||
if (bcc_emails) payload.append('message[bcc_emails]', bcc_emails);
|
||||
};
|
||||
|
||||
export const createConversationPayload = ({ params, contactId, files }) => {
|
||||
const { inboxId, message, sourceId, mailSubject, assigneeId } = params;
|
||||
const payload = new FormData();
|
||||
|
||||
if (message) {
|
||||
createMessagePayload(payload, message);
|
||||
}
|
||||
|
||||
if (files && files.length > 0) {
|
||||
files.forEach(file => payload.append('message[attachments][]', file));
|
||||
}
|
||||
|
||||
payload.append('inbox_id', inboxId);
|
||||
payload.append('contact_id', contactId);
|
||||
payload.append('source_id', sourceId);
|
||||
payload.append('additional_attributes[mail_subject]', mailSubject);
|
||||
payload.append('assignee_id', assigneeId);
|
||||
|
||||
return payload;
|
||||
};
|
||||
|
||||
const state = {
|
||||
records: {},
|
||||
uiFlags: {
|
||||
@@ -24,29 +52,17 @@ export const actions = {
|
||||
commit(types.default.SET_CONTACT_CONVERSATIONS_UI_FLAG, {
|
||||
isCreating: true,
|
||||
});
|
||||
const {
|
||||
inboxId,
|
||||
message,
|
||||
contactId,
|
||||
sourceId,
|
||||
mailSubject,
|
||||
assigneeId,
|
||||
} = params;
|
||||
const { contactId, files } = params;
|
||||
|
||||
try {
|
||||
const { data } = await ConversationApi.create({
|
||||
inbox_id: inboxId,
|
||||
contact_id: contactId,
|
||||
source_id: sourceId,
|
||||
additional_attributes: {
|
||||
mail_subject: mailSubject,
|
||||
},
|
||||
message,
|
||||
assignee_id: assigneeId,
|
||||
});
|
||||
const payload = createConversationPayload({ params, contactId, files });
|
||||
|
||||
const { data } = await ConversationApi.create(payload);
|
||||
commit(types.default.ADD_CONTACT_CONVERSATION, {
|
||||
id: contactId,
|
||||
data,
|
||||
});
|
||||
|
||||
return data;
|
||||
} catch (error) {
|
||||
throw new Error(error);
|
||||
|
||||
@@ -1,5 +1,9 @@
|
||||
import axios from 'axios';
|
||||
import { actions } from '../../contactConversations';
|
||||
import {
|
||||
actions,
|
||||
createMessagePayload,
|
||||
createConversationPayload,
|
||||
} from '../../contactConversations';
|
||||
import * as types from '../../../mutation-types';
|
||||
import conversationList from './fixtures';
|
||||
|
||||
@@ -49,6 +53,35 @@ describe('#actions', () => {
|
||||
contactId: 4,
|
||||
sourceId: 5,
|
||||
mailSubject: 'Mail Subject',
|
||||
assigneeId: 6,
|
||||
files: [],
|
||||
}
|
||||
);
|
||||
expect(commit.mock.calls).toEqual([
|
||||
[types.default.SET_CONTACT_CONVERSATIONS_UI_FLAG, { isCreating: true }],
|
||||
|
||||
[
|
||||
types.default.ADD_CONTACT_CONVERSATION,
|
||||
{ id: 4, data: conversationList[0] },
|
||||
],
|
||||
[
|
||||
types.default.SET_CONTACT_CONVERSATIONS_UI_FLAG,
|
||||
{ isCreating: false },
|
||||
],
|
||||
]);
|
||||
});
|
||||
it('sends correct actions with files if API is success', async () => {
|
||||
axios.post.mockResolvedValue({ data: conversationList[0] });
|
||||
await actions.create(
|
||||
{ commit },
|
||||
{
|
||||
inboxId: 1,
|
||||
message: { content: 'hi' },
|
||||
contactId: 4,
|
||||
sourceId: 5,
|
||||
assigneeId: 6,
|
||||
mailSubject: 'Mail Subject',
|
||||
files: [new File([], 'file1')],
|
||||
}
|
||||
);
|
||||
expect(commit.mock.calls).toEqual([
|
||||
@@ -74,6 +107,7 @@ describe('#actions', () => {
|
||||
inboxId: 1,
|
||||
message: { content: 'hi' },
|
||||
contactId: 4,
|
||||
assigneeId: 6,
|
||||
sourceId: 5,
|
||||
mailSubject: 'Mail Subject',
|
||||
}
|
||||
@@ -87,5 +121,121 @@ describe('#actions', () => {
|
||||
],
|
||||
]);
|
||||
});
|
||||
it('sends correct actions with files if API is error', async () => {
|
||||
axios.post.mockRejectedValue({ message: 'Incorrect header' });
|
||||
|
||||
await expect(
|
||||
actions.create(
|
||||
{ commit },
|
||||
{
|
||||
inboxId: 1,
|
||||
message: { content: 'hi' },
|
||||
contactId: 4,
|
||||
assigneeId: 6,
|
||||
sourceId: 5,
|
||||
mailSubject: 'Mail Subject',
|
||||
files: [new File([], 'file1')],
|
||||
}
|
||||
)
|
||||
).rejects.toThrow(Error);
|
||||
expect(commit.mock.calls).toEqual([
|
||||
[types.default.SET_CONTACT_CONVERSATIONS_UI_FLAG, { isCreating: true }],
|
||||
[
|
||||
types.default.SET_CONTACT_CONVERSATIONS_UI_FLAG,
|
||||
{ isCreating: false },
|
||||
],
|
||||
]);
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
describe('createMessagePayload', () => {
|
||||
it('creates message payload with cc and bcc emails', () => {
|
||||
const payload = new FormData();
|
||||
const message = {
|
||||
content: 'Test message content',
|
||||
cc_emails: 'cc@example.com',
|
||||
bcc_emails: 'bcc@example.com',
|
||||
};
|
||||
|
||||
createMessagePayload(payload, message);
|
||||
|
||||
expect(payload.get('message[content]')).toBe(message.content);
|
||||
expect(payload.get('message[cc_emails]')).toBe(message.cc_emails);
|
||||
expect(payload.get('message[bcc_emails]')).toBe(message.bcc_emails);
|
||||
});
|
||||
|
||||
it('creates message payload without cc and bcc emails', () => {
|
||||
const payload = new FormData();
|
||||
const message = {
|
||||
content: 'Test message content',
|
||||
};
|
||||
|
||||
createMessagePayload(payload, message);
|
||||
|
||||
expect(payload.get('message[content]')).toBe(message.content);
|
||||
expect(payload.get('message[cc_emails]')).toBeNull();
|
||||
expect(payload.get('message[bcc_emails]')).toBeNull();
|
||||
});
|
||||
});
|
||||
|
||||
describe('createConversationPayload', () => {
|
||||
it('creates conversation payload with message and attachments', () => {
|
||||
const options = {
|
||||
params: {
|
||||
inboxId: '1',
|
||||
message: {
|
||||
content: 'Test message content',
|
||||
},
|
||||
sourceId: '12',
|
||||
mailSubject: 'Test Subject',
|
||||
assigneeId: '123',
|
||||
},
|
||||
contactId: '23',
|
||||
files: ['file1.pdf', 'file2.jpg'],
|
||||
};
|
||||
|
||||
const payload = createConversationPayload(options);
|
||||
|
||||
expect(payload.get('message[content]')).toBe(
|
||||
options.params.message.content
|
||||
);
|
||||
expect(payload.get('inbox_id')).toBe(options.params.inboxId);
|
||||
expect(payload.get('contact_id')).toBe(options.contactId);
|
||||
expect(payload.get('source_id')).toBe(options.params.sourceId);
|
||||
expect(payload.get('additional_attributes[mail_subject]')).toBe(
|
||||
options.params.mailSubject
|
||||
);
|
||||
expect(payload.get('assignee_id')).toBe(options.params.assigneeId);
|
||||
expect(payload.getAll('message[attachments][]')).toEqual(options.files);
|
||||
});
|
||||
|
||||
it('creates conversation payload with message and without attachments', () => {
|
||||
const options = {
|
||||
params: {
|
||||
inboxId: '1',
|
||||
message: {
|
||||
content: 'Test message content',
|
||||
},
|
||||
sourceId: '12',
|
||||
mailSubject: 'Test Subject',
|
||||
assigneeId: '123',
|
||||
},
|
||||
contactId: '23',
|
||||
};
|
||||
|
||||
const payload = createConversationPayload(options);
|
||||
|
||||
expect(payload.get('message[content]')).toBe(
|
||||
options.params.message.content
|
||||
);
|
||||
expect(payload.get('inbox_id')).toBe(options.params.inboxId);
|
||||
expect(payload.get('contact_id')).toBe(options.contactId);
|
||||
expect(payload.get('source_id')).toBe(options.params.sourceId);
|
||||
expect(payload.get('additional_attributes[mail_subject]')).toBe(
|
||||
options.params.mailSubject
|
||||
);
|
||||
expect(payload.get('assignee_id')).toBe(options.params.assigneeId);
|
||||
expect(payload.getAll('message[attachments][]')).toEqual([]);
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user