## Description This PR fixes a data-corruption bug in the new conversation flow that surfaces in conversation search results as `Subject: undefined`. ### The Problem When creating a conversation through the "New conversation" modal without typing a subject (every non-email channel never shows the subject input, and email channels can be left blank), the conversation gets persisted with `additional_attributes.mail_subject = "undefined"` (the literal string). The bogus value shows up in conversation search results as `Subject: undefined`, which started rendering after #10843. ### Root Cause `createConversationPayload` in `app/javascript/dashboard/store/modules/contactConversations.js` appends the `mail_subject` field unconditionally: ```js payload.append('additional_attributes[mail_subject]', mailSubject); ``` `composeConversationHelper.js` only sets `payload.mailSubject` when `subject` is truthy, so when no subject is provided, `mailSubject` is destructured as `undefined` in `createConversationPayload`. `FormData.append` coerces `undefined` to the string `"undefined"`, and the backend persists it as-is into the JSONB column. ### The Fix Skip the append when `mailSubject` is falsy. The backend already treats a missing key the same as an empty subject (the email mailer falls back to a default subject when `mail_subject` is `nil`), so omitting the field is safe across channels. ### Key Changes - Guarded the `additional_attributes[mail_subject]` append in `createConversationPayload`. - Added a spec covering the case where `mailSubject` is omitted. > Note: existing rows persisted before this fix will continue to show `Subject: undefined` in search until cleaned up. A simple SQL cleanup for non-email inboxes: > > ```sql > UPDATE conversations > SET additional_attributes = additional_attributes - 'mail_subject' > FROM inboxes > WHERE conversations.inbox_id = inboxes.id > AND inboxes.channel_type <> 'Channel::Email' > AND conversations.additional_attributes->>'mail_subject' = 'undefined'; > ``` > > I'm leaving any data-cleanup migration out of this PR since it's a maintenance concern that may be handled differently per installation. ## Type of change - [X] Bug fix (non-breaking change which fixes an issue) ## How Has This Been Tested? 1. Open the "New conversation" modal on a non-email inbox (e.g., WhatsApp, SMS, API). 2. Create a conversation without filling any subject (field is not present on those, so regular flow). 3. Open the global search and search for the conversation. 4. **Before:** the result card shows `Subject: undefined`. **After:** the subject row is hidden. 5. Repeat on an email inbox leaving the subject blank — same result. 6. On an email inbox, type a subject and verify it still persists and renders correctly. 7. Run the new spec: `pnpm test contactConversations`. ## Checklist: - [X] My code follows the style guidelines of this project - [X] I have performed a self-review of my code - [ ] I have commented on my code, particularly in hard-to-understand areas - [ ] I have made corresponding changes to the documentation - [X] My changes generate no new warnings - [X] I have added tests that prove my fix is effective or that my feature works - [X] New and existing unit tests pass locally with my changes - [ ] Any dependent changes have been merged and published in downstream modules Co-authored-by: Sojan Jose <sojan@pepalo.com> Co-authored-by: Sony Mathew <sony@chatwoot.com>
181 lines
4.5 KiB
JavaScript
181 lines
4.5 KiB
JavaScript
import * as types from '../mutation-types';
|
|
import ContactAPI from '../../api/contacts';
|
|
import ConversationApi from '../../api/conversations';
|
|
import camelcaseKeys from 'camelcase-keys';
|
|
|
|
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);
|
|
if (mailSubject) {
|
|
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 = {
|
|
records: {},
|
|
uiFlags: {
|
|
isFetching: false,
|
|
},
|
|
};
|
|
|
|
export const getters = {
|
|
getUIFlags($state) {
|
|
return $state.uiFlags;
|
|
},
|
|
getContactConversation: $state => id => {
|
|
return $state.records[Number(id)] || [];
|
|
},
|
|
getAllConversationsByContactId: $state => id => {
|
|
const records = $state.records[Number(id)] || [];
|
|
return camelcaseKeys(records, { deep: true });
|
|
},
|
|
};
|
|
|
|
export const actions = {
|
|
create: async ({ commit }, { params, isFromWhatsApp }) => {
|
|
commit(types.default.SET_CONTACT_CONVERSATIONS_UI_FLAG, {
|
|
isCreating: true,
|
|
});
|
|
const { contactId, files } = params;
|
|
try {
|
|
const payload = setNewConversationPayload({
|
|
isFromWhatsApp,
|
|
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);
|
|
} finally {
|
|
commit(types.default.SET_CONTACT_CONVERSATIONS_UI_FLAG, {
|
|
isCreating: false,
|
|
});
|
|
}
|
|
},
|
|
get: async ({ commit }, contactId) => {
|
|
commit(types.default.SET_CONTACT_CONVERSATIONS_UI_FLAG, {
|
|
isFetching: true,
|
|
});
|
|
try {
|
|
const response = await ContactAPI.getConversations(contactId);
|
|
commit(types.default.SET_CONTACT_CONVERSATIONS, {
|
|
id: contactId,
|
|
data: response.data.payload,
|
|
});
|
|
commit(types.default.SET_CONTACT_CONVERSATIONS_UI_FLAG, {
|
|
isFetching: false,
|
|
});
|
|
} catch (error) {
|
|
commit(types.default.SET_CONTACT_CONVERSATIONS_UI_FLAG, {
|
|
isFetching: false,
|
|
});
|
|
}
|
|
},
|
|
};
|
|
|
|
export const mutations = {
|
|
[types.default.SET_CONTACT_CONVERSATIONS_UI_FLAG]($state, data) {
|
|
$state.uiFlags = {
|
|
...$state.uiFlags,
|
|
...data,
|
|
};
|
|
},
|
|
[types.default.SET_CONTACT_CONVERSATIONS]: ($state, { id, data }) => {
|
|
$state.records = {
|
|
...$state.records,
|
|
[id]: data,
|
|
};
|
|
},
|
|
[types.default.ADD_CONTACT_CONVERSATION]: ($state, { id, data }) => {
|
|
const conversations = $state.records[id] || [];
|
|
|
|
const updatedConversations = [...conversations];
|
|
const index = conversations.findIndex(
|
|
conversation => conversation.id === data.id
|
|
);
|
|
|
|
if (index !== -1) {
|
|
updatedConversations[index] = { ...conversations[index], ...data };
|
|
} else {
|
|
updatedConversations.push(data);
|
|
}
|
|
|
|
$state.records = {
|
|
...$state.records,
|
|
[id]: updatedConversations,
|
|
};
|
|
},
|
|
[types.default.DELETE_CONTACT_CONVERSATION]: ($state, id) => {
|
|
const { [id]: deletedRecord, ...remainingRecords } = $state.records;
|
|
$state.records = remainingRecords;
|
|
},
|
|
};
|
|
|
|
export default {
|
|
namespaced: true,
|
|
state,
|
|
getters,
|
|
actions,
|
|
mutations,
|
|
};
|