Skip to content

Add an audit trail for receipt additions - #103279

Merged
arosiclair merged 17 commits into
Expensify:mainfrom
dukenv0307:add-an-audit-trail-for-receipt-additions
Oct 13, 2026
Merged

arosiclair merged 17 commits into
Expensify:mainfrom
dukenv0307:add-an-audit-trail-for-receipt-additions

Conversation

@dukenv0307

@dukenv0307 dukenv0307 commented Oct 7, 2026 •

Copy link
Copy Markdown
Contributor

Explanation of Change

NewDot now posts an optimistic audit action when a receipt is added or removed, and a replacement shows as a removal followed by an addition.

Fixed Issues

$ #102533
$ #102537

PROPOSAL:

Tests

Test case 1:

  1. Submitter submit an expense without receipt
  2. Submitter or admin add new receipt
  3. Verify that the message added a receipt is created
  4. Replace the receipt
  5. Verify that there're 2 messages: Removed a receipt & Added a receipt

Test case 2:

  1. Submitter submit an expense without receipt
  2. Submitter or admin add new receipt
  3. Verify that the message added a receipt is created
  4. Remove the receipt
  5. Observe the message removed a receipt is shown
  6. Reset cache and restart
  7. Observe the message removed a receipt is preserved

Test case 3:

  1. Launch app
  2. Open workspace chat
  3. Toggle on forced offline
  4. Create a manual expense entering Merchant
  5. Open the expense and add the receipt via camera image
  6. Note add receipt message is shown
  7. Remove the receipt and verify remove receipt message is shown
  8. Add receipt via choose file and note add receipt message is shown
  9. Now delete receipt
  10. Toggle off forced offline
  11. Verify that: Add receipt message must not be missing and system message must not reorder.

Test case 4:

  1. Launch app
  2. Create a manual expense in self dm entering Merchant
  3. Open the expense and add a receipt via camera image
  4. Note added a receipt system message is shown
  5. Tap receipt and delete it
  6. Verify that: Remove receipt system message must be shown in self dm on removing the receipt.
  • Verify that no errors appear in the JS console

Offline tests

Same as above

QA Steps

Same as above

  • Verify that no errors appear in the JS console

PR Author Checklist

  • I linked the correct issue in the ### Fixed Issues section above
  • I wrote clear testing steps that cover the changes made in this PR
    • I added steps for local testing in the Tests section
    • I added steps for the expected offline behavior in the Offline steps section
    • I added steps for Staging and/or Production testing in the QA steps section
    • I added steps to cover failure scenarios (i.e. verify an input displays the correct error message if the entered data is not correct)
    • I turned off my network connection and tested it while offline to ensure it matches the expected behavior (i.e. verify the default avatar icon is displayed if app is offline)
    • I tested this PR with a High Traffic account against the staging or production API to ensure there are no regressions (e.g. long loading states that impact usability).
  • I included screenshots or videos for tests on all platforms
  • I ran the tests on all platforms & verified they passed on:
    • Android: Native
    • Android: mWeb Chrome
    • iOS: Native
    • iOS: mWeb Safari
    • MacOS: Chrome / Safari
  • I verified there are no console errors (if there's a console error not related to the PR, report it or open an issue for it to be fixed)
  • I followed proper code patterns (see Reviewing the code)
    • I verified that any callback methods that were added or modified are named for what the method does and never what callback they handle (i.e. toggleReport and not onIconClick)
    • I verified that comments were added to code that is not self explanatory
    • I verified that any new or modified comments were clear, correct English, and explained "why" the code was doing something instead of only explaining "what" the code was doing.
    • I verified any copy / text that was added to the app is grammatically correct in English. It adheres to proper capitalization guidelines (note: only the first word of header/labels should be capitalized), and is either coming verbatim from figma or has been approved by marketing (in order to get marketing approval, ask the Bug Zero team member to add the Waiting for copy label to the issue)
  • If a new code pattern is added I verified it was agreed to be used by multiple Expensify engineers
  • I followed the guidelines as stated in the Review Guidelines
  • I tested other components that can be impacted by my changes (i.e. if the PR modifies a shared library or component like Avatar, I verified the components using Avatar are working as expected)
  • If any new file was added I verified that:
    • The file has a description of what it does and/or why is needed at the top of the file if the code is not self explanatory
  • If a new CSS style is added I verified that:
    • A similar style doesn't already exist
    • The style can't be created with an existing StyleUtils function (i.e. StyleUtils.getBackgroundAndBorderStyle(theme.componentBG))
  • If new assets were added or existing ones were modified, I verified that:
    • The assets are optimized and compressed (for SVG files, run npm run compress-svg)
    • The assets load correctly across all supported platforms.
  • If the PR modifies code that runs when editing or sending messages, I tested and verified there is no unexpected behavior for all supported markdown - URLs, single line code, code blocks, quotes, headings, bold, strikethrough, and italic.
  • If the PR modifies a generic component, I tested and verified that those changes do not break usages of that component in the rest of the App (i.e. if a shared library or component like Avatar is modified, I verified that Avatar is working as expected in all cases)
  • If the PR modifies a component related to any of the existing Storybook stories, I tested and verified all stories for that component are still working as expected.
  • If the PR modifies a component or page that can be accessed by a direct deeplink, I verified that the code functions as expected when the deeplink is used - from a logged in and logged out account.
  • If the PR modifies the UI (e.g. new buttons, new UI components, changing the padding/spacing/sizing, moving components, etc) or modifies the form input styles:
    • I verified that all the inputs inside a form are aligned with each other.
    • I added Design label and/or tagged @Expensify/design so the design team can review the changes.
  • I added unit tests for any new feature or bug fix in this PR to help automatically prevent regressions in this user flow.
  • If the main branch was merged into this PR after a review, I tested again and verified the outcome was still expected according to the Test steps.

Screenshots/Videos

Android: Native
Android: mWeb Chrome
iOS: Native
iOS: mWeb Safari
MacOS: Chrome / Safari
Screen.Recording.2026-09-30.at.08.48.12.mov
Screen.Recording.2026-09-29.at.22.54.11.mov
Screen.Recording.2026-09-30.at.08.44.27.mov

@melvin-bot

melvin-bot Bot commented Oct 7, 2026

Copy link
Copy Markdown

Hey, I noticed you changed src/languages/en.ts in a PR from a fork. For security reasons, translations are not generated automatically for PRs from forks.

If you want to automatically generate translations for other locales, an Expensify employee will have to:

  1. Look at the code and make sure there are no malicious changes.
  2. Run the Generate static translations GitHub workflow. If you have write access and the K2 extension, you can simply click: [this button]

Alternatively, if you are an external contributor, you can run the translation script locally with your own OpenAI API key. To learn more, try running:

npx bun ./scripts/generateTranslations.ts --help

Typically, you'd want to translate only what you changed by running npx bun ./scripts/generateTranslations.ts --compare-ref main

@codecov

codecov Bot commented Oct 7, 2026 •

Copy link
Copy Markdown

Codecov Report

❌ Looks like you've decreased code coverage for some files. Please write tests to increase, or at least maintain, the existing level of code coverage. See our documentation here for how to interpret this table.

Files with missing lines Coverage Δ
...nents/ReportActionItem/MoneyRequestReceiptView.tsx 72.26% <100.00%> (+0.10%) ⬆️
src/libs/ModifiedExpenseMessage.ts 91.59% <100.00%> (+0.07%) ⬆️
...oadRetryHandler/buildReplaceReceiptRetryPayload.ts 93.54% <100.00%> (+0.21%) ⬆️
src/libs/ReportActionsUtils.ts 84.55% <100.00%> (+0.03%) ⬆️
src/libs/actions/IOU/Receipt.ts 87.96% <100.00%> (+1.41%) ⬆️
src/libs/actions/RequestConflictUtils.ts 100.00% <ø> (+0.84%) ⬆️
...inbox/report/ReportActionCompose/useReceiptDrop.ts 56.25% <100.00%> (+0.93%) ⬆️
.../IOURequestStepScan/components/ScanEditReceipt.tsx 91.89% <100.00%> (+0.22%) ⬆️
src/libs/ReportUtils.ts 88.66% <77.77%> (-0.01%) ⬇️
...alScreen/routes/TransactionReceiptModalContent.tsx 0.00% <0.00%> (ø)
... and 10 files with indirect coverage changes

@dukenv0307

Copy link
Copy Markdown
Contributor Author

@MelvinBot review

@MelvinBot

Copy link
Copy Markdown
Contributor

Code review

The overall approach works. Each receipt request now carries its own audit action IDs, so dropping resolveDetachReceiptConflicts makes sense. I found two bugs worth fixing before merge, plus two smaller notes.

1. "Removed" and "added" can show in the wrong order on a replace

In Receipt.ts:386-406, the code builds the "added" action first with its own getDBTime(). It then calls getDBTime() a second time and subtracts 1 ms for the "removed" action. If the clock moves forward 1 ms between the two calls, both actions get the same created. If it moves forward more, "removed" comes after "added".

sortReportActions breaks a created tie using the random reportActionID. So in that case the order is random.

Fix: Get the time once with const created = DateUtils.getDBTime(). Pass created to buildOptimisticReceiptAddedAction (it needs a new created param) and created - 1ms to the removed action.

2. Offline "add, then replace" shows no "removed a receipt" message

The removed action only appears when hasUploadedReceipt(transaction) is true, and that checks receipt.receiptID. The optimistic receipt from replaceReceipt has no receiptID, and detachReceipt sets receipt to null. So a receipt added while offline (or after a removal) has no receiptID.

Steps to reproduce:

  1. Go offline.
  2. Add a receipt to an expense that has none.
  3. Replace that receipt.

Only "added a receipt" shows. The "removed" message never appears optimistically, and the backend's version has no ID to reconcile with. Test case 3 doesn't catch this because it never replaces a receipt that was added offline.

Fix: Check for any existing receipt, for example hasReceiptSource(transaction) || hasUploadedReceipt(transaction). Add a unit test for this flow.

3. Minor: the self DM fallback only covers the receipt modal

The new transactionThread fallback in TransactionReceiptModalContent.tsx:180 is limited to that screen. These callers still find the thread through transaction.reportID, which fails for unreported self DM expenses:

In self DM, replacing a receipt through drag-and-drop, the receipt view, or the scan edit flow still shows no optimistic audit messages. Think about moving the fallback into a shared helper.

4. Please confirm backend support

DetachReceipt and ReplaceReceipt now send receiptRemovedReportActionID. Please confirm the backend reads this field. If it doesn't, the server creates its own "removed" action and you'll see duplicates after sync. This also matters because DetachReceipt no longer shows an optimistic MANAGER_DETACH_RECEIPT action on the expense report.

I reviewed the code only and didn't run the test steps.

@dukenv0307

Copy link
Copy Markdown
Contributor Author

@MelvinBot BE is working well. Pls review again. Is it ok now?

@MelvinBot

Copy link
Copy Markdown
Contributor

Yes, this looks good now. Commit 5794f77 fixes all four points from my last review. All CI checks pass except Check independent approval, which is expected before approval.

  1. Order on replace: fixed. Both actions now use one created value, and the "removed" action is 1 ms earlier. They can no longer tie or swap.
  2. Offline add, then replace: fixed. isReplacingReceipt now also checks hasReceiptSource, so a receipt added offline counts as being replaced. The new test covers this flow.
  3. Self DM thread: fixed. The shared getTransactionThreadReport helper is now used by the receipt modal, MoneyRequestReceiptView, useReceiptDrop, and ScanEditReceipt. It has its own unit tests.
  4. Backend: you confirmed the backend works with the new action IDs.

I didn't find any new problems. Map-distance expenses carry a map image as receipt.source, but they can't trigger a false "removed" message because their receipts can't be edited (ReportUtils.ts:5654).

I reviewed the code only and didn't run the test steps.

@dukenv0307
dukenv0307 marked this pull request as ready for review October 7, 2026 04:33
@dukenv0307
dukenv0307 requested review from a team as code owners October 7, 2026 04:33
@melvin-bot
melvin-bot Bot requested review from flaviadefaria and truph01 and removed request for a team October 7, 2026 04:33
@melvin-bot

melvin-bot Bot commented Oct 7, 2026

Copy link
Copy Markdown

@truph01 Please copy/paste the Reviewer Checklist from here into a new comment on this PR and complete it. If you have the K2 extension, you can simply click: [this button]

@melvin-bot
melvin-bot Bot removed the request for review from a team October 7, 2026 04:33
Comment thread src/libs/ReportActionsUtils.ts Resolved
@github-actions

github-actions Bot commented Oct 7, 2026

Copy link
Copy Markdown
Contributor

🚧 flaviadefaria has triggered a test Expensify/App build. You can view the workflow run here.

@github-actions

This comment has been minimized.

@dukenv0307

Copy link
Copy Markdown
Contributor Author

@truph01 could you please review it?

@flaviadefaria

Copy link
Copy Markdown
Contributor

Just tested and it seems to be working as expected 🙌

flaviadefaria
flaviadefaria previously approved these changes Oct 8, 2026

@flaviadefaria flaviadefaria left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM!

@melvin-bot
melvin-bot Bot requested a review from arosiclair October 12, 2026 11:45
Comment thread src/types/onyx/OriginalMessage.ts Resolved
Comment thread src/libs/actions/IOU/Receipt.ts Resolved
Comment thread src/libs/ReportActionsUtils.ts Resolved
dukenv0307 and others added 4 commits October 13, 2026 08:40
@dukenv0307

Copy link
Copy Markdown
Contributor Author

@MelvinBot Is the failing ts check related to this PR?

@MelvinBot

Copy link
Copy Markdown
Contributor

No, the failing typecheck is unrelated to this PR. It's already broken on main.

All 3 errors are in tests/actions/IOUTest/SplitTest.ts, which this PR doesn't touch. Thread report drafts explicitly through the split-transaction update flow merged yesterday and made reportDrafts a required param of UpdateSplitTransactionsParams. However, 3 updateSplitTransactionsFromSplitExpensesFlow calls in that test still don't pass it. You can fix this by merging main once a fix lands there.

Evidence

view run

@flaviadefaria

Copy link
Copy Markdown
Contributor

Can we try to merge this today?

@dukenv0307
dukenv0307 requested a review from arosiclair October 13, 2026 11:41
@arosiclair
arosiclair merged commit f6d346b into Expensify:main Oct 13, 2026
35 of 36 checks passed
@OSBotify

Copy link
Copy Markdown
Contributor

✋ This PR was not deployed to staging yet because QA is ongoing. It will be automatically deployed to staging after the next production release.

@github-actions

Copy link
Copy Markdown
Contributor

🚧 arosiclair has triggered a test Expensify/App build. You can view the workflow run here.

@OSBotify

Copy link
Copy Markdown
Contributor

🚀 Deployed to staging by https://github.com/arosiclair in version: 9.5.9-0 🚀

platform result
🕸 web 🕸 success ✅
🤖 android 🤖 success ✅
🍎 iOS 🍎 success ✅

@MelvinBot

Copy link
Copy Markdown
Contributor

No help site update needed. Attach and edit receipts on expenses already says that adding or replacing a receipt posts an "added a receipt" message and that removing one also posts a system message. This PR keeps that behavior true, so no sentence in the article is now wrong.


view run

@OSBotify

Copy link
Copy Markdown
Contributor

🚀 Deployed to production by https://github.com/cristipaval in version: 9.5.9-4 🚀

platform result
🕸 web 🕸 success ✅
🤖 android 🤖 success ✅
🍎 iOS 🍎 success ✅

Bundle Size Analysis (Sentry):

@truph01

truph01 commented Oct 26, 2026

Copy link
Copy Markdown
Contributor

@MelvinBot Could you identify the PR that introduced the bug fixed by this PR and add a brief comment summarizing the bug and its root cause in that PR?

@MelvinBot

Copy link
Copy Markdown
Contributor

This PR fixed two bugs, and each came from a different PR. I posted a root-cause comment on each one:

Bug Offending PR Comment
[Due for payment 2026-10-22] Expense - Remove receipt system message is not shown in self dm on removing the receipt. fix: display message when user detach a receipt (merged 2025-01-30) link
[Due for payment 2026-10-22] Expense - Add receipt message is missing and system message reorder. [Payment due @truph01] Add an audit trail for receipt additions (merged 2026-09-28) link

The second bug changes your earlier checklist answer. Only the reordering was there before. The missing "added a receipt" message is a regression from that PR, because it added the rollback that deletes the message.

Root causes
  • Self DM: detachReceipt sent the optimistic detach action to the report under transaction.reportID (IOU.ts:8710-8718). Unreported self-DM expenses have reportID '0', so the action went to reportActions_undefined.
  • Offline add/remove: resolveDetachReceiptConflicts keeps only the last queued ReplaceReceipt. That rule came from fix: Unexpected error when delete cropped receipt. The audit-trail PR then deletes the "added a receipt" action of each dropped request (RequestConflictUtils.ts:381-402). On the second removal, the first add and its message are dropped. The reordering most likely comes from the older drop rule. I based that on the code and didn't check logs.

view run

checkAndFixConflictingRequest: (persistedRequests) => resolveDetachReceiptConflicts(persistedRequests, parameters, transactionThreadReportID),
},
);
API.write(WRITE_COMMANDS.DETACH_RECEIPT, parameters, {optimisticData, successData, failureData});

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This call lost the checkAndFixConflictingRequest option, and the PR also removed resolveDetachReceiptConflicts. That resolver was what dropped the older queued ReplaceReceipt requests for the same transaction once a detach was queued.

I understand why it went (it also dropped "added a receipt" messages from earlier add/delete cycles), but nothing took its place. Now if you replace a receipt several times offline and then delete it, every ReplaceReceipt gets sent on reconnect: #107135. Fixed in #107358.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

7 participants