Skip to content

Commit

Permalink
Merge pull request #46057 from bernhardoj/fix/45779-allow-delete-expe…
Browse files Browse the repository at this point in the history
…nse-until-its-approved-2

Allow user to delete expense as long as report is not approved yet
  • Loading branch information
cristipaval authored Jul 26, 2024
2 parents 33d29a4 + ddb5fc8 commit 8728fc0
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 14 deletions.
2 changes: 1 addition & 1 deletion src/components/MoneyReportHeader.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ function MoneyReportHeader({policy, report: moneyRequestReport, transactionThrea
// Only the requestor can delete the request, admins can only edit it.
const isActionOwner =
typeof requestParentReportAction?.actorAccountID === 'number' && typeof session?.accountID === 'number' && requestParentReportAction.actorAccountID === session?.accountID;
const canDeleteRequest = isActionOwner && ReportUtils.canAddOrDeleteTransactions(moneyRequestReport) && !isDeletedParentAction;
const canDeleteRequest = isActionOwner && ReportUtils.canDeleteTransaction(moneyRequestReport) && !isDeletedParentAction;
const [isHoldMenuVisible, setIsHoldMenuVisible] = useState(false);
const [paymentType, setPaymentType] = useState<PaymentMethodType>();
const [requestType, setRequestType] = useState<ActionHandledType>();
Expand Down
43 changes: 31 additions & 12 deletions src/libs/ReportUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1601,13 +1601,6 @@ function getChildReportNotificationPreference(reportAction: OnyxInputOrEntry<Rep
return isActionCreator(reportAction) ? CONST.REPORT.NOTIFICATION_PREFERENCE.ALWAYS : CONST.REPORT.NOTIFICATION_PREFERENCE.HIDDEN;
}

/**
* Checks whether the supplied report supports adding more transactions to it.
* Return true if:
* - report is a non-settled IOU
* - report is a draft
* - report is a processing expense report and its policy has Instant reporting frequency
*/
function canAddOrDeleteTransactions(moneyRequestReport: OnyxEntry<Report>): boolean {
if (!isMoneyRequestReport(moneyRequestReport)) {
return false;
Expand All @@ -1622,11 +1615,36 @@ function canAddOrDeleteTransactions(moneyRequestReport: OnyxEntry<Report>): bool
return false;
}

return true;
}

/**
* Checks whether the supplied report supports adding more transactions to it.
* Return true if:
* - report is a non-settled IOU
* - report is a draft
* - report is a processing expense report and its policy has Instant reporting frequency
*/
function canAddTransaction(moneyRequestReport: OnyxEntry<Report>): boolean {
if (!isMoneyRequestReport(moneyRequestReport)) {
return false;
}

if (isReportInGroupPolicy(moneyRequestReport) && isProcessingReport(moneyRequestReport) && !PolicyUtils.isInstantSubmitEnabled(getPolicy(moneyRequestReport?.policyID))) {
return false;
}

return true;
return canAddOrDeleteTransactions(moneyRequestReport);
}

/**
* Checks whether the supplied report supports deleting more transactions from it.
* Return true if:
* - report is a non-settled IOU
* - report is a non-approved IOU
*/
function canDeleteTransaction(moneyRequestReport: OnyxEntry<Report>): boolean {
return canAddOrDeleteTransactions(moneyRequestReport);
}

/**
Expand All @@ -1650,7 +1668,7 @@ function canDeleteReportAction(reportAction: OnyxInputOrEntry<ReportAction>, rep
const linkedReport = isThreadFirstChat(reportAction, reportID) ? getReportOrDraftReport(report?.parentReportID) : report;
if (isActionOwner) {
if (!isEmptyObject(linkedReport) && isMoneyRequestReport(linkedReport)) {
return canAddOrDeleteTransactions(linkedReport);
return canDeleteTransaction(linkedReport);
}
return true;
}
Expand Down Expand Up @@ -5902,7 +5920,7 @@ function canRequestMoney(report: OnyxEntry<Report>, policy: OnyxEntry<Policy>, o
// User can submit expenses in any IOU report, unless paid, but the user can only submit expenses in an expense report
// which is tied to their workspace chat.
if (isMoneyRequestReport(report)) {
const canAddTransactions = canAddOrDeleteTransactions(report);
const canAddTransactions = canAddTransaction(report);
return isReportInGroupPolicy(report) ? isOwnPolicyExpenseChat && canAddTransactions : canAddTransactions;
}

Expand Down Expand Up @@ -6946,7 +6964,7 @@ function hasMissingPaymentMethod(userWallet: OnyxEntry<UserWallet>, iouReportID:
* - we have one, but we can't add more transactions to it due to: report is approved or settled, or report is processing and policy isn't on Instant submit reporting frequency
*/
function shouldCreateNewMoneyRequestReport(existingIOUReport: OnyxInputOrEntry<Report> | undefined, chatReport: OnyxInputOrEntry<Report>): boolean {
return !existingIOUReport || hasIOUWaitingOnCurrentUserBankAccount(chatReport) || !canAddOrDeleteTransactions(existingIOUReport);
return !existingIOUReport || hasIOUWaitingOnCurrentUserBankAccount(chatReport) || !canAddTransaction(existingIOUReport);
}

function getTripTransactions(tripRoomReportID: string | undefined, reportFieldToCompare: 'parentReportID' | 'reportID' = 'parentReportID'): Transaction[] {
Expand Down Expand Up @@ -7295,7 +7313,8 @@ export {
buildParticipantsFromAccountIDs,
buildTransactionThread,
canAccessReport,
canAddOrDeleteTransactions,
canAddTransaction,
canDeleteTransaction,
canBeAutoReimbursed,
canCreateRequest,
canCreateTaskInReport,
Expand Down
2 changes: 1 addition & 1 deletion src/pages/ReportDetailsPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,7 @@ function ReportDetailsPage({policies, report, session, personalDetails}: ReportD
report.stateNum !== CONST.REPORT.STATE_NUM.APPROVED &&
!ReportUtils.isClosedReport(report) &&
canModifyTask;
const canDeleteRequest = isActionOwner && (ReportUtils.canAddOrDeleteTransactions(moneyRequestReport) || isSelfDMTrackExpenseReport) && !isDeletedParentAction;
const canDeleteRequest = isActionOwner && (ReportUtils.canDeleteTransaction(moneyRequestReport) || isSelfDMTrackExpenseReport) && !isDeletedParentAction;
const shouldShowDeleteButton = shouldShowTaskDeleteButton || canDeleteRequest;

const canUnapproveRequest =
Expand Down

0 comments on commit 8728fc0

Please sign in to comment.