Skip to content

Commit fd3c052

Browse files
authored
Merge pull request Expensify#84407 from Expensify/claude-fixDeleteReceiptClosedReport
Block restricted field edits on closed expense reports
2 parents 2063a58 + e4256d8 commit fd3c052

2 files changed

Lines changed: 104 additions & 0 deletions

File tree

src/libs/ReportUtils.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4937,6 +4937,7 @@ function canEditFieldOfMoneyRequest(
49374937

49384938
if (fieldToEdit === CONST.EDIT_REQUEST_FIELD.RECEIPT) {
49394939
return (
4940+
!isClosedReport(moneyRequestReport) &&
49404941
!isInvoiceReport(moneyRequestReport) &&
49414942
!isReceiptBeingScanned(transaction) &&
49424943
!isPerDiemRequest(transaction) &&

tests/unit/canEditFieldOfMoneyRequestTest.ts

Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -396,4 +396,107 @@ describe('canEditFieldOfMoneyRequest', () => {
396396
});
397397
});
398398
});
399+
400+
describe('receipt field', () => {
401+
const RECEIPT_IOU_REPORT_ID = '5001';
402+
const RECEIPT_IOU_TRANSACTION_ID = '5002';
403+
const RECEIPT_AMOUNT = 100;
404+
const receiptPolicyID = '5003';
405+
406+
const randomReportAction = createRandomReportAction(501);
407+
const adminPolicy = {...createRandomPolicy(Number(receiptPolicyID), CONST.POLICY.TYPE.TEAM), role: CONST.POLICY.ROLE.ADMIN};
408+
409+
const reportAction = {
410+
...randomReportAction,
411+
actionName: CONST.REPORT.ACTIONS.TYPE.IOU,
412+
actorAccountID: currentUserAccountID,
413+
childStateNum: CONST.REPORT.STATE_NUM.OPEN,
414+
childStatusNum: CONST.REPORT.STATUS_NUM.OPEN,
415+
originalMessage: {
416+
// eslint-disable-next-line @typescript-eslint/no-deprecated
417+
...randomReportAction.originalMessage,
418+
IOUReportID: RECEIPT_IOU_REPORT_ID,
419+
IOUTransactionID: RECEIPT_IOU_TRANSACTION_ID,
420+
type: CONST.IOU.ACTION.CREATE,
421+
amount: RECEIPT_AMOUNT,
422+
currency: CONST.CURRENCY.USD,
423+
},
424+
};
425+
426+
const moneyRequestTransaction = {
427+
...createRandomTransaction(Number(RECEIPT_IOU_TRANSACTION_ID)),
428+
reportID: RECEIPT_IOU_REPORT_ID,
429+
transactionID: RECEIPT_IOU_TRANSACTION_ID,
430+
amount: RECEIPT_AMOUNT,
431+
managedCard: false,
432+
status: CONST.TRANSACTION.STATUS.POSTED,
433+
};
434+
435+
beforeAll(() => {
436+
Onyx.init({keys: ONYXKEYS});
437+
438+
Onyx.multiSet({
439+
[ONYXKEYS.SESSION]: {email: currentUserEmail, accountID: currentUserAccountID},
440+
});
441+
initOnyxDerivedValues();
442+
443+
return waitForBatchedUpdates();
444+
});
445+
446+
beforeEach(() => {
447+
const policyCollectionDataSet = toCollectionDataSet(ONYXKEYS.COLLECTION.POLICY, [adminPolicy], (current) => current.id);
448+
Onyx.multiSet({
449+
[`${ONYXKEYS.COLLECTION.TRANSACTION}${RECEIPT_IOU_TRANSACTION_ID}`]: moneyRequestTransaction,
450+
...policyCollectionDataSet,
451+
});
452+
return waitForBatchedUpdates();
453+
});
454+
455+
afterEach(() => {
456+
Onyx.clear();
457+
return waitForBatchedUpdates();
458+
});
459+
460+
it('should return false for receipt field when the expense report is closed', async () => {
461+
// Given a closed expense report where the current user is an admin
462+
const closedExpenseReport = {
463+
...createExpenseReport(Number(RECEIPT_IOU_REPORT_ID)),
464+
policyID: receiptPolicyID,
465+
ownerAccountID: currentUserAccountID,
466+
managerID: secondUserAccountID,
467+
stateNum: CONST.REPORT.STATE_NUM.APPROVED,
468+
statusNum: CONST.REPORT.STATUS_NUM.CLOSED,
469+
};
470+
471+
await Onyx.merge(`${ONYXKEYS.COLLECTION.REPORT}${RECEIPT_IOU_REPORT_ID}`, closedExpenseReport);
472+
await waitForBatchedUpdates();
473+
474+
// When the admin tries to edit the receipt field
475+
const canEditReceipt = canEditFieldOfMoneyRequest(reportAction, CONST.EDIT_REQUEST_FIELD.RECEIPT);
476+
477+
// Then they should not be able to edit the receipt on a closed report
478+
expect(canEditReceipt).toBe(false);
479+
});
480+
481+
it('should return true for receipt field when the expense report is open', async () => {
482+
// Given an open expense report where the current user is an admin
483+
const openExpenseReport = {
484+
...createExpenseReport(Number(RECEIPT_IOU_REPORT_ID)),
485+
policyID: receiptPolicyID,
486+
ownerAccountID: currentUserAccountID,
487+
managerID: secondUserAccountID,
488+
stateNum: CONST.REPORT.STATE_NUM.OPEN,
489+
statusNum: CONST.REPORT.STATUS_NUM.OPEN,
490+
};
491+
492+
await Onyx.merge(`${ONYXKEYS.COLLECTION.REPORT}${RECEIPT_IOU_REPORT_ID}`, openExpenseReport);
493+
await waitForBatchedUpdates();
494+
495+
// When the admin tries to edit the receipt field
496+
const canEditReceipt = canEditFieldOfMoneyRequest(reportAction, CONST.EDIT_REQUEST_FIELD.RECEIPT);
497+
498+
// Then they should be able to edit the receipt on an open report
499+
expect(canEditReceipt).toBe(true);
500+
});
501+
});
399502
});

0 commit comments

Comments
 (0)