-
Notifications
You must be signed in to change notification settings - Fork 3k
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
Improve cancelling money in a different currency offline #13329
Merged
Merged
Changes from all commits
Commits
Show all changes
37 commits
Select commit
Hold shift + click to select a range
157184d
Display a pending message offline when currency request or cancelled …
youssef-lr 099cca6
Improve cancel money request flow when dealing with different currencies
youssef-lr ed02180
Cleanup
youssef-lr cf638e5
Set iouReport total to 0 if all requests have been cancelled
youssef-lr 7038110
Fix logic in isIOUReportPendingCurrencyConversion
youssef-lr d531cec
Cleanup
youssef-lr b7c2ac9
Improve comments
youssef-lr c4492d2
Fix comments, propTypes, and JSDocs
youssef-lr 37e457c
Apply suggestions from code review
youssef-lr b1442f8
use CONST for report action type
youssef-lr 18ec67e
Add more context to function comment
youssef-lr ce98c1e
Simplify non trivial logic for deciding if an IOU is pending currency…
youssef-lr 2ade174
Fix logic and clarify comments
youssef-lr 865c684
Revert back to using previous logic as it's reliable
youssef-lr 85f17d3
Remove bug fix to be fixed in a separate issue
youssef-lr 966e018
Simply logic once more
youssef-lr 5f030c6
Fix CSS styling and add converting message
youssef-lr fb6a160
Small code improvement
youssef-lr db6a7bd
Add initial tests
youssef-lr 9e94108
Simplify logic
youssef-lr c069432
Small improvement
youssef-lr 3602b13
Cleanup
youssef-lr b20b524
Remove 'converting' message
youssef-lr 5f9eb76
Cleanup
youssef-lr 4279965
Improvement
youssef-lr 503ffc4
Change param name
youssef-lr 88531ae
Merge branch 'main' into youssef_iou_pending_currency_conversion
youssef-lr 7cab87d
Add some more tests and comments
youssef-lr b792cb6
Merge branch 'main' into youssef_iou_pending_currency_conversion
youssef-lr 40b79f9
Add another test
youssef-lr 7661434
Merge branch 'main' into youssef_iou_pending_currency_conversion
youssef-lr f8c0f43
Merge branch 'main' into youssef_iou_pending_currency_conversion
youssef-lr a4672eb
Fix indentation
youssef-lr 16e3940
Remove fix to be included in a separate issue
youssef-lr fb811c5
Add translation
youssef-lr 8a683dd
Apply suggestions from code review
youssef-lr ba2ab0b
Apply suggestions from code review
youssef-lr File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,134 @@ | ||
import * as IOUUtils from '../../src/libs/IOUUtils'; | ||
import * as ReportUtils from '../../src/libs/ReportUtils'; | ||
|
||
let iouReport; | ||
let reportActions; | ||
const ownerEmail = 'owner@iou.com'; | ||
const managerEmail = 'manager@iou.com'; | ||
|
||
function createIOUReportAction(type, amount, currency, {IOUTransactionID, isOnline} = {}) { | ||
const moneyRequestAction = ReportUtils.buildOptimisticIOUReportAction( | ||
1, | ||
type, | ||
amount, | ||
currency, | ||
'Test comment', | ||
[managerEmail], | ||
'', | ||
IOUTransactionID, | ||
iouReport.reportID, | ||
); | ||
|
||
// Default is to create requests offline, if this is specified then we need to remove the pendingAction | ||
moneyRequestAction.pendingAction = isOnline ? null : 'add'; | ||
|
||
reportActions.push(moneyRequestAction); | ||
return moneyRequestAction; | ||
} | ||
|
||
function cancelMoneyRequest(moneyRequestAction, {isOnline} = {}) { | ||
createIOUReportAction( | ||
'cancel', | ||
moneyRequestAction.originalMessage.amount, | ||
moneyRequestAction.originalMessage.currency, | ||
{ | ||
IOUTransactionID: moneyRequestAction.originalMessage.IOUTransactionID, | ||
isOnline, | ||
}, | ||
); | ||
} | ||
|
||
beforeEach(() => { | ||
reportActions = []; | ||
const chatReportID = ReportUtils.generateReportID(); | ||
const amount = 1000; | ||
const currency = 'USD'; | ||
|
||
iouReport = ReportUtils.buildOptimisticIOUReport( | ||
ownerEmail, | ||
managerEmail, | ||
amount, | ||
chatReportID, | ||
currency, | ||
'en', | ||
); | ||
|
||
// The starting point of all tests is the IOUReport containing a single non-pending transaction in USD | ||
// All requests in the tests are assumed to be offline, unless isOnline is specified | ||
createIOUReportAction('create', amount, currency, {IOUTransactionID: '', isOnline: true}); | ||
}); | ||
|
||
describe('isIOUReportPendingCurrencyConversion', () => { | ||
test('Requesting money offline in a different currency will show the pending conversion message', () => { | ||
// Request money offline in AED | ||
createIOUReportAction('create', 100, 'AED'); | ||
|
||
// We requested money offline in a different currency, we don't know the total of the iouReport until we're back online | ||
expect(IOUUtils.isIOUReportPendingCurrencyConversion(reportActions, iouReport)).toBe(true); | ||
}); | ||
|
||
test('IOUReport is not pending conversion when all requests made offline have been cancelled', () => { | ||
// Create two requests offline | ||
const moneyRequestA = createIOUReportAction('create', 1000, 'AED'); | ||
const moneyRequestB = createIOUReportAction('create', 1000, 'AED'); | ||
|
||
// Cancel both requests | ||
cancelMoneyRequest(moneyRequestA); | ||
cancelMoneyRequest(moneyRequestB); | ||
|
||
// Both requests made offline have been cancelled, total won't update so no need to show a pending conversion message | ||
expect(IOUUtils.isIOUReportPendingCurrencyConversion(reportActions, iouReport)).toBe(false); | ||
}); | ||
|
||
test('Cancelling a request made online shows the preview', () => { | ||
// Request money online in AED | ||
const moneyRequest = createIOUReportAction('create', 1000, 'AED', {isOnline: true}); | ||
|
||
// Cancel it offline | ||
cancelMoneyRequest(moneyRequest); | ||
|
||
// We don't know what the total is because we need to subtract the converted amount of the offline request from the total | ||
expect(IOUUtils.isIOUReportPendingCurrencyConversion(reportActions, iouReport)).toBe(true); | ||
}); | ||
|
||
test('Cancelling a request made offline while there\'s a previous one made online will not show the pending conversion message', () => { | ||
// Request money online in AED | ||
createIOUReportAction('create', 1000, 'AED', {isOnline: true}); | ||
|
||
// Another request offline | ||
const moneyRequestOffline = createIOUReportAction('create', 1000, 'AED'); | ||
|
||
// Cancel the request made offline | ||
cancelMoneyRequest(moneyRequestOffline); | ||
|
||
expect(IOUUtils.isIOUReportPendingCurrencyConversion(reportActions, iouReport)).toBe(false); | ||
}); | ||
|
||
test('Cancelling a request made online while we have one made offline will show the pending conversion message', () => { | ||
// Request money online in AED | ||
const moneyRequestOnline = createIOUReportAction('create', 1000, 'AED', {isOnline: true}); | ||
|
||
// Requet money again but offline | ||
createIOUReportAction('create', 1000, 'AED'); | ||
|
||
// Cancel the request made online | ||
cancelMoneyRequest(moneyRequestOnline); | ||
|
||
// We don't know what the total is because we need to subtract the converted amount of the offline request from the total | ||
expect(IOUUtils.isIOUReportPendingCurrencyConversion(reportActions, iouReport)).toBe(true); | ||
}); | ||
|
||
test('Cancelling a request offline in the report\'s currency when we have requests in a different currency does not show the pending conversion message', () => { | ||
// Request money in the report's curreny (USD) | ||
const onlineMoneyRequestInUSD = createIOUReportAction('create', 1000, 'USD', {isOnline: true}); | ||
|
||
// Request money online in a different currency | ||
createIOUReportAction('create', 2000, 'AED', {isOnline: true}); | ||
|
||
// Cancel the USD request offline | ||
cancelMoneyRequest(onlineMoneyRequestInUSD); | ||
|
||
expect(IOUUtils.isIOUReportPendingCurrencyConversion(reportActions, iouReport)).toBe(false); | ||
}); | ||
}); | ||
|
||
techievivek marked this conversation as resolved.
Show resolved
Hide resolved
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can't we just do
props.network.isOffline && IOUUtils.isIOUReportPendingCurrencyConversion(props.reportActions, props.iouReport)
here?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We also need to check if we are in the IOUPreview action so that we avoid having to run this code unnecessarily in other actions.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I am thinking if we ever show a pending message then it has to be for the IOUPreview, right? So can't we just do
at line no 94.
Because only when IOUPreview will be rendered this expression will get evaluated and I can also see in the
isIOUReportPendingCurrencyConversion
we are already checking for pending actions to theif
block seems unnecessary to me. I am pretty sure we will need to test it out to be sure but this is my guess by looking at the code.What do you think about it?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not sure. This will make
isIOUReportPendingCurrencyConversion
even when we're online, and even when we're showing an IOUPreview of an action of typepay
(the one with a messageYou paid user@email.com
. I added that check so that we only that function if it's the last IOUPreview of a chat that has an outstanding IOU and we're offline. Let me know what you think.