From 72dee1d56db92fa67da0fabd852453fc645687a3 Mon Sep 17 00:00:00 2001 From: dukenv0307 Date: Mon, 5 Feb 2024 11:07:15 +0700 Subject: [PATCH 01/14] Add policy avatar --- src/libs/ReportUtils.ts | 9 ++++++--- src/pages/home/ReportScreen.js | 2 ++ src/types/onyx/Report.ts | 3 +++ 3 files changed, 11 insertions(+), 3 deletions(-) diff --git a/src/libs/ReportUtils.ts b/src/libs/ReportUtils.ts index 568ce49ff961..ad9fd58ae5d1 100644 --- a/src/libs/ReportUtils.ts +++ b/src/libs/ReportUtils.ts @@ -1458,9 +1458,11 @@ function getIconsForParticipants(participants: number[], personalDetails: OnyxCo */ function getWorkspaceIcon(report: OnyxEntry, policy: OnyxEntry = null): Icon { const workspaceName = getPolicyName(report, false, policy); - const policyExpenseChatAvatarSource = allPolicies?.[`${ONYXKEYS.COLLECTION.POLICY}${report?.policyID}`]?.avatar - ? allPolicies?.[`${ONYXKEYS.COLLECTION.POLICY}${report?.policyID}`]?.avatar - : getDefaultWorkspaceAvatar(workspaceName); + // disabling to protect against empty strings + // eslint-disable-next-line @typescript-eslint/prefer-nullish-coalescing + const policyAvatarURL = report?.policyAvatar || allPolicies?.[`${ONYXKEYS.COLLECTION.POLICY}${report?.policyID}`]?.avatar; + // eslint-disable-next-line @typescript-eslint/prefer-nullish-coalescing + const policyExpenseChatAvatarSource = policyAvatarURL || getDefaultWorkspaceAvatar(workspaceName); const workspaceIcon: Icon = { source: policyExpenseChatAvatarSource ?? '', @@ -1553,6 +1555,7 @@ function getIcons( return [domainIcon]; } if (isAdminRoom(report) || isAnnounceRoom(report) || isChatRoom(report) || isArchivedRoom(report)) { + console.log('run in here'); const workspaceIcon = getWorkspaceIcon(report, policy); return [workspaceIcon]; } diff --git a/src/pages/home/ReportScreen.js b/src/pages/home/ReportScreen.js index bfe27910c943..d903bc10bbf7 100644 --- a/src/pages/home/ReportScreen.js +++ b/src/pages/home/ReportScreen.js @@ -205,6 +205,7 @@ function ReportScreen({ visibility: reportProp.visibility, oldPolicyName: reportProp.oldPolicyName, policyName: reportProp.policyName, + policyAvatar: reportProp.policyAvatar, isOptimisticReport: reportProp.isOptimisticReport, }), [ @@ -241,6 +242,7 @@ function ReportScreen({ reportProp.visibility, reportProp.oldPolicyName, reportProp.policyName, + reportProp.policyAvatar, reportProp.isOptimisticReport, ], ); diff --git a/src/types/onyx/Report.ts b/src/types/onyx/Report.ts index d26bdd4f282e..f7493480273a 100644 --- a/src/types/onyx/Report.ts +++ b/src/types/onyx/Report.ts @@ -64,6 +64,9 @@ type Report = { /** The current user's notification preference for this report */ notificationPreference?: NotificationPreference; + /** The policy avatar to use, if any */ + policyAvatar?: string | null; + /** The policy name to use */ policyName?: string | null; From 1e3e91e0c2bd882ceb5d605ce5743cd0c6a7c212 Mon Sep 17 00:00:00 2001 From: dukenv0307 Date: Mon, 5 Feb 2024 11:09:12 +0700 Subject: [PATCH 02/14] fix lint --- src/libs/ReportUtils.ts | 1 - 1 file changed, 1 deletion(-) diff --git a/src/libs/ReportUtils.ts b/src/libs/ReportUtils.ts index ad9fd58ae5d1..cb806fa43620 100644 --- a/src/libs/ReportUtils.ts +++ b/src/libs/ReportUtils.ts @@ -1555,7 +1555,6 @@ function getIcons( return [domainIcon]; } if (isAdminRoom(report) || isAnnounceRoom(report) || isChatRoom(report) || isArchivedRoom(report)) { - console.log('run in here'); const workspaceIcon = getWorkspaceIcon(report, policy); return [workspaceIcon]; } From a6ffc90c3eeb0a52be530572ab268427b84d2afe Mon Sep 17 00:00:00 2001 From: dukenv0307 Date: Tue, 6 Feb 2024 14:55:30 +0700 Subject: [PATCH 03/14] update total amount when offline and change currency --- .../ReportActionItem/MoneyRequestAction.tsx | 2 +- src/libs/IOUUtils.ts | 4 +- src/libs/actions/IOU.ts | 49 +++++++++++-------- 3 files changed, 31 insertions(+), 24 deletions(-) diff --git a/src/components/ReportActionItem/MoneyRequestAction.tsx b/src/components/ReportActionItem/MoneyRequestAction.tsx index ff29bf5b0ee8..35314a4dad11 100644 --- a/src/components/ReportActionItem/MoneyRequestAction.tsx +++ b/src/components/ReportActionItem/MoneyRequestAction.tsx @@ -93,7 +93,7 @@ function MoneyRequestAction({ // If the childReportID is not present, we need to create a new thread const childReportID = action?.childReportID ?? '0'; - if (!childReportID) { + if (!Number(childReportID)) { const thread = ReportUtils.buildTransactionThread(action, requestReportID); const userLogins = PersonalDetailsUtils.getLoginsByAccountIDs(thread.participantAccountIDs ?? []); Report.openReport(thread.reportID, userLogins, thread, action.reportActionID); diff --git a/src/libs/IOUUtils.ts b/src/libs/IOUUtils.ts index d70f4fc08102..eaca60414b3a 100644 --- a/src/libs/IOUUtils.ts +++ b/src/libs/IOUUtils.ts @@ -55,10 +55,10 @@ function calculateAmount(numberOfParticipants: number, total: number, currency: * @param isDeleting - whether the user is deleting the request */ function updateIOUOwnerAndTotal>(iouReport: TReport, actorAccountID: number, amount: number, currency: string, isDeleting = false): TReport { - if (currency !== iouReport?.currency) { + if(!iouReport?.currency){ return iouReport; } - + // Make a copy so we don't mutate the original object const iouReportUpdate = {...iouReport}; diff --git a/src/libs/actions/IOU.ts b/src/libs/actions/IOU.ts index 569b6b26b728..297f1096b0ca 100644 --- a/src/libs/actions/IOU.ts +++ b/src/libs/actions/IOU.ts @@ -1053,28 +1053,35 @@ function getUpdateMoneyRequestParams( // Should only update if the transaction matches the currency of the report, else we wait for the update // from the server with the currency conversion let updatedMoneyRequestReport = {...iouReport}; - if (updatedTransaction?.currency === iouReport?.currency && updatedTransaction?.modifiedAmount) { - const diff = TransactionUtils.getAmount(transaction, true) - TransactionUtils.getAmount(updatedTransaction, true); - if (ReportUtils.isExpenseReport(iouReport) && typeof updatedMoneyRequestReport.total === 'number') { - updatedMoneyRequestReport.total += diff; - } else { - updatedMoneyRequestReport = iouReport - ? IOUUtils.updateIOUOwnerAndTotal(iouReport, updatedReportAction.actorAccountID ?? -1, diff, TransactionUtils.getCurrency(transaction), false) - : {}; - } - - updatedMoneyRequestReport.cachedTotal = CurrencyUtils.convertToDisplayString(updatedMoneyRequestReport.total, updatedTransaction.currency); - optimisticData.push({ - onyxMethod: Onyx.METHOD.MERGE, - key: `${ONYXKEYS.COLLECTION.REPORT}${iouReport?.reportID}`, - value: updatedMoneyRequestReport, - }); - successData.push({ - onyxMethod: Onyx.METHOD.MERGE, - key: `${ONYXKEYS.COLLECTION.REPORT}${iouReport?.reportID}`, - value: {pendingAction: null}, - }); + let diff = 0; + const isExpenseReport = ReportUtils.isExpenseReport(iouReport); + const updatedCurrency = TransactionUtils.getCurrency(updatedTransaction); + const currentCurrency = TransactionUtils.getCurrency(transaction); + if (updatedCurrency === iouReport?.currency && currentCurrency !== iouReport?.currency) { + diff = TransactionUtils.getAmount(updatedTransaction, isExpenseReport); + } else if (updatedCurrency !== iouReport?.currency && currentCurrency === iouReport?.currency) { + diff = -TransactionUtils.getAmount(updatedTransaction, isExpenseReport); + } else if (updatedCurrency === iouReport?.currency && updatedTransaction?.modifiedAmount) { + diff = TransactionUtils.getAmount(updatedTransaction, isExpenseReport) - TransactionUtils.getAmount(transaction, isExpenseReport); + } + if (isExpenseReport && typeof updatedMoneyRequestReport.total === 'number') { + updatedMoneyRequestReport.total += diff; + } else { + updatedMoneyRequestReport = iouReport + ? IOUUtils.updateIOUOwnerAndTotal(iouReport, updatedReportAction.actorAccountID ?? -1, diff, TransactionUtils.getCurrency(updatedTransaction), false) + : {}; } + updatedMoneyRequestReport.cachedTotal = CurrencyUtils.convertToDisplayString(updatedMoneyRequestReport.total, updatedTransaction?.modifiedCurrency); + optimisticData.push({ + onyxMethod: Onyx.METHOD.MERGE, + key: `${ONYXKEYS.COLLECTION.REPORT}${iouReport?.reportID}`, + value: updatedMoneyRequestReport, + }); + successData.push({ + onyxMethod: Onyx.METHOD.MERGE, + key: `${ONYXKEYS.COLLECTION.REPORT}${iouReport?.reportID}`, + value: {pendingAction: null}, + }); } // Optimistically modify the transaction From b2ce3f01984eb8b8247fb7e12fce6df0b14319c7 Mon Sep 17 00:00:00 2001 From: dukenv0307 Date: Tue, 6 Feb 2024 15:54:01 +0700 Subject: [PATCH 04/14] fix lint --- src/components/ReportActionItem/MoneyRequestAction.tsx | 2 +- src/libs/IOUUtils.ts | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/components/ReportActionItem/MoneyRequestAction.tsx b/src/components/ReportActionItem/MoneyRequestAction.tsx index 35314a4dad11..ff29bf5b0ee8 100644 --- a/src/components/ReportActionItem/MoneyRequestAction.tsx +++ b/src/components/ReportActionItem/MoneyRequestAction.tsx @@ -93,7 +93,7 @@ function MoneyRequestAction({ // If the childReportID is not present, we need to create a new thread const childReportID = action?.childReportID ?? '0'; - if (!Number(childReportID)) { + if (!childReportID) { const thread = ReportUtils.buildTransactionThread(action, requestReportID); const userLogins = PersonalDetailsUtils.getLoginsByAccountIDs(thread.participantAccountIDs ?? []); Report.openReport(thread.reportID, userLogins, thread, action.reportActionID); diff --git a/src/libs/IOUUtils.ts b/src/libs/IOUUtils.ts index eaca60414b3a..55317c9c050c 100644 --- a/src/libs/IOUUtils.ts +++ b/src/libs/IOUUtils.ts @@ -55,10 +55,10 @@ function calculateAmount(numberOfParticipants: number, total: number, currency: * @param isDeleting - whether the user is deleting the request */ function updateIOUOwnerAndTotal>(iouReport: TReport, actorAccountID: number, amount: number, currency: string, isDeleting = false): TReport { - if(!iouReport?.currency){ + if (!iouReport?.currency) { return iouReport; } - + // Make a copy so we don't mutate the original object const iouReportUpdate = {...iouReport}; From 89abd0225021556c32db86e552b5ddbb2d695ce5 Mon Sep 17 00:00:00 2001 From: dukenv0307 Date: Tue, 6 Feb 2024 16:04:18 +0700 Subject: [PATCH 05/14] Revert "Add policy avatar" This reverts commit 72dee1d56db92fa67da0fabd852453fc645687a3. --- src/libs/ReportUtils.ts | 8 +++----- src/pages/home/ReportScreen.js | 2 -- src/types/onyx/Report.ts | 3 --- 3 files changed, 3 insertions(+), 10 deletions(-) diff --git a/src/libs/ReportUtils.ts b/src/libs/ReportUtils.ts index cde5b8b1a5e2..965ee5d8433b 100644 --- a/src/libs/ReportUtils.ts +++ b/src/libs/ReportUtils.ts @@ -1446,11 +1446,9 @@ function getIconsForParticipants(participants: number[], personalDetails: OnyxCo */ function getWorkspaceIcon(report: OnyxEntry, policy: OnyxEntry = null): Icon { const workspaceName = getPolicyName(report, false, policy); - // disabling to protect against empty strings - // eslint-disable-next-line @typescript-eslint/prefer-nullish-coalescing - const policyAvatarURL = report?.policyAvatar || allPolicies?.[`${ONYXKEYS.COLLECTION.POLICY}${report?.policyID}`]?.avatar; - // eslint-disable-next-line @typescript-eslint/prefer-nullish-coalescing - const policyExpenseChatAvatarSource = policyAvatarURL || getDefaultWorkspaceAvatar(workspaceName); + const policyExpenseChatAvatarSource = allPolicies?.[`${ONYXKEYS.COLLECTION.POLICY}${report?.policyID}`]?.avatar + ? allPolicies?.[`${ONYXKEYS.COLLECTION.POLICY}${report?.policyID}`]?.avatar + : getDefaultWorkspaceAvatar(workspaceName); const workspaceIcon: Icon = { source: policyExpenseChatAvatarSource ?? '', diff --git a/src/pages/home/ReportScreen.js b/src/pages/home/ReportScreen.js index d903bc10bbf7..bfe27910c943 100644 --- a/src/pages/home/ReportScreen.js +++ b/src/pages/home/ReportScreen.js @@ -205,7 +205,6 @@ function ReportScreen({ visibility: reportProp.visibility, oldPolicyName: reportProp.oldPolicyName, policyName: reportProp.policyName, - policyAvatar: reportProp.policyAvatar, isOptimisticReport: reportProp.isOptimisticReport, }), [ @@ -242,7 +241,6 @@ function ReportScreen({ reportProp.visibility, reportProp.oldPolicyName, reportProp.policyName, - reportProp.policyAvatar, reportProp.isOptimisticReport, ], ); diff --git a/src/types/onyx/Report.ts b/src/types/onyx/Report.ts index 1b8d026898c0..fbd61a9c5365 100644 --- a/src/types/onyx/Report.ts +++ b/src/types/onyx/Report.ts @@ -64,9 +64,6 @@ type Report = { /** The current user's notification preference for this report */ notificationPreference?: NotificationPreference; - /** The policy avatar to use, if any */ - policyAvatar?: string | null; - /** The policy name to use */ policyName?: string | null; From 312ddbc75e9e274806d99e86eebfa09e4a6e0b53 Mon Sep 17 00:00:00 2001 From: dukenv0307 Date: Tue, 6 Feb 2024 16:04:33 +0700 Subject: [PATCH 06/14] Revert "fix lint" This reverts commit 1e3e91e0c2bd882ceb5d605ce5743cd0c6a7c212. --- src/libs/ReportUtils.ts | 1 + 1 file changed, 1 insertion(+) diff --git a/src/libs/ReportUtils.ts b/src/libs/ReportUtils.ts index 965ee5d8433b..bd0708878443 100644 --- a/src/libs/ReportUtils.ts +++ b/src/libs/ReportUtils.ts @@ -1541,6 +1541,7 @@ function getIcons( return [domainIcon]; } if (isAdminRoom(report) || isAnnounceRoom(report) || isChatRoom(report) || isArchivedRoom(report)) { + console.log('run in here'); const workspaceIcon = getWorkspaceIcon(report, policy); return [workspaceIcon]; } From c8a3c4c472d12e37d2471bdc506fb00107687158 Mon Sep 17 00:00:00 2001 From: dukenv0307 Date: Tue, 6 Feb 2024 16:06:35 +0700 Subject: [PATCH 07/14] remove useless code --- src/libs/ReportUtils.ts | 1 - 1 file changed, 1 deletion(-) diff --git a/src/libs/ReportUtils.ts b/src/libs/ReportUtils.ts index bd0708878443..965ee5d8433b 100644 --- a/src/libs/ReportUtils.ts +++ b/src/libs/ReportUtils.ts @@ -1541,7 +1541,6 @@ function getIcons( return [domainIcon]; } if (isAdminRoom(report) || isAnnounceRoom(report) || isChatRoom(report) || isArchivedRoom(report)) { - console.log('run in here'); const workspaceIcon = getWorkspaceIcon(report, policy); return [workspaceIcon]; } From 7c5889effa9707b60a0b8f9bc560fed5d90b027b Mon Sep 17 00:00:00 2001 From: dukenv0307 Date: Wed, 7 Feb 2024 16:18:28 +0700 Subject: [PATCH 08/14] create a new function to update the total of money request report --- src/libs/actions/IOU.ts | 65 +++++++++++++++++++++++++++-------------- 1 file changed, 43 insertions(+), 22 deletions(-) diff --git a/src/libs/actions/IOU.ts b/src/libs/actions/IOU.ts index 297f1096b0ca..bd2d22fc6b9f 100644 --- a/src/libs/actions/IOU.ts +++ b/src/libs/actions/IOU.ts @@ -969,6 +969,48 @@ function createDistanceRequest( Report.notifyNewAction(chatReport.reportID, userAccountID); } +/** + * We will update the total of money request report when we update a transaction if + * + * - change the currency from different currency to the currency of the iou report + * - change the currency from the currency of iou report to different currency + * - change the amount and the currency of the transaction is the currency of the report + */ +function getUpdatedMoneyRequestReport( + iouReport: OnyxEntry, + updatedTransaction: OnyxTypes.Transaction | null, + transaction: OnyxTypes.Transaction | null, + updatedReportAction: OnyxEntry, +): OnyxEntry { + if (!iouReport) { + return null; + } + let updatedMoneyRequestReport = {...iouReport}; + let diff = 0; + const isExpenseReport = ReportUtils.isExpenseReport(iouReport); + const updatedCurrency = TransactionUtils.getCurrency(updatedTransaction); + const currentCurrency = TransactionUtils.getCurrency(transaction); + + if (updatedCurrency === iouReport?.currency && currentCurrency !== iouReport?.currency) { + // add the diff with the total if we change the currency from different currency to the currency of the iou report + diff = TransactionUtils.getAmount(updatedTransaction, isExpenseReport); + } else if (updatedCurrency !== iouReport?.currency && currentCurrency === iouReport?.currency) { + // subtract the diff with the total if we change the currency from the currency of iou report to different currency + diff = -TransactionUtils.getAmount(updatedTransaction, isExpenseReport); + } else if (updatedCurrency === iouReport?.currency && updatedTransaction?.modifiedAmount) { + // get the diff between the updated amount and the current amount if we change the amount and the currency of the transaction is the currency of the report + diff = TransactionUtils.getAmount(updatedTransaction, isExpenseReport) - TransactionUtils.getAmount(transaction, isExpenseReport); + } + if (isExpenseReport && typeof updatedMoneyRequestReport.total === 'number') { + // For expense report, the amount is negative so we should subtract total from diff + updatedMoneyRequestReport.total -= diff; + } else { + updatedMoneyRequestReport = IOUUtils.updateIOUOwnerAndTotal(iouReport, updatedReportAction?.actorAccountID ?? -1, diff, TransactionUtils.getCurrency(updatedTransaction), false); + } + updatedMoneyRequestReport.cachedTotal = CurrencyUtils.convertToDisplayString(updatedMoneyRequestReport.total, updatedTransaction?.modifiedCurrency); + return updatedMoneyRequestReport; +} + /** * @param transactionChanges * @param [transactionChanges.created] Present when updated the date field @@ -1050,28 +1092,7 @@ function getUpdateMoneyRequestParams( }); // Step 4: Compute the IOU total and update the report preview message (and report header) so LHN amount owed is correct. - // Should only update if the transaction matches the currency of the report, else we wait for the update - // from the server with the currency conversion - let updatedMoneyRequestReport = {...iouReport}; - let diff = 0; - const isExpenseReport = ReportUtils.isExpenseReport(iouReport); - const updatedCurrency = TransactionUtils.getCurrency(updatedTransaction); - const currentCurrency = TransactionUtils.getCurrency(transaction); - if (updatedCurrency === iouReport?.currency && currentCurrency !== iouReport?.currency) { - diff = TransactionUtils.getAmount(updatedTransaction, isExpenseReport); - } else if (updatedCurrency !== iouReport?.currency && currentCurrency === iouReport?.currency) { - diff = -TransactionUtils.getAmount(updatedTransaction, isExpenseReport); - } else if (updatedCurrency === iouReport?.currency && updatedTransaction?.modifiedAmount) { - diff = TransactionUtils.getAmount(updatedTransaction, isExpenseReport) - TransactionUtils.getAmount(transaction, isExpenseReport); - } - if (isExpenseReport && typeof updatedMoneyRequestReport.total === 'number') { - updatedMoneyRequestReport.total += diff; - } else { - updatedMoneyRequestReport = iouReport - ? IOUUtils.updateIOUOwnerAndTotal(iouReport, updatedReportAction.actorAccountID ?? -1, diff, TransactionUtils.getCurrency(updatedTransaction), false) - : {}; - } - updatedMoneyRequestReport.cachedTotal = CurrencyUtils.convertToDisplayString(updatedMoneyRequestReport.total, updatedTransaction?.modifiedCurrency); + const updatedMoneyRequestReport = getUpdatedMoneyRequestReport(iouReport, updatedTransaction, transaction, updatedReportAction as OnyxTypes.ReportAction); optimisticData.push({ onyxMethod: Onyx.METHOD.MERGE, key: `${ONYXKEYS.COLLECTION.REPORT}${iouReport?.reportID}`, From 296fb19b75401cca0ab935cfe23349e9964632fc Mon Sep 17 00:00:00 2001 From: dukenv0307 Date: Wed, 7 Feb 2024 17:02:24 +0700 Subject: [PATCH 09/14] create a function to compute diff amount --- src/libs/actions/IOU.ts | 49 ++++++++++++++++++----------------------- 1 file changed, 21 insertions(+), 28 deletions(-) diff --git a/src/libs/actions/IOU.ts b/src/libs/actions/IOU.ts index bd2d22fc6b9f..ba54eac56fdb 100644 --- a/src/libs/actions/IOU.ts +++ b/src/libs/actions/IOU.ts @@ -970,45 +970,28 @@ function createDistanceRequest( } /** - * We will update the total of money request report when we update a transaction if - * - * - change the currency from different currency to the currency of the iou report - * - change the currency from the currency of iou report to different currency - * - change the amount and the currency of the transaction is the currency of the report + * Compute the diff amount when we update the transaction */ -function getUpdatedMoneyRequestReport( - iouReport: OnyxEntry, - updatedTransaction: OnyxTypes.Transaction | null, - transaction: OnyxTypes.Transaction | null, - updatedReportAction: OnyxEntry, -): OnyxEntry { +function computeDiffAmount(iouReport: OnyxEntry, updatedTransaction: OnyxEntry, transaction: OnyxEntry): number { if (!iouReport) { - return null; + return 0; } - let updatedMoneyRequestReport = {...iouReport}; - let diff = 0; const isExpenseReport = ReportUtils.isExpenseReport(iouReport); const updatedCurrency = TransactionUtils.getCurrency(updatedTransaction); const currentCurrency = TransactionUtils.getCurrency(transaction); if (updatedCurrency === iouReport?.currency && currentCurrency !== iouReport?.currency) { // add the diff with the total if we change the currency from different currency to the currency of the iou report - diff = TransactionUtils.getAmount(updatedTransaction, isExpenseReport); - } else if (updatedCurrency !== iouReport?.currency && currentCurrency === iouReport?.currency) { + return TransactionUtils.getAmount(updatedTransaction, isExpenseReport); + } if (updatedCurrency !== iouReport?.currency && currentCurrency === iouReport?.currency) { // subtract the diff with the total if we change the currency from the currency of iou report to different currency - diff = -TransactionUtils.getAmount(updatedTransaction, isExpenseReport); - } else if (updatedCurrency === iouReport?.currency && updatedTransaction?.modifiedAmount) { + return -TransactionUtils.getAmount(updatedTransaction, isExpenseReport); + } if (updatedCurrency === iouReport?.currency && updatedTransaction?.modifiedAmount) { // get the diff between the updated amount and the current amount if we change the amount and the currency of the transaction is the currency of the report - diff = TransactionUtils.getAmount(updatedTransaction, isExpenseReport) - TransactionUtils.getAmount(transaction, isExpenseReport); + return TransactionUtils.getAmount(updatedTransaction, isExpenseReport) - TransactionUtils.getAmount(transaction, isExpenseReport); } - if (isExpenseReport && typeof updatedMoneyRequestReport.total === 'number') { - // For expense report, the amount is negative so we should subtract total from diff - updatedMoneyRequestReport.total -= diff; - } else { - updatedMoneyRequestReport = IOUUtils.updateIOUOwnerAndTotal(iouReport, updatedReportAction?.actorAccountID ?? -1, diff, TransactionUtils.getCurrency(updatedTransaction), false); - } - updatedMoneyRequestReport.cachedTotal = CurrencyUtils.convertToDisplayString(updatedMoneyRequestReport.total, updatedTransaction?.modifiedCurrency); - return updatedMoneyRequestReport; + + return 0; } /** @@ -1092,7 +1075,17 @@ function getUpdateMoneyRequestParams( }); // Step 4: Compute the IOU total and update the report preview message (and report header) so LHN amount owed is correct. - const updatedMoneyRequestReport = getUpdatedMoneyRequestReport(iouReport, updatedTransaction, transaction, updatedReportAction as OnyxTypes.ReportAction); + let updatedMoneyRequestReport = {...iouReport}; + const diff = computeDiffAmount(iouReport, updatedTransaction, transaction); + + if (ReportUtils.isExpenseReport(iouReport) && typeof updatedMoneyRequestReport.total === 'number') { + // For expense report, the amount is negative so we should subtract total from diff + updatedMoneyRequestReport.total -= diff; + } else { + updatedMoneyRequestReport = IOUUtils.updateIOUOwnerAndTotal(iouReport, updatedReportAction?.actorAccountID ?? -1, diff, TransactionUtils.getCurrency(updatedTransaction), false); + } + updatedMoneyRequestReport.cachedTotal = CurrencyUtils.convertToDisplayString(updatedMoneyRequestReport.total, updatedTransaction?.modifiedCurrency); + optimisticData.push({ onyxMethod: Onyx.METHOD.MERGE, key: `${ONYXKEYS.COLLECTION.REPORT}${iouReport?.reportID}`, From 0626907cda6fa904489462cf04dd8dd7b2d2768a Mon Sep 17 00:00:00 2001 From: dukenv0307 Date: Wed, 7 Feb 2024 17:07:05 +0700 Subject: [PATCH 10/14] fix ts error --- src/libs/actions/IOU.ts | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/libs/actions/IOU.ts b/src/libs/actions/IOU.ts index ba54eac56fdb..209ff2df04d6 100644 --- a/src/libs/actions/IOU.ts +++ b/src/libs/actions/IOU.ts @@ -1082,7 +1082,9 @@ function getUpdateMoneyRequestParams( // For expense report, the amount is negative so we should subtract total from diff updatedMoneyRequestReport.total -= diff; } else { - updatedMoneyRequestReport = IOUUtils.updateIOUOwnerAndTotal(iouReport, updatedReportAction?.actorAccountID ?? -1, diff, TransactionUtils.getCurrency(updatedTransaction), false); + updatedMoneyRequestReport = iouReport + ? IOUUtils.updateIOUOwnerAndTotal(iouReport, updatedReportAction.actorAccountID ?? -1, diff, TransactionUtils.getCurrency(transaction), false) + : {}; } updatedMoneyRequestReport.cachedTotal = CurrencyUtils.convertToDisplayString(updatedMoneyRequestReport.total, updatedTransaction?.modifiedCurrency); From 271bd238cda8b36d14f089962a20ea2e06195b6f Mon Sep 17 00:00:00 2001 From: dukenv0307 Date: Wed, 7 Feb 2024 17:12:37 +0700 Subject: [PATCH 11/14] fix prettier --- src/libs/actions/IOU.ts | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/src/libs/actions/IOU.ts b/src/libs/actions/IOU.ts index 209ff2df04d6..3acbf3c03895 100644 --- a/src/libs/actions/IOU.ts +++ b/src/libs/actions/IOU.ts @@ -983,10 +983,12 @@ function computeDiffAmount(iouReport: OnyxEntry, updatedTransa if (updatedCurrency === iouReport?.currency && currentCurrency !== iouReport?.currency) { // add the diff with the total if we change the currency from different currency to the currency of the iou report return TransactionUtils.getAmount(updatedTransaction, isExpenseReport); - } if (updatedCurrency !== iouReport?.currency && currentCurrency === iouReport?.currency) { + } + if (updatedCurrency !== iouReport?.currency && currentCurrency === iouReport?.currency) { // subtract the diff with the total if we change the currency from the currency of iou report to different currency return -TransactionUtils.getAmount(updatedTransaction, isExpenseReport); - } if (updatedCurrency === iouReport?.currency && updatedTransaction?.modifiedAmount) { + } + if (updatedCurrency === iouReport?.currency && updatedTransaction?.modifiedAmount) { // get the diff between the updated amount and the current amount if we change the amount and the currency of the transaction is the currency of the report return TransactionUtils.getAmount(updatedTransaction, isExpenseReport) - TransactionUtils.getAmount(transaction, isExpenseReport); } @@ -1083,8 +1085,8 @@ function getUpdateMoneyRequestParams( updatedMoneyRequestReport.total -= diff; } else { updatedMoneyRequestReport = iouReport - ? IOUUtils.updateIOUOwnerAndTotal(iouReport, updatedReportAction.actorAccountID ?? -1, diff, TransactionUtils.getCurrency(transaction), false) - : {}; + ? IOUUtils.updateIOUOwnerAndTotal(iouReport, updatedReportAction.actorAccountID ?? -1, diff, TransactionUtils.getCurrency(transaction), false) + : {}; } updatedMoneyRequestReport.cachedTotal = CurrencyUtils.convertToDisplayString(updatedMoneyRequestReport.total, updatedTransaction?.modifiedCurrency); From 8796fae65044142f2fcee296ca2b55df278a8ab7 Mon Sep 17 00:00:00 2001 From: dukenv0307 Date: Thu, 15 Feb 2024 15:51:54 +0700 Subject: [PATCH 12/14] edit comment and refactor new function --- src/libs/actions/IOU.ts | 49 +++++++++-------------------------------- 1 file changed, 11 insertions(+), 38 deletions(-) diff --git a/src/libs/actions/IOU.ts b/src/libs/actions/IOU.ts index 89b010efdafd..6be6d347e223 100644 --- a/src/libs/actions/IOU.ts +++ b/src/libs/actions/IOU.ts @@ -985,7 +985,7 @@ function createDistanceRequest( /** * Compute the diff amount when we update the transaction */ -function computeDiffAmount(iouReport: OnyxEntry, updatedTransaction: OnyxEntry, transaction: OnyxEntry): number { +function calculateDiffAmount(iouReport: OnyxEntry, updatedTransaction: OnyxEntry, transaction: OnyxEntry): number { if (!iouReport) { return 0; } @@ -993,17 +993,20 @@ function computeDiffAmount(iouReport: OnyxEntry, updatedTransa const updatedCurrency = TransactionUtils.getCurrency(updatedTransaction); const currentCurrency = TransactionUtils.getCurrency(transaction); + const currentAmount = TransactionUtils.getAmount(transaction, isExpenseReport); + const updatedAmount = TransactionUtils.getAmount(updatedTransaction, isExpenseReport); + if (updatedCurrency === iouReport?.currency && currentCurrency !== iouReport?.currency) { - // add the diff with the total if we change the currency from different currency to the currency of the iou report - return TransactionUtils.getAmount(updatedTransaction, isExpenseReport); + // Add the diff to the total if we change the currency from a different currency to the currency of the IOU report + return updatedAmount; } if (updatedCurrency !== iouReport?.currency && currentCurrency === iouReport?.currency) { - // subtract the diff with the total if we change the currency from the currency of iou report to different currency - return -TransactionUtils.getAmount(updatedTransaction, isExpenseReport); + // Subtract the diff from the total if we change the currency from the currency of IOU report to a different currency + return -updatedAmount; } if (updatedCurrency === iouReport?.currency && updatedTransaction?.modifiedAmount) { - // get the diff between the updated amount and the current amount if we change the amount and the currency of the transaction is the currency of the report - return TransactionUtils.getAmount(updatedTransaction, isExpenseReport) - TransactionUtils.getAmount(transaction, isExpenseReport); + // Calculate the diff between the updated amount and the current amount if we change the amount and the currency of the transaction is the currency of the report + return updatedAmount - currentAmount; } return 0; @@ -1125,11 +1128,10 @@ function getUpdateMoneyRequestParams( }, }, }); -<<<<<<< HEAD // Step 4: Compute the IOU total and update the report preview message (and report header) so LHN amount owed is correct. let updatedMoneyRequestReport = {...iouReport}; - const diff = computeDiffAmount(iouReport, updatedTransaction, transaction); + const diff = calculateDiffAmount(iouReport, updatedTransaction, transaction); if (ReportUtils.isExpenseReport(iouReport) && typeof updatedMoneyRequestReport.total === 'number') { // For expense report, the amount is negative so we should subtract total from diff @@ -1141,35 +1143,6 @@ function getUpdateMoneyRequestParams( } updatedMoneyRequestReport.cachedTotal = CurrencyUtils.convertToDisplayString(updatedMoneyRequestReport.total, updatedTransaction?.modifiedCurrency); - optimisticData.push({ - onyxMethod: Onyx.METHOD.MERGE, - key: `${ONYXKEYS.COLLECTION.REPORT}${iouReport?.reportID}`, - value: updatedMoneyRequestReport, - }); - successData.push({ - onyxMethod: Onyx.METHOD.MERGE, - key: `${ONYXKEYS.COLLECTION.REPORT}${iouReport?.reportID}`, - value: {pendingAction: null}, - }); -======= ->>>>>>> main - } - - // Step 4: Compute the IOU total and update the report preview message (and report header) so LHN amount owed is correct. - // Should only update if the transaction matches the currency of the report, else we wait for the update - // from the server with the currency conversion - let updatedMoneyRequestReport = {...iouReport}; - if ((hasPendingWaypoints || updatedTransaction?.modifiedAmount) && updatedTransaction?.currency === iouReport?.currency) { - const diff = TransactionUtils.getAmount(transaction, true) - TransactionUtils.getAmount(updatedTransaction, true); - if (ReportUtils.isExpenseReport(iouReport) && typeof updatedMoneyRequestReport.total === 'number') { - updatedMoneyRequestReport.total += diff; - } else { - updatedMoneyRequestReport = iouReport - ? IOUUtils.updateIOUOwnerAndTotal(iouReport, updatedReportAction.actorAccountID ?? -1, diff, TransactionUtils.getCurrency(transaction), false) - : {}; - } - - updatedMoneyRequestReport.cachedTotal = CurrencyUtils.convertToDisplayString(updatedMoneyRequestReport.total, updatedTransaction?.currency); optimisticData.push({ onyxMethod: Onyx.METHOD.MERGE, key: `${ONYXKEYS.COLLECTION.REPORT}${iouReport?.reportID}`, From 2a481433f859f2a7e280967593fc4b5c66ca8313 Mon Sep 17 00:00:00 2001 From: dukenv0307 Date: Thu, 15 Feb 2024 20:17:09 +0700 Subject: [PATCH 13/14] fix incorrect amount for the case create or delete a request with foreign currency --- src/libs/IOUUtils.ts | 13 +++++++++++-- src/libs/actions/IOU.ts | 17 ++++++----------- 2 files changed, 17 insertions(+), 13 deletions(-) diff --git a/src/libs/IOUUtils.ts b/src/libs/IOUUtils.ts index 55317c9c050c..1aafe09c53fe 100644 --- a/src/libs/IOUUtils.ts +++ b/src/libs/IOUUtils.ts @@ -53,9 +53,18 @@ function calculateAmount(numberOfParticipants: number, total: number, currency: * If user1 requests $17 from user2, then we have: {ownerAccountID: user1, managerID: user2, total: $7 (still a positive amount, but now owed to user1)} * * @param isDeleting - whether the user is deleting the request + * @param isUpdating - whether the user is updating the request */ -function updateIOUOwnerAndTotal>(iouReport: TReport, actorAccountID: number, amount: number, currency: string, isDeleting = false): TReport { - if (!iouReport?.currency) { +function updateIOUOwnerAndTotal>( + iouReport: TReport, + actorAccountID: number, + amount: number, + currency: string, + isDeleting = false, + isUpdating = false, +): TReport { + // For the update case, we have calculated the diff amount in the calculateDiffAmount function so there is no need to compare currencies here + if ((currency !== iouReport?.currency && !isUpdating) || !iouReport) { return iouReport; } diff --git a/src/libs/actions/IOU.ts b/src/libs/actions/IOU.ts index 6be6d347e223..fb61bf3565ab 100644 --- a/src/libs/actions/IOU.ts +++ b/src/libs/actions/IOU.ts @@ -1138,7 +1138,7 @@ function getUpdateMoneyRequestParams( updatedMoneyRequestReport.total -= diff; } else { updatedMoneyRequestReport = iouReport - ? IOUUtils.updateIOUOwnerAndTotal(iouReport, updatedReportAction.actorAccountID ?? -1, diff, TransactionUtils.getCurrency(transaction), false) + ? IOUUtils.updateIOUOwnerAndTotal(iouReport, updatedReportAction.actorAccountID ?? -1, diff, TransactionUtils.getCurrency(transaction), false, true) : {}; } updatedMoneyRequestReport.cachedTotal = CurrencyUtils.convertToDisplayString(updatedMoneyRequestReport.total, updatedTransaction?.modifiedCurrency); @@ -1739,7 +1739,7 @@ function createSplitsAndOnyxData( oneOnOneIOUReport.total -= splitAmount; } } else { - oneOnOneIOUReport = IOUUtils.updateIOUOwnerAndTotal(oneOnOneIOUReport, currentUserAccountID, splitAmount, currency); + oneOnOneIOUReport = IOUUtils.updateIOUOwnerAndTotal(oneOnOneIOUReport, currentUserAccountID, splitAmount, currency, false); } // STEP 3: Build optimistic transaction @@ -2363,7 +2363,7 @@ function completeSplitBill(chatReportID: string, reportAction: OnyxTypes.ReportA oneOnOneIOUReport.total -= splitAmount; } } else { - oneOnOneIOUReport = IOUUtils.updateIOUOwnerAndTotal(oneOnOneIOUReport, sessionAccountID, splitAmount, currency ?? ''); + oneOnOneIOUReport = IOUUtils.updateIOUOwnerAndTotal(oneOnOneIOUReport, sessionAccountID, splitAmount, currency ?? '', false); } const oneOnOneTransaction = TransactionUtils.buildOptimisticTransaction( @@ -2810,23 +2810,18 @@ function deleteMoneyRequest(transactionID: string, reportAction: OnyxTypes.Repor // STEP 4: Update the iouReport and reportPreview with new totals and messages if it wasn't deleted let updatedIOUReport: OnyxTypes.Report | null; + const currency = TransactionUtils.getCurrency(transaction); const updatedReportPreviewAction: OnyxTypes.ReportAction | EmptyObject = {...reportPreviewAction}; updatedReportPreviewAction.pendingAction = shouldDeleteIOUReport ? CONST.RED_BRICK_ROAD_PENDING_ACTION.DELETE : CONST.RED_BRICK_ROAD_PENDING_ACTION.UPDATE; if (iouReport && ReportUtils.isExpenseReport(iouReport)) { updatedIOUReport = {...iouReport}; - if (typeof updatedIOUReport.total === 'number') { + if (typeof updatedIOUReport.total === 'number' && currency === iouReport?.currency) { // Because of the Expense reports are stored as negative values, we add the total from the amount updatedIOUReport.total += TransactionUtils.getAmount(transaction, true); } } else { - updatedIOUReport = IOUUtils.updateIOUOwnerAndTotal( - iouReport, - reportAction.actorAccountID ?? -1, - TransactionUtils.getAmount(transaction, false), - TransactionUtils.getCurrency(transaction), - true, - ); + updatedIOUReport = IOUUtils.updateIOUOwnerAndTotal(iouReport, reportAction.actorAccountID ?? -1, TransactionUtils.getAmount(transaction, false), currency, true); } if (updatedIOUReport) { From 9f075c489096d4e0556d2916d09245c7956e8865 Mon Sep 17 00:00:00 2001 From: dukenv0307 Date: Thu, 15 Feb 2024 20:21:41 +0700 Subject: [PATCH 14/14] revert hard code --- src/libs/actions/IOU.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/libs/actions/IOU.ts b/src/libs/actions/IOU.ts index fb61bf3565ab..c3f7c44c86b0 100644 --- a/src/libs/actions/IOU.ts +++ b/src/libs/actions/IOU.ts @@ -1739,7 +1739,7 @@ function createSplitsAndOnyxData( oneOnOneIOUReport.total -= splitAmount; } } else { - oneOnOneIOUReport = IOUUtils.updateIOUOwnerAndTotal(oneOnOneIOUReport, currentUserAccountID, splitAmount, currency, false); + oneOnOneIOUReport = IOUUtils.updateIOUOwnerAndTotal(oneOnOneIOUReport, currentUserAccountID, splitAmount, currency); } // STEP 3: Build optimistic transaction @@ -2363,7 +2363,7 @@ function completeSplitBill(chatReportID: string, reportAction: OnyxTypes.ReportA oneOnOneIOUReport.total -= splitAmount; } } else { - oneOnOneIOUReport = IOUUtils.updateIOUOwnerAndTotal(oneOnOneIOUReport, sessionAccountID, splitAmount, currency ?? '', false); + oneOnOneIOUReport = IOUUtils.updateIOUOwnerAndTotal(oneOnOneIOUReport, sessionAccountID, splitAmount, currency ?? ''); } const oneOnOneTransaction = TransactionUtils.buildOptimisticTransaction(