feat: Add compose conversation components (#10457)
Co-authored-by: Pranav <pranav@chatwoot.com> Co-authored-by: Pranav <pranavrajs@gmail.com>
This commit is contained in:
@@ -0,0 +1,118 @@
|
||||
import { INBOX_TYPES } from 'dashboard/helper/inbox';
|
||||
|
||||
export const convertChannelTypeToLabel = channelType => {
|
||||
const [, type] = channelType.split('::');
|
||||
return type ? type.charAt(0).toUpperCase() + type.slice(1) : channelType;
|
||||
};
|
||||
|
||||
export const generateLabelForContactableInboxesList = ({
|
||||
name,
|
||||
email,
|
||||
channelType,
|
||||
phoneNumber,
|
||||
}) => {
|
||||
if (channelType === INBOX_TYPES.EMAIL) {
|
||||
return `${name} (${email})`;
|
||||
}
|
||||
if (
|
||||
channelType === INBOX_TYPES.TWILIO ||
|
||||
channelType === INBOX_TYPES.WHATSAPP
|
||||
) {
|
||||
return `${name} (${phoneNumber})`;
|
||||
}
|
||||
return `${name} (${convertChannelTypeToLabel(channelType)})`;
|
||||
};
|
||||
|
||||
export const buildContactableInboxesList = contactInboxes => {
|
||||
if (!contactInboxes) return [];
|
||||
return contactInboxes.map(
|
||||
({ name, id, email, channelType, phoneNumber, ...rest }) => ({
|
||||
id,
|
||||
label: generateLabelForContactableInboxesList({
|
||||
name,
|
||||
email,
|
||||
channelType,
|
||||
phoneNumber,
|
||||
}),
|
||||
action: 'inbox',
|
||||
value: id,
|
||||
name,
|
||||
email,
|
||||
phoneNumber,
|
||||
channelType,
|
||||
...rest,
|
||||
})
|
||||
);
|
||||
};
|
||||
|
||||
export const prepareAttachmentPayload = (
|
||||
attachedFiles,
|
||||
directUploadsEnabled
|
||||
) => {
|
||||
const files = [];
|
||||
attachedFiles.forEach(attachment => {
|
||||
if (directUploadsEnabled) {
|
||||
files.push(attachment.blobSignedId);
|
||||
} else {
|
||||
files.push(attachment.resource.file);
|
||||
}
|
||||
});
|
||||
return files;
|
||||
};
|
||||
|
||||
export const prepareNewMessagePayload = ({
|
||||
targetInbox,
|
||||
selectedContact,
|
||||
message,
|
||||
subject,
|
||||
ccEmails,
|
||||
bccEmails,
|
||||
currentUser,
|
||||
attachedFiles = [],
|
||||
directUploadsEnabled = false,
|
||||
}) => {
|
||||
const payload = {
|
||||
inboxId: targetInbox.id,
|
||||
sourceId: targetInbox.sourceId,
|
||||
contactId: Number(selectedContact.id),
|
||||
message: { content: message },
|
||||
assigneeId: currentUser.id,
|
||||
};
|
||||
|
||||
if (attachedFiles?.length) {
|
||||
payload.files = prepareAttachmentPayload(
|
||||
attachedFiles,
|
||||
directUploadsEnabled
|
||||
);
|
||||
}
|
||||
|
||||
if (subject) {
|
||||
payload.mailSubject = subject;
|
||||
}
|
||||
|
||||
if (ccEmails) {
|
||||
payload.message.cc_emails = ccEmails;
|
||||
}
|
||||
|
||||
if (bccEmails) {
|
||||
payload.message.bcc_emails = bccEmails;
|
||||
}
|
||||
|
||||
return payload;
|
||||
};
|
||||
|
||||
export const prepareWhatsAppMessagePayload = ({
|
||||
targetInbox,
|
||||
selectedContact,
|
||||
message,
|
||||
templateParams,
|
||||
currentUser,
|
||||
}) => {
|
||||
return {
|
||||
inboxId: targetInbox.id,
|
||||
sourceId: targetInbox.sourceId,
|
||||
contactId: selectedContact.id,
|
||||
message: { content: message, template_params: templateParams },
|
||||
assigneeId: currentUser.id,
|
||||
};
|
||||
};
|
||||
@@ -0,0 +1,171 @@
|
||||
import { describe, it, expect } from 'vitest';
|
||||
import { INBOX_TYPES } from 'dashboard/helper/inbox';
|
||||
import * as helpers from '../composeConversationHelper';
|
||||
|
||||
describe('composeConversationHelper', () => {
|
||||
describe('convertChannelTypeToLabel', () => {
|
||||
it('converts channel type with namespace to capitalized label', () => {
|
||||
expect(helpers.convertChannelTypeToLabel('Channel::Email')).toBe('Email');
|
||||
expect(helpers.convertChannelTypeToLabel('Channel::Whatsapp')).toBe(
|
||||
'Whatsapp'
|
||||
);
|
||||
});
|
||||
|
||||
it('returns original value if no namespace found', () => {
|
||||
expect(helpers.convertChannelTypeToLabel('email')).toBe('email');
|
||||
});
|
||||
});
|
||||
|
||||
describe('generateLabelForContactableInboxesList', () => {
|
||||
const contact = {
|
||||
name: 'John Doe',
|
||||
email: 'john@example.com',
|
||||
phoneNumber: '+1234567890',
|
||||
};
|
||||
|
||||
it('generates label for email inbox', () => {
|
||||
expect(
|
||||
helpers.generateLabelForContactableInboxesList({
|
||||
...contact,
|
||||
channelType: INBOX_TYPES.EMAIL,
|
||||
})
|
||||
).toBe('John Doe (john@example.com)');
|
||||
});
|
||||
|
||||
it('generates label for twilio inbox', () => {
|
||||
expect(
|
||||
helpers.generateLabelForContactableInboxesList({
|
||||
...contact,
|
||||
channelType: INBOX_TYPES.TWILIO,
|
||||
})
|
||||
).toBe('John Doe (+1234567890)');
|
||||
});
|
||||
|
||||
it('generates label for whatsapp inbox', () => {
|
||||
expect(
|
||||
helpers.generateLabelForContactableInboxesList({
|
||||
...contact,
|
||||
channelType: INBOX_TYPES.WHATSAPP,
|
||||
})
|
||||
).toBe('John Doe (+1234567890)');
|
||||
});
|
||||
|
||||
it('generates label for other inbox types', () => {
|
||||
expect(
|
||||
helpers.generateLabelForContactableInboxesList({
|
||||
...contact,
|
||||
channelType: 'Channel::Api',
|
||||
})
|
||||
).toBe('John Doe (Api)');
|
||||
});
|
||||
});
|
||||
|
||||
describe('buildContactableInboxesList', () => {
|
||||
it('returns empty array if no contact inboxes', () => {
|
||||
expect(helpers.buildContactableInboxesList(null)).toEqual([]);
|
||||
expect(helpers.buildContactableInboxesList(undefined)).toEqual([]);
|
||||
});
|
||||
|
||||
it('builds list of contactable inboxes with correct format', () => {
|
||||
const inboxes = [
|
||||
{
|
||||
id: 1,
|
||||
name: 'Email Inbox',
|
||||
email: 'support@example.com',
|
||||
channelType: INBOX_TYPES.EMAIL,
|
||||
phoneNumber: null,
|
||||
},
|
||||
];
|
||||
|
||||
const result = helpers.buildContactableInboxesList(inboxes);
|
||||
expect(result[0]).toMatchObject({
|
||||
id: 1,
|
||||
label: 'Email Inbox (support@example.com)',
|
||||
action: 'inbox',
|
||||
value: 1,
|
||||
name: 'Email Inbox',
|
||||
email: 'support@example.com',
|
||||
channelType: INBOX_TYPES.EMAIL,
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
describe('prepareAttachmentPayload', () => {
|
||||
it('prepares direct upload files', () => {
|
||||
const files = [{ blobSignedId: 'signed1' }];
|
||||
expect(helpers.prepareAttachmentPayload(files, true)).toEqual([
|
||||
'signed1',
|
||||
]);
|
||||
});
|
||||
|
||||
it('prepares regular files', () => {
|
||||
const files = [{ resource: { file: 'file1' } }];
|
||||
expect(helpers.prepareAttachmentPayload(files, false)).toEqual(['file1']);
|
||||
});
|
||||
});
|
||||
|
||||
describe('prepareNewMessagePayload', () => {
|
||||
const baseParams = {
|
||||
targetInbox: { id: 1, sourceId: 'source1' },
|
||||
selectedContact: { id: '2' },
|
||||
message: 'Hello',
|
||||
currentUser: { id: 3 },
|
||||
};
|
||||
|
||||
it('prepares basic message payload', () => {
|
||||
const result = helpers.prepareNewMessagePayload(baseParams);
|
||||
expect(result).toEqual({
|
||||
inboxId: 1,
|
||||
sourceId: 'source1',
|
||||
contactId: 2,
|
||||
message: { content: 'Hello' },
|
||||
assigneeId: 3,
|
||||
});
|
||||
});
|
||||
|
||||
it('includes optional fields when provided', () => {
|
||||
const result = helpers.prepareNewMessagePayload({
|
||||
...baseParams,
|
||||
subject: 'Test',
|
||||
ccEmails: 'cc@test.com',
|
||||
bccEmails: 'bcc@test.com',
|
||||
attachedFiles: [{ blobSignedId: 'file1' }],
|
||||
directUploadsEnabled: true,
|
||||
});
|
||||
|
||||
expect(result).toMatchObject({
|
||||
mailSubject: 'Test',
|
||||
message: {
|
||||
content: 'Hello',
|
||||
cc_emails: 'cc@test.com',
|
||||
bcc_emails: 'bcc@test.com',
|
||||
},
|
||||
files: ['file1'],
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
describe('prepareWhatsAppMessagePayload', () => {
|
||||
it('prepares whatsapp message payload', () => {
|
||||
const params = {
|
||||
targetInbox: { id: 1, sourceId: 'source1' },
|
||||
selectedContact: { id: 2 },
|
||||
message: 'Hello',
|
||||
templateParams: { param1: 'value1' },
|
||||
currentUser: { id: 3 },
|
||||
};
|
||||
|
||||
const result = helpers.prepareWhatsAppMessagePayload(params);
|
||||
expect(result).toEqual({
|
||||
inboxId: 1,
|
||||
sourceId: 'source1',
|
||||
contactId: 2,
|
||||
message: {
|
||||
content: 'Hello',
|
||||
template_params: { param1: 'value1' },
|
||||
},
|
||||
assigneeId: 3,
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user