Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: get_last_email #20711

Merged
merged 6 commits into from
May 4, 2023
Merged

fix: get_last_email #20711

merged 6 commits into from
May 4, 2023

Conversation

uepselon
Copy link
Contributor

@uepselon uepselon commented Apr 14, 2023

Reopens #20692

Description:
The function should return the last email which is part of the timeline according to the creation date of the email. Therefore, it is ordering the communication items in descending order. Since the forEach loop will not break on a return false, it will continue to loop through all items and will return whatever is the last item in the array. I assume this is not as intended. Instead we can use the .every method which breaks on a return false.

Steps to reproduce:

Add multiple email items to a timeline
Call the get_last_email function in form_timeline.js
-> It will return the oldest element of the timeline instead of the newest.

@uepselon uepselon requested review from a team and phot0n and removed request for a team April 14, 2023 13:53
@phot0n phot0n self-assigned this Apr 14, 2023
@stale
Copy link

stale bot commented Apr 21, 2023

This pull request has been automatically marked as stale because it has not had recent activity. It will be closed within 3 days if no further activity occurs, but it only takes a comment to keep a contribution alive :) Also, even if it is closed, you can always reopen the PR when you're ready. Thank you for contributing.

@stale stale bot added the inactive label Apr 21, 2023
@uepselon
Copy link
Contributor Author

Unstale

@stale stale bot removed the inactive label Apr 22, 2023
barredterra

This comment was marked as outdated.

@sagarvora
Copy link
Collaborator

sagarvora commented Apr 28, 2023

This won't work:

image

returning nothing (undefined) means that we are returning a falsy value, which will cause the loop to end early:
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/every#description

Array.prototype.every is generally used to ensure that every element in your array matches the test you specify.

Just use for..of IMO (you can use the break keyword to end loop):
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/for...of

I am converting this to Draft, please fix and mark ready for review.


Edit: original author may have expected Array.prototype.forEach to work like jQuery.each which does allow return false:
https://api.jquery.com/jquery.each/

@sagarvora sagarvora marked this pull request as draft April 28, 2023 17:29
@barredterra barredterra marked this pull request as ready for review May 3, 2023 10:44
@barredterra barredterra requested a review from sagarvora May 3, 2023 10:44
@barredterra barredterra changed the title fix .every vs. .forEach fix get_last_email May 3, 2023
@barredterra barredterra changed the title fix get_last_email fix: get_last_email May 3, 2023
@barredterra
Copy link
Collaborator

@sagarvora when testing, it worked just fine. Looks like my test cases were not exhaustive, I see your point. Refactored the function to address your concerns and solve the original bug.

@codecov
Copy link

codecov bot commented May 3, 2023

Codecov Report

Merging #20711 (fc520a0) into develop (7042dca) will increase coverage by 0.05%.
The diff coverage is 13.33%.

❗ Current head fc520a0 differs from pull request most recent head c20ac26. Consider uploading reports for the commit c20ac26 to get more accurate results

Additional details and impacted files
@@             Coverage Diff             @@
##           develop   #20711      +/-   ##
===========================================
+ Coverage    63.87%   63.93%   +0.05%     
===========================================
  Files          761      761              
  Lines        68844    68828      -16     
  Branches      6224     6222       -2     
===========================================
+ Hits         43977    44006      +29     
+ Misses       21320    21267      -53     
- Partials      3547     3555       +8     
Flag Coverage Δ
server-ui 32.16% <22.22%> (-0.06%) ⬇️
ui-tests 51.74% <0.00%> (+0.15%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

@phot0n
Copy link
Contributor

phot0n commented May 3, 2023

What happens in the scenario where there are multiple senders? Does the received communication's "from" gets picked up no matter how many sent communications (from different senders) are in between there?

@barredterra
Copy link
Collaborator

What happens in the scenario where there are multiple senders?

I think an email always has exactly one sender.

@phot0n
Copy link
Contributor

phot0n commented May 3, 2023

I think an email always has exactly one sender.

Ah I'm not talking about multiple senders of an email/communication (they cannot be multiple).

Since communications have the status of sent or received, I'm talking about multiple people sending multiple communications to recipients in the timeline.

@barredterra
Copy link
Collaborator

barredterra commented May 3, 2023

I don't get your question, sorry.

What this function does is to return the most recent email, if any. When the parameter from_recipient is true, it returns the most recent email from the email address contained in the email_field of the current doc, if any.

Co-authored-by: Raffael Meyer <14891507+barredterra@users.noreply.github.com>
sagarvora
sagarvora previously approved these changes May 4, 2023
const communications = this.frm.get_docinfo().communications || [];
const email = this.get_recipient();

const filteredRecords = communications
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Frappe's style of naming should be preferred. I will fix it.

Suggested change
const filteredRecords = communications
const filtered_records = communications

@sagarvora sagarvora merged commit 4b7c735 into frappe:develop May 4, 2023
9 of 14 checks passed
mergify bot pushed a commit that referenced this pull request May 4, 2023
Co-authored-by: Raffael Meyer <14891507+barredterra@users.noreply.github.com>
Co-authored-by: Sagar Vora <sagar@resilient.tech>
(cherry picked from commit 4b7c735)

# Conflicts:
#	frappe/public/js/frappe/form/footer/form_timeline.js
mergify bot pushed a commit that referenced this pull request May 4, 2023
Co-authored-by: Raffael Meyer <14891507+barredterra@users.noreply.github.com>
Co-authored-by: Sagar Vora <sagar@resilient.tech>
(cherry picked from commit 4b7c735)
sagarvora added a commit that referenced this pull request May 4, 2023
…port #20711)

Co-authored-by: Raffael Meyer <14891507+barredterra@users.noreply.github.com>
Co-authored-by: Sagar Vora <sagar@resilient.tech>
Co-authored-by: Leonard Goertz <49870752+uepselon@users.noreply.github.com>
sagarvora added a commit that referenced this pull request May 4, 2023
…-20711

fix: ensure that `get_last_email` returns the most recent email (backport #20711)
frappe-pr-bot pushed a commit that referenced this pull request May 9, 2023
## [13.56.2](v13.56.1...v13.56.2) (2023-05-09)

### Bug Fixes

* ensure that `get_last_email` returns the most recent email ([#20711](#20711)) ([d416159](d416159))
* make operator in link filters translatable (backport [#20884](#20884)) ([#20910](#20910)) ([688a2b4](688a2b4))
* merge conflict ([64d5c8f](64d5c8f))
* translate lowercase operators to german ([#20912](#20912)) ([#20915](#20915)) ([598bdaf](598bdaf))
* use smaller font only if the report doesnt have a standard print format (backport [#20878](#20878)) ([#20946](#20946)) ([8b0c1f8](8b0c1f8))
frappe-pr-bot pushed a commit that referenced this pull request May 9, 2023
# [14.36.0](v14.35.0...v14.36.0) (2023-05-09)

### Bug Fixes

* ensure that `get_last_email` returns the most recent email (backport [#20711](#20711)) ([624f96b](624f96b))
* escape html from listview row title ([56bec1d](56bec1d))
* escape html from workspace title ([e68fc43](e68fc43))
* ignore virtual doctypes during data export ([#20891](#20891)) ([#20899](#20899)) ([d6bfaae](d6bfaae))
* make operator in link filters translatable (backport [#20884](#20884)) ([#20911](#20911)) ([1ec3bad](1ec3bad))
* message.py executing script ([#20887](#20887)) ([#20897](#20897)) ([1bcf5d4](1bcf5d4))
* **multi-pdf:** change response type to pdf ([997559c](997559c))
* **oauth:** add exp to idToken ([#20694](#20694)) ([#20903](#20903)) ([1a8e671](1a8e671))
* reload communication before re-save ([#20914](#20914)) ([#20921](#20921)) ([37a8ec0](37a8ec0))
* set default letterhead and print format ([a5a6965](a5a6965))
* strip comma, space from recipients before sending email for auto repeat ([#20940](#20940)) ([#20945](#20945)) ([042a1d2](042a1d2))
* translate lowercase operators to german ([#20912](#20912)) ([#20916](#20916)) ([c47b146](c47b146))
* type hints for get_address_display ([#20923](#20923)) ([#20924](#20924)) ([15df963](15df963))
* use smaller font only if the report doesnt have a standard print format ([#20878](#20878)) ([#20947](#20947)) ([35165d0](35165d0))

### Features

* helper method for address display ([#20900](#20900)) ([#20901](#20901)) ([f914770](f914770))
* telemetry using posthog (backport [#20825](#20825)) ([#20934](#20934)) ([bbe29ee](bbe29ee))

### Performance Improvements

* get all file data at once when downloading private file ([#20902](#20902)) ([e106594](e106594))
@github-actions github-actions bot locked as resolved and limited conversation to collaborators May 19, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

5 participants