forked from Expensify/App
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request Expensify#50258 from callstack-internal/gedu/openA…
…pp_conflict_resolver [NoQA] Request conflict resolution for OpenApp
- Loading branch information
Showing
4 changed files
with
93 additions
and
18 deletions.
There are no files selected for viewing
This file contains 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 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,30 @@ | ||
import type {WriteCommand} from '@libs/API/types'; | ||
import type OnyxRequest from '@src/types/onyx/Request'; | ||
import type {ConflictActionData} from '@src/types/onyx/Request'; | ||
|
||
/** | ||
* Resolves duplication conflicts between persisted requests and a given command. | ||
* | ||
* This method checks if a specific command exists within a list of persisted requests. | ||
* - If the command is not found, it suggests adding the command to the list, indicating a 'push' action. | ||
* - If the command is found, it suggests updating the existing entry, indicating a 'replace' action at the found index. | ||
*/ | ||
function resolveDuplicationConflictAction(persistedRequests: OnyxRequest[], commandToFind: WriteCommand): ConflictActionData { | ||
const index = persistedRequests.findIndex((request) => request.command === commandToFind); | ||
if (index === -1) { | ||
return { | ||
conflictAction: { | ||
type: 'push', | ||
}, | ||
}; | ||
} | ||
|
||
return { | ||
conflictAction: { | ||
type: 'replace', | ||
index, | ||
}, | ||
}; | ||
} | ||
|
||
export default resolveDuplicationConflictAction; |
This file contains 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 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,21 @@ | ||
import resolveDuplicationConflictAction from '@libs/actions/RequestConflictUtils'; | ||
import type {WriteCommand} from '@libs/API/types'; | ||
|
||
describe('RequestConflictUtils', () => { | ||
it.each([['OpenApp'], ['ReconnectApp']])('resolveDuplicationConflictAction when %s do not exist in the queue should push %i', (command) => { | ||
const persistedRequests = [{command: 'OpenReport'}, {command: 'AddComment'}, {command: 'CloseAccount'}]; | ||
const commandToFind = command as WriteCommand; | ||
const result = resolveDuplicationConflictAction(persistedRequests, commandToFind); | ||
expect(result).toEqual({conflictAction: {type: 'push'}}); | ||
}); | ||
|
||
it.each([ | ||
['OpenApp', 0], | ||
['ReconnectApp', 2], | ||
])('resolveDuplicationConflictAction when %s exist in the queue should replace at index %i', (command, index) => { | ||
const persistedRequests = [{command: 'OpenApp'}, {command: 'AddComment'}, {command: 'ReconnectApp'}]; | ||
const commandToFind = command as WriteCommand; | ||
const result = resolveDuplicationConflictAction(persistedRequests, commandToFind); | ||
expect(result).toEqual({conflictAction: {type: 'replace', index}}); | ||
}); | ||
}); |