[FIX] mail: set default values for suggested_recipients

Steps to reproduce:

  - Install `CRM` module (for test purposes)
  - Create a lead and set the name, email, phone, company name and
    contact name
  - Save the lead
  - Click on `Send message` button in the chatter
  - Uncheck/Check the recipient checkbox
  - Save the partner
  - Discard the changes on the lead and open the partner created

Issue:

  - In the wizard, the `phone` value is not set with default value and
    the name is set with the company name instead of the contact name
  - On the partner created, the company and phone are not set

Cause:

  The default values (used in the partner creation wizard) retrieved
  from "/mail/thread/data" route are not set on the recipient object.

opw-3512045

closes odoo/odoo#148213

X-original-commit: 372fe2fe61e3121b27bf1305dfac8fbf0ff8e9b5
Signed-off-by: Nasreddin Boulif (bon) <bon@odoo.com>
This commit is contained in:
Nasreddin Boulif (bon)
2024-01-10 19:39:24 +00:00
parent db858c7a13
commit bccaced50d
3 changed files with 59 additions and 4 deletions
@@ -39,6 +39,12 @@ export class SuggestedRecipient extends Component {
default_email: this.props.recipient.email,
default_name: this.props.recipient.name,
default_lang: this.props.recipient.lang,
...Object.fromEntries(
Object.entries(this.props.recipient.defaultCreateValues).map(([k, v]) => [
"default_" + k,
v,
])
),
force_email: true,
ref: "compound_context",
},
@@ -128,10 +128,10 @@ patch(ThreadService.prototype, {
async insertSuggestedRecipients(thread, dataList) {
const recipients = [];
for (const data of dataList) {
const [partner_id, emailInfo, lang, reason, customerInfo] = data;
const [partner_id, emailInfo, lang, reason, defaultCreateValues] = data;
let [name, email] = emailInfo ? parseEmail(emailInfo) : [];
if ((!name || name === email) && customerInfo?.name) {
name = customerInfo.name;
if ((!name || name === email) && defaultCreateValues?.name) {
name = defaultCreateValues.name;
}
recipients.push({
id: nextId++,
@@ -141,6 +141,7 @@ patch(ThreadService.prototype, {
reason,
persona: partner_id ? { type: "partner", id: partner_id } : false,
checked: true,
defaultCreateValues,
});
}
thread.suggestedRecipients = recipients;
@@ -4,7 +4,7 @@ import { startServer } from "@bus/../tests/helpers/mock_python_environment";
import { start } from "@mail/../tests/helpers/test_utils";
import { makeDeferred, patchWithCleanup } from "@web/../tests/helpers/utils";
import { makeDeferred, nextTick, patchWithCleanup } from "@web/../tests/helpers/utils";
import { click, contains, insertText } from "@web/../tests/utils";
const views = {
@@ -16,6 +16,14 @@ const views = {
<field name="message_follower_ids"/>
</div>
</form>`,
"res.partner,false,form": `
<form string="Partner">
<sheet>
<field name="name"/>
<field name="email"/>
<field name="phone"/>
</sheet>
</form>`,
};
QUnit.module("suggested_recipients");
@@ -290,3 +298,43 @@ QUnit.test("suggested recipients should be added as follower when posting a mess
await contains(".o-mail-Message");
await contains(".o-mail-Followers-counter", { text: "1" });
});
QUnit.test(
"suggested partner unchecked/checked -> partner creation in wizard with defaults",
async (assert) => {
const pyEnv = await startServer();
const fakeId = pyEnv["res.fake"].create({
email_cc: "john@test.be",
});
let partner = pyEnv["res.partner"].search([["email", "=", "john@test.be"]]);
assert.strictEqual(partner.length, 0);
const { openFormView } = await start({
serverData: { views },
async mockRPC(route, args, performRPC) {
// Override mockRPC response to simulate retrieving default values
// for the suggested recipient through `_get_customer_information`
if (route === "/mail/thread/data") {
const res = await performRPC(route, args);
assert.strictEqual(res["suggestedRecipients"].length, 1);
assert.deepEqual(res["suggestedRecipients"][0][1], "john@test.be");
res["suggestedRecipients"][0].push({
company_name: "Test Company",
});
return res;
}
},
});
openFormView("res.fake", fakeId);
await click("button", { text: "Send message" });
await click(".o-mail-SuggestedRecipient input");
await click(".o-mail-SuggestedRecipient input");
await nextTick();
await click(".o_dialog .o_form_button_save");
await nextTick();
partner = pyEnv["res.partner"].search([
["email", "=", "john@test.be"],
["company_name", "=", "Test Company"],
]);
assert.strictEqual(partner.length, 1);
}
);