Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Clear hold reason of all transactions when the admin approves all requests #39579

Merged
merged 10 commits into from
Apr 12, 2024
9 changes: 9 additions & 0 deletions src/libs/ReportUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5457,6 +5457,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 @@ -5979,6 +5987,7 @@ export {
hasActionsWithErrors,
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 @@ -4861,7 +4861,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 @@ -4956,6 +4957,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
Loading