From a06fb15de4ce43ff1b65716e0ae5d9948dd0d1d3 Mon Sep 17 00:00:00 2001 From: nkdengineer Date: Thu, 4 Apr 2024 14:37:29 +0700 Subject: [PATCH 1/5] Clear hold reason of all transactions when the admin approves all requests --- src/libs/actions/IOU.ts | 32 +++++++++++++++++++++++++++++++- 1 file changed, 31 insertions(+), 1 deletion(-) diff --git a/src/libs/actions/IOU.ts b/src/libs/actions/IOU.ts index 5c92cd87a2bc..cacb8e2b2b80 100644 --- a/src/libs/actions/IOU.ts +++ b/src/libs/actions/IOU.ts @@ -4673,7 +4673,10 @@ function hasIOUToApproveOrPay(chatReport: OnyxEntry | EmptyObj function approveMoneyRequest(expenseReport: OnyxTypes.Report | EmptyObject, full?: boolean) { const currentNextStep = allNextSteps[`${ONYXKEYS.COLLECTION.NEXT_STEP}${expenseReport.reportID}`] ?? null; let total = expenseReport.total ?? 0; - if (ReportUtils.hasHeldExpenses(expenseReport.reportID) && !full && !!expenseReport.unheldTotal) { + const transactions = TransactionUtils.getAllReportTransactions(expenseReport.reportID); + const hasHeldExpenses = transactions.some((transaction) => TransactionUtils.isOnHold(transaction)); + + if (hasHeldExpenses && !full && !!expenseReport.unheldTotal) { total = expenseReport.unheldTotal; } const optimisticApprovedReportAction = ReportUtils.buildOptimisticApprovedReportAction(total, expenseReport.currency ?? '', expenseReport.reportID); @@ -4768,6 +4771,33 @@ function approveMoneyRequest(expenseReport: OnyxTypes.Report | EmptyObject, full }, ]; + // Clear hold reason of all transactions if we approve all requests + if (full && hasHeldExpenses) { + const heldTransactions = transactions.filter(TransactionUtils.isOnHold); + heldTransactions.forEach((heldTransaction) => { + optimisticData.push({ + onyxMethod: Onyx.METHOD.MERGE, + key: `${ONYXKEYS.COLLECTION.TRANSACTION}${heldTransaction.transactionID}`, + value: { + comment: { + ...heldTransaction.comment, + hold: '', + }, + }, + }); + failureData.push({ + onyxMethod: Onyx.METHOD.MERGE, + key: `${ONYXKEYS.COLLECTION.TRANSACTION}${heldTransaction.transactionID}`, + value: { + comment: { + ...heldTransaction.comment, + hold: heldTransaction.comment.hold, + }, + }, + }); + }); + } + const parameters: ApproveMoneyRequestParams = { reportID: expenseReport.reportID, approvedReportActionID: optimisticApprovedReportAction.reportActionID, From 932937bc2e239150d6467cb274a1b9f1d01743a2 Mon Sep 17 00:00:00 2001 From: nkdengineer Date: Thu, 4 Apr 2024 22:05:55 +0700 Subject: [PATCH 2/5] remove unnecessary code --- src/libs/actions/IOU.ts | 2 -- 1 file changed, 2 deletions(-) diff --git a/src/libs/actions/IOU.ts b/src/libs/actions/IOU.ts index cacb8e2b2b80..87438672dcd4 100644 --- a/src/libs/actions/IOU.ts +++ b/src/libs/actions/IOU.ts @@ -4780,7 +4780,6 @@ function approveMoneyRequest(expenseReport: OnyxTypes.Report | EmptyObject, full key: `${ONYXKEYS.COLLECTION.TRANSACTION}${heldTransaction.transactionID}`, value: { comment: { - ...heldTransaction.comment, hold: '', }, }, @@ -4790,7 +4789,6 @@ function approveMoneyRequest(expenseReport: OnyxTypes.Report | EmptyObject, full key: `${ONYXKEYS.COLLECTION.TRANSACTION}${heldTransaction.transactionID}`, value: { comment: { - ...heldTransaction.comment, hold: heldTransaction.comment.hold, }, }, From 7af629255727477478c1473a1b3df586ab68194a Mon Sep 17 00:00:00 2001 From: nkdengineer Date: Fri, 5 Apr 2024 13:37:47 +0700 Subject: [PATCH 3/5] create getAllHeldTransactions --- src/libs/ReportUtils.ts | 9 +++++++++ src/libs/actions/IOU.ts | 8 +++----- 2 files changed, 12 insertions(+), 5 deletions(-) diff --git a/src/libs/ReportUtils.ts b/src/libs/ReportUtils.ts index fec64efaac7f..fe4c1bc891fc 100644 --- a/src/libs/ReportUtils.ts +++ b/src/libs/ReportUtils.ts @@ -5446,6 +5446,14 @@ function isHoldCreator(transaction: OnyxEntry, reportID: string): b return isActionCreator(holdReportAction); } +/** + * Get all held transactions of a iouReport + */ +function getAllHeldTransactions(iouReportID: string): Transaction[] { + const transactions = TransactionUtils.getAllReportTransactions(iouReportID); + return transactions.filter((transaction) => TransactionUtils.isOnHold(transaction)); +} + /** * Check if Report has any held expenses */ @@ -5966,6 +5974,7 @@ export { hasActionsWithErrors, getGroupChatName, getOutstandingChildRequest, + getAllHeldTransactions, }; export type { diff --git a/src/libs/actions/IOU.ts b/src/libs/actions/IOU.ts index 40918b0e3cab..1ebc5816ce6c 100644 --- a/src/libs/actions/IOU.ts +++ b/src/libs/actions/IOU.ts @@ -4634,10 +4634,8 @@ function hasIOUToApproveOrPay(chatReport: OnyxEntry | EmptyObj function approveMoneyRequest(expenseReport: OnyxTypes.Report | EmptyObject, full?: boolean) { const currentNextStep = allNextSteps[`${ONYXKEYS.COLLECTION.NEXT_STEP}${expenseReport.reportID}`] ?? null; let total = expenseReport.total ?? 0; - const transactions = TransactionUtils.getAllReportTransactions(expenseReport.reportID); - const hasHeldExpenses = transactions.some((transaction) => TransactionUtils.isOnHold(transaction)); - if (hasHeldExpenses && !full && !!expenseReport.unheldTotal) { + if (ReportUtils.hasHeldExpenses(expenseReport.reportID) && !full && !!expenseReport.unheldTotal) { total = expenseReport.unheldTotal; } const optimisticApprovedReportAction = ReportUtils.buildOptimisticApprovedReportAction(total, expenseReport.currency ?? '', expenseReport.reportID); @@ -4733,8 +4731,8 @@ function approveMoneyRequest(expenseReport: OnyxTypes.Report | EmptyObject, full ]; // Clear hold reason of all transactions if we approve all requests - if (full && hasHeldExpenses) { - const heldTransactions = transactions.filter(TransactionUtils.isOnHold); + const heldTransactions = ReportUtils.getAllHeldTransactions(expenseReport.reportID); + if (full && heldTransactions.length) { heldTransactions.forEach((heldTransaction) => { optimisticData.push({ onyxMethod: Onyx.METHOD.MERGE, From eeccc2b78e8167777924e7fec08f9bcd2f64ae0e Mon Sep 17 00:00:00 2001 From: nkdengineer Date: Fri, 5 Apr 2024 13:39:06 +0700 Subject: [PATCH 4/5] remove empty line --- src/libs/actions/IOU.ts | 1 - 1 file changed, 1 deletion(-) diff --git a/src/libs/actions/IOU.ts b/src/libs/actions/IOU.ts index 1ebc5816ce6c..90302fc95361 100644 --- a/src/libs/actions/IOU.ts +++ b/src/libs/actions/IOU.ts @@ -4634,7 +4634,6 @@ function hasIOUToApproveOrPay(chatReport: OnyxEntry | EmptyObj function approveMoneyRequest(expenseReport: OnyxTypes.Report | EmptyObject, full?: boolean) { const currentNextStep = allNextSteps[`${ONYXKEYS.COLLECTION.NEXT_STEP}${expenseReport.reportID}`] ?? null; let total = expenseReport.total ?? 0; - if (ReportUtils.hasHeldExpenses(expenseReport.reportID) && !full && !!expenseReport.unheldTotal) { total = expenseReport.unheldTotal; } From d44456c3028f552f34457fd71ebf892518ed791d Mon Sep 17 00:00:00 2001 From: nkdengineer Date: Mon, 8 Apr 2024 15:55:51 +0700 Subject: [PATCH 5/5] create a const for held request check --- src/libs/actions/IOU.ts | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/src/libs/actions/IOU.ts b/src/libs/actions/IOU.ts index 0657729ff2e9..784f86c20b92 100644 --- a/src/libs/actions/IOU.ts +++ b/src/libs/actions/IOU.ts @@ -4634,7 +4634,8 @@ function hasIOUToApproveOrPay(chatReport: OnyxEntry | EmptyObj function approveMoneyRequest(expenseReport: OnyxTypes.Report | EmptyObject, full?: boolean) { const currentNextStep = allNextSteps[`${ONYXKEYS.COLLECTION.NEXT_STEP}${expenseReport.reportID}`] ?? null; let total = expenseReport.total ?? 0; - if (ReportUtils.hasHeldExpenses(expenseReport.reportID) && !full && !!expenseReport.unheldTotal) { + const hasHeldExpenses = ReportUtils.hasHeldExpenses(expenseReport.reportID); + if (hasHeldExpenses && !full && !!expenseReport.unheldTotal) { total = expenseReport.unheldTotal; } const optimisticApprovedReportAction = ReportUtils.buildOptimisticApprovedReportAction(total, expenseReport.currency ?? '', expenseReport.reportID); @@ -4730,8 +4731,8 @@ function approveMoneyRequest(expenseReport: OnyxTypes.Report | EmptyObject, full ]; // Clear hold reason of all transactions if we approve all requests - const heldTransactions = ReportUtils.getAllHeldTransactions(expenseReport.reportID); - if (full && heldTransactions.length) { + if (full && hasHeldExpenses) { + const heldTransactions = ReportUtils.getAllHeldTransactions(expenseReport.reportID); heldTransactions.forEach((heldTransaction) => { optimisticData.push({ onyxMethod: Onyx.METHOD.MERGE,