Skip to content

Commit

Permalink
refactor: get_last_email
Browse files Browse the repository at this point in the history
  • Loading branch information
barredterra committed May 3, 2023
1 parent b246267 commit fc520a0
Showing 1 changed file with 19 additions and 24 deletions.
43 changes: 19 additions & 24 deletions frappe/public/js/frappe/form/footer/form_timeline.js
Original file line number Diff line number Diff line change
Expand Up @@ -607,31 +607,26 @@ class FormTimeline extends BaseTimeline {
});
}

/**
* Return the latest email communication.
*
* @param {boolean} from_recipient If true, only considers emails where the recipient is in the list of senders.
* @returns {object|null} The latest email communication, or null if no communication is found.
*/
get_last_email(from_recipient) {
let last_email = null;
let communications = this.frm.get_docinfo().communications || [];
let email = this.get_recipient();
// REDESIGN TODO: What is this? Check again
communications
.sort((a, b) => (a.creation > b.creation ? -1 : 1))
.every((c) => {
if (
c.communication_type === "Communication" &&
c.communication_medium === "Email"
) {
if (from_recipient) {
if (c.sender.indexOf(email) !== -1) {
last_email = c;
return false;
}
} else {
last_email = c;
return false;
}
}
});

return last_email;
const communications = this.frm.get_docinfo().communications || [];
const email = this.get_recipient();

const filteredRecords = communications
.filter(
(record) =>
record.communication_type === "Communication" &&
record.communication_medium === "Email" &&
(!from_recipient || record.sender.indexOf(email) !== -1)
)
.sort((a, b) => b.creation - a.creation);

return filteredRecords.length > 0 ? filteredRecords[0] : null;
}

delete_comment(comment_name) {
Expand Down

0 comments on commit fc520a0

Please sign in to comment.