Skip to content

Commit

Permalink
fix: ensure that get_last_email returns the most recent email (#20711)
Browse files Browse the repository at this point in the history
Co-authored-by: Raffael Meyer <14891507+barredterra@users.noreply.github.com>
Co-authored-by: Sagar Vora <sagar@resilient.tech>
(cherry picked from commit 4b7c735)
  • Loading branch information
uepselon authored and mergify[bot] committed May 4, 2023
1 parent bf99a02 commit 7b5f0ba
Showing 1 changed file with 20 additions and 24 deletions.
44 changes: 20 additions & 24 deletions frappe/public/js/frappe/form/footer/form_timeline.js
Original file line number Diff line number Diff line change
Expand Up @@ -582,30 +582,26 @@ class FormTimeline extends BaseTimeline {
}

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))
.forEach((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;
/**
* Return the latest email communication.
*
* @param {boolean} from_recipient If true, only considers emails where current form's recipient is the sender.
* @returns {object|null} The latest email communication, or null if no communication is found.
*/

const communications = this.frm.get_docinfo().communications || [];
const recipient = this.get_recipient();

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

return filtered_records[0] || null;
}

delete_comment(comment_name) {
Expand Down

0 comments on commit 7b5f0ba

Please sign in to comment.