Compare commits
2
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
14c2048c1c | ||
|
|
a236d4da3a |
+10
-5
@@ -220,7 +220,7 @@ export default {
|
|||||||
conversationsUiFlags: 'contactConversations/getUIFlags',
|
conversationsUiFlags: 'contactConversations/getUIFlags',
|
||||||
currentUser: 'getCurrentUser',
|
currentUser: 'getCurrentUser',
|
||||||
}),
|
}),
|
||||||
emailMessagePayload() {
|
newMessagePayload() {
|
||||||
const payload = {
|
const payload = {
|
||||||
inboxId: this.targetInbox.id,
|
inboxId: this.targetInbox.id,
|
||||||
sourceId: this.targetInbox.sourceId,
|
sourceId: this.targetInbox.sourceId,
|
||||||
@@ -323,15 +323,19 @@ export default {
|
|||||||
return payload;
|
return payload;
|
||||||
},
|
},
|
||||||
onFormSubmit() {
|
onFormSubmit() {
|
||||||
|
const isFromWhatsApp = false;
|
||||||
this.$v.$touch();
|
this.$v.$touch();
|
||||||
if (this.$v.$invalid) {
|
if (this.$v.$invalid) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
this.createConversation(this.emailMessagePayload);
|
this.createConversation({
|
||||||
|
payload: this.newMessagePayload,
|
||||||
|
isFromWhatsApp,
|
||||||
|
});
|
||||||
},
|
},
|
||||||
async createConversation(payload) {
|
async createConversation({ payload, isFromWhatsApp }) {
|
||||||
try {
|
try {
|
||||||
const data = await this.onSubmit(payload);
|
const data = await this.onSubmit(payload, isFromWhatsApp);
|
||||||
const action = {
|
const action = {
|
||||||
type: 'link',
|
type: 'link',
|
||||||
to: `/app/accounts/${data.account_id}/conversations/${data.id}`,
|
to: `/app/accounts/${data.account_id}/conversations/${data.id}`,
|
||||||
@@ -355,8 +359,9 @@ export default {
|
|||||||
this.whatsappTemplateSelected = val;
|
this.whatsappTemplateSelected = val;
|
||||||
},
|
},
|
||||||
async onSendWhatsAppReply(messagePayload) {
|
async onSendWhatsAppReply(messagePayload) {
|
||||||
|
const isFromWhatsApp = true;
|
||||||
const payload = this.prepareWhatsAppMessagePayload(messagePayload);
|
const payload = this.prepareWhatsAppMessagePayload(messagePayload);
|
||||||
await this.createConversation(payload);
|
await this.createConversation({ payload, isFromWhatsApp });
|
||||||
},
|
},
|
||||||
inboxReadableIdentifier(inbox) {
|
inboxReadableIdentifier(inbox) {
|
||||||
return `${inbox.name} (${inbox.channel_type})`;
|
return `${inbox.name} (${inbox.channel_type})`;
|
||||||
|
|||||||
@@ -48,11 +48,11 @@ export default {
|
|||||||
onSuccess() {
|
onSuccess() {
|
||||||
this.$emit('cancel');
|
this.$emit('cancel');
|
||||||
},
|
},
|
||||||
async onSubmit(contactItem) {
|
async onSubmit(params, isFromWhatsApp) {
|
||||||
const data = await this.$store.dispatch(
|
const data = await this.$store.dispatch('contactConversations/create', {
|
||||||
'contactConversations/create',
|
params,
|
||||||
contactItem
|
isFromWhatsApp,
|
||||||
);
|
});
|
||||||
return data;
|
return data;
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
|
|||||||
@@ -3,6 +3,64 @@ import * as types from '../mutation-types';
|
|||||||
import ContactAPI from '../../api/contacts';
|
import ContactAPI from '../../api/contacts';
|
||||||
import ConversationApi from '../../api/conversations';
|
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;
|
||||||
|
};
|
||||||
|
|
||||||
|
export const createWhatsAppConversationPayload = ({ params }) => {
|
||||||
|
const { inboxId, message, contactId, sourceId, assigneeId } = params;
|
||||||
|
|
||||||
|
const payload = {
|
||||||
|
inbox_id: inboxId,
|
||||||
|
contact_id: contactId,
|
||||||
|
source_id: sourceId,
|
||||||
|
message,
|
||||||
|
assignee_id: assigneeId,
|
||||||
|
};
|
||||||
|
|
||||||
|
return payload;
|
||||||
|
};
|
||||||
|
|
||||||
|
const setNewConversationPayload = ({
|
||||||
|
isFromWhatsApp,
|
||||||
|
params,
|
||||||
|
contactId,
|
||||||
|
files,
|
||||||
|
}) => {
|
||||||
|
if (isFromWhatsApp) {
|
||||||
|
return createWhatsAppConversationPayload({ params });
|
||||||
|
}
|
||||||
|
return createConversationPayload({
|
||||||
|
params,
|
||||||
|
contactId,
|
||||||
|
files,
|
||||||
|
});
|
||||||
|
};
|
||||||
|
|
||||||
const state = {
|
const state = {
|
||||||
records: {},
|
records: {},
|
||||||
uiFlags: {
|
uiFlags: {
|
||||||
@@ -20,33 +78,25 @@ export const getters = {
|
|||||||
};
|
};
|
||||||
|
|
||||||
export const actions = {
|
export const actions = {
|
||||||
create: async ({ commit }, params) => {
|
create: async ({ commit }, { params, isFromWhatsApp }) => {
|
||||||
commit(types.default.SET_CONTACT_CONVERSATIONS_UI_FLAG, {
|
commit(types.default.SET_CONTACT_CONVERSATIONS_UI_FLAG, {
|
||||||
isCreating: true,
|
isCreating: true,
|
||||||
});
|
});
|
||||||
const {
|
const { contactId, files } = params;
|
||||||
inboxId,
|
|
||||||
message,
|
|
||||||
contactId,
|
|
||||||
sourceId,
|
|
||||||
mailSubject,
|
|
||||||
assigneeId,
|
|
||||||
} = params;
|
|
||||||
try {
|
try {
|
||||||
const { data } = await ConversationApi.create({
|
const payload = setNewConversationPayload({
|
||||||
inbox_id: inboxId,
|
isFromWhatsApp,
|
||||||
contact_id: contactId,
|
params,
|
||||||
source_id: sourceId,
|
contactId,
|
||||||
additional_attributes: {
|
files,
|
||||||
mail_subject: mailSubject,
|
|
||||||
},
|
|
||||||
message,
|
|
||||||
assignee_id: assigneeId,
|
|
||||||
});
|
});
|
||||||
|
|
||||||
|
const { data } = await ConversationApi.create(payload);
|
||||||
commit(types.default.ADD_CONTACT_CONVERSATION, {
|
commit(types.default.ADD_CONTACT_CONVERSATION, {
|
||||||
id: contactId,
|
id: contactId,
|
||||||
data,
|
data,
|
||||||
});
|
});
|
||||||
|
|
||||||
return data;
|
return data;
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
throw new Error(error);
|
throw new Error(error);
|
||||||
|
|||||||
+229
-13
@@ -1,5 +1,10 @@
|
|||||||
import axios from 'axios';
|
import axios from 'axios';
|
||||||
import { actions } from '../../contactConversations';
|
import {
|
||||||
|
actions,
|
||||||
|
createMessagePayload,
|
||||||
|
createConversationPayload,
|
||||||
|
createWhatsAppConversationPayload,
|
||||||
|
} from '../../contactConversations';
|
||||||
import * as types from '../../../mutation-types';
|
import * as types from '../../../mutation-types';
|
||||||
import conversationList from './fixtures';
|
import conversationList from './fixtures';
|
||||||
|
|
||||||
@@ -44,16 +49,84 @@ describe('#actions', () => {
|
|||||||
await actions.create(
|
await actions.create(
|
||||||
{ commit },
|
{ commit },
|
||||||
{
|
{
|
||||||
inboxId: 1,
|
params: {
|
||||||
message: { content: 'hi' },
|
inboxId: 1,
|
||||||
contactId: 4,
|
message: { content: 'hi' },
|
||||||
sourceId: 5,
|
contactId: 4,
|
||||||
mailSubject: 'Mail Subject',
|
sourceId: 5,
|
||||||
|
mailSubject: 'Mail Subject',
|
||||||
|
assigneeId: 6,
|
||||||
|
files: [],
|
||||||
|
},
|
||||||
|
isFromWhatsApp: false,
|
||||||
|
}
|
||||||
|
);
|
||||||
|
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 },
|
||||||
|
{
|
||||||
|
params: {
|
||||||
|
inboxId: 1,
|
||||||
|
message: { content: 'hi' },
|
||||||
|
contactId: 4,
|
||||||
|
sourceId: 5,
|
||||||
|
mailSubject: 'Mail Subject',
|
||||||
|
assigneeId: 6,
|
||||||
|
files: ['file1.pdf', 'file2.jpg'],
|
||||||
|
},
|
||||||
|
isFromWhatsApp: false,
|
||||||
|
}
|
||||||
|
);
|
||||||
|
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 actions if API is success for whatsapp conversation', async () => {
|
||||||
|
axios.post.mockResolvedValue({ data: conversationList[0] });
|
||||||
|
await actions.create(
|
||||||
|
{ commit },
|
||||||
|
{
|
||||||
|
params: {
|
||||||
|
inboxId: 1,
|
||||||
|
message: {
|
||||||
|
content: 'hi',
|
||||||
|
template_params: {
|
||||||
|
name: 'hello_world',
|
||||||
|
category: 'MARKETING',
|
||||||
|
language: 'en_US',
|
||||||
|
processed_params: {},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
contactId: 4,
|
||||||
|
sourceId: 5,
|
||||||
|
assigneeId: 6,
|
||||||
|
},
|
||||||
|
isFromWhatsApp: true,
|
||||||
}
|
}
|
||||||
);
|
);
|
||||||
expect(commit.mock.calls).toEqual([
|
expect(commit.mock.calls).toEqual([
|
||||||
[types.default.SET_CONTACT_CONVERSATIONS_UI_FLAG, { isCreating: true }],
|
[types.default.SET_CONTACT_CONVERSATIONS_UI_FLAG, { isCreating: true }],
|
||||||
|
|
||||||
[
|
[
|
||||||
types.default.ADD_CONTACT_CONVERSATION,
|
types.default.ADD_CONTACT_CONVERSATION,
|
||||||
{ id: 4, data: conversationList[0] },
|
{ id: 4, data: conversationList[0] },
|
||||||
@@ -66,16 +139,46 @@ describe('#actions', () => {
|
|||||||
});
|
});
|
||||||
it('sends correct actions if API is error', async () => {
|
it('sends correct actions if API is error', async () => {
|
||||||
axios.post.mockRejectedValue({ message: 'Incorrect header' });
|
axios.post.mockRejectedValue({ message: 'Incorrect header' });
|
||||||
|
|
||||||
await expect(
|
await expect(
|
||||||
actions.create(
|
actions.create(
|
||||||
{ commit },
|
{ commit },
|
||||||
{
|
{
|
||||||
inboxId: 1,
|
params: {
|
||||||
message: { content: 'hi' },
|
inboxId: 1,
|
||||||
contactId: 4,
|
message: { content: 'hi' },
|
||||||
sourceId: 5,
|
contactId: 4,
|
||||||
mailSubject: 'Mail Subject',
|
assigneeId: 6,
|
||||||
|
sourceId: 5,
|
||||||
|
mailSubject: 'Mail Subject',
|
||||||
|
},
|
||||||
|
isFromWhatsApp: false,
|
||||||
|
}
|
||||||
|
)
|
||||||
|
).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 },
|
||||||
|
],
|
||||||
|
]);
|
||||||
|
});
|
||||||
|
it('sends correct actions with files if API is error', async () => {
|
||||||
|
axios.post.mockRejectedValue({ message: 'Incorrect header' });
|
||||||
|
await expect(
|
||||||
|
actions.create(
|
||||||
|
{ commit },
|
||||||
|
{
|
||||||
|
params: {
|
||||||
|
inboxId: 1,
|
||||||
|
message: { content: 'hi' },
|
||||||
|
contactId: 4,
|
||||||
|
sourceId: 5,
|
||||||
|
mailSubject: 'Mail Subject',
|
||||||
|
assigneeId: 6,
|
||||||
|
files: ['file1.pdf', 'file2.jpg'],
|
||||||
|
},
|
||||||
|
isFromWhatsApp: false,
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
).rejects.toThrow(Error);
|
).rejects.toThrow(Error);
|
||||||
@@ -89,3 +192,116 @@ describe('#actions', () => {
|
|||||||
});
|
});
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
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([]);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
describe('createWhatsAppConversationPayload', () => {
|
||||||
|
it('creates conversation payload with message', () => {
|
||||||
|
const options = {
|
||||||
|
params: {
|
||||||
|
inboxId: '1',
|
||||||
|
message: {
|
||||||
|
content: 'Test message content',
|
||||||
|
},
|
||||||
|
sourceId: '12',
|
||||||
|
assigneeId: '123',
|
||||||
|
},
|
||||||
|
};
|
||||||
|
|
||||||
|
const payload = createWhatsAppConversationPayload(options);
|
||||||
|
|
||||||
|
expect(payload.message).toBe(options.params.message);
|
||||||
|
expect(payload.inbox_id).toBe(options.params.inboxId);
|
||||||
|
expect(payload.source_id).toBe(options.params.sourceId);
|
||||||
|
expect(payload.assignee_id).toBe(options.params.assigneeId);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|||||||
Reference in New Issue
Block a user