Repository navigation
[Payment due @cretadn22] Fix silent receipt drop when file is missing from cache #86581
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
Merged
Merged
Changes from all commits
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
c364ec8
Fix silent receipt drop when file is missing from cache
neil-marcellini 326cb51
Replace throw with Log.alert when receipt file is missing
neil-marcellini dfdaeef
Add durable storage tests for receipt crop/rotate flows
neil-marcellini 842d289
Remove branch-relative comments from durable storage test
neil-marcellini 0995ca7
Remove empty-source receipt test case
neil-marcellini ce7b65c
Fix CI: prettier formatting and deprecated absoluteFillObject
neil-marcellini d83e3d3
Merge remote-tracking branch 'origin/main' into neil-marcellini/fix-s…
neil-marcellini dadbfd3
Address AI review feedback: extract helper, preserve File, add comment
neil-marcellini e80445c
Preserve File prototype in applyDurableReceipt after durable storage …
neil-marcellini 7b3ab0d
Address reviewer feedback: eliminate redundant param, use moveFile
neil-marcellini eece09c
Use transaction.transactionID directly instead of abbreviated local
neil-marcellini 54d1d94
Simplify upload folder creation: mkdir first, verify after
neil-marcellini cab6596
Generate unique durable paths to prevent filename collisions
neil-marcellini f693d7e
Use rand64() for unique durable receipt filenames
neil-marcellini 8c511f2
Retain original filename in durable receipt path
neil-marcellini 21742dd
Merge origin/main into neil-marcellini/fix-silent-receipt-drop
neil-marcellini 545c88f
Remove weird file
neil-marcellini File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,42 @@ | ||
| import RNFS from 'react-native-fs'; | ||
| import getReceiptsUploadFolderPath from '@libs/getReceiptsUploadFolderPath'; | ||
| import Log from '@libs/Log'; | ||
| import {rand64} from '@libs/NumberUtils'; | ||
| import type MoveReceiptToDurableStorage from './types'; | ||
|
|
||
| /** | ||
| * Moves a receipt image from an ephemeral location (e.g., ImageManipulator cache) | ||
| * to the durable Receipts-Upload directory so it persists until the upload completes. | ||
| * | ||
| * iOS can purge files in Library/Caches/ at any time, so receipts that are queued | ||
| * for upload via the SequentialQueue must live in a durable directory. | ||
| */ | ||
| const moveReceiptToDurableStorage: MoveReceiptToDurableStorage = async (sourceUri, fileName) => { | ||
| const uploadFolder = getReceiptsUploadFolderPath(); | ||
| if (!uploadFolder) { | ||
| return sourceUri; | ||
| } | ||
|
|
||
| try { | ||
| await RNFS.mkdir(uploadFolder); | ||
|
|
||
| if (!(await RNFS.exists(uploadFolder))) { | ||
| Log.alert('[moveReceiptToDurableStorage] Upload folder does not exist after mkdir', {uploadFolder}); | ||
| return sourceUri; | ||
| } | ||
|
|
||
| const sourcePath = sourceUri.replace('file://', ''); | ||
| const dotIndex = fileName.lastIndexOf('.'); | ||
| const uniqueName = dotIndex > 0 ? `${fileName.slice(0, dotIndex)}_${rand64()}${fileName.slice(dotIndex)}` : `${fileName}_${rand64()}`; | ||
| const destPath = `${uploadFolder}/${uniqueName}`; | ||
|
|
||
| await RNFS.moveFile(sourcePath, destPath); | ||
|
|
||
| return `file://${destPath}`; | ||
| } catch (error) { | ||
| Log.warn('[moveReceiptToDurableStorage] Failed to move receipt, using original path', {error: error instanceof Error ? error.message : String(error)}); | ||
| return sourceUri; | ||
| } | ||
| }; | ||
|
|
||
| export default moveReceiptToDurableStorage; | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,8 @@ | ||
| import type MoveReceiptToDurableStorage from './types'; | ||
|
|
||
| /** | ||
| * Web no-op: blob/object URLs are held in memory and don't need durable filesystem storage. | ||
| */ | ||
| const moveReceiptToDurableStorage: MoveReceiptToDurableStorage = (sourceUri) => Promise.resolve(sourceUri); | ||
|
|
||
| export default moveReceiptToDurableStorage; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| type MoveReceiptToDurableStorage = (sourceUri: string, fileName: string) => Promise<string>; | ||
|
|
||
| export default MoveReceiptToDurableStorage; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,87 @@ | ||
| import type PrepareRequestPayload from '@libs/prepareRequestPayload/types'; | ||
|
|
||
| const mockCheckFileExists = jest.fn<Promise<boolean>, [string | undefined]>(); | ||
| jest.mock('@libs/fileDownload/checkFileExists', () => ({ | ||
| // eslint-disable-next-line @typescript-eslint/naming-convention | ||
| __esModule: true, | ||
| default: mockCheckFileExists, | ||
| })); | ||
|
|
||
| jest.mock('@libs/fileDownload/FileUtils', () => ({ | ||
| readFileAsync: jest.fn(() => Promise.resolve(null)), | ||
| })); | ||
|
|
||
| jest.mock('@libs/validateFormDataParameter', () => ({ | ||
| // eslint-disable-next-line @typescript-eslint/naming-convention | ||
| __esModule: true, | ||
| default: jest.fn(), | ||
| })); | ||
|
|
||
| const mockLogAlert = jest.fn(); | ||
| jest.mock('@libs/Log', () => ({ | ||
| // eslint-disable-next-line @typescript-eslint/naming-convention | ||
| __esModule: true, | ||
| default: {alert: mockLogAlert}, | ||
| })); | ||
|
|
||
| // Bypass the global jest/setup.ts mock to test the real native implementation. | ||
| // Dependencies above are still resolved through their respective mocks. | ||
| // eslint-disable-next-line @typescript-eslint/no-require-imports, @typescript-eslint/no-var-requires | ||
| const {default: prepareRequestPayload}: {default: PrepareRequestPayload} = jest.requireActual('@libs/prepareRequestPayload/index.native.ts'); | ||
|
|
||
| describe('prepareRequestPayload (native)', () => { | ||
| afterEach(() => { | ||
| jest.clearAllMocks(); | ||
| }); | ||
|
|
||
| it('should include receipt in FormData when the file exists', async () => { | ||
| mockCheckFileExists.mockResolvedValue(true); | ||
|
|
||
| const receipt = { | ||
| source: 'file:///var/mobile/Documents/Receipts-Upload/receipt.jpg', | ||
| name: 'receipt.jpg', | ||
| type: 'image/jpeg', | ||
| uri: 'file:///var/mobile/Documents/Receipts-Upload/receipt.jpg', | ||
| }; | ||
|
|
||
| const formData = await prepareRequestPayload('RequestMoney', {receipt, amount: '100'}, false); | ||
|
|
||
| expect(formData.has('receipt')).toBe(true); | ||
| expect(formData.get('amount')).toBe('100'); | ||
| }); | ||
|
|
||
| it('should log an alert and omit receipt from FormData when file does not exist', async () => { | ||
| mockCheckFileExists.mockResolvedValue(false); | ||
|
|
||
| const receipt = { | ||
| source: 'file:///var/mobile/Library/Caches/ImageManipulator/receipt.jpg', | ||
| name: 'receipt.jpg', | ||
| type: 'image/jpeg', | ||
| uri: 'file:///var/mobile/Library/Caches/ImageManipulator/receipt.jpg', | ||
| }; | ||
|
|
||
| const formData = await prepareRequestPayload('RequestMoney', {receipt, amount: '100'}, false); | ||
|
|
||
| expect(formData.has('receipt')).toBe(false); | ||
| expect(formData.get('amount')).toBe('100'); | ||
| expect(mockLogAlert).toHaveBeenCalledWith('[prepareRequestPayload] Receipt file missing at upload time', { | ||
| command: 'RequestMoney', | ||
| source: 'file:///var/mobile/Library/Caches/ImageManipulator/receipt.jpg', | ||
| fileName: 'receipt.jpg', | ||
| }); | ||
| }); | ||
|
|
||
| it('should handle non-receipt data normally', async () => { | ||
| const formData = await prepareRequestPayload('SomeCommand', {amount: '100', currency: 'USD'}, false); | ||
|
|
||
| expect(formData.get('amount')).toBe('100'); | ||
| expect(formData.get('currency')).toBe('USD'); | ||
| }); | ||
|
|
||
| it('should skip undefined values', async () => { | ||
| const formData = await prepareRequestPayload('SomeCommand', {amount: '100', undefinedField: undefined}, false); | ||
|
|
||
| expect(formData.get('amount')).toBe('100'); | ||
| expect(formData.has('undefinedField')).toBe(false); | ||
| }); | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,64 @@ | ||
| jest.mock('react-native-fs', () => ({ | ||
| exists: jest.fn(() => Promise.resolve(true)), | ||
| mkdir: jest.fn(() => Promise.resolve()), | ||
| moveFile: jest.fn(() => Promise.resolve()), | ||
| })); | ||
|
|
||
| const DURABLE_UPLOAD_DIR = '/var/mobile/Documents/Receipts-Upload'; | ||
| jest.mock('@libs/getReceiptsUploadFolderPath', () => ({ | ||
| // eslint-disable-next-line @typescript-eslint/naming-convention | ||
| __esModule: true, | ||
| default: () => DURABLE_UPLOAD_DIR, | ||
| })); | ||
|
|
||
| jest.mock('@libs/Log', () => ({ | ||
| // eslint-disable-next-line @typescript-eslint/naming-convention | ||
| __esModule: true, | ||
| default: {warn: jest.fn(), alert: jest.fn()}, | ||
| })); | ||
|
|
||
| type MoveReceiptFn = (uri: string, fileName: string) => Promise<string>; | ||
|
|
||
| // Bypass the global jest/setup.ts mock to test the real native implementation. | ||
| // eslint-disable-next-line @typescript-eslint/no-require-imports, @typescript-eslint/no-var-requires | ||
|
neil-marcellini marked this conversation as resolved.
Resolved
|
||
| const {default: moveReceiptToDurableStorage}: {default: MoveReceiptFn} = jest.requireActual('@libs/moveReceiptToDurableStorage/index.native.ts'); | ||
|
|
||
| describe('Receipt flows should persist to durable storage after crop/rotate', () => { | ||
| it('should move a cropped receipt out of ImageManipulator cache into Receipts-Upload', async () => { | ||
| const cachePath = 'file:///var/mobile/Library/Caches/ImageManipulator/cropped-abc123.jpg'; | ||
|
|
||
| const result = await moveReceiptToDurableStorage(cachePath, 'receipt.jpg'); | ||
|
|
||
| expect(result).toContain('Receipts-Upload'); | ||
| expect(result).not.toContain('Library/Caches'); | ||
| }); | ||
|
|
||
| it('should move a rotated receipt out of ImageManipulator cache into Receipts-Upload', async () => { | ||
| const cachePath = 'file:///var/mobile/Library/Caches/ImageManipulator/rotated-def456.jpg'; | ||
|
|
||
| const result = await moveReceiptToDurableStorage(cachePath, 'rotated-receipt.jpg'); | ||
|
|
||
| expect(result).toContain('Receipts-Upload'); | ||
| expect(result).not.toContain('Library/Caches'); | ||
| }); | ||
|
|
||
| it('should generate unique destination paths to avoid filename collisions', async () => { | ||
| const cachePath1 = 'file:///var/mobile/Library/Caches/ImageManipulator/img1.jpg'; | ||
| const cachePath2 = 'file:///var/mobile/Library/Caches/ImageManipulator/img2.jpg'; | ||
|
|
||
| const result1 = await moveReceiptToDurableStorage(cachePath1, 'receipt.jpg'); | ||
| const result2 = await moveReceiptToDurableStorage(cachePath2, 'receipt.jpg'); | ||
|
|
||
| expect(result1).not.toBe(result2); | ||
| expect(result1).toMatch(/\.jpg$/); | ||
| expect(result2).toMatch(/\.jpg$/); | ||
| }); | ||
|
|
||
| it('should preserve receipts already in a durable directory', async () => { | ||
| const durablePath = `file://${DURABLE_UPLOAD_DIR}/existing-receipt.jpg`; | ||
|
|
||
| const result = await moveReceiptToDurableStorage(durablePath, 'existing-receipt.jpg'); | ||
|
|
||
| expect(result).toContain('Receipts-Upload'); | ||
| }); | ||
| }); | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.