Skip to content

Commit 95bdf2b

Browse files
authored
Merge pull request Expensify#66981 from FitseTLT/fix-disable-expense-update-for-archived-invoice
Fix - Invoices - Archived invoice report change to original amount when edit second time
2 parents 37cc644 + 42f3d5d commit 95bdf2b

3 files changed

Lines changed: 63 additions & 12 deletions

File tree

src/components/ReportActionItem/MoneyRequestView.tsx

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import useActiveRoute from '@hooks/useActiveRoute';
1717
import useLocalize from '@hooks/useLocalize';
1818
import useNetwork from '@hooks/useNetwork';
1919
import useOnyx from '@hooks/useOnyx';
20+
import useReportIsArchived from '@hooks/useReportIsArchived';
2021
import useResponsiveLayout from '@hooks/useResponsiveLayout';
2122
import useThemeStyles from '@hooks/useThemeStyles';
2223
import useTransactionViolations from '@hooks/useTransactionViolations';
@@ -199,20 +200,21 @@ function MoneyRequestView({allReports, report, policy, shouldShowAnimatedBackgro
199200

200201
const isSettled = isSettledReportUtils(moneyRequestReport?.reportID);
201202
const isCancelled = moneyRequestReport && moneyRequestReport?.isCancelledIOU;
203+
const isChatReportArchived = useReportIsArchived(moneyRequestReport?.chatReportID);
202204

203205
// Flags for allowing or disallowing editing an expense
204206
// Used for non-restricted fields such as: description, category, tag, billable, etc...
205207
const canUserPerformWriteAction = !!canUserPerformWriteActionReportUtils(report) && !readonly;
206-
const canEdit = isMoneyRequestAction(parentReportAction) && canEditMoneyRequest(parentReportAction, transaction) && canUserPerformWriteAction;
208+
const canEdit = isMoneyRequestAction(parentReportAction) && canEditMoneyRequest(parentReportAction, transaction, isChatReportArchived) && canUserPerformWriteAction;
207209

208210
const canEditTaxFields = canEdit && !isDistanceRequest;
209-
const canEditAmount = canUserPerformWriteAction && canEditFieldOfMoneyRequest(parentReportAction, CONST.EDIT_REQUEST_FIELD.AMOUNT);
210-
const canEditMerchant = canUserPerformWriteAction && canEditFieldOfMoneyRequest(parentReportAction, CONST.EDIT_REQUEST_FIELD.MERCHANT);
211-
const canEditDate = canUserPerformWriteAction && canEditFieldOfMoneyRequest(parentReportAction, CONST.EDIT_REQUEST_FIELD.DATE);
212-
const canEditReceipt = canUserPerformWriteAction && canEditFieldOfMoneyRequest(parentReportAction, CONST.EDIT_REQUEST_FIELD.RECEIPT);
213-
const canEditDistance = canUserPerformWriteAction && canEditFieldOfMoneyRequest(parentReportAction, CONST.EDIT_REQUEST_FIELD.DISTANCE);
214-
const canEditDistanceRate = canUserPerformWriteAction && canEditFieldOfMoneyRequest(parentReportAction, CONST.EDIT_REQUEST_FIELD.DISTANCE_RATE);
215-
const canEditReport = canUserPerformWriteAction && canEditFieldOfMoneyRequest(parentReportAction, CONST.EDIT_REQUEST_FIELD.REPORT);
211+
const canEditAmount = canUserPerformWriteAction && canEditFieldOfMoneyRequest(parentReportAction, CONST.EDIT_REQUEST_FIELD.AMOUNT, undefined, isChatReportArchived);
212+
const canEditMerchant = canUserPerformWriteAction && canEditFieldOfMoneyRequest(parentReportAction, CONST.EDIT_REQUEST_FIELD.MERCHANT, undefined, isChatReportArchived);
213+
const canEditDate = canUserPerformWriteAction && canEditFieldOfMoneyRequest(parentReportAction, CONST.EDIT_REQUEST_FIELD.DATE, undefined, isChatReportArchived);
214+
const canEditReceipt = canUserPerformWriteAction && canEditFieldOfMoneyRequest(parentReportAction, CONST.EDIT_REQUEST_FIELD.RECEIPT, undefined, isChatReportArchived);
215+
const canEditDistance = canUserPerformWriteAction && canEditFieldOfMoneyRequest(parentReportAction, CONST.EDIT_REQUEST_FIELD.DISTANCE, undefined, isChatReportArchived);
216+
const canEditDistanceRate = canUserPerformWriteAction && canEditFieldOfMoneyRequest(parentReportAction, CONST.EDIT_REQUEST_FIELD.DISTANCE_RATE, undefined, isChatReportArchived);
217+
const canEditReport = canUserPerformWriteAction && canEditFieldOfMoneyRequest(parentReportAction, CONST.EDIT_REQUEST_FIELD.REPORT, undefined, isChatReportArchived);
216218

217219
// A flag for verifying that the current report is a sub-report of a expense chat
218220
// if the policy of the report is either Collect or Control, then this report must be tied to expense chat

src/libs/ReportUtils.ts

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4031,7 +4031,11 @@ function isWorkspacePayer(memberLogin: string, policy: OnyxEntry<Policy>): boole
40314031
* This is used in conjunction with canEditRestrictedField to control editing of specific fields like amount, currency, created, receipt, and distance.
40324032
* On its own, it only controls allowing/disallowing navigating to the editing pages or showing/hiding the 'Edit' icon on report actions
40334033
*/
4034-
function canEditMoneyRequest(reportAction: OnyxInputOrEntry<ReportAction<typeof CONST.REPORT.ACTIONS.TYPE.IOU>>, linkedTransaction?: OnyxEntry<Transaction>): boolean {
4034+
function canEditMoneyRequest(
4035+
reportAction: OnyxInputOrEntry<ReportAction<typeof CONST.REPORT.ACTIONS.TYPE.IOU>>,
4036+
linkedTransaction?: OnyxEntry<Transaction>,
4037+
isChatReportArchived = false,
4038+
): boolean {
40354039
const isDeleted = isDeletedAction(reportAction);
40364040

40374041
if (isDeleted) {
@@ -4072,7 +4076,7 @@ function canEditMoneyRequest(reportAction: OnyxInputOrEntry<ReportAction<typeof
40724076
const isAdmin = policy?.role === CONST.POLICY.ROLE.ADMIN;
40734077
const isManager = currentUserAccountID === moneyRequestReport?.managerID;
40744078

4075-
if (isInvoiceReport(moneyRequestReport) && isManager) {
4079+
if (isInvoiceReport(moneyRequestReport) && (isManager || isChatReportArchived)) {
40764080
return false;
40774081
}
40784082

@@ -4155,7 +4159,12 @@ function canEditReportPolicy(report: OnyxEntry<Report>, reportPolicy: OnyxEntry<
41554159
* Checks if the current user can edit the provided property of an expense
41564160
*
41574161
*/
4158-
function canEditFieldOfMoneyRequest(reportAction: OnyxInputOrEntry<ReportAction>, fieldToEdit: ValueOf<typeof CONST.EDIT_REQUEST_FIELD>, isDeleteAction?: boolean): boolean {
4162+
function canEditFieldOfMoneyRequest(
4163+
reportAction: OnyxInputOrEntry<ReportAction>,
4164+
fieldToEdit: ValueOf<typeof CONST.EDIT_REQUEST_FIELD>,
4165+
isDeleteAction?: boolean,
4166+
isChatReportArchived = false,
4167+
): boolean {
41594168
// A list of fields that cannot be edited by anyone, once an expense has been settled
41604169
const restrictedFields: string[] = [
41614170
CONST.EDIT_REQUEST_FIELD.AMOUNT,
@@ -4168,7 +4177,7 @@ function canEditFieldOfMoneyRequest(reportAction: OnyxInputOrEntry<ReportAction>
41684177
CONST.EDIT_REQUEST_FIELD.REPORT,
41694178
];
41704179

4171-
if (!isMoneyRequestAction(reportAction) || !canEditMoneyRequest(reportAction)) {
4180+
if (!isMoneyRequestAction(reportAction) || !canEditMoneyRequest(reportAction, undefined, isChatReportArchived)) {
41724181
return false;
41734182
}
41744183

tests/unit/ReportUtilsTest.ts

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import {
2323
canDeleteReportAction,
2424
canDeleteTransaction,
2525
canEditFieldOfMoneyRequest,
26+
canEditMoneyRequest,
2627
canEditReportDescription,
2728
canEditRoomVisibility,
2829
canEditWriteCapability,
@@ -1906,6 +1907,45 @@ describe('ReportUtils', () => {
19061907
});
19071908
});
19081909

1910+
describe('canEditMoneyRequest', () => {
1911+
it('it should return false for archived invoice', async () => {
1912+
const invoiceReport: Report = {
1913+
reportID: '1',
1914+
type: CONST.REPORT.TYPE.INVOICE,
1915+
};
1916+
const transaction = createRandomTransaction(22);
1917+
const moneyRequestAction: ReportAction<typeof CONST.REPORT.ACTIONS.TYPE.IOU> = {
1918+
reportActionID: '22',
1919+
actorAccountID: currentUserAccountID,
1920+
actionName: CONST.REPORT.ACTIONS.TYPE.IOU,
1921+
originalMessage: {
1922+
IOUReportID: invoiceReport.reportID,
1923+
IOUTransactionID: transaction.transactionID,
1924+
amount: 530,
1925+
currency: CONST.CURRENCY.USD,
1926+
type: CONST.IOU.REPORT_ACTION_TYPE.CREATE,
1927+
},
1928+
message: [
1929+
{
1930+
type: 'COMMENT',
1931+
html: 'USD 5.30 expense',
1932+
text: 'USD 5.30 expense',
1933+
isEdited: false,
1934+
whisperedTo: [],
1935+
isDeletedParentAction: false,
1936+
deleted: '',
1937+
},
1938+
],
1939+
created: '2025-03-05 16:34:27',
1940+
};
1941+
await Onyx.set(`${ONYXKEYS.COLLECTION.REPORT}${invoiceReport.reportID}`, invoiceReport);
1942+
1943+
const canEditRequest = canEditMoneyRequest(moneyRequestAction, transaction, true);
1944+
1945+
expect(canEditRequest).toEqual(false);
1946+
});
1947+
});
1948+
19091949
describe('getChatByParticipants', () => {
19101950
const userAccountID = 1;
19111951
const userAccountID2 = 2;

0 commit comments

Comments
 (0)