Skip to content

Commit

Permalink
Merge pull request #39579 from nkdengineer/fix/39342
Browse files Browse the repository at this point in the history
Clear hold reason of all transactions when the admin approves all requests
  • Loading branch information
robertjchen authored Apr 12, 2024
2 parents d4bc1b0 + addcb9a commit 0665fa4
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 1 deletion.
9 changes: 9 additions & 0 deletions src/libs/ReportUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5488,6 +5488,14 @@ function isHoldCreator(transaction: OnyxEntry<Transaction>, 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
*/
Expand Down Expand Up @@ -6024,6 +6032,7 @@ export {
getReportActionActorAccountID,
getGroupChatName,
getOutstandingChildRequest,
getAllHeldTransactions,
};

export type {
Expand Down
28 changes: 27 additions & 1 deletion src/libs/actions/IOU.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4887,7 +4887,8 @@ function hasIOUToApproveOrPay(chatReport: OnyxEntry<OnyxTypes.Report> | 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);
Expand Down Expand Up @@ -4982,6 +4983,31 @@ function approveMoneyRequest(expenseReport: OnyxTypes.Report | EmptyObject, full
},
];

// Clear hold reason of all transactions if we approve all requests
if (full && hasHeldExpenses) {
const heldTransactions = ReportUtils.getAllHeldTransactions(expenseReport.reportID);
heldTransactions.forEach((heldTransaction) => {
optimisticData.push({
onyxMethod: Onyx.METHOD.MERGE,
key: `${ONYXKEYS.COLLECTION.TRANSACTION}${heldTransaction.transactionID}`,
value: {
comment: {
hold: '',
},
},
});
failureData.push({
onyxMethod: Onyx.METHOD.MERGE,
key: `${ONYXKEYS.COLLECTION.TRANSACTION}${heldTransaction.transactionID}`,
value: {
comment: {
hold: heldTransaction.comment.hold,
},
},
});
});
}

const parameters: ApproveMoneyRequestParams = {
reportID: expenseReport.reportID,
approvedReportActionID: optimisticApprovedReportAction.reportActionID,
Expand Down

0 comments on commit 0665fa4

Please sign in to comment.