diff --git a/src/libs/actions/App.ts b/src/libs/actions/App.ts index 77427b5f42cc..5a594a19e15a 100644 --- a/src/libs/actions/App.ts +++ b/src/libs/actions/App.ts @@ -28,7 +28,7 @@ import type {OnyxData} from '@src/types/onyx/Request'; import {setShouldForceOffline} from './Network'; import * as PersistedRequests from './PersistedRequests'; import * as Policy from './Policy/Policy'; -import resolveDuplicationConflictAction from './RequestConflictUtils'; +import {resolveDuplicationConflictAction} from './RequestConflictUtils'; import * as Session from './Session'; import Timing from './Timing'; diff --git a/src/libs/actions/Report.ts b/src/libs/actions/Report.ts index dce8f2d19559..4af2357fc572 100644 --- a/src/libs/actions/Report.ts +++ b/src/libs/actions/Report.ts @@ -111,7 +111,7 @@ import {isEmptyObject} from '@src/types/utils/EmptyObject'; import * as CachedPDFPaths from './CachedPDFPaths'; import * as Modal from './Modal'; import navigateFromNotification from './navigateFromNotification'; -import resolveDuplicationConflictAction from './RequestConflictUtils'; +import {createUpdateCommentMatcher, resolveDuplicationConflictAction} from './RequestConflictUtils'; import * as Session from './Session'; import * as Welcome from './Welcome'; import * as OnboardingFlow from './Welcome/OnboardingFlow'; @@ -1695,7 +1695,14 @@ function editReportComment(reportID: string, originalReportAction: OnyxEntry resolveDuplicationConflictAction(persistedRequests, createUpdateCommentMatcher(reportActionID)), + }, + ); } /** Deletes the draft for a comment report action. */ diff --git a/src/libs/actions/RequestConflictUtils.ts b/src/libs/actions/RequestConflictUtils.ts index fcf9ff439b11..36552a6fb5e2 100644 --- a/src/libs/actions/RequestConflictUtils.ts +++ b/src/libs/actions/RequestConflictUtils.ts @@ -1,6 +1,13 @@ +import {WRITE_COMMANDS} from '@libs/API/types'; import type OnyxRequest from '@src/types/onyx/Request'; import type {ConflictActionData} from '@src/types/onyx/Request'; +function createUpdateCommentMatcher(reportActionID: string) { + return function (request: OnyxRequest) { + return request.command === WRITE_COMMANDS.UPDATE_COMMENT && request.data?.reportActionID === reportActionID; + }; +} + type RequestMatcher = (request: OnyxRequest) => boolean; /** @@ -28,4 +35,4 @@ function resolveDuplicationConflictAction(persistedRequests: OnyxRequest[], requ }; } -export default resolveDuplicationConflictAction; +export {resolveDuplicationConflictAction, createUpdateCommentMatcher}; diff --git a/tests/actions/ReportTest.ts b/tests/actions/ReportTest.ts index 5ac32866c77f..c4327158b562 100644 --- a/tests/actions/ReportTest.ts +++ b/tests/actions/ReportTest.ts @@ -808,4 +808,32 @@ describe('actions/Report', () => { TestHelper.expectAPICommandToHaveBeenCalled(WRITE_COMMANDS.OPEN_REPORT, 4); }); + + it('it should only send the last sequential UpdateComment request to BE', async () => { + global.fetch = TestHelper.getGlobalFetchMock(); + const reportID = '123'; + + await Onyx.set(ONYXKEYS.NETWORK, {isOffline: true}); + + const action: OnyxEntry = { + reportID, + reportActionID: '722', + actionName: 'ADDCOMMENT', + created: '2024-10-21 10:37:59.881', + }; + + Report.editReportComment(reportID, action, 'value1'); + Report.editReportComment(reportID, action, 'value2'); + Report.editReportComment(reportID, action, 'value3'); + + const requests = PersistedRequests?.getAll(); + + expect(requests.length).toBe(1); + expect(requests?.at(0)?.command).toBe(WRITE_COMMANDS.UPDATE_COMMENT); + expect(requests?.at(0)?.data?.reportComment).toBe('value3'); + + await Onyx.set(ONYXKEYS.NETWORK, {isOffline: false}); + + TestHelper.expectAPICommandToHaveBeenCalled(WRITE_COMMANDS.UPDATE_COMMENT, 1); + }); }); diff --git a/tests/unit/RequestConflictUtilsTest.ts b/tests/unit/RequestConflictUtilsTest.ts index 103834ce52d2..98ffe50e62ba 100644 --- a/tests/unit/RequestConflictUtilsTest.ts +++ b/tests/unit/RequestConflictUtilsTest.ts @@ -1,4 +1,4 @@ -import resolveDuplicationConflictAction from '@libs/actions/RequestConflictUtils'; +import {resolveDuplicationConflictAction} from '@libs/actions/RequestConflictUtils'; import type {WriteCommand} from '@libs/API/types'; describe('RequestConflictUtils', () => {