[FIX] mail: TypeError in cleanTerm function when email is falsy

The `cleanTerm` function was throwing a TypeError ("term.toLowerCase is
not a function") when the email value of a contact was falsy (like
`false`, `null`, or `undefined`), as it was attempting to call
`toLowerCase` on a non-string value.

This was particularly problematic when two contacts had the same name
and at least one of them didn't have an email.

The solution was to make `cleanTerm` consider non-sring values as
empty string, preventing the TypeError when value is `false` from
partner having no email.

opw-3439376

closes odoo/odoo#130233

X-original-commit: 2abdf7a8635ed90f0ad26c6407b21cf3c8c60fcd
Signed-off-by: Alexandre Kühn (aku) <aku@odoo.com>
Signed-off-by: Pedram Bi Ria (pebr) <pebr@odoo.com>
This commit is contained in:
Pedram (PEBR)
2023-08-01 23:52:26 +02:00
parent c2c35f32cc
commit a18b19284f
5 changed files with 11 additions and 11 deletions
@@ -43,8 +43,8 @@ patch(SuggestionService.prototype, "im_livechat", {
};
});
const sortFunc = (c1, c2) => {
const cleanedName1 = cleanTerm(c1.name ?? "");
const cleanedName2 = cleanTerm(c2.name ?? "");
const cleanedName1 = cleanTerm(c1.name);
const cleanedName2 = cleanTerm(c2.name);
if (
cleanedName1.startsWith(cleanedSearchTerm) &&
!cleanedName2.startsWith(cleanedSearchTerm)
@@ -47,8 +47,8 @@ partnerCompareRegistry.add(
partnerCompareRegistry.add(
"mail.name",
(p1, p2, { searchTerm }) => {
const cleanedName1 = cleanTerm(p1.name ?? "");
const cleanedName2 = cleanTerm(p2.name ?? "");
const cleanedName1 = cleanTerm(p1.name);
const cleanedName2 = cleanTerm(p2.name);
if (cleanedName1.startsWith(searchTerm) && !cleanedName2.startsWith(searchTerm)) {
return -1;
}
@@ -68,8 +68,8 @@ partnerCompareRegistry.add(
partnerCompareRegistry.add(
"mail.email",
(p1, p2, { searchTerm }) => {
const cleanedEmail1 = cleanTerm(p1.email ?? "");
const cleanedEmail2 = cleanTerm(p2.email ?? "");
const cleanedEmail1 = cleanTerm(p1.email);
const cleanedEmail2 = cleanTerm(p2.email);
if (cleanedEmail1.startsWith(searchTerm) && !cleanedEmail1.startsWith(searchTerm)) {
return -1;
}
@@ -218,8 +218,8 @@ export class SuggestionService {
if (!c1.hasSelfAsMember && c2.hasSelfAsMember) {
return 1;
}
const cleanedDisplayName1 = cleanTerm(c1.displayName ?? "");
const cleanedDisplayName2 = cleanTerm(c2.displayName ?? "");
const cleanedDisplayName1 = cleanTerm(c1.displayName);
const cleanedDisplayName2 = cleanTerm(c2.displayName);
if (
cleanedDisplayName1.startsWith(cleanedSearchTerm) &&
!cleanedDisplayName2.startsWith(cleanedSearchTerm)
@@ -53,8 +53,8 @@ patch(SuggestionService.prototype, "discuss/core/common", {
if (!c1.channel_types && c2.channel_types) {
return 1;
}
const cleanedName1 = cleanTerm(c1.name || "");
const cleanedName2 = cleanTerm(c2.name || "");
const cleanedName1 = cleanTerm(c1.name);
const cleanedName2 = cleanTerm(c2.name);
if (
cleanedName1.startsWith(cleanedSearchTerm) &&
!cleanedName2.startsWith(cleanedSearchTerm)
@@ -242,5 +242,5 @@ export function convertBrToLineBreak(str) {
}
export function cleanTerm(term) {
return unaccent(term.toLowerCase());
return unaccent((typeof term === "string" ? term : "").toLowerCase());
}