Skip to content

Commit

Permalink
Merge pull request #20889 from frappe/mergify/bp/version-13-hotfix/pr…
Browse files Browse the repository at this point in the history
…-20711

fix: ensure that `get_last_email` returns the most recent email (backport #20711)
  • Loading branch information
sagarvora committed May 4, 2023
2 parents 9c52be4 + 64d5c8f commit 329972e
Showing 1 changed file with 20 additions and 20 deletions.
40 changes: 20 additions & 20 deletions frappe/public/js/frappe/form/footer/form_timeline.js
Original file line number Diff line number Diff line change
Expand Up @@ -550,26 +550,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 329972e

Please sign in to comment.