From 1d21ded647fbd25ab4d1375133fe345a31beed02 Mon Sep 17 00:00:00 2001 From: tienifr Date: Mon, 22 Jan 2024 11:01:20 +0700 Subject: [PATCH 1/4] fix: Delay in updating green dot and total amount --- src/libs/actions/IOU.js | 35 +++++++++++++++++++++++++---------- 1 file changed, 25 insertions(+), 10 deletions(-) diff --git a/src/libs/actions/IOU.js b/src/libs/actions/IOU.js index 7ee752a1f0ef..aeff964cac6b 100644 --- a/src/libs/actions/IOU.js +++ b/src/libs/actions/IOU.js @@ -329,7 +329,7 @@ function getReceiptError(receipt, filename, isScanRequest = true) { * @param {Object} policy - May be undefined, an empty object, or an object matching the Policy type (src/types/onyx/Policy.ts) * @param {Array} policyTags * @param {Array} policyCategories - * @param {Boolean} hasOutstandingChildRequest + * @param {Array} needsToBeManuallySubmitted * @returns {Array} - An array containing the optimistic data, success data, and failure data. */ function buildOnyxDataForMoneyRequest( @@ -348,7 +348,7 @@ function buildOnyxDataForMoneyRequest( policy, policyTags, policyCategories, - hasOutstandingChildRequest = false, + needsToBeManuallySubmitted, ) { const isScanRequest = TransactionUtils.isScanRequest(transaction); const optimisticData = [ @@ -361,7 +361,7 @@ function buildOnyxDataForMoneyRequest( lastReadTime: DateUtils.getDBTime(), lastMessageTranslationKey: '', iouReportID: iouReport.reportID, - hasOutstandingChildRequest, + hasOutstandingChildRequest: needsToBeManuallySubmitted && iouReport.managerID === userAccountID && iouReport.total !== 0, ...(isNewChatReport ? {pendingFields: {createChat: CONST.RED_BRICK_ROAD_PENDING_ACTION.ADD}} : {}), }, }, @@ -687,7 +687,7 @@ function getMoneyRequestInformation( let iouReport = isNewIOUReport ? null : allReports[`${ONYXKEYS.COLLECTION.REPORT}${chatReport.iouReportID}`]; // Check if the Scheduled Submit is enabled in case of expense report - let needsToBeManuallySubmitted = false; + let needsToBeManuallySubmitted = true; let isFromPaidPolicy = false; if (isPolicyExpenseChat) { isFromPaidPolicy = PolicyUtils.isPaidGroupPolicy(policy); @@ -806,10 +806,6 @@ function getMoneyRequestInformation( } : undefined; - // The policy expense chat should have the GBR only when its a paid policy and the scheduled submit is turned off - // so the employee has to submit to their manager manually. - const hasOutstandingChildRequest = isPolicyExpenseChat && needsToBeManuallySubmitted; - // STEP 5: Build Onyx Data const [optimisticData, successData, failureData] = buildOnyxDataForMoneyRequest( chatReport, @@ -827,7 +823,7 @@ function getMoneyRequestInformation( policy, policyTags, policyCategories, - hasOutstandingChildRequest, + needsToBeManuallySubmitted, ); return { @@ -2544,7 +2540,19 @@ function deleteMoneyRequest(transactionID, reportAction, isSingleTransactionView let updatedIOUReport = {...iouReport}; const updatedReportPreviewAction = {...reportPreviewAction}; updatedReportPreviewAction.pendingAction = shouldDeleteIOUReport ? CONST.RED_BRICK_ROAD_PENDING_ACTION.DELETE : CONST.RED_BRICK_ROAD_PENDING_ACTION.UPDATE; - if (ReportUtils.isExpenseReport(iouReport)) { + + const isPolicyExpenseChat = ReportUtils.isExpenseReport(iouReport); + + let needsToBeManuallySubmitted = true; + if (isPolicyExpenseChat) { + const policy = ReportUtils.getPolicy(iouReport.policyID); + const isFromPaidPolicy = PolicyUtils.isPaidGroupPolicy(policy); + + // If the scheduled submit is turned off on the policy, user needs to manually submit the report which is indicated by GBR in LHN + needsToBeManuallySubmitted = isFromPaidPolicy && !(policy.isHarvestingEnabled || false); + } + + if (isPolicyExpenseChat) { updatedIOUReport = {...iouReport}; // Because of the Expense reports are stored as negative values, we add the total from the amount @@ -2646,6 +2654,13 @@ function deleteMoneyRequest(transactionID, reportAction, isSingleTransactionView }, ] : []), + { + onyxMethod: Onyx.METHOD.MERGE, + key: `${ONYXKEYS.COLLECTION.REPORT}${chatReport.reportID}`, + value: { + hasOutstandingChildRequest: needsToBeManuallySubmitted && updatedIOUReport.managerID === userAccountID && updatedIOUReport.total !== 0, + }, + }, ]; const successData = [ From 5c5b0ab9df2e4193c11e9218de31ce087c894360 Mon Sep 17 00:00:00 2001 From: tienifr Date: Mon, 29 Jan 2024 11:07:00 +0700 Subject: [PATCH 2/4] change params order --- 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 2de0ccef71b0..4b90d5fd33d4 100644 --- a/src/libs/actions/IOU.js +++ b/src/libs/actions/IOU.js @@ -377,7 +377,7 @@ function buildOnyxDataForMoneyRequest( needsToBeManuallySubmitted = true, ) { const isScanRequest = TransactionUtils.isScanRequest(transaction); - const outstandingChildRequest = getOutstandingChildRequest(needsToBeManuallySubmitted, policy, iouReport); + const outstandingChildRequest = getOutstandingChildRequest(policy, iouReport, needsToBeManuallySubmitted); const optimisticData = [ { // Use SET for new reports because it doesn't exist yet, is faster and we need the data to be available when we navigate to the chat page From 3f10fe467b1bd17408eb8ffa1a8bb628a2429e46 Mon Sep 17 00:00:00 2001 From: tienifr Date: Mon, 5 Feb 2024 17:48:58 +0700 Subject: [PATCH 3/4] fix update amount --- src/libs/actions/IOU.js | 73 ++++++++++++++++++++++++----------------- 1 file changed, 42 insertions(+), 31 deletions(-) diff --git a/src/libs/actions/IOU.js b/src/libs/actions/IOU.js index f5167eed9afa..bd9773dcbfa2 100644 --- a/src/libs/actions/IOU.js +++ b/src/libs/actions/IOU.js @@ -312,15 +312,33 @@ function getReceiptError(receipt, filename, isScanRequest = true) { : ErrorUtils.getMicroSecondOnyxErrorObject({error: CONST.IOU.RECEIPT_ERROR, source: receipt.source, filename}); } +/** + * @param {Object} iouReport + * + * @returns {Boolean} + */ +function needsToBeManuallySubmitted(iouReport) { + const isPolicyExpenseChat = ReportUtils.isExpenseReport(iouReport); + + if (isPolicyExpenseChat) { + const policy = ReportUtils.getPolicy(iouReport.policyID); + const isFromPaidPolicy = PolicyUtils.isPaidGroupPolicy(policy); + + // If the scheduled submit is turned off on the policy, user needs to manually submit the report which is indicated by GBR in LHN + return isFromPaidPolicy && !(policy.isHarvestingEnabled || false); + } + + return true; +} + /** * Return the object to update hasOutstandingChildRequest * @param {Object} [policy] * @param {Object} iouReport - * @param {Boolean} needsToBeManuallySubmitted * @returns {Object} */ -function getOutstandingChildRequest(policy, iouReport, needsToBeManuallySubmitted) { - if (!needsToBeManuallySubmitted) { +function getOutstandingChildRequest(policy, iouReport) { + if (!needsToBeManuallySubmitted(iouReport)) { return { hasOutstandingChildRequest: false, }; @@ -355,7 +373,6 @@ function getOutstandingChildRequest(policy, iouReport, needsToBeManuallySubmitte * @param {Object} policy - May be undefined, an empty object, or an object matching the Policy type (src/types/onyx/Policy.ts) * @param {Array} policyTags * @param {Array} policyCategories - * @param {Boolean} needsToBeManuallySubmitted * @returns {Array} - An array containing the optimistic data, success data, and failure data. */ function buildOnyxDataForMoneyRequest( @@ -374,10 +391,9 @@ function buildOnyxDataForMoneyRequest( policy, policyTags, policyCategories, - needsToBeManuallySubmitted = true, ) { const isScanRequest = TransactionUtils.isScanRequest(transaction); - const outstandingChildRequest = getOutstandingChildRequest(policy, iouReport, needsToBeManuallySubmitted); + const outstandingChildRequest = getOutstandingChildRequest(policy, iouReport); const optimisticData = [ { // Use SET for new reports because it doesn't exist yet, is faster and we need the data to be available when we navigate to the chat page @@ -722,15 +738,10 @@ function getMoneyRequestInformation( iouReport = allReports[`${ONYXKEYS.COLLECTION.REPORT}${chatReport.iouReportID}`]; } - // Check if the Scheduled Submit is enabled in case of expense report - let needsToBeManuallySubmitted = true; let isFromPaidPolicy = false; if (isPolicyExpenseChat) { isFromPaidPolicy = PolicyUtils.isPaidGroupPolicy(policy); - // If the scheduled submit is turned off on the policy, user needs to manually submit the report which is indicated by GBR in LHN - needsToBeManuallySubmitted = isFromPaidPolicy && !(lodashGet(policy, 'harvesting.enabled', policy.isHarvestingEnabled) || false); - // If the linked expense report on paid policy is not draft, we need to create a new draft expense report if (iouReport && isFromPaidPolicy && !ReportUtils.isDraftExpenseReport(iouReport)) { iouReport = null; @@ -859,7 +870,6 @@ function getMoneyRequestInformation( policy, policyTags, policyCategories, - needsToBeManuallySubmitted, ); return { @@ -1033,7 +1043,7 @@ function getUpdateMoneyRequestParams(transactionID, transactionThreadReportID, t // from the server with the currency conversion let updatedMoneyRequestReport = {...iouReport}; if (updatedTransaction.currency === iouReport.currency && updatedTransaction.modifiedAmount) { - const diff = TransactionUtils.getAmount(transaction, true) - TransactionUtils.getAmount(updatedTransaction, true); + const diff = TransactionUtils.getAmount(updatedTransaction, isFromExpenseReport) - TransactionUtils.getAmount(transaction, isFromExpenseReport); if (ReportUtils.isExpenseReport(iouReport)) { updatedMoneyRequestReport.total += diff; } else { @@ -1041,11 +1051,23 @@ function getUpdateMoneyRequestParams(transactionID, transactionThreadReportID, t } updatedMoneyRequestReport.cachedTotal = CurrencyUtils.convertToDisplayString(updatedMoneyRequestReport.total, updatedTransaction.currency); - optimisticData.push({ - onyxMethod: Onyx.METHOD.MERGE, - key: `${ONYXKEYS.COLLECTION.REPORT}${iouReport.reportID}`, - value: updatedMoneyRequestReport, - }); + optimisticData.push( + ...[ + { + onyxMethod: Onyx.METHOD.MERGE, + key: `${ONYXKEYS.COLLECTION.REPORT}${iouReport.reportID}`, + value: updatedMoneyRequestReport, + }, + { + onyxMethod: Onyx.METHOD.MERGE, + key: `${ONYXKEYS.COLLECTION.REPORT}${iouReport.parentReportID}`, + value: { + hasOutstandingChildRequest: + needsToBeManuallySubmitted(iouReport) && updatedMoneyRequestReport.managerID === userAccountID && updatedMoneyRequestReport.total !== 0, + }, + }, + ], + ); successData.push({ onyxMethod: Onyx.METHOD.MERGE, key: `${ONYXKEYS.COLLECTION.REPORT}${iouReport.reportID}`, @@ -2653,18 +2675,7 @@ function deleteMoneyRequest(transactionID, reportAction, isSingleTransactionView const updatedReportPreviewAction = {...reportPreviewAction}; updatedReportPreviewAction.pendingAction = shouldDeleteIOUReport ? CONST.RED_BRICK_ROAD_PENDING_ACTION.DELETE : CONST.RED_BRICK_ROAD_PENDING_ACTION.UPDATE; - const isPolicyExpenseChat = ReportUtils.isExpenseReport(iouReport); - - let needsToBeManuallySubmitted = true; - if (isPolicyExpenseChat) { - const policy = ReportUtils.getPolicy(iouReport.policyID); - const isFromPaidPolicy = PolicyUtils.isPaidGroupPolicy(policy); - - // If the scheduled submit is turned off on the policy, user needs to manually submit the report which is indicated by GBR in LHN - needsToBeManuallySubmitted = isFromPaidPolicy && !(policy.isHarvestingEnabled || false); - } - - if (isPolicyExpenseChat) { + if (ReportUtils.isExpenseReport(iouReport)) { updatedIOUReport = {...iouReport}; // Because of the Expense reports are stored as negative values, we add the total from the amount @@ -2770,7 +2781,7 @@ function deleteMoneyRequest(transactionID, reportAction, isSingleTransactionView onyxMethod: Onyx.METHOD.MERGE, key: `${ONYXKEYS.COLLECTION.REPORT}${chatReport.reportID}`, value: { - hasOutstandingChildRequest: needsToBeManuallySubmitted && updatedIOUReport.managerID === userAccountID && updatedIOUReport.total !== 0, + hasOutstandingChildRequest: needsToBeManuallySubmitted(iouReport) && updatedIOUReport.managerID === userAccountID && updatedIOUReport.total !== 0, }, }, ]; From fe5465074a8ab1acfbc6b207965e4cad4e093e03 Mon Sep 17 00:00:00 2001 From: tienifr Date: Thu, 15 Feb 2024 14:55:53 +0700 Subject: [PATCH 4/4] lint fix --- src/libs/actions/IOU.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/libs/actions/IOU.ts b/src/libs/actions/IOU.ts index 589bb7bff15b..f6fb82990968 100644 --- a/src/libs/actions/IOU.ts +++ b/src/libs/actions/IOU.ts @@ -377,7 +377,7 @@ function needsToBeManuallySubmitted(iouReport: OnyxTypes.Report) { /** * Return the object to update hasOutstandingChildRequest */ -function getOutstandingChildRequest(policy: OnyxEntry | EmptyObject, iouReport: OnyxTypes.Report) { +function getOutstandingChildRequest(policy: OnyxEntry | EmptyObject, iouReport: OnyxTypes.Report): OutstandingChildRequest { if (!needsToBeManuallySubmitted(iouReport)) { return { hasOutstandingChildRequest: false,