From 2d0469d88a13cb9a980e97ca6828f56cc9b6a15e Mon Sep 17 00:00:00 2001 From: Carlos Martins Date: Wed, 9 Aug 2023 13:57:18 -0600 Subject: [PATCH 01/44] create getTransaction --- src/libs/ReportActionsUtils.js | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/src/libs/ReportActionsUtils.js b/src/libs/ReportActionsUtils.js index 6d777533360d..7b6e17517126 100644 --- a/src/libs/ReportActionsUtils.js +++ b/src/libs/ReportActionsUtils.js @@ -37,6 +37,20 @@ Onyx.connect({ }, }); +let allTransactions; +Onyx.connect({ + key: ONYXKEYS.COLLECTION.TRANSACTION, + waitForCollectionCallback: true, + callback: (val) => { + if (!val) { + allTransactions = {}; + return; + } + + allTransactions = val; + }, +}); + let isNetworkOffline = false; Onyx.connect({ key: ONYXKEYS.NETWORK, @@ -572,6 +586,15 @@ function isMessageDeleted(reportAction) { return lodashGet(reportAction, ['message', 0, 'isDeletedParentAction'], false); } +function getTransaction(reportAction) { + const transactionID = lodashGet(reportAction, ['originalMessage', 'IOUTransactionID'], ''); + if (!transactionID) { + return {}; + } + + return allTransactions[transactionID] || {}; +} + export { getSortedReportActions, getLastVisibleAction, @@ -605,4 +628,5 @@ export { isWhisperAction, isPendingRemove, getReportAction, + getTransaction, }; From 6c8ca4cd514683e754b8f695edec26cb98e3baab Mon Sep 17 00:00:00 2001 From: Carlos Martins Date: Wed, 9 Aug 2023 13:59:11 -0600 Subject: [PATCH 02/44] add jsdocs --- src/libs/ReportActionsUtils.js | 25 ++++++++++++++++--------- 1 file changed, 16 insertions(+), 9 deletions(-) diff --git a/src/libs/ReportActionsUtils.js b/src/libs/ReportActionsUtils.js index 7b6e17517126..09f72296a0d3 100644 --- a/src/libs/ReportActionsUtils.js +++ b/src/libs/ReportActionsUtils.js @@ -144,6 +144,21 @@ function getParentReportActionInReport(childReportID, parentReportID) { return _.find(allReportActions[parentReportID], (reportAction) => reportAction && `${reportAction.childReportID}` === `${childReportID}`); } +/** + * Gets the transaction associated with the IOU reportAction + * + * @param {Object} reportAction + * @returns {Object} + */ +function getTransaction(reportAction) { + const transactionID = lodashGet(reportAction, ['originalMessage', 'IOUTransactionID'], ''); + if (!transactionID) { + return {}; + } + + return allTransactions[transactionID] || {}; +} + /** * Determines if the given report action is sent money report action by checking for 'pay' type and presence of IOUDetails object. * @@ -167,6 +182,7 @@ function isSentMoneyReportAction(reportAction) { * @returns {Number} */ function getFormattedAmount(reportAction) { + const transaction = getTransaction(reportAction); return lodashGet(reportAction, 'originalMessage.type', '') === CONST.IOU.REPORT_ACTION_TYPE.PAY && lodashGet(reportAction, 'originalMessage.IOUDetails', false) ? CurrencyUtils.convertToDisplayString(lodashGet(reportAction, 'originalMessage.IOUDetails.amount', 0), lodashGet(reportAction, 'originalMessage.IOUDetails.currency', '')) : CurrencyUtils.convertToDisplayString(lodashGet(reportAction, 'originalMessage.amount', 0), lodashGet(reportAction, 'originalMessage.currency', '')); @@ -586,15 +602,6 @@ function isMessageDeleted(reportAction) { return lodashGet(reportAction, ['message', 0, 'isDeletedParentAction'], false); } -function getTransaction(reportAction) { - const transactionID = lodashGet(reportAction, ['originalMessage', 'IOUTransactionID'], ''); - if (!transactionID) { - return {}; - } - - return allTransactions[transactionID] || {}; -} - export { getSortedReportActions, getLastVisibleAction, From ad95fb04007c2371d5a821b95b2547994a58cde9 Mon Sep 17 00:00:00 2001 From: Carlos Martins Date: Wed, 9 Aug 2023 14:49:27 -0600 Subject: [PATCH 03/44] refactor getFormattedAmount --- src/libs/ReportActionsUtils.js | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/src/libs/ReportActionsUtils.js b/src/libs/ReportActionsUtils.js index 09f72296a0d3..e8e29e003219 100644 --- a/src/libs/ReportActionsUtils.js +++ b/src/libs/ReportActionsUtils.js @@ -175,17 +175,19 @@ function isSentMoneyReportAction(reportAction) { } /** - * Returns the formatted amount of a money request. The request and money sent (from send money flow) have - * currency and amount in IOUDetails object. + * Returns the formatted amount of a money request. * * @param {Object} reportAction * @returns {Number} */ function getFormattedAmount(reportAction) { + // IOU actions of type = 'pay' OR actions with existing IOUDetails ('send' actions) don't have linked transactions, so we get the details from the originalMessage key instead + if (lodashGet(reportAction, 'originalMessage.type', '') === CONST.IOU.REPORT_ACTION_TYPE.PAY || lodashGet(reportAction, 'originalMessage.IOUDetails', false)) { + return CurrencyUtils.convertToDisplayString(lodashGet(reportAction, 'originalMessage.IOUDetails.amount', 0), lodashGet(reportAction, 'originalMessage.IOUDetails.currency', '')); + } + const transaction = getTransaction(reportAction); - return lodashGet(reportAction, 'originalMessage.type', '') === CONST.IOU.REPORT_ACTION_TYPE.PAY && lodashGet(reportAction, 'originalMessage.IOUDetails', false) - ? CurrencyUtils.convertToDisplayString(lodashGet(reportAction, 'originalMessage.IOUDetails.amount', 0), lodashGet(reportAction, 'originalMessage.IOUDetails.currency', '')) - : CurrencyUtils.convertToDisplayString(lodashGet(reportAction, 'originalMessage.amount', 0), lodashGet(reportAction, 'originalMessage.currency', '')); + return CurrencyUtils.convertToDisplayString(lodashGet(transaction, 'amount', 0), lodashGet(transaction, 'currency', '')); } /** From f5b5a01e0faa3769c90733ec2651b4631d3e3af3 Mon Sep 17 00:00:00 2001 From: Carlos Martins Date: Wed, 9 Aug 2023 15:26:26 -0600 Subject: [PATCH 04/44] update logic and rename to getMoneyRequestDetails --- src/libs/ReportActionsUtils.js | 34 +++++++++++++++++++--------------- 1 file changed, 19 insertions(+), 15 deletions(-) diff --git a/src/libs/ReportActionsUtils.js b/src/libs/ReportActionsUtils.js index e8e29e003219..d3b49b7970b3 100644 --- a/src/libs/ReportActionsUtils.js +++ b/src/libs/ReportActionsUtils.js @@ -145,18 +145,27 @@ function getParentReportActionInReport(childReportID, parentReportID) { } /** - * Gets the transaction associated with the IOU reportAction + * Get the details linked to the IOU reportAction * - * @param {Object} reportAction + * @param {Object} reportAction * @returns {Object} */ -function getTransaction(reportAction) { - const transactionID = lodashGet(reportAction, ['originalMessage', 'IOUTransactionID'], ''); - if (!transactionID) { - return {}; +function getMoneyRequestDetails(reportAction = {}) { + // 'pay' actions don't have a linked transaction since they pay the report. So we get the details from the originalMessage instead + const originalMessage = lodashGet(reportAction, 'originalMessage', {}); + if (lodashGet(originalMessage, 'type', '') === CONST.IOU.REPORT_ACTION_TYPE.PAY) { + return originalMessage; + } + + // Similarly, 'send' actions store their details in IOUDetails + const iouDetails = lodashGet(originalMessage, 'IOUDetails', null); + if (iouDetails) { + return iouDetails; } - return allTransactions[transactionID] || {}; + // For all other actions, retrieve the details from the linked transaction + const transactionID = lodashGet(originalMessage, 'IOUTransactionID', ''); + return allTransactions[transactionID] || {amount: 0, currency: CONST.CURRENCY.USD, comment: ''}; } /** @@ -181,13 +190,8 @@ function isSentMoneyReportAction(reportAction) { * @returns {Number} */ function getFormattedAmount(reportAction) { - // IOU actions of type = 'pay' OR actions with existing IOUDetails ('send' actions) don't have linked transactions, so we get the details from the originalMessage key instead - if (lodashGet(reportAction, 'originalMessage.type', '') === CONST.IOU.REPORT_ACTION_TYPE.PAY || lodashGet(reportAction, 'originalMessage.IOUDetails', false)) { - return CurrencyUtils.convertToDisplayString(lodashGet(reportAction, 'originalMessage.IOUDetails.amount', 0), lodashGet(reportAction, 'originalMessage.IOUDetails.currency', '')); - } - - const transaction = getTransaction(reportAction); - return CurrencyUtils.convertToDisplayString(lodashGet(transaction, 'amount', 0), lodashGet(transaction, 'currency', '')); + const moneyRequestDetails = getMoneyRequestDetails(reportAction); + return CurrencyUtils.convertToDisplayString(lodashGet(moneyRequestDetails, 'amount', 0), lodashGet(moneyRequestDetails, 'currency', '')); } /** @@ -637,5 +641,5 @@ export { isWhisperAction, isPendingRemove, getReportAction, - getTransaction, + getMoneyRequestDetails, }; From 12fbdd79a902b557f8082e021071b99535bee75b Mon Sep 17 00:00:00 2001 From: Carlos Martins Date: Wed, 9 Aug 2023 15:32:40 -0600 Subject: [PATCH 05/44] replace usages of getMoneyRequestAction --- src/components/MoneyRequestDetails.js | 3 ++- src/components/ReportActionItem/IOUPreview.js | 9 ++++--- .../ReportActionItem/MoneyRequestView.js | 2 +- src/libs/ReportUtils.js | 27 ------------------- src/pages/EditRequestPage.js | 5 ++-- 5 files changed, 10 insertions(+), 36 deletions(-) diff --git a/src/components/MoneyRequestDetails.js b/src/components/MoneyRequestDetails.js index a690c31c000c..0bd8d6bd93a9 100644 --- a/src/components/MoneyRequestDetails.js +++ b/src/components/MoneyRequestDetails.js @@ -6,6 +6,7 @@ import lodashGet from 'lodash/get'; import iouReportPropTypes from '../pages/iouReportPropTypes'; import withLocalize, {withLocalizePropTypes} from './withLocalize'; import * as ReportUtils from '../libs/ReportUtils'; +import * as ReportActionsUtils from '../libs/ReportActionsUtils'; import * as Expensicons from './Icon/Expensicons'; import Text from './Text'; import participantPropTypes from './participantPropTypes'; @@ -74,7 +75,7 @@ const defaultProps = { function MoneyRequestDetails(props) { // These are only used for the single transaction view and not for expense and iou reports - const {amount: transactionAmount, currency: transactionCurrency, comment: transactionDescription} = ReportUtils.getMoneyRequestAction(props.parentReportAction); + const {amount: transactionAmount, currency: transactionCurrency, comment: transactionDescription} = ReportActionsUtils.getMoneyRequestDetails(props.parentReportAction); const formattedTransactionAmount = transactionAmount && transactionCurrency && CurrencyUtils.convertToDisplayString(transactionAmount, transactionCurrency); const transactionDate = lodashGet(props.parentReportAction, ['created']); const formattedTransactionDate = DateUtils.getDateStringFromISOTimestamp(transactionDate); diff --git a/src/components/ReportActionItem/IOUPreview.js b/src/components/ReportActionItem/IOUPreview.js index 85a0b22ac327..cda9bbe83380 100644 --- a/src/components/ReportActionItem/IOUPreview.js +++ b/src/components/ReportActionItem/IOUPreview.js @@ -26,6 +26,7 @@ import * as OptionsListUtils from '../../libs/OptionsListUtils'; import * as CurrencyUtils from '../../libs/CurrencyUtils'; import * as IOUUtils from '../../libs/IOUUtils'; import * as ReportUtils from '../../libs/ReportUtils'; +import * as ReportActionsUtils from '../../libs/ReportActionsUtils'; import refPropTypes from '../refPropTypes'; import PressableWithFeedback from '../Pressable/PressableWithoutFeedback'; @@ -137,11 +138,11 @@ function IOUPreview(props) { // Pay button should only be visible to the manager of the report. const isCurrentUserManager = managerID === sessionAccountID; - const moneyRequestAction = ReportUtils.getMoneyRequestAction(props.action); + const moneyRequestDetails = ReportUtils.getMoneyRequestAction(props.action); - const requestAmount = moneyRequestAction.amount; - const requestCurrency = moneyRequestAction.currency; - const requestComment = moneyRequestAction.comment.trim(); + const requestAmount = moneyRequestDetails.amount; + const requestCurrency = moneyRequestDetails.currency; + const requestComment = moneyRequestDetails.comment.trim(); const getSettledMessage = () => { switch (lodashGet(props.action, 'originalMessage.paymentType', '')) { diff --git a/src/components/ReportActionItem/MoneyRequestView.js b/src/components/ReportActionItem/MoneyRequestView.js index dc8916bdaecb..0991ec68d97c 100644 --- a/src/components/ReportActionItem/MoneyRequestView.js +++ b/src/components/ReportActionItem/MoneyRequestView.js @@ -40,7 +40,7 @@ const defaultProps = { function MoneyRequestView(props) { const parentReportAction = ReportActionsUtils.getParentReportAction(props.report); - const {amount: transactionAmount, currency: transactionCurrency, comment: transactionDescription} = ReportUtils.getMoneyRequestAction(parentReportAction); + const {amount: transactionAmount, currency: transactionCurrency, comment: transactionDescription} = ReportActionsUtils.getMoneyRequestDetails(parentReportAction); const formattedTransactionAmount = transactionAmount && transactionCurrency && CurrencyUtils.convertToDisplayString(transactionAmount, transactionCurrency); const transactionDate = lodashGet(parentReportAction, ['created']); const formattedTransactionDate = DateUtils.getDateStringFromISOTimestamp(transactionDate); diff --git a/src/libs/ReportUtils.js b/src/libs/ReportUtils.js index e46f1b39971e..9efc66d0d28a 100644 --- a/src/libs/ReportUtils.js +++ b/src/libs/ReportUtils.js @@ -1104,32 +1104,6 @@ function getDisplayNamesWithTooltips(personalDetailsList, isMultipleParticipantR }); } -/** - * We get the amount, currency and comment money request value from the action.originalMessage. - * But for the send money action, the above value is put in the IOUDetails object. - * - * @param {Object} reportAction - * @param {Number} reportAction.amount - * @param {String} reportAction.currency - * @param {String} reportAction.comment - * @param {Object} [reportAction.IOUDetails] - * @returns {Object} - */ -function getMoneyRequestAction(reportAction = {}) { - const originalMessage = lodashGet(reportAction, 'originalMessage', {}); - let amount = originalMessage.amount || 0; - let currency = originalMessage.currency || CONST.CURRENCY.USD; - let comment = originalMessage.comment || ''; - - if (_.has(originalMessage, 'IOUDetails')) { - amount = lodashGet(originalMessage, 'IOUDetails.amount', 0); - currency = lodashGet(originalMessage, 'IOUDetails.currency', CONST.CURRENCY.USD); - comment = lodashGet(originalMessage, 'IOUDetails.comment', ''); - } - - return {amount, currency, comment}; -} - /** * Determines if a report has an IOU that is waiting for an action from the current user (either Pay or Add a credit bank account) * @@ -2998,7 +2972,6 @@ export { isReportDataReady, isSettled, isAllowedToComment, - getMoneyRequestAction, getBankAccountRoute, getParentReport, getTaskParentReportActionIDInAssigneeReport, diff --git a/src/pages/EditRequestPage.js b/src/pages/EditRequestPage.js index d2280ec78fd7..79ff113dc6bd 100644 --- a/src/pages/EditRequestPage.js +++ b/src/pages/EditRequestPage.js @@ -10,7 +10,6 @@ import ONYXKEYS from '../ONYXKEYS'; import * as ReportActionsUtils from '../libs/ReportActionsUtils'; import EditRequestDescriptionPage from './EditRequestDescriptionPage'; import reportPropTypes from './reportPropTypes'; -import * as ReportUtils from '../libs/ReportUtils'; const propTypes = { ...withLocalizePropTypes, @@ -37,8 +36,8 @@ const defaultProps = { function EditRequestPage(props) { const parentReportAction = ReportActionsUtils.getParentReportAction(props.report); - const moneyRequestReportAction = ReportUtils.getMoneyRequestAction(parentReportAction); - const transactionDescription = moneyRequestReportAction.comment; + const moneyRequestDetails = ReportActionsUtils.getMoneyRequestDetails(parentReportAction); + const transactionDescription = moneyRequestDetails.comment; const field = lodashGet(props, ['route', 'params', 'field'], ''); function updateTransactionWithChanges(changes) { From f595a75c75c3b100f28584920336065bc5fb487d Mon Sep 17 00:00:00 2001 From: Carlos Martins Date: Wed, 9 Aug 2023 15:33:59 -0600 Subject: [PATCH 06/44] replace missed instance --- src/components/ReportActionItem/IOUPreview.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/components/ReportActionItem/IOUPreview.js b/src/components/ReportActionItem/IOUPreview.js index cda9bbe83380..924e6e6a6ed1 100644 --- a/src/components/ReportActionItem/IOUPreview.js +++ b/src/components/ReportActionItem/IOUPreview.js @@ -138,7 +138,7 @@ function IOUPreview(props) { // Pay button should only be visible to the manager of the report. const isCurrentUserManager = managerID === sessionAccountID; - const moneyRequestDetails = ReportUtils.getMoneyRequestAction(props.action); + const moneyRequestDetails = ReportActionsUtils.getMoneyRequestDetails(props.action); const requestAmount = moneyRequestDetails.amount; const requestCurrency = moneyRequestDetails.currency; From ad49161f5843f23974f3d27004ea6c68c0256add Mon Sep 17 00:00:00 2001 From: Carlos Martins Date: Wed, 9 Aug 2023 15:39:26 -0600 Subject: [PATCH 07/44] refactor logic to retrieve transaction --- src/libs/ReportActionsUtils.js | 15 +++++++++++++-- src/libs/actions/IOU.js | 4 ++-- 2 files changed, 15 insertions(+), 4 deletions(-) diff --git a/src/libs/ReportActionsUtils.js b/src/libs/ReportActionsUtils.js index d3b49b7970b3..f5a4a2805e99 100644 --- a/src/libs/ReportActionsUtils.js +++ b/src/libs/ReportActionsUtils.js @@ -144,6 +144,17 @@ function getParentReportActionInReport(childReportID, parentReportID) { return _.find(allReportActions[parentReportID], (reportAction) => reportAction && `${reportAction.childReportID}` === `${childReportID}`); } +/** + * Get the details linked to the IOU reportAction + * + * @param {Object} reportAction + * @returns {Object} + */ +function getTransaction(reportAction = {}) { + const transactionID = lodashGet(reportAction, ['originalMessage', 'IOUTransactionID'], ''); + return allTransactions[transactionID] || {}; +} + /** * Get the details linked to the IOU reportAction * @@ -164,8 +175,7 @@ function getMoneyRequestDetails(reportAction = {}) { } // For all other actions, retrieve the details from the linked transaction - const transactionID = lodashGet(originalMessage, 'IOUTransactionID', ''); - return allTransactions[transactionID] || {amount: 0, currency: CONST.CURRENCY.USD, comment: ''}; + return getTransaction(reportAction); } /** @@ -642,4 +652,5 @@ export { isPendingRemove, getReportAction, getMoneyRequestDetails, + getTransaction, }; diff --git a/src/libs/actions/IOU.js b/src/libs/actions/IOU.js index bbb313929f39..aac40a416967 100644 --- a/src/libs/actions/IOU.js +++ b/src/libs/actions/IOU.js @@ -846,9 +846,9 @@ function deleteMoneyRequest(transactionID, reportAction, isSingleTransactionView updatedIOUReport = {...iouReport}; // Because of the Expense reports are stored as negative values, we add the total from the amount - updatedIOUReport.total += reportAction.originalMessage.amount; + updatedIOUReport.total += transaction.amount; } else { - updatedIOUReport = IOUUtils.updateIOUOwnerAndTotal(iouReport, reportAction.actorAccountID, reportAction.originalMessage.amount, reportAction.originalMessage.currency, true); + updatedIOUReport = IOUUtils.updateIOUOwnerAndTotal(iouReport, reportAction.actorAccountID, transaction.amount, transaction.currency, true); } updatedIOUReport.lastMessageText = iouReportLastMessageText; From 4bcd07233cfb776e4c992124b2b86bbdc3ec2198 Mon Sep 17 00:00:00 2001 From: Carlos Martins Date: Wed, 9 Aug 2023 15:42:22 -0600 Subject: [PATCH 08/44] refactor SplitBillDetailsPage --- src/pages/iou/SplitBillDetailsPage.js | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/src/pages/iou/SplitBillDetailsPage.js b/src/pages/iou/SplitBillDetailsPage.js index b638da091874..2abf35a58ef7 100644 --- a/src/pages/iou/SplitBillDetailsPage.js +++ b/src/pages/iou/SplitBillDetailsPage.js @@ -18,6 +18,7 @@ import withReportAndReportActionOrNotFound from '../home/report/withReportAndRep import FullPageNotFoundView from '../../components/BlockingViews/FullPageNotFoundView'; import CONST from '../../CONST'; import HeaderWithBackButton from '../../components/HeaderWithBackButton'; +import * as ReportActionsUtils from '../../libs/ReportActionsUtils'; const propTypes = { /* Onyx Props */ @@ -52,6 +53,7 @@ const defaultProps = { function SplitBillDetailsPage(props) { const reportAction = props.reportActions[`${props.route.params.reportActionID.toString()}`]; + const transaction = ReportActionsUtils.getTransaction(reportAction); const participantAccountIDs = reportAction.originalMessage.participantAccountIDs; const participants = OptionsListUtils.getParticipantsOptions( _.map(participantAccountIDs, (accountID) => ({accountID, selected: true})), @@ -59,9 +61,9 @@ function SplitBillDetailsPage(props) { ); const payeePersonalDetails = props.personalDetails[reportAction.actorAccountID]; const participantsExcludingPayee = _.filter(participants, (participant) => participant.accountID !== reportAction.actorAccountID); - const splitAmount = parseInt(lodashGet(reportAction, 'originalMessage.amount', 0), 10); - const splitComment = lodashGet(reportAction, 'originalMessage.comment'); - const splitCurrency = lodashGet(reportAction, 'originalMessage.currency'); + const splitAmount = parseInt(lodashGet(transaction, 'amount', 0), 10); + const splitComment = lodashGet(transaction, 'comment'); + const splitCurrency = lodashGet(transaction, 'currency'); return ( From a1448ff865c0ec174cfbffd0f2b05b46910c12b3 Mon Sep 17 00:00:00 2001 From: Carlos Martins Date: Wed, 9 Aug 2023 15:46:51 -0600 Subject: [PATCH 09/44] refactor getIOUReportActions --- src/libs/IOUUtils.js | 2 +- src/libs/ReportActionsUtils.js | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/libs/IOUUtils.js b/src/libs/IOUUtils.js index 241baf26c998..2b47427ec6e1 100644 --- a/src/libs/IOUUtils.js +++ b/src/libs/IOUUtils.js @@ -78,7 +78,7 @@ function getIOUReportActions(reportActions, iouReport, type = '', pendingAction .filter((action) => action.originalMessage && ReportActionsUtils.isMoneyRequestAction(action) && (!_.isEmpty(type) ? action.originalMessage.type === type : true)) .filter((action) => action.originalMessage.IOUReportID.toString() === iouReport.reportID.toString()) .filter((action) => (!_.isEmpty(pendingAction) ? action.pendingAction === pendingAction : true)) - .filter((action) => (filterRequestsInDifferentCurrency ? action.originalMessage.currency !== iouReport.currency : true)) + .filter((action) => (filterRequestsInDifferentCurrency ? ReportActionsUtils.getMoneyRequestDetails(action).currency !== iouReport.currency : true)) .value(); } diff --git a/src/libs/ReportActionsUtils.js b/src/libs/ReportActionsUtils.js index f5a4a2805e99..d5b7290b2440 100644 --- a/src/libs/ReportActionsUtils.js +++ b/src/libs/ReportActionsUtils.js @@ -169,7 +169,7 @@ function getMoneyRequestDetails(reportAction = {}) { } // Similarly, 'send' actions store their details in IOUDetails - const iouDetails = lodashGet(originalMessage, 'IOUDetails', null); + const iouDetails = lodashGet(originalMessage, 'IOUDetails'); if (iouDetails) { return iouDetails; } From 10dcaf2f3d834ba617c46c790a058ab11ac923c2 Mon Sep 17 00:00:00 2001 From: Carlos Martins Date: Wed, 9 Aug 2023 15:48:20 -0600 Subject: [PATCH 10/44] refactor MoneyRequestAction --- src/components/ReportActionItem/MoneyRequestAction.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/components/ReportActionItem/MoneyRequestAction.js b/src/components/ReportActionItem/MoneyRequestAction.js index 5790e55b2c78..ff84dddabc3b 100644 --- a/src/components/ReportActionItem/MoneyRequestAction.js +++ b/src/components/ReportActionItem/MoneyRequestAction.js @@ -102,7 +102,7 @@ function MoneyRequestAction(props) { participantAccountIDs, props.translate(ReportActionsUtils.isSentMoneyReportAction(props.action) ? 'iou.threadSentMoneyReportName' : 'iou.threadRequestReportName', { formattedAmount: ReportActionsUtils.getFormattedAmount(props.action), - comment: props.action.originalMessage.comment, + comment: ReportActionsUtils.getMoneyRequestDetails(props.action).comment, }), '', CONST.POLICY.OWNER_EMAIL_FAKE, From acb4efb82aab67959ab5eca905e0874ca113afe5 Mon Sep 17 00:00:00 2001 From: Carlos Martins Date: Wed, 9 Aug 2023 15:51:01 -0600 Subject: [PATCH 11/44] refactor getTransactionReportName --- src/components/ReportActionItem/MoneyRequestAction.js | 5 +---- src/libs/ReportUtils.js | 2 +- 2 files changed, 2 insertions(+), 5 deletions(-) diff --git a/src/components/ReportActionItem/MoneyRequestAction.js b/src/components/ReportActionItem/MoneyRequestAction.js index ff84dddabc3b..cc885fe897b2 100644 --- a/src/components/ReportActionItem/MoneyRequestAction.js +++ b/src/components/ReportActionItem/MoneyRequestAction.js @@ -100,10 +100,7 @@ function MoneyRequestAction(props) { const participantAccountIDs = _.uniq([props.session.accountID, Number(props.action.actorAccountID)]); const thread = ReportUtils.buildOptimisticChatReport( participantAccountIDs, - props.translate(ReportActionsUtils.isSentMoneyReportAction(props.action) ? 'iou.threadSentMoneyReportName' : 'iou.threadRequestReportName', { - formattedAmount: ReportActionsUtils.getFormattedAmount(props.action), - comment: ReportActionsUtils.getMoneyRequestDetails(props.action).comment, - }), + ReportUtils.getTransactionReportName(props.action), '', CONST.POLICY.OWNER_EMAIL_FAKE, CONST.POLICY.OWNER_ACCOUNT_ID_FAKE, diff --git a/src/libs/ReportUtils.js b/src/libs/ReportUtils.js index 9efc66d0d28a..dadffd2ef04f 100644 --- a/src/libs/ReportUtils.js +++ b/src/libs/ReportUtils.js @@ -1235,7 +1235,7 @@ function getTransactionReportName(reportAction) { return Localize.translateLocal(ReportActionsUtils.isSentMoneyReportAction(reportAction) ? 'iou.threadSentMoneyReportName' : 'iou.threadRequestReportName', { formattedAmount: ReportActionsUtils.getFormattedAmount(reportAction), - comment: lodashGet(reportAction, 'originalMessage.comment'), + comment: ReportActionsUtils.getMoneyRequestDetails.comment, }); } From a6fd68a3fd9aac236ce6da58aea7f50cc4a276fd Mon Sep 17 00:00:00 2001 From: Carlos Martins Date: Wed, 9 Aug 2023 16:06:23 -0600 Subject: [PATCH 12/44] fix comment bugs --- src/libs/ReportActionsUtils.js | 5 +++-- src/pages/iou/SplitBillDetailsPage.js | 2 +- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/src/libs/ReportActionsUtils.js b/src/libs/ReportActionsUtils.js index d5b7290b2440..809c93655ddd 100644 --- a/src/libs/ReportActionsUtils.js +++ b/src/libs/ReportActionsUtils.js @@ -152,7 +152,7 @@ function getParentReportActionInReport(childReportID, parentReportID) { */ function getTransaction(reportAction = {}) { const transactionID = lodashGet(reportAction, ['originalMessage', 'IOUTransactionID'], ''); - return allTransactions[transactionID] || {}; + return allTransactions[`${ONYXKEYS.COLLECTION.TRANSACTION}${transactionID}`] || {}; } /** @@ -175,7 +175,8 @@ function getMoneyRequestDetails(reportAction = {}) { } // For all other actions, retrieve the details from the linked transaction - return getTransaction(reportAction); + const transaction = getTransaction(reportAction); + return {...transaction, comment: lodashGet(transaction, 'comment.comment', '')}; } /** diff --git a/src/pages/iou/SplitBillDetailsPage.js b/src/pages/iou/SplitBillDetailsPage.js index 2abf35a58ef7..62dc8b5c243b 100644 --- a/src/pages/iou/SplitBillDetailsPage.js +++ b/src/pages/iou/SplitBillDetailsPage.js @@ -62,7 +62,7 @@ function SplitBillDetailsPage(props) { const payeePersonalDetails = props.personalDetails[reportAction.actorAccountID]; const participantsExcludingPayee = _.filter(participants, (participant) => participant.accountID !== reportAction.actorAccountID); const splitAmount = parseInt(lodashGet(transaction, 'amount', 0), 10); - const splitComment = lodashGet(transaction, 'comment'); + const splitComment = lodashGet(transaction, 'comment.comment'); const splitCurrency = lodashGet(transaction, 'currency'); return ( From e067001c5b48b90edd9129251ac02da935cfbaed Mon Sep 17 00:00:00 2001 From: Carlos Martins Date: Wed, 9 Aug 2023 16:11:21 -0600 Subject: [PATCH 13/44] export getTransactionReportName --- src/libs/ReportUtils.js | 1 + 1 file changed, 1 insertion(+) diff --git a/src/libs/ReportUtils.js b/src/libs/ReportUtils.js index dadffd2ef04f..d49312828289 100644 --- a/src/libs/ReportUtils.js +++ b/src/libs/ReportUtils.js @@ -2985,4 +2985,5 @@ export { shouldDisableSettings, shouldDisableRename, hasSingleParticipant, + getTransactionReportName, }; From b2f4406afe9731b074ae6a2a7dca7a09df479e21 Mon Sep 17 00:00:00 2001 From: Carlos Martins Date: Wed, 9 Aug 2023 16:29:01 -0600 Subject: [PATCH 14/44] update split case --- src/libs/ReportActionsUtils.js | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/src/libs/ReportActionsUtils.js b/src/libs/ReportActionsUtils.js index 809c93655ddd..76800b4b7394 100644 --- a/src/libs/ReportActionsUtils.js +++ b/src/libs/ReportActionsUtils.js @@ -162,19 +162,21 @@ function getTransaction(reportAction = {}) { * @returns {Object} */ function getMoneyRequestDetails(reportAction = {}) { - // 'pay' actions don't have a linked transaction since they pay the report. So we get the details from the originalMessage instead + // 'pay' actions don't have a linked transaction since they pay the report. So we get the details from the originalMessage instead. + // 'split' actions have a linked transaction, but we don't return it from the backend since they are stored in an invisible report - see https://github.com/Expensify/Auth/blob/bdadc27d649df6feef51525cc58fa16c1619eee5/auth/lib/Transaction.h#L49-L51 const originalMessage = lodashGet(reportAction, 'originalMessage', {}); - if (lodashGet(originalMessage, 'type', '') === CONST.IOU.REPORT_ACTION_TYPE.PAY) { + const type = lodashGet(originalMessage, 'type', ''); + if (type === CONST.IOU.REPORT_ACTION_TYPE.PAY || type === CONST.IOU.REPORT_ACTION_TYPE.SPLIT) { return originalMessage; } - // Similarly, 'send' actions store their details in IOUDetails + // Similarly, 'send' actions store their details in IOUDetails. const iouDetails = lodashGet(originalMessage, 'IOUDetails'); if (iouDetails) { return iouDetails; } - // For all other actions, retrieve the details from the linked transaction + // For all other actions, retrieve the details from the linked transaction. const transaction = getTransaction(reportAction); return {...transaction, comment: lodashGet(transaction, 'comment.comment', '')}; } From 8969b0511a997f199f5ad2e7d75d1fd688007d79 Mon Sep 17 00:00:00 2001 From: Carlos Martins Date: Wed, 9 Aug 2023 17:54:49 -0600 Subject: [PATCH 15/44] add transaction default --- src/libs/IOUUtils.js | 5 ++++- src/libs/ReportActionsUtils.js | 2 +- 2 files changed, 5 insertions(+), 2 deletions(-) diff --git a/src/libs/IOUUtils.js b/src/libs/IOUUtils.js index 2b47427ec6e1..22ae4986185e 100644 --- a/src/libs/IOUUtils.js +++ b/src/libs/IOUUtils.js @@ -78,7 +78,10 @@ function getIOUReportActions(reportActions, iouReport, type = '', pendingAction .filter((action) => action.originalMessage && ReportActionsUtils.isMoneyRequestAction(action) && (!_.isEmpty(type) ? action.originalMessage.type === type : true)) .filter((action) => action.originalMessage.IOUReportID.toString() === iouReport.reportID.toString()) .filter((action) => (!_.isEmpty(pendingAction) ? action.pendingAction === pendingAction : true)) - .filter((action) => (filterRequestsInDifferentCurrency ? ReportActionsUtils.getMoneyRequestDetails(action).currency !== iouReport.currency : true)) + .filter((action) => { + const currency = ReportActionsUtils.getMoneyRequestDetails(action).currency; + return filterRequestsInDifferentCurrency ? (currency && currency !== iouReport.currency) : true; + }) .value(); } diff --git a/src/libs/ReportActionsUtils.js b/src/libs/ReportActionsUtils.js index 76800b4b7394..d0f49c01d210 100644 --- a/src/libs/ReportActionsUtils.js +++ b/src/libs/ReportActionsUtils.js @@ -37,7 +37,7 @@ Onyx.connect({ }, }); -let allTransactions; +let allTransactions = {}; Onyx.connect({ key: ONYXKEYS.COLLECTION.TRANSACTION, waitForCollectionCallback: true, From 00f61c2460854865d6f388488275d9dfcd030a81 Mon Sep 17 00:00:00 2001 From: Carlos Martins Date: Thu, 10 Aug 2023 15:48:59 -0600 Subject: [PATCH 16/44] rename method to getLinkedTransaction --- src/libs/ReportActionsUtils.js | 7 +++---- src/pages/iou/SplitBillDetailsPage.js | 2 +- 2 files changed, 4 insertions(+), 5 deletions(-) diff --git a/src/libs/ReportActionsUtils.js b/src/libs/ReportActionsUtils.js index d0f49c01d210..756ada1e58af 100644 --- a/src/libs/ReportActionsUtils.js +++ b/src/libs/ReportActionsUtils.js @@ -43,7 +43,6 @@ Onyx.connect({ waitForCollectionCallback: true, callback: (val) => { if (!val) { - allTransactions = {}; return; } @@ -150,7 +149,7 @@ function getParentReportActionInReport(childReportID, parentReportID) { * @param {Object} reportAction * @returns {Object} */ -function getTransaction(reportAction = {}) { +function getLinkedTransaction(reportAction = {}) { const transactionID = lodashGet(reportAction, ['originalMessage', 'IOUTransactionID'], ''); return allTransactions[`${ONYXKEYS.COLLECTION.TRANSACTION}${transactionID}`] || {}; } @@ -177,7 +176,7 @@ function getMoneyRequestDetails(reportAction = {}) { } // For all other actions, retrieve the details from the linked transaction. - const transaction = getTransaction(reportAction); + const transaction = getLinkedTransaction(reportAction); return {...transaction, comment: lodashGet(transaction, 'comment.comment', '')}; } @@ -655,5 +654,5 @@ export { isPendingRemove, getReportAction, getMoneyRequestDetails, - getTransaction, + getLinkedTransaction, }; diff --git a/src/pages/iou/SplitBillDetailsPage.js b/src/pages/iou/SplitBillDetailsPage.js index 62dc8b5c243b..bff2fa370ac5 100644 --- a/src/pages/iou/SplitBillDetailsPage.js +++ b/src/pages/iou/SplitBillDetailsPage.js @@ -53,7 +53,7 @@ const defaultProps = { function SplitBillDetailsPage(props) { const reportAction = props.reportActions[`${props.route.params.reportActionID.toString()}`]; - const transaction = ReportActionsUtils.getTransaction(reportAction); + const transaction = ReportActionsUtils.getLinkedTransaction(reportAction); const participantAccountIDs = reportAction.originalMessage.participantAccountIDs; const participants = OptionsListUtils.getParticipantsOptions( _.map(participantAccountIDs, (accountID) => ({accountID, selected: true})), From 29ed60b5d85558993439d5daeb26caa49b85ba16 Mon Sep 17 00:00:00 2001 From: Carlos Martins Date: Fri, 11 Aug 2023 13:55:27 -0600 Subject: [PATCH 17/44] add getAllReportTransactions --- src/libs/ReportUtils.js | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/src/libs/ReportUtils.js b/src/libs/ReportUtils.js index e0c3282303f6..9637c53b42f7 100644 --- a/src/libs/ReportUtils.js +++ b/src/libs/ReportUtils.js @@ -87,6 +87,19 @@ Onyx.connect({ callback: (val) => (loginList = val), }); +let allTransactions = {}; +Onyx.connect({ + key: ONYXKEYS.COLLECTION.TRANSACTION, + waitForCollectionCallback: true, + callback: (val) => { + if (!val) { + return; + } + + allTransactions = val; + }, +}); + function getChatType(report) { return report ? report.chatType : ''; } @@ -2877,6 +2890,10 @@ function shouldDisableRename(report, policy) { return !_.keys(loginList).includes(policy.owner) && policy.role !== CONST.POLICY.ROLE.ADMIN; } +function getAllReportTransactions(reportID) { + return _.filter(allTransactions, transaction => transaction.reportID === reportID); +} + export { getReportParticipantsTitle, isReportMessageAttachment, @@ -2998,4 +3015,5 @@ export { shouldDisableRename, hasSingleParticipant, getTransactionReportName, + getAllReportTransactions, }; From e7c2cac0b7546eac9e5c91bf73ec7b40dc191149 Mon Sep 17 00:00:00 2001 From: Carlos Martins Date: Fri, 11 Aug 2023 16:38:00 -0600 Subject: [PATCH 18/44] fix style --- src/libs/IOUUtils.js | 2 +- src/libs/ReportUtils.js | 5 ++--- 2 files changed, 3 insertions(+), 4 deletions(-) diff --git a/src/libs/IOUUtils.js b/src/libs/IOUUtils.js index 22ae4986185e..f1fcdb55b809 100644 --- a/src/libs/IOUUtils.js +++ b/src/libs/IOUUtils.js @@ -80,7 +80,7 @@ function getIOUReportActions(reportActions, iouReport, type = '', pendingAction .filter((action) => (!_.isEmpty(pendingAction) ? action.pendingAction === pendingAction : true)) .filter((action) => { const currency = ReportActionsUtils.getMoneyRequestDetails(action).currency; - return filterRequestsInDifferentCurrency ? (currency && currency !== iouReport.currency) : true; + return filterRequestsInDifferentCurrency ? currency && currency !== iouReport.currency : true; }) .value(); } diff --git a/src/libs/ReportUtils.js b/src/libs/ReportUtils.js index a4255552dc35..eb4d4c1d3b2c 100644 --- a/src/libs/ReportUtils.js +++ b/src/libs/ReportUtils.js @@ -2892,12 +2892,11 @@ function shouldDisableRename(report, policy) { } /** - * @param {String} reportID + * @param {String} reportID * @returns {Array} */ function getAllReportTransactions(reportID) { - return _.filter(allTransactions, transaction => transaction.reportID === reportID); - + return _.filter(allTransactions, (transaction) => transaction.reportID === reportID); } /** From b6b9a8b1fdd32d6e6702a77f8150fc16d790bc35 Mon Sep 17 00:00:00 2001 From: Carlos Martins Date: Mon, 14 Aug 2023 15:10:25 -0500 Subject: [PATCH 19/44] rm unused component --- src/components/MoneyRequestDetails.js | 232 -------------------------- 1 file changed, 232 deletions(-) delete mode 100644 src/components/MoneyRequestDetails.js diff --git a/src/components/MoneyRequestDetails.js b/src/components/MoneyRequestDetails.js deleted file mode 100644 index 0bd8d6bd93a9..000000000000 --- a/src/components/MoneyRequestDetails.js +++ /dev/null @@ -1,232 +0,0 @@ -import React from 'react'; -import {withOnyx} from 'react-native-onyx'; -import {View} from 'react-native'; -import PropTypes from 'prop-types'; -import lodashGet from 'lodash/get'; -import iouReportPropTypes from '../pages/iouReportPropTypes'; -import withLocalize, {withLocalizePropTypes} from './withLocalize'; -import * as ReportUtils from '../libs/ReportUtils'; -import * as ReportActionsUtils from '../libs/ReportActionsUtils'; -import * as Expensicons from './Icon/Expensicons'; -import Text from './Text'; -import participantPropTypes from './participantPropTypes'; -import Avatar from './Avatar'; -import styles from '../styles/styles'; -import themeColors from '../styles/themes/default'; -import CONST from '../CONST'; -import withWindowDimensions from './withWindowDimensions'; -import compose from '../libs/compose'; -import ROUTES from '../ROUTES'; -import Icon from './Icon'; -import SettlementButton from './SettlementButton'; -import * as Policy from '../libs/actions/Policy'; -import ONYXKEYS from '../ONYXKEYS'; -import * as IOU from '../libs/actions/IOU'; -import * as CurrencyUtils from '../libs/CurrencyUtils'; -import MenuItemWithTopDescription from './MenuItemWithTopDescription'; -import DateUtils from '../libs/DateUtils'; -import reportPropTypes from '../pages/reportPropTypes'; -import * as UserUtils from '../libs/UserUtils'; -import OfflineWithFeedback from './OfflineWithFeedback'; - -const propTypes = { - /** The report currently being looked at */ - report: iouReportPropTypes.isRequired, - - /** The expense report or iou report (only will have a value if this is a transaction thread) */ - parentReport: iouReportPropTypes, - - /** The policy object for the current route */ - policy: PropTypes.shape({ - /** The name of the policy */ - name: PropTypes.string, - - /** The URL for the policy avatar */ - avatar: PropTypes.string, - }), - - /** The chat report this report is linked to */ - chatReport: reportPropTypes, - - /** Personal details so we can get the ones for the report participants */ - personalDetails: PropTypes.objectOf(participantPropTypes).isRequired, - - /** Whether we're viewing a report with a single transaction in it */ - isSingleTransactionView: PropTypes.bool, - - /** Session info for the currently logged in user. */ - session: PropTypes.shape({ - /** Currently logged in user email */ - email: PropTypes.string, - }), - - ...withLocalizePropTypes, -}; - -const defaultProps = { - isSingleTransactionView: false, - chatReport: {}, - session: { - email: null, - }, - parentReport: {}, - policy: null, -}; - -function MoneyRequestDetails(props) { - // These are only used for the single transaction view and not for expense and iou reports - const {amount: transactionAmount, currency: transactionCurrency, comment: transactionDescription} = ReportActionsUtils.getMoneyRequestDetails(props.parentReportAction); - const formattedTransactionAmount = transactionAmount && transactionCurrency && CurrencyUtils.convertToDisplayString(transactionAmount, transactionCurrency); - const transactionDate = lodashGet(props.parentReportAction, ['created']); - const formattedTransactionDate = DateUtils.getDateStringFromISOTimestamp(transactionDate); - - const reportTotal = ReportUtils.getMoneyRequestTotal(props.report); - const formattedAmount = CurrencyUtils.convertToDisplayString(reportTotal, props.report.currency); - const moneyRequestReport = props.isSingleTransactionView ? props.parentReport : props.report; - const isSettled = ReportUtils.isSettled(moneyRequestReport.reportID); - const isExpenseReport = ReportUtils.isExpenseReport(moneyRequestReport); - const payeeName = isExpenseReport ? ReportUtils.getPolicyName(moneyRequestReport) : ReportUtils.getDisplayNameForParticipant(moneyRequestReport.managerID); - const payeeAvatar = isExpenseReport - ? ReportUtils.getWorkspaceAvatar(moneyRequestReport) - : UserUtils.getAvatar(lodashGet(props.personalDetails, [moneyRequestReport.managerID, 'avatar']), moneyRequestReport.managerID); - const isPayer = - Policy.isAdminOfFreePolicy([props.policy]) || (ReportUtils.isMoneyRequestReport(moneyRequestReport) && lodashGet(props.session, 'accountID', null) === moneyRequestReport.managerID); - const shouldShowSettlementButton = - moneyRequestReport.reportID && !isSettled && !props.isSingleTransactionView && isPayer && !moneyRequestReport.isWaitingOnBankAccount && reportTotal !== 0; - const bankAccountRoute = ReportUtils.getBankAccountRoute(props.chatReport); - const shouldShowPaypal = Boolean(lodashGet(props.personalDetails, [moneyRequestReport.ownerAccountID, 'payPalMeAddress'])); - let description = `${props.translate('iou.amount')} • ${props.translate('iou.cash')}`; - if (isSettled) { - description += ` • ${props.translate('iou.settledExpensify')}`; - } else if (props.report.isWaitingOnBankAccount) { - description += ` • ${props.translate('iou.pending')}`; - } - - const {addWorkspaceRoomOrChatPendingAction, addWorkspaceRoomOrChatErrors} = ReportUtils.getReportOfflinePendingActionAndErrors(props.report); - return ( - - - - {props.translate('common.to')} - - - - - - {payeeName} - - {isExpenseReport && ( - - {props.translate('workspace.common.workspace')} - - )} - - - - {!props.isSingleTransactionView && {formattedAmount}} - {!props.isSingleTransactionView && isSettled && ( - - - - )} - {shouldShowSettlementButton && !props.isSmallScreenWidth && ( - - IOU.payMoneyRequest(paymentType, props.chatReport, props.report)} - enablePaymentsRoute={ROUTES.BANK_ACCOUNT_NEW} - addBankAccountRoute={bankAccountRoute} - shouldShowPaymentOptions - /> - - )} - - - {shouldShowSettlementButton && props.isSmallScreenWidth && ( - IOU.payMoneyRequest(paymentType, props.chatReport, props.report)} - enablePaymentsRoute={ROUTES.BANK_ACCOUNT_NEW} - addBankAccountRoute={bankAccountRoute} - shouldShowPaymentOptions - /> - )} - - {props.isSingleTransactionView && ( - <> - Navigation.navigate(ROUTES.getEditRequestRoute(props.report.reportID, CONST.EDIT_REQUEST_FIELD.AMOUNT))} - /> - Navigation.navigate(ROUTES.getEditRequestRoute(props.report.reportID, CONST.EDIT_REQUEST_FIELD.DESCRIPTION))} - /> - Navigation.navigate(ROUTES.getEditRequestRoute(props.report.reportID, CONST.EDIT_REQUEST_FIELD.DATE))} - /> - - )} - - - ); -} - -MoneyRequestDetails.displayName = 'MoneyRequestDetails'; -MoneyRequestDetails.propTypes = propTypes; -MoneyRequestDetails.defaultProps = defaultProps; - -export default compose( - withWindowDimensions, - withLocalize, - withOnyx({ - chatReport: { - key: ({report}) => `${ONYXKEYS.COLLECTION.REPORT}${report.chatReportID}`, - }, - session: { - key: ONYXKEYS.SESSION, - }, - parentReport: { - key: (props) => `${ONYXKEYS.COLLECTION.REPORT}${props.report.parentReportID}`, - }, - }), -)(MoneyRequestDetails); From 523d61bb685f0253e04ae18eb7daf98a3e0809d3 Mon Sep 17 00:00:00 2001 From: Carlos Martins Date: Mon, 14 Aug 2023 15:44:19 -0500 Subject: [PATCH 20/44] use transactionUtils methods --- src/components/ReportActionItem/IOUPreview.js | 10 +++++----- src/libs/IOUUtils.js | 4 +++- 2 files changed, 8 insertions(+), 6 deletions(-) diff --git a/src/components/ReportActionItem/IOUPreview.js b/src/components/ReportActionItem/IOUPreview.js index 924e6e6a6ed1..2e0d2a112ae1 100644 --- a/src/components/ReportActionItem/IOUPreview.js +++ b/src/components/ReportActionItem/IOUPreview.js @@ -29,6 +29,7 @@ import * as ReportUtils from '../../libs/ReportUtils'; import * as ReportActionsUtils from '../../libs/ReportActionsUtils'; import refPropTypes from '../refPropTypes'; import PressableWithFeedback from '../Pressable/PressableWithoutFeedback'; +import * as TransactionsUtils from '../../libs/TransactionUtils'; const propTypes = { /** The active IOUReport, used for Onyx subscription */ @@ -138,11 +139,10 @@ function IOUPreview(props) { // Pay button should only be visible to the manager of the report. const isCurrentUserManager = managerID === sessionAccountID; - const moneyRequestDetails = ReportActionsUtils.getMoneyRequestDetails(props.action); - - const requestAmount = moneyRequestDetails.amount; - const requestCurrency = moneyRequestDetails.currency; - const requestComment = moneyRequestDetails.comment.trim(); + const transaction = ReportActionsUtils.getLinkedTransaction(props.action); + const requestAmount = TransactionsUtils.getAmount(transaction, ReportUtils.isExpenseReport(props.iouReport)); + const requestCurrency = TransactionsUtils.getCurrency(transaction); + const requestComment = TransactionsUtils.getDescription(transaction); const getSettledMessage = () => { switch (lodashGet(props.action, 'originalMessage.paymentType', '')) { diff --git a/src/libs/IOUUtils.js b/src/libs/IOUUtils.js index f1fcdb55b809..a3ade416c45a 100644 --- a/src/libs/IOUUtils.js +++ b/src/libs/IOUUtils.js @@ -1,6 +1,7 @@ import _ from 'underscore'; import CONST from '../CONST'; import * as ReportActionsUtils from './ReportActionsUtils'; +import * as TransactionUtils from './TransactionUtils'; /** * Calculates the amount per user given a list of participants @@ -79,7 +80,8 @@ function getIOUReportActions(reportActions, iouReport, type = '', pendingAction .filter((action) => action.originalMessage.IOUReportID.toString() === iouReport.reportID.toString()) .filter((action) => (!_.isEmpty(pendingAction) ? action.pendingAction === pendingAction : true)) .filter((action) => { - const currency = ReportActionsUtils.getMoneyRequestDetails(action).currency; + const transaction = ReportActionsUtils.getLinkedTransaction(action); + const currency = TransactionUtils.getCurrency(transaction); return filterRequestsInDifferentCurrency ? currency && currency !== iouReport.currency : true; }) .value(); From 901370ef2002fc9f6c201ddf4bdaf4bd5a206c18 Mon Sep 17 00:00:00 2001 From: Carlos Martins Date: Mon, 14 Aug 2023 15:53:48 -0500 Subject: [PATCH 21/44] rm getMoneyRequestDetails --- .../ReportActionItem/MoneyRequestView.js | 9 +++++-- src/libs/ReportActionsUtils.js | 26 ------------------- 2 files changed, 7 insertions(+), 28 deletions(-) diff --git a/src/components/ReportActionItem/MoneyRequestView.js b/src/components/ReportActionItem/MoneyRequestView.js index 47d6413cc6cb..2794916a8c48 100644 --- a/src/components/ReportActionItem/MoneyRequestView.js +++ b/src/components/ReportActionItem/MoneyRequestView.js @@ -23,6 +23,7 @@ import * as CurrencyUtils from '../../libs/CurrencyUtils'; import EmptyStateBackgroundImage from '../../../assets/images/empty-state_background-fade.png'; import useLocalize from '../../hooks/useLocalize'; import useWindowDimensions from '../../hooks/useWindowDimensions'; +import * as TransactionUtils from '../../libs/TransactionUtils'; const propTypes = { /** The report currently being looked at */ @@ -65,12 +66,16 @@ function MoneyRequestView({report, parentReport, shouldShowHorizontalRule, polic const {translate} = useLocalize(); const parentReportAction = ReportActionsUtils.getParentReportAction(report); - const {amount: transactionAmount, currency: transactionCurrency, comment: transactionDescription} = ReportUtils.getMoneyRequestDetails(parentReportAction); + const moneyRequestReport = parentReport; + const transaction = ReportActionsUtils.getLinkedTransaction(parentReportAction); + const transactionAmount = TransactionUtils.getAmount(transaction, ReportUtils.isExpenseReport(parentReport)); + const transactionCurrency = TransactionUtils.getCurrency(transaction); + const transactionDescription = TransactionUtils.getDescription(transaction); + const formattedTransactionAmount = transactionAmount && transactionCurrency && CurrencyUtils.convertToDisplayString(transactionAmount, transactionCurrency); const transactionDate = lodashGet(parentReportAction, ['created']); const formattedTransactionDate = DateUtils.getDateStringFromISOTimestamp(transactionDate); - const moneyRequestReport = parentReport; const isSettled = ReportUtils.isSettled(moneyRequestReport.reportID); const isAdmin = Policy.isAdminOfFreePolicy([policy]) && ReportUtils.isExpenseReport(moneyRequestReport); const isRequestor = ReportUtils.isMoneyRequestReport(moneyRequestReport) && lodashGet(session, 'accountID', null) === parentReportAction.actorAccountID; diff --git a/src/libs/ReportActionsUtils.js b/src/libs/ReportActionsUtils.js index 527f732381bd..3c3b3c4b6b50 100644 --- a/src/libs/ReportActionsUtils.js +++ b/src/libs/ReportActionsUtils.js @@ -162,32 +162,6 @@ function getLinkedTransaction(reportAction = {}) { return allTransactions[`${ONYXKEYS.COLLECTION.TRANSACTION}${transactionID}`] || {}; } -/** - * Get the details linked to the IOU reportAction - * - * @param {Object} reportAction - * @returns {Object} - */ -function getMoneyRequestDetails(reportAction = {}) { - // 'pay' actions don't have a linked transaction since they pay the report. So we get the details from the originalMessage instead. - // 'split' actions have a linked transaction, but we don't return it from the backend since they are stored in an invisible report - see https://github.com/Expensify/Auth/blob/bdadc27d649df6feef51525cc58fa16c1619eee5/auth/lib/Transaction.h#L49-L51 - const originalMessage = lodashGet(reportAction, 'originalMessage', {}); - const type = lodashGet(originalMessage, 'type', ''); - if (type === CONST.IOU.REPORT_ACTION_TYPE.PAY || type === CONST.IOU.REPORT_ACTION_TYPE.SPLIT) { - return originalMessage; - } - - // Similarly, 'send' actions store their details in IOUDetails. - const iouDetails = lodashGet(originalMessage, 'IOUDetails'); - if (iouDetails) { - return iouDetails; - } - - // For all other actions, retrieve the details from the linked transaction. - const transaction = getLinkedTransaction(reportAction); - return {...transaction, comment: lodashGet(transaction, 'comment.comment', '')}; -} - /** * Determines if the given report action is sent money report action by checking for 'pay' type and presence of IOUDetails object. * From b31a09817f06f285101c873b1b0989d2d1981210 Mon Sep 17 00:00:00 2001 From: Carlos Martins Date: Mon, 14 Aug 2023 16:02:24 -0500 Subject: [PATCH 22/44] refactor SplitBillDetailsPage --- src/libs/ReportUtils.js | 4 ++-- src/pages/iou/SplitBillDetailsPage.js | 7 ++++--- 2 files changed, 6 insertions(+), 5 deletions(-) diff --git a/src/libs/ReportUtils.js b/src/libs/ReportUtils.js index 6962939cad9c..809eeae376e2 100644 --- a/src/libs/ReportUtils.js +++ b/src/libs/ReportUtils.js @@ -1249,8 +1249,8 @@ function getTransactionReportName(reportAction) { } return Localize.translateLocal(ReportActionsUtils.isSentMoneyReportAction(reportAction) ? 'iou.threadSentMoneyReportName' : 'iou.threadRequestReportName', { - formattedAmount: ReportActionsUtils.getFormattedAmount(reportAction), - comment: ReportActionsUtils.getMoneyRequestDetails.comment, + // formattedAmount: ReportActionsUtils.getFormattedAmount(reportAction), + // comment: ReportActionsUtils.getMoneyRequestDetails.comment, }); } diff --git a/src/pages/iou/SplitBillDetailsPage.js b/src/pages/iou/SplitBillDetailsPage.js index bff2fa370ac5..dcd4b20ce949 100644 --- a/src/pages/iou/SplitBillDetailsPage.js +++ b/src/pages/iou/SplitBillDetailsPage.js @@ -19,6 +19,7 @@ import FullPageNotFoundView from '../../components/BlockingViews/FullPageNotFoun import CONST from '../../CONST'; import HeaderWithBackButton from '../../components/HeaderWithBackButton'; import * as ReportActionsUtils from '../../libs/ReportActionsUtils'; +import * as TransactionUtils from '../../libs/TransactionUtils'; const propTypes = { /* Onyx Props */ @@ -61,9 +62,9 @@ function SplitBillDetailsPage(props) { ); const payeePersonalDetails = props.personalDetails[reportAction.actorAccountID]; const participantsExcludingPayee = _.filter(participants, (participant) => participant.accountID !== reportAction.actorAccountID); - const splitAmount = parseInt(lodashGet(transaction, 'amount', 0), 10); - const splitComment = lodashGet(transaction, 'comment.comment'); - const splitCurrency = lodashGet(transaction, 'currency'); + const splitAmount = TransactionUtils.getAmount(transaction, false); + const splitComment = TransactionUtils.getDescription(transaction); + const splitCurrency = TransactionUtils.getCurrency(transaction); return ( From e0f37f6fb919c0f00b2c059f737b114a9a253af4 Mon Sep 17 00:00:00 2001 From: Carlos Martins Date: Mon, 14 Aug 2023 16:02:37 -0500 Subject: [PATCH 23/44] rm unused import --- src/pages/iou/SplitBillDetailsPage.js | 1 - 1 file changed, 1 deletion(-) diff --git a/src/pages/iou/SplitBillDetailsPage.js b/src/pages/iou/SplitBillDetailsPage.js index dcd4b20ce949..b652fd14a1be 100644 --- a/src/pages/iou/SplitBillDetailsPage.js +++ b/src/pages/iou/SplitBillDetailsPage.js @@ -3,7 +3,6 @@ import _ from 'underscore'; import {View} from 'react-native'; import PropTypes from 'prop-types'; import {withOnyx} from 'react-native-onyx'; -import lodashGet from 'lodash/get'; import styles from '../../styles/styles'; import ONYXKEYS from '../../ONYXKEYS'; import * as OptionsListUtils from '../../libs/OptionsListUtils'; From cd0e7e4dd601f7dff823d0a6fd8907e7fd3019c4 Mon Sep 17 00:00:00 2001 From: Carlos Martins Date: Mon, 14 Aug 2023 16:13:56 -0500 Subject: [PATCH 24/44] update getTransactionReportName --- .../ReportActionItem/MoneyRequestAction.js | 2 +- src/libs/ReportActionsUtils.js | 13 ------------- src/libs/ReportUtils.js | 14 ++++++++++---- 3 files changed, 11 insertions(+), 18 deletions(-) diff --git a/src/components/ReportActionItem/MoneyRequestAction.js b/src/components/ReportActionItem/MoneyRequestAction.js index cc885fe897b2..3af96538c6c2 100644 --- a/src/components/ReportActionItem/MoneyRequestAction.js +++ b/src/components/ReportActionItem/MoneyRequestAction.js @@ -100,7 +100,7 @@ function MoneyRequestAction(props) { const participantAccountIDs = _.uniq([props.session.accountID, Number(props.action.actorAccountID)]); const thread = ReportUtils.buildOptimisticChatReport( participantAccountIDs, - ReportUtils.getTransactionReportName(props.action), + ReportUtils.getTransactionReportName(props.action, props.iouReport), '', CONST.POLICY.OWNER_EMAIL_FAKE, CONST.POLICY.OWNER_ACCOUNT_ID_FAKE, diff --git a/src/libs/ReportActionsUtils.js b/src/libs/ReportActionsUtils.js index 3c3b3c4b6b50..b21294dce418 100644 --- a/src/libs/ReportActionsUtils.js +++ b/src/libs/ReportActionsUtils.js @@ -177,17 +177,6 @@ function isSentMoneyReportAction(reportAction) { ); } -/** - * Returns the formatted amount of a money request. - * - * @param {Object} reportAction - * @returns {Number} - */ -function getFormattedAmount(reportAction) { - const moneyRequestDetails = getMoneyRequestDetails(reportAction); - return CurrencyUtils.convertToDisplayString(lodashGet(moneyRequestDetails, 'amount', 0), lodashGet(moneyRequestDetails, 'currency', '')); -} - /** * Returns whether the thread is a transaction thread, which is any thread with IOU parent * report action from requesting money (type - create) or from sending money (type - pay with IOUDetails field) @@ -626,7 +615,6 @@ export { getParentReportAction, getParentReportActionInReport, isTransactionThread, - getFormattedAmount, isSentMoneyReportAction, isDeletedParentAction, isReportPreviewAction, @@ -636,6 +624,5 @@ export { isWhisperAction, isPendingRemove, getReportAction, - getMoneyRequestDetails, getLinkedTransaction, }; diff --git a/src/libs/ReportUtils.js b/src/libs/ReportUtils.js index 809eeae376e2..6b6d9bc403e9 100644 --- a/src/libs/ReportUtils.js +++ b/src/libs/ReportUtils.js @@ -1241,16 +1241,22 @@ function getMoneyRequestReportName(report, policy = undefined) { * Given a parent IOU report action get report name for the LHN. * * @param {Object} reportAction + * @param {Object} report * @returns {String} */ -function getTransactionReportName(reportAction) { +function getTransactionReportName(reportAction, report) { if (ReportActionsUtils.isDeletedParentAction(reportAction)) { return Localize.translateLocal('parentReportAction.deletedRequest'); } + const transaction = ReportActionsUtils.getLinkedTransaction(reportAction); + const amount = TransactionUtils.getAmount(transaction, isExpenseReport(report)); + const currency = TransactionUtils.getCurrency(transaction); + const comment = TransactionUtils.getDescription(transaction); + return Localize.translateLocal(ReportActionsUtils.isSentMoneyReportAction(reportAction) ? 'iou.threadSentMoneyReportName' : 'iou.threadRequestReportName', { - // formattedAmount: ReportActionsUtils.getFormattedAmount(reportAction), - // comment: ReportActionsUtils.getMoneyRequestDetails.comment, + formattedAmount: CurrencyUtils.convertToDisplayString(amount, currency), + comment, }); } @@ -1388,7 +1394,7 @@ function getReportName(report, policy = undefined) { if (isChatThread(report)) { const parentReportAction = ReportActionsUtils.getParentReportAction(report); if (ReportActionsUtils.isTransactionThread(parentReportAction)) { - return getTransactionReportName(parentReportAction); + return getTransactionReportName(parentReportAction, report); } const isAttachment = _.has(parentReportAction, 'isAttachment') ? parentReportAction.isAttachment : isReportMessageAttachment(_.last(lodashGet(parentReportAction, 'message', [{}]))); From 94cba002cc712b3e5c1eb0686f3e583e51bc886a Mon Sep 17 00:00:00 2001 From: Carlos Martins Date: Mon, 14 Aug 2023 16:19:47 -0500 Subject: [PATCH 25/44] rm unused import --- src/libs/ReportActionsUtils.js | 1 - 1 file changed, 1 deletion(-) diff --git a/src/libs/ReportActionsUtils.js b/src/libs/ReportActionsUtils.js index b21294dce418..9b7f9324b29c 100644 --- a/src/libs/ReportActionsUtils.js +++ b/src/libs/ReportActionsUtils.js @@ -8,7 +8,6 @@ import * as CollectionUtils from './CollectionUtils'; import CONST from '../CONST'; import ONYXKEYS from '../ONYXKEYS'; import Log from './Log'; -import * as CurrencyUtils from './CurrencyUtils'; import isReportMessageAttachment from './isReportMessageAttachment'; const allReports = {}; From 7bfd673475b3127bf2eaaba2e305f058608c740a Mon Sep 17 00:00:00 2001 From: Carlos Martins Date: Mon, 14 Aug 2023 16:32:53 -0500 Subject: [PATCH 26/44] add default currency --- src/libs/TransactionUtils.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/libs/TransactionUtils.js b/src/libs/TransactionUtils.js index a05cd377514c..398d0671bd5f 100644 --- a/src/libs/TransactionUtils.js +++ b/src/libs/TransactionUtils.js @@ -140,7 +140,7 @@ function getCurrency(transaction) { if (currency) { return currency; } - return lodashGet(transaction, 'currency', ''); + return lodashGet(transaction, 'currency', CONST.CURRENCY.USD); } /** From 7629ba09a6d2379d4033f9d577f3a60dd7d61b9d Mon Sep 17 00:00:00 2001 From: Carlos Martins Date: Mon, 14 Aug 2023 16:41:21 -0500 Subject: [PATCH 27/44] use parentReport for transaction thread --- src/libs/ReportUtils.js | 29 +++++++++++++++-------------- 1 file changed, 15 insertions(+), 14 deletions(-) diff --git a/src/libs/ReportUtils.js b/src/libs/ReportUtils.js index 6b6d9bc403e9..2859834c862a 100644 --- a/src/libs/ReportUtils.js +++ b/src/libs/ReportUtils.js @@ -1382,6 +1382,19 @@ function getModifiedExpenseOriginalMessage(oldTransaction, transactionChanges, i return originalMessage; } +/** + * Returns the parentReport if the given report is a thread. + * + * @param {Object} report + * @returns {Object} + */ +function getParentReport(report) { + if (!report || !report.parentReportID) { + return {}; + } + return lodashGet(allReports, `${ONYXKEYS.COLLECTION.REPORT}${report.parentReportID}`, {}); +} + /** * Get the title for a report. * @@ -1394,7 +1407,8 @@ function getReportName(report, policy = undefined) { if (isChatThread(report)) { const parentReportAction = ReportActionsUtils.getParentReportAction(report); if (ReportActionsUtils.isTransactionThread(parentReportAction)) { - return getTransactionReportName(parentReportAction, report); + const parentReport = getParentReport(report); + return getTransactionReportName(parentReportAction, parentReport); } const isAttachment = _.has(parentReportAction, 'isAttachment') ? parentReportAction.isAttachment : isReportMessageAttachment(_.last(lodashGet(parentReportAction, 'message', [{}]))); @@ -2912,19 +2926,6 @@ function isReportDataReady() { return !_.isEmpty(allReports) && _.some(_.keys(allReports), (key) => allReports[key].reportID); } -/** - * Returns the parentReport if the given report is a thread. - * - * @param {Object} report - * @returns {Object} - */ -function getParentReport(report) { - if (!report || !report.parentReportID) { - return {}; - } - return lodashGet(allReports, `${ONYXKEYS.COLLECTION.REPORT}${report.parentReportID}`, {}); -} - /** * Find the parent report action in assignee report for a task report * Returns an empty object if assignee report is the same as the share destination report From ba3b7dfe9490c5d820502b87a1f64bc725db9334 Mon Sep 17 00:00:00 2001 From: Carlos Martins Date: Mon, 14 Aug 2023 16:49:49 -0500 Subject: [PATCH 28/44] rm getAllReportTransactions --- src/libs/ReportUtils.js | 9 --------- 1 file changed, 9 deletions(-) diff --git a/src/libs/ReportUtils.js b/src/libs/ReportUtils.js index 2859834c862a..9b612981e71b 100644 --- a/src/libs/ReportUtils.js +++ b/src/libs/ReportUtils.js @@ -3033,14 +3033,6 @@ function shouldDisableRename(report, policy) { return !_.keys(loginList).includes(policy.owner) && policy.role !== CONST.POLICY.ROLE.ADMIN; } -/** - * @param {String} reportID - * @returns {Array} - */ -function getAllReportTransactions(reportID) { - return _.filter(allTransactions, (transaction) => transaction.reportID === reportID); -} - /** * Returns the onyx data needed for the task assignee chat * @param {Number} accountID @@ -3281,6 +3273,5 @@ export { shouldDisableRename, hasSingleParticipant, getTransactionReportName, - getAllReportTransactions, getTaskAssigneeChatOnyxData, }; From 946b907f25cfb65482b5e8b2da2b01d16e7a7a24 Mon Sep 17 00:00:00 2001 From: Carlos Martins Date: Mon, 14 Aug 2023 16:55:57 -0500 Subject: [PATCH 29/44] rm allTransactions --- src/libs/ReportUtils.js | 13 ------------- 1 file changed, 13 deletions(-) diff --git a/src/libs/ReportUtils.js b/src/libs/ReportUtils.js index 9b612981e71b..854fa68f2ddd 100644 --- a/src/libs/ReportUtils.js +++ b/src/libs/ReportUtils.js @@ -89,19 +89,6 @@ Onyx.connect({ callback: (val) => (loginList = val), }); -let allTransactions = {}; -Onyx.connect({ - key: ONYXKEYS.COLLECTION.TRANSACTION, - waitForCollectionCallback: true, - callback: (val) => { - if (!val) { - return; - } - - allTransactions = val; - }, -}); - function getChatType(report) { return report ? report.chatType : ''; } From 63c86957bbafa35b16ea39ae1f560114ee7a042a Mon Sep 17 00:00:00 2001 From: Carlos Martins Date: Tue, 15 Aug 2023 11:18:24 -0500 Subject: [PATCH 30/44] add getTransactionDetails --- src/libs/ReportUtils.js | 37 +++++++++++++++++++++++++++---------- 1 file changed, 27 insertions(+), 10 deletions(-) diff --git a/src/libs/ReportUtils.js b/src/libs/ReportUtils.js index 854fa68f2ddd..8b86f59eecb1 100644 --- a/src/libs/ReportUtils.js +++ b/src/libs/ReportUtils.js @@ -1247,6 +1247,32 @@ function getTransactionReportName(reportAction, report) { }); } +/** + * Get the report for a reportID + * + * @param {String} reportID + * @returns {Object} + */ +function getReport(reportID) { + return lodashGet(allReports, `${ONYXKEYS.COLLECTION.REPORT}${reportID}`, {}); +} + +/** + * Gets transaction amount, currency and comment + * + * @param {Object} transaction + * @returns {Object} + */ +function getTransactionDetails(transaction) { + const reportID = transaction.reportID; + const report = getReport(reportID); + return { + amount: TransactionUtils.getAmount(transaction, isExpenseReport(report)), + currency: TransactionUtils.getCurrency(transaction), + comment: TransactionUtils.getDescription(transaction), + } +} + /** * Get money request message for an IOU report * @@ -1512,16 +1538,6 @@ function getParentNavigationSubtitle(report) { return {}; } -/** - * Get the report for a reportID - * - * @param {String} reportID - * @returns {Object} - */ -function getReport(reportID) { - return lodashGet(allReports, `${ONYXKEYS.COLLECTION.REPORT}${reportID}`, {}); -} - /** * Navigate to the details page of a given report * @@ -3260,5 +3276,6 @@ export { shouldDisableRename, hasSingleParticipant, getTransactionReportName, + getTransactionDetails, getTaskAssigneeChatOnyxData, }; From 4930a800c16e4808fbe28cdf3d0bb287a5d32746 Mon Sep 17 00:00:00 2001 From: Carlos Martins Date: Tue, 15 Aug 2023 11:33:24 -0500 Subject: [PATCH 31/44] refactor usages of transactionutils --- src/components/ReportActionItem/IOUPreview.js | 5 +- .../ReportActionItem/MoneyRequestAction.js | 2 +- .../ReportActionItem/MoneyRequestView.js | 5 +- src/libs/ReportUtils.js | 47 +++++++++---------- src/libs/actions/IOU.js | 11 ++--- src/pages/EditRequestPage.js | 4 +- src/pages/iou/SplitBillDetailsPage.js | 6 +-- 7 files changed, 33 insertions(+), 47 deletions(-) diff --git a/src/components/ReportActionItem/IOUPreview.js b/src/components/ReportActionItem/IOUPreview.js index 2e0d2a112ae1..313e0322fd83 100644 --- a/src/components/ReportActionItem/IOUPreview.js +++ b/src/components/ReportActionItem/IOUPreview.js @@ -29,7 +29,6 @@ import * as ReportUtils from '../../libs/ReportUtils'; import * as ReportActionsUtils from '../../libs/ReportActionsUtils'; import refPropTypes from '../refPropTypes'; import PressableWithFeedback from '../Pressable/PressableWithoutFeedback'; -import * as TransactionsUtils from '../../libs/TransactionUtils'; const propTypes = { /** The active IOUReport, used for Onyx subscription */ @@ -140,9 +139,7 @@ function IOUPreview(props) { const isCurrentUserManager = managerID === sessionAccountID; const transaction = ReportActionsUtils.getLinkedTransaction(props.action); - const requestAmount = TransactionsUtils.getAmount(transaction, ReportUtils.isExpenseReport(props.iouReport)); - const requestCurrency = TransactionsUtils.getCurrency(transaction); - const requestComment = TransactionsUtils.getDescription(transaction); + const {amount: requestAmount, currency: requestCurrency, comment: requestComment} = ReportUtils.getTransactionDetails(transaction); const getSettledMessage = () => { switch (lodashGet(props.action, 'originalMessage.paymentType', '')) { diff --git a/src/components/ReportActionItem/MoneyRequestAction.js b/src/components/ReportActionItem/MoneyRequestAction.js index 3af96538c6c2..cc885fe897b2 100644 --- a/src/components/ReportActionItem/MoneyRequestAction.js +++ b/src/components/ReportActionItem/MoneyRequestAction.js @@ -100,7 +100,7 @@ function MoneyRequestAction(props) { const participantAccountIDs = _.uniq([props.session.accountID, Number(props.action.actorAccountID)]); const thread = ReportUtils.buildOptimisticChatReport( participantAccountIDs, - ReportUtils.getTransactionReportName(props.action, props.iouReport), + ReportUtils.getTransactionReportName(props.action), '', CONST.POLICY.OWNER_EMAIL_FAKE, CONST.POLICY.OWNER_ACCOUNT_ID_FAKE, diff --git a/src/components/ReportActionItem/MoneyRequestView.js b/src/components/ReportActionItem/MoneyRequestView.js index 2794916a8c48..938d6419d57b 100644 --- a/src/components/ReportActionItem/MoneyRequestView.js +++ b/src/components/ReportActionItem/MoneyRequestView.js @@ -23,7 +23,6 @@ import * as CurrencyUtils from '../../libs/CurrencyUtils'; import EmptyStateBackgroundImage from '../../../assets/images/empty-state_background-fade.png'; import useLocalize from '../../hooks/useLocalize'; import useWindowDimensions from '../../hooks/useWindowDimensions'; -import * as TransactionUtils from '../../libs/TransactionUtils'; const propTypes = { /** The report currently being looked at */ @@ -68,9 +67,7 @@ function MoneyRequestView({report, parentReport, shouldShowHorizontalRule, polic const parentReportAction = ReportActionsUtils.getParentReportAction(report); const moneyRequestReport = parentReport; const transaction = ReportActionsUtils.getLinkedTransaction(parentReportAction); - const transactionAmount = TransactionUtils.getAmount(transaction, ReportUtils.isExpenseReport(parentReport)); - const transactionCurrency = TransactionUtils.getCurrency(transaction); - const transactionDescription = TransactionUtils.getDescription(transaction); + const {amount: transactionAmount, currency: transactionCurrency, description: transactionDescription} = ReportUtils.getTransactionDetails(transaction); const formattedTransactionAmount = transactionAmount && transactionCurrency && CurrencyUtils.convertToDisplayString(transactionAmount, transactionCurrency); const transactionDate = lodashGet(parentReportAction, ['created']); diff --git a/src/libs/ReportUtils.js b/src/libs/ReportUtils.js index 8b86f59eecb1..2f7ca2df041f 100644 --- a/src/libs/ReportUtils.js +++ b/src/libs/ReportUtils.js @@ -1224,29 +1224,6 @@ function getMoneyRequestReportName(report, policy = undefined) { return payerPaidAmountMesssage; } -/** - * Given a parent IOU report action get report name for the LHN. - * - * @param {Object} reportAction - * @param {Object} report - * @returns {String} - */ -function getTransactionReportName(reportAction, report) { - if (ReportActionsUtils.isDeletedParentAction(reportAction)) { - return Localize.translateLocal('parentReportAction.deletedRequest'); - } - - const transaction = ReportActionsUtils.getLinkedTransaction(reportAction); - const amount = TransactionUtils.getAmount(transaction, isExpenseReport(report)); - const currency = TransactionUtils.getCurrency(transaction); - const comment = TransactionUtils.getDescription(transaction); - - return Localize.translateLocal(ReportActionsUtils.isSentMoneyReportAction(reportAction) ? 'iou.threadSentMoneyReportName' : 'iou.threadRequestReportName', { - formattedAmount: CurrencyUtils.convertToDisplayString(amount, currency), - comment, - }); -} - /** * Get the report for a reportID * @@ -1267,12 +1244,33 @@ function getTransactionDetails(transaction) { const reportID = transaction.reportID; const report = getReport(reportID); return { + created: TransactionUtils.getCreated(transaction), amount: TransactionUtils.getAmount(transaction, isExpenseReport(report)), currency: TransactionUtils.getCurrency(transaction), comment: TransactionUtils.getDescription(transaction), } } +/** + * Given a parent IOU report action get report name for the LHN. + * + * @param {Object} reportAction + * @returns {String} + */ +function getTransactionReportName(reportAction) { + if (ReportActionsUtils.isDeletedParentAction(reportAction)) { + return Localize.translateLocal('parentReportAction.deletedRequest'); + } + + const transaction = ReportActionsUtils.getLinkedTransaction(reportAction); + const {amount, currency, comment} = getTransactionDetails(transaction); + + return Localize.translateLocal(ReportActionsUtils.isSentMoneyReportAction(reportAction) ? 'iou.threadSentMoneyReportName' : 'iou.threadRequestReportName', { + formattedAmount: CurrencyUtils.convertToDisplayString(amount, currency), + comment, + }); +} + /** * Get money request message for an IOU report * @@ -1420,8 +1418,7 @@ function getReportName(report, policy = undefined) { if (isChatThread(report)) { const parentReportAction = ReportActionsUtils.getParentReportAction(report); if (ReportActionsUtils.isTransactionThread(parentReportAction)) { - const parentReport = getParentReport(report); - return getTransactionReportName(parentReportAction, parentReport); + return getTransactionReportName(parentReportAction); } const isAttachment = _.has(parentReportAction, 'isAttachment') ? parentReportAction.isAttachment : isReportMessageAttachment(_.last(lodashGet(parentReportAction, 'message', [{}]))); diff --git a/src/libs/actions/IOU.js b/src/libs/actions/IOU.js index e36444f0bea0..2578b0036326 100644 --- a/src/libs/actions/IOU.js +++ b/src/libs/actions/IOU.js @@ -857,17 +857,16 @@ function editMoneyRequest(transactionID, transactionThreadReportID, transactionC ]; // STEP 6: Call the API endpoint + const {created, amount, currency, comment} = ReportUtils.getTransactionDetails(transaction); API.write( 'EditMoneyRequest', { transactionID, reportActionID: updatedReportAction.reportActionID, - - // Using the getter methods here to ensure we pass modified field if present - created: TransactionUtils.getCreated(updatedTransaction), - amount: TransactionUtils.getAmount(updatedTransaction, isFromExpenseReport), - currency: TransactionUtils.getCurrency(updatedTransaction), - comment: TransactionUtils.getDescription(updatedTransaction), + created, + amount, + currency, + comment, }, {optimisticData, successData, failureData}, ); diff --git a/src/pages/EditRequestPage.js b/src/pages/EditRequestPage.js index 971ad056ae7e..445f10c1812e 100644 --- a/src/pages/EditRequestPage.js +++ b/src/pages/EditRequestPage.js @@ -40,9 +40,7 @@ const defaultProps = { function EditRequestPage({report, route}) { const transactionID = lodashGet(ReportActionsUtils.getParentReportAction(report), 'originalMessage.IOUTransactionID', ''); const transaction = TransactionUtils.getTransaction(transactionID); - const transactionDescription = TransactionUtils.getDescription(transaction); - const transactionAmount = TransactionUtils.getAmount(transaction, ReportUtils.isExpenseReport(ReportUtils.getParentReport(report))); - const transactionCurrency = TransactionUtils.getCurrency(transaction); + const {amount: transactionAmount, currency: transactionCurrency, comment: transactionDescription} = ReportUtils.getTransactionDetails(transaction); // Take only the YYYY-MM-DD value const transactionCreatedDate = new Date(TransactionUtils.getCreated(transaction)); diff --git a/src/pages/iou/SplitBillDetailsPage.js b/src/pages/iou/SplitBillDetailsPage.js index b652fd14a1be..4d5be4786ca5 100644 --- a/src/pages/iou/SplitBillDetailsPage.js +++ b/src/pages/iou/SplitBillDetailsPage.js @@ -18,7 +18,7 @@ import FullPageNotFoundView from '../../components/BlockingViews/FullPageNotFoun import CONST from '../../CONST'; import HeaderWithBackButton from '../../components/HeaderWithBackButton'; import * as ReportActionsUtils from '../../libs/ReportActionsUtils'; -import * as TransactionUtils from '../../libs/TransactionUtils'; +import * as ReportUtils from '../../libs/ReportUtils'; const propTypes = { /* Onyx Props */ @@ -61,9 +61,7 @@ function SplitBillDetailsPage(props) { ); const payeePersonalDetails = props.personalDetails[reportAction.actorAccountID]; const participantsExcludingPayee = _.filter(participants, (participant) => participant.accountID !== reportAction.actorAccountID); - const splitAmount = TransactionUtils.getAmount(transaction, false); - const splitComment = TransactionUtils.getDescription(transaction); - const splitCurrency = TransactionUtils.getCurrency(transaction); + const {amount: splitAmount, currency: splitCurrency, comment: splitComment} = ReportUtils.getTransactionDetails(transaction); return ( From a1aef0359e3beb3d5ce2e119b176aa0e57478940 Mon Sep 17 00:00:00 2001 From: Carlos Martins Date: Tue, 15 Aug 2023 11:38:35 -0500 Subject: [PATCH 32/44] fix style --- src/libs/ReportUtils.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/libs/ReportUtils.js b/src/libs/ReportUtils.js index 2f7ca2df041f..4af8cac5ed5d 100644 --- a/src/libs/ReportUtils.js +++ b/src/libs/ReportUtils.js @@ -1237,7 +1237,7 @@ function getReport(reportID) { /** * Gets transaction amount, currency and comment * - * @param {Object} transaction + * @param {Object} transaction * @returns {Object} */ function getTransactionDetails(transaction) { @@ -1248,7 +1248,7 @@ function getTransactionDetails(transaction) { amount: TransactionUtils.getAmount(transaction, isExpenseReport(report)), currency: TransactionUtils.getCurrency(transaction), comment: TransactionUtils.getDescription(transaction), - } + }; } /** From e9b05e28ce5c02db473196030c75a46d4ab0336d Mon Sep 17 00:00:00 2001 From: Carlos Martins Date: Tue, 15 Aug 2023 11:57:24 -0500 Subject: [PATCH 33/44] move getLinkedTransaction to TransactionUtils; --- src/components/ReportActionItem/IOUPreview.js | 4 +-- .../ReportActionItem/MoneyRequestView.js | 3 ++- src/libs/IOUUtils.js | 2 +- src/libs/ReportActionsUtils.js | 25 ------------------- src/libs/ReportUtils.js | 2 +- src/libs/TransactionUtils.js | 13 +++++++++- src/pages/iou/SplitBillDetailsPage.js | 4 +-- 7 files changed, 20 insertions(+), 33 deletions(-) diff --git a/src/components/ReportActionItem/IOUPreview.js b/src/components/ReportActionItem/IOUPreview.js index 313e0322fd83..371343f12fda 100644 --- a/src/components/ReportActionItem/IOUPreview.js +++ b/src/components/ReportActionItem/IOUPreview.js @@ -26,7 +26,7 @@ import * as OptionsListUtils from '../../libs/OptionsListUtils'; import * as CurrencyUtils from '../../libs/CurrencyUtils'; import * as IOUUtils from '../../libs/IOUUtils'; import * as ReportUtils from '../../libs/ReportUtils'; -import * as ReportActionsUtils from '../../libs/ReportActionsUtils'; +import * as TransactionUtils from '../../libs/TransactionUtils'; import refPropTypes from '../refPropTypes'; import PressableWithFeedback from '../Pressable/PressableWithoutFeedback'; @@ -138,7 +138,7 @@ function IOUPreview(props) { // Pay button should only be visible to the manager of the report. const isCurrentUserManager = managerID === sessionAccountID; - const transaction = ReportActionsUtils.getLinkedTransaction(props.action); + const transaction = TransactionUtils.getLinkedTransaction(props.action); const {amount: requestAmount, currency: requestCurrency, comment: requestComment} = ReportUtils.getTransactionDetails(transaction); const getSettledMessage = () => { diff --git a/src/components/ReportActionItem/MoneyRequestView.js b/src/components/ReportActionItem/MoneyRequestView.js index 938d6419d57b..f02a835a4ad6 100644 --- a/src/components/ReportActionItem/MoneyRequestView.js +++ b/src/components/ReportActionItem/MoneyRequestView.js @@ -14,6 +14,7 @@ import MenuItemWithTopDescription from '../MenuItemWithTopDescription'; import styles from '../../styles/styles'; import * as ReportUtils from '../../libs/ReportUtils'; import * as ReportActionsUtils from '../../libs/ReportActionsUtils'; +import * as TransactionUtils from '../../libs/TransactionUtils'; import * as StyleUtils from '../../styles/StyleUtils'; import CONST from '../../CONST'; import * as Expensicons from '../Icon/Expensicons'; @@ -66,7 +67,7 @@ function MoneyRequestView({report, parentReport, shouldShowHorizontalRule, polic const parentReportAction = ReportActionsUtils.getParentReportAction(report); const moneyRequestReport = parentReport; - const transaction = ReportActionsUtils.getLinkedTransaction(parentReportAction); + const transaction = TransactionUtils.getLinkedTransaction(parentReportAction); const {amount: transactionAmount, currency: transactionCurrency, description: transactionDescription} = ReportUtils.getTransactionDetails(transaction); const formattedTransactionAmount = transactionAmount && transactionCurrency && CurrencyUtils.convertToDisplayString(transactionAmount, transactionCurrency); diff --git a/src/libs/IOUUtils.js b/src/libs/IOUUtils.js index a3ade416c45a..6d0dbf994475 100644 --- a/src/libs/IOUUtils.js +++ b/src/libs/IOUUtils.js @@ -80,7 +80,7 @@ function getIOUReportActions(reportActions, iouReport, type = '', pendingAction .filter((action) => action.originalMessage.IOUReportID.toString() === iouReport.reportID.toString()) .filter((action) => (!_.isEmpty(pendingAction) ? action.pendingAction === pendingAction : true)) .filter((action) => { - const transaction = ReportActionsUtils.getLinkedTransaction(action); + const transaction = TransactionUtils.getLinkedTransaction(action); const currency = TransactionUtils.getCurrency(transaction); return filterRequestsInDifferentCurrency ? currency && currency !== iouReport.currency : true; }) diff --git a/src/libs/ReportActionsUtils.js b/src/libs/ReportActionsUtils.js index 9b7f9324b29c..e5fe1437512e 100644 --- a/src/libs/ReportActionsUtils.js +++ b/src/libs/ReportActionsUtils.js @@ -36,19 +36,6 @@ Onyx.connect({ }, }); -let allTransactions = {}; -Onyx.connect({ - key: ONYXKEYS.COLLECTION.TRANSACTION, - waitForCollectionCallback: true, - callback: (val) => { - if (!val) { - return; - } - - allTransactions = val; - }, -}); - let isNetworkOffline = false; Onyx.connect({ key: ONYXKEYS.NETWORK, @@ -150,17 +137,6 @@ function getParentReportActionInReport(childReportID, parentReportID) { return _.find(allReportActions[parentReportID], (reportAction) => reportAction && `${reportAction.childReportID}` === `${childReportID}`); } -/** - * Get the details linked to the IOU reportAction - * - * @param {Object} reportAction - * @returns {Object} - */ -function getLinkedTransaction(reportAction = {}) { - const transactionID = lodashGet(reportAction, ['originalMessage', 'IOUTransactionID'], ''); - return allTransactions[`${ONYXKEYS.COLLECTION.TRANSACTION}${transactionID}`] || {}; -} - /** * Determines if the given report action is sent money report action by checking for 'pay' type and presence of IOUDetails object. * @@ -623,5 +599,4 @@ export { isWhisperAction, isPendingRemove, getReportAction, - getLinkedTransaction, }; diff --git a/src/libs/ReportUtils.js b/src/libs/ReportUtils.js index 4af8cac5ed5d..cb65b4a4924c 100644 --- a/src/libs/ReportUtils.js +++ b/src/libs/ReportUtils.js @@ -1262,7 +1262,7 @@ function getTransactionReportName(reportAction) { return Localize.translateLocal('parentReportAction.deletedRequest'); } - const transaction = ReportActionsUtils.getLinkedTransaction(reportAction); + const transaction = TransactionUtils.getLinkedTransaction(reportAction); const {amount, currency, comment} = getTransactionDetails(transaction); return Localize.translateLocal(ReportActionsUtils.isSentMoneyReportAction(reportAction) ? 'iou.threadSentMoneyReportName' : 'iou.threadRequestReportName', { diff --git a/src/libs/TransactionUtils.js b/src/libs/TransactionUtils.js index 398d0671bd5f..044661777d30 100644 --- a/src/libs/TransactionUtils.js +++ b/src/libs/TransactionUtils.js @@ -157,4 +157,15 @@ function getCreated(transaction) { return format(new Date(lodashGet(transaction, 'created', '')), CONST.DATE.FNS_FORMAT_STRING); } -export {buildOptimisticTransaction, getUpdatedTransaction, getTransaction, getDescription, getAmount, getCurrency, getCreated}; +/** + * Get the details linked to the IOU reportAction + * + * @param {Object} reportAction + * @returns {Object} + */ +function getLinkedTransaction(reportAction = {}) { + const transactionID = lodashGet(reportAction, ['originalMessage', 'IOUTransactionID'], ''); + return allTransactions[`${ONYXKEYS.COLLECTION.TRANSACTION}${transactionID}`] || {}; +} + +export {buildOptimisticTransaction, getUpdatedTransaction, getTransaction, getDescription, getAmount, getCurrency, getCreated, getLinkedTransaction}; diff --git a/src/pages/iou/SplitBillDetailsPage.js b/src/pages/iou/SplitBillDetailsPage.js index 4d5be4786ca5..9e67d7ea2a87 100644 --- a/src/pages/iou/SplitBillDetailsPage.js +++ b/src/pages/iou/SplitBillDetailsPage.js @@ -17,7 +17,7 @@ import withReportAndReportActionOrNotFound from '../home/report/withReportAndRep import FullPageNotFoundView from '../../components/BlockingViews/FullPageNotFoundView'; import CONST from '../../CONST'; import HeaderWithBackButton from '../../components/HeaderWithBackButton'; -import * as ReportActionsUtils from '../../libs/ReportActionsUtils'; +import * as TransactionUtils from '../../libs/TransactionUtils'; import * as ReportUtils from '../../libs/ReportUtils'; const propTypes = { @@ -53,7 +53,7 @@ const defaultProps = { function SplitBillDetailsPage(props) { const reportAction = props.reportActions[`${props.route.params.reportActionID.toString()}`]; - const transaction = ReportActionsUtils.getLinkedTransaction(reportAction); + const transaction = TransactionUtils.getLinkedTransaction(reportAction); const participantAccountIDs = reportAction.originalMessage.participantAccountIDs; const participants = OptionsListUtils.getParticipantsOptions( _.map(participantAccountIDs, (accountID) => ({accountID, selected: true})), From 5356802b00465841df3f747f04a05193a1c7de2f Mon Sep 17 00:00:00 2001 From: Carlos Martins Date: Tue, 15 Aug 2023 11:59:28 -0500 Subject: [PATCH 34/44] update comments --- src/libs/ReportUtils.js | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/src/libs/ReportUtils.js b/src/libs/ReportUtils.js index cb65b4a4924c..ad268da0b2e9 100644 --- a/src/libs/ReportUtils.js +++ b/src/libs/ReportUtils.js @@ -1225,7 +1225,7 @@ function getMoneyRequestReportName(report, policy = undefined) { } /** - * Get the report for a reportID + * Get the report given a reportID * * @param {String} reportID * @returns {Object} @@ -1235,14 +1235,13 @@ function getReport(reportID) { } /** - * Gets transaction amount, currency and comment + * Gets transaction created, amount, currency and comment * * @param {Object} transaction * @returns {Object} */ function getTransactionDetails(transaction) { - const reportID = transaction.reportID; - const report = getReport(reportID); + const report = getReport(transaction.reportID); return { created: TransactionUtils.getCreated(transaction), amount: TransactionUtils.getAmount(transaction, isExpenseReport(report)), From 7c72468a9c527351acaab9b1f40ecea6b7d3bd58 Mon Sep 17 00:00:00 2001 From: Carlos Martins Date: Tue, 15 Aug 2023 12:35:52 -0500 Subject: [PATCH 35/44] create getAllReportTransactions, refactor isIOUReportPendingCurrencyConversion --- src/libs/IOUUtils.js | 32 ++++---------------------------- src/libs/TransactionUtils.js | 6 +++++- 2 files changed, 9 insertions(+), 29 deletions(-) diff --git a/src/libs/IOUUtils.js b/src/libs/IOUUtils.js index 6d0dbf994475..64aedb4770c7 100644 --- a/src/libs/IOUUtils.js +++ b/src/libs/IOUUtils.js @@ -91,37 +91,13 @@ function getIOUReportActions(reportActions, iouReport, type = '', pendingAction * Returns whether or not an IOU report contains money requests in a different currency * that are either created or cancelled offline, and thus haven't been converted to the report's currency yet * - * @param {Array} reportActions * @param {Object} iouReport - * * @returns {Boolean} */ -function isIOUReportPendingCurrencyConversion(reportActions, iouReport) { - // Pending money requests that are in a different currency - const pendingRequestsInDifferentCurrency = _.chain(getIOUReportActions(reportActions, iouReport, CONST.IOU.REPORT_ACTION_TYPE.CREATE, CONST.RED_BRICK_ROAD_PENDING_ACTION.ADD, true)) - .map((action) => action.originalMessage.IOUTransactionID) - .sort() - .value(); - - // Pending deleted money requests that are in a different currency - const pendingDeletedRequestsInDifferentCurrency = _.chain( - getIOUReportActions(reportActions, iouReport, CONST.IOU.REPORT_ACTION_TYPE.DELETE, CONST.RED_BRICK_ROAD_PENDING_ACTION.ADD, true), - ) - .map((action) => action.originalMessage.IOUTransactionID) - .sort() - .value(); - - const hasPendingRequests = Boolean(pendingRequestsInDifferentCurrency.length || pendingDeletedRequestsInDifferentCurrency.length); - - // If we have pending money requests made offline, check if all of them have been cancelled offline - // In order to do that, we can grab transactionIDs of all the created and cancelled money requests and check if they're identical - if (hasPendingRequests && _.isEqual(pendingRequestsInDifferentCurrency, pendingDeletedRequestsInDifferentCurrency)) { - return false; - } - - // Not all requests made offline had been cancelled, - // simply return if we have any pending created or cancelled requests - return hasPendingRequests; +function isIOUReportPendingCurrencyConversion(iouReport) { + const reportTransactions = TransactionUtils.getAllReportTransactions(iouReport.reportID); + const pendingRequestsInDifferentCurrency = _.filter(reportTransactions, transaction => transaction.pendingAction && TransactionUtils.getCurrency(transaction) !== iouReport.currency); + return pendingRequestsInDifferentCurrency.length > 0; } /** diff --git a/src/libs/TransactionUtils.js b/src/libs/TransactionUtils.js index 044661777d30..cb90fcfe904e 100644 --- a/src/libs/TransactionUtils.js +++ b/src/libs/TransactionUtils.js @@ -168,4 +168,8 @@ function getLinkedTransaction(reportAction = {}) { return allTransactions[`${ONYXKEYS.COLLECTION.TRANSACTION}${transactionID}`] || {}; } -export {buildOptimisticTransaction, getUpdatedTransaction, getTransaction, getDescription, getAmount, getCurrency, getCreated, getLinkedTransaction}; +function getAllReportTransactions(reportID) { + _.filter(allTransactions, transaction => transaction.reportID === reportID); +} + +export {buildOptimisticTransaction, getUpdatedTransaction, getTransaction, getDescription, getAmount, getCurrency, getCreated, getLinkedTransaction, getAllReportTransactions}; From c70f1933cd6194054773a9155d7939b1751b27df Mon Sep 17 00:00:00 2001 From: Carlos Martins Date: Tue, 15 Aug 2023 13:29:51 -0500 Subject: [PATCH 36/44] fix tests --- src/libs/TransactionUtils.js | 2 +- tests/unit/IOUUtilsTest.js | 145 +++++++++-------------------------- 2 files changed, 38 insertions(+), 109 deletions(-) diff --git a/src/libs/TransactionUtils.js b/src/libs/TransactionUtils.js index cb90fcfe904e..ef8e95ffbd6c 100644 --- a/src/libs/TransactionUtils.js +++ b/src/libs/TransactionUtils.js @@ -169,7 +169,7 @@ function getLinkedTransaction(reportAction = {}) { } function getAllReportTransactions(reportID) { - _.filter(allTransactions, transaction => transaction.reportID === reportID); + return _.filter(allTransactions, transaction => transaction.reportID === reportID); } export {buildOptimisticTransaction, getUpdatedTransaction, getTransaction, getDescription, getAmount, getCurrency, getCreated, getLinkedTransaction, getAllReportTransactions}; diff --git a/tests/unit/IOUUtilsTest.js b/tests/unit/IOUUtilsTest.js index 1fa7974e762d..cf3ed0a79af2 100644 --- a/tests/unit/IOUUtilsTest.js +++ b/tests/unit/IOUUtilsTest.js @@ -1,39 +1,10 @@ import Onyx from 'react-native-onyx'; import * as IOUUtils from '../../src/libs/IOUUtils'; import * as ReportUtils from '../../src/libs/ReportUtils'; -import * as NumberUtils from '../../src/libs/NumberUtils'; -import CONST from '../../src/CONST'; import ONYXKEYS from '../../src/ONYXKEYS'; import waitForPromisesToResolve from '../utils/waitForPromisesToResolve'; import currencyList from './currencyList.json'; - -let iouReport; -let reportActions; -const ownerAccountID = 5; -const managerEmail = 'manager@iou.com'; -const managerID = 10; - -function createIOUReportAction(type, amount, currency, isOffline = false, IOUTransactionID = NumberUtils.rand64()) { - const moneyRequestAction = ReportUtils.buildOptimisticIOUReportAction(type, amount, currency, 'Test comment', [managerEmail], IOUTransactionID, '', iouReport.reportID); - - // Default is to create requests online, if `isOffline` is not specified then we need to remove the pendingAction - if (!isOffline) { - moneyRequestAction.pendingAction = null; - } - - reportActions.push(moneyRequestAction); - return moneyRequestAction; -} - -function deleteMoneyRequest(moneyRequestAction, isOffline = false) { - createIOUReportAction( - CONST.IOU.REPORT_ACTION_TYPE.DELETE, - moneyRequestAction.originalMessage.amount, - moneyRequestAction.originalMessage.currency, - isOffline, - moneyRequestAction.originalMessage.IOUTransactionID, - ); -} +import * as TransactionUtils from '../../src/libs/TransactionUtils'; function initCurrencyList() { Onyx.init({ @@ -47,90 +18,48 @@ function initCurrencyList() { describe('IOUUtils', () => { describe('isIOUReportPendingCurrencyConversion', () => { - beforeEach(() => { - reportActions = []; - const chatReportID = ReportUtils.generateReportID(); - const amount = 1000; - const currency = 'USD'; - - iouReport = ReportUtils.buildOptimisticIOUReport(ownerAccountID, managerID, amount, chatReportID, currency); - - // 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 online, unless isOffline is specified - createIOUReportAction('create', amount, currency); + beforeAll(() => { + Onyx.init({ + keys: ONYXKEYS, + }); }); test('Requesting money offline in a different currency will show the pending conversion message', () => { - // Request money offline in AED - createIOUReportAction('create', 100, 'AED', true); - - // 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 deleted', () => { - // Create two requests offline - const moneyRequestA = createIOUReportAction('create', 1000, 'AED', true); - const moneyRequestB = createIOUReportAction('create', 1000, 'AED', true); - - // Delete both requests - deleteMoneyRequest(moneyRequestA, true); - deleteMoneyRequest(moneyRequestB, true); - - // Both requests made offline have been deleted, total won't update so no need to show a pending conversion message - expect(IOUUtils.isIOUReportPendingCurrencyConversion(reportActions, iouReport)).toBe(false); + const iouReport = ReportUtils.buildOptimisticIOUReport(1, 2, 100, 1, 'USD'); + const usdPendingTransaction = TransactionUtils.buildOptimisticTransaction(100, 'USD', iouReport.reportID); + const aedPendingTransaction = TransactionUtils.buildOptimisticTransaction(100, 'AED', iouReport.reportID); + + return Onyx.mergeCollection(ONYXKEYS.COLLECTION.TRANSACTION, { + [`${ONYXKEYS.COLLECTION.TRANSACTION}${usdPendingTransaction.transactionID}`]: usdPendingTransaction, + [`${ONYXKEYS.COLLECTION.TRANSACTION}${aedPendingTransaction.transactionID}`]: aedPendingTransaction, + }) + .then(() => { + // 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(iouReport)).toBe(true); + }); }); - test('Deleting a request made online shows the preview', () => { - // Request money online in AED - const moneyRequest = createIOUReportAction('create', 1000, 'AED'); - - // Delete it offline - deleteMoneyRequest(moneyRequest, true); - - // 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("Deleting 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'); - - // Another request offline - const moneyRequestOffline = createIOUReportAction('create', 1000, 'AED', true); - - // Delete the request made offline - deleteMoneyRequest(moneyRequestOffline, true); - - expect(IOUUtils.isIOUReportPendingCurrencyConversion(reportActions, iouReport)).toBe(false); - }); - - test('Deleting 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'); - - // Request money again but offline - createIOUReportAction('create', 1000, 'AED', true); - - // Delete the request made online - deleteMoneyRequest(moneyRequestOnline, true); - - // 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("Deleting 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 currency (USD) - const onlineMoneyRequestInUSD = createIOUReportAction('create', 1000, 'USD'); - - // Request money online in a different currency - createIOUReportAction('create', 2000, 'AED'); - - // Delete the USD request offline - deleteMoneyRequest(onlineMoneyRequestInUSD, true); - - expect(IOUUtils.isIOUReportPendingCurrencyConversion(reportActions, iouReport)).toBe(false); + test('Requesting money online in a different currency will not show the pending conversion message', () => { + const iouReport = ReportUtils.buildOptimisticIOUReport(2, 3, 100, 1, 'USD'); + const usdPendingTransaction = TransactionUtils.buildOptimisticTransaction(100, 'USD', iouReport.reportID); + const aedPendingTransaction = TransactionUtils.buildOptimisticTransaction(100, 'AED', iouReport.reportID); + + return Onyx.mergeCollection(ONYXKEYS.COLLECTION.TRANSACTION, { + [`${ONYXKEYS.COLLECTION.TRANSACTION}${usdPendingTransaction.transactionID}`]: { + ...usdPendingTransaction, + pendingAction: null, + }, + [`${ONYXKEYS.COLLECTION.TRANSACTION}${aedPendingTransaction.transactionID}`]: { + ...aedPendingTransaction, + pendingAction: null, + }, + }) + .then(() => { + // We requested money online in a different currency, we know the iouReport total and there's no need to show the pending conversion message + expect(IOUUtils.isIOUReportPendingCurrencyConversion(iouReport)).toBe(false); + }); }); + }); describe('calculateAmount', () => { From 98dc4a0595771f9afbe3479c5c1f95e0f0efca7c Mon Sep 17 00:00:00 2001 From: Carlos Martins Date: Tue, 15 Aug 2023 13:33:19 -0500 Subject: [PATCH 37/44] fix styles --- src/libs/IOUUtils.js | 2 +- src/libs/TransactionUtils.js | 2 +- tests/unit/IOUUtilsTest.js | 19 ++++++++----------- 3 files changed, 10 insertions(+), 13 deletions(-) diff --git a/src/libs/IOUUtils.js b/src/libs/IOUUtils.js index 64aedb4770c7..f122e6eef46e 100644 --- a/src/libs/IOUUtils.js +++ b/src/libs/IOUUtils.js @@ -96,7 +96,7 @@ function getIOUReportActions(reportActions, iouReport, type = '', pendingAction */ function isIOUReportPendingCurrencyConversion(iouReport) { const reportTransactions = TransactionUtils.getAllReportTransactions(iouReport.reportID); - const pendingRequestsInDifferentCurrency = _.filter(reportTransactions, transaction => transaction.pendingAction && TransactionUtils.getCurrency(transaction) !== iouReport.currency); + const pendingRequestsInDifferentCurrency = _.filter(reportTransactions, (transaction) => transaction.pendingAction && TransactionUtils.getCurrency(transaction) !== iouReport.currency); return pendingRequestsInDifferentCurrency.length > 0; } diff --git a/src/libs/TransactionUtils.js b/src/libs/TransactionUtils.js index ef8e95ffbd6c..e4bcbd8ac1c3 100644 --- a/src/libs/TransactionUtils.js +++ b/src/libs/TransactionUtils.js @@ -169,7 +169,7 @@ function getLinkedTransaction(reportAction = {}) { } function getAllReportTransactions(reportID) { - return _.filter(allTransactions, transaction => transaction.reportID === reportID); + return _.filter(allTransactions, (transaction) => transaction.reportID === reportID); } export {buildOptimisticTransaction, getUpdatedTransaction, getTransaction, getDescription, getAmount, getCurrency, getCreated, getLinkedTransaction, getAllReportTransactions}; diff --git a/tests/unit/IOUUtilsTest.js b/tests/unit/IOUUtilsTest.js index cf3ed0a79af2..9696cdea92d6 100644 --- a/tests/unit/IOUUtilsTest.js +++ b/tests/unit/IOUUtilsTest.js @@ -32,11 +32,10 @@ describe('IOUUtils', () => { return Onyx.mergeCollection(ONYXKEYS.COLLECTION.TRANSACTION, { [`${ONYXKEYS.COLLECTION.TRANSACTION}${usdPendingTransaction.transactionID}`]: usdPendingTransaction, [`${ONYXKEYS.COLLECTION.TRANSACTION}${aedPendingTransaction.transactionID}`]: aedPendingTransaction, - }) - .then(() => { - // 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(iouReport)).toBe(true); - }); + }).then(() => { + // 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(iouReport)).toBe(true); + }); }); test('Requesting money online in a different currency will not show the pending conversion message', () => { @@ -53,13 +52,11 @@ describe('IOUUtils', () => { ...aedPendingTransaction, pendingAction: null, }, - }) - .then(() => { - // We requested money online in a different currency, we know the iouReport total and there's no need to show the pending conversion message - expect(IOUUtils.isIOUReportPendingCurrencyConversion(iouReport)).toBe(false); - }); + }).then(() => { + // We requested money online in a different currency, we know the iouReport total and there's no need to show the pending conversion message + expect(IOUUtils.isIOUReportPendingCurrencyConversion(iouReport)).toBe(false); + }); }); - }); describe('calculateAmount', () => { From 079a0e837cda636b69f39f5c9a873eb2822ec87a Mon Sep 17 00:00:00 2001 From: Carlos Martins Date: Tue, 15 Aug 2023 13:51:26 -0500 Subject: [PATCH 38/44] rm getIOUReportActions --- src/libs/IOUUtils.js | 27 +-------------------------- 1 file changed, 1 insertion(+), 26 deletions(-) diff --git a/src/libs/IOUUtils.js b/src/libs/IOUUtils.js index f122e6eef46e..5138b1858fef 100644 --- a/src/libs/IOUUtils.js +++ b/src/libs/IOUUtils.js @@ -62,31 +62,6 @@ function updateIOUOwnerAndTotal(iouReport, actorAccountID, amount, currency, isD return iouReportUpdate; } -/** - * Returns the list of IOU actions depending on the type and whether or not they are pending. - * Used below so that we can decide if an IOU report is pending currency conversion. - * - * @param {Array} reportActions - * @param {Object} iouReport - * @param {String} type - iouReportAction type. Can be oneOf(create, delete, pay, split) - * @param {String} pendingAction - * @param {Boolean} filterRequestsInDifferentCurrency - * - * @returns {Array} - */ -function getIOUReportActions(reportActions, iouReport, type = '', pendingAction = '', filterRequestsInDifferentCurrency = false) { - return _.chain(reportActions) - .filter((action) => action.originalMessage && ReportActionsUtils.isMoneyRequestAction(action) && (!_.isEmpty(type) ? action.originalMessage.type === type : true)) - .filter((action) => action.originalMessage.IOUReportID.toString() === iouReport.reportID.toString()) - .filter((action) => (!_.isEmpty(pendingAction) ? action.pendingAction === pendingAction : true)) - .filter((action) => { - const transaction = TransactionUtils.getLinkedTransaction(action); - const currency = TransactionUtils.getCurrency(transaction); - return filterRequestsInDifferentCurrency ? currency && currency !== iouReport.currency : true; - }) - .value(); -} - /** * Returns whether or not an IOU report contains money requests in a different currency * that are either created or cancelled offline, and thus haven't been converted to the report's currency yet @@ -109,4 +84,4 @@ function isValidMoneyRequestType(iouType) { return [CONST.IOU.MONEY_REQUEST_TYPE.REQUEST, CONST.IOU.MONEY_REQUEST_TYPE.SPLIT].includes(iouType); } -export {calculateAmount, updateIOUOwnerAndTotal, getIOUReportActions, isIOUReportPendingCurrencyConversion, isValidMoneyRequestType}; +export {calculateAmount, updateIOUOwnerAndTotal, isIOUReportPendingCurrencyConversion, isValidMoneyRequestType}; From 66b0facf52c5427b43074f410f9286656fd0c009 Mon Sep 17 00:00:00 2001 From: Carlos Martins Date: Tue, 15 Aug 2023 13:52:41 -0500 Subject: [PATCH 39/44] rm unused import --- src/libs/IOUUtils.js | 1 - 1 file changed, 1 deletion(-) diff --git a/src/libs/IOUUtils.js b/src/libs/IOUUtils.js index 5138b1858fef..8da9ca962505 100644 --- a/src/libs/IOUUtils.js +++ b/src/libs/IOUUtils.js @@ -1,6 +1,5 @@ import _ from 'underscore'; import CONST from '../CONST'; -import * as ReportActionsUtils from './ReportActionsUtils'; import * as TransactionUtils from './TransactionUtils'; /** From d7c89108bb937d30118b3d29698605c3f9f41701 Mon Sep 17 00:00:00 2001 From: Carlos Martins Date: Tue, 15 Aug 2023 13:54:55 -0500 Subject: [PATCH 40/44] fix editMoneyRequest params --- src/libs/actions/IOU.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/libs/actions/IOU.js b/src/libs/actions/IOU.js index 2578b0036326..653d734bd37d 100644 --- a/src/libs/actions/IOU.js +++ b/src/libs/actions/IOU.js @@ -857,7 +857,7 @@ function editMoneyRequest(transactionID, transactionThreadReportID, transactionC ]; // STEP 6: Call the API endpoint - const {created, amount, currency, comment} = ReportUtils.getTransactionDetails(transaction); + const {created, amount, currency, comment} = ReportUtils.getTransactionDetails(updatedTransaction); API.write( 'EditMoneyRequest', { From 88b2b645df77d7bb201de3b6bbff416305e6219d Mon Sep 17 00:00:00 2001 From: Carlos Martins Date: Tue, 15 Aug 2023 14:16:31 -0500 Subject: [PATCH 41/44] use getCreated --- src/components/ReportActionItem/MoneyRequestView.js | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) diff --git a/src/components/ReportActionItem/MoneyRequestView.js b/src/components/ReportActionItem/MoneyRequestView.js index f02a835a4ad6..92aa99df24d6 100644 --- a/src/components/ReportActionItem/MoneyRequestView.js +++ b/src/components/ReportActionItem/MoneyRequestView.js @@ -19,7 +19,6 @@ import * as StyleUtils from '../../styles/StyleUtils'; import CONST from '../../CONST'; import * as Expensicons from '../Icon/Expensicons'; import iouReportPropTypes from '../../pages/iouReportPropTypes'; -import DateUtils from '../../libs/DateUtils'; import * as CurrencyUtils from '../../libs/CurrencyUtils'; import EmptyStateBackgroundImage from '../../../assets/images/empty-state_background-fade.png'; import useLocalize from '../../hooks/useLocalize'; @@ -68,11 +67,8 @@ function MoneyRequestView({report, parentReport, shouldShowHorizontalRule, polic const parentReportAction = ReportActionsUtils.getParentReportAction(report); const moneyRequestReport = parentReport; const transaction = TransactionUtils.getLinkedTransaction(parentReportAction); - const {amount: transactionAmount, currency: transactionCurrency, description: transactionDescription} = ReportUtils.getTransactionDetails(transaction); - + const {created: transactionDate, amount: transactionAmount, currency: transactionCurrency, comment: transactionDescription} = ReportUtils.getTransactionDetails(transaction); const formattedTransactionAmount = transactionAmount && transactionCurrency && CurrencyUtils.convertToDisplayString(transactionAmount, transactionCurrency); - const transactionDate = lodashGet(parentReportAction, ['created']); - const formattedTransactionDate = DateUtils.getDateStringFromISOTimestamp(transactionDate); const isSettled = ReportUtils.isSettled(moneyRequestReport.reportID); const isAdmin = Policy.isAdminOfFreePolicy([policy]) && ReportUtils.isExpenseReport(moneyRequestReport); @@ -114,7 +110,7 @@ function MoneyRequestView({report, parentReport, shouldShowHorizontalRule, polic /> Navigation.navigate(ROUTES.getEditRequestRoute(report.reportID, CONST.EDIT_REQUEST_FIELD.DATE))} From add8d3cc0a05259297c5d16ac4921a47dc367963 Mon Sep 17 00:00:00 2001 From: Carlos Martins Date: Tue, 15 Aug 2023 14:56:26 -0500 Subject: [PATCH 42/44] update date default --- src/libs/TransactionUtils.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/libs/TransactionUtils.js b/src/libs/TransactionUtils.js index e4bcbd8ac1c3..efce0800e849 100644 --- a/src/libs/TransactionUtils.js +++ b/src/libs/TransactionUtils.js @@ -154,7 +154,7 @@ function getCreated(transaction) { if (created) { return format(new Date(created), CONST.DATE.FNS_FORMAT_STRING); } - return format(new Date(lodashGet(transaction, 'created', '')), CONST.DATE.FNS_FORMAT_STRING); + return format(new Date(lodashGet(transaction, 'created', Date.now())), CONST.DATE.FNS_FORMAT_STRING); } /** From b629521257e2128eacd856d3aee4dde13392be6b Mon Sep 17 00:00:00 2001 From: Carlos Martins Date: Wed, 16 Aug 2023 10:53:32 -0500 Subject: [PATCH 43/44] use onyx set --- src/libs/actions/IOU.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/libs/actions/IOU.js b/src/libs/actions/IOU.js index 653d734bd37d..fd612f3ad781 100644 --- a/src/libs/actions/IOU.js +++ b/src/libs/actions/IOU.js @@ -516,7 +516,7 @@ function createSplitsAndOnyxData(participants, currentUserLogin, currentUserAcco }, }, { - onyxMethod: Onyx.METHOD.MERGE, + onyxMethod: Onyx.METHOD.SET, key: `${ONYXKEYS.COLLECTION.TRANSACTION}${groupTransaction.transactionID}`, value: groupTransaction, }, From 4d90f2c967647a63aa8ac59a397c15b562f2f6b7 Mon Sep 17 00:00:00 2001 From: Carlos Martins Date: Wed, 16 Aug 2023 14:38:24 -0500 Subject: [PATCH 44/44] use getters --- src/libs/actions/IOU.js | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/src/libs/actions/IOU.js b/src/libs/actions/IOU.js index 026ddc7fed3a..c5e2c532a888 100644 --- a/src/libs/actions/IOU.js +++ b/src/libs/actions/IOU.js @@ -938,9 +938,15 @@ function deleteMoneyRequest(transactionID, reportAction, isSingleTransactionView updatedIOUReport = {...iouReport}; // Because of the Expense reports are stored as negative values, we add the total from the amount - updatedIOUReport.total += transaction.amount; + updatedIOUReport.total += TransactionUtils.getAmount(transaction, true); } else { - updatedIOUReport = IOUUtils.updateIOUOwnerAndTotal(iouReport, reportAction.actorAccountID, transaction.amount, transaction.currency, true); + updatedIOUReport = IOUUtils.updateIOUOwnerAndTotal( + iouReport, + reportAction.actorAccountID, + TransactionUtils.getAmount(transaction, false), + TransactionUtils.getCurrency(transaction), + true, + ); } updatedIOUReport.lastMessageText = iouReportLastMessageText;