-
Notifications
You must be signed in to change notification settings - Fork 3k
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 #23457 from neonbhai/23221-onyxMigrationForPreviou…
…sReportActionID Onyx Migration for checkPreviousReportActionID
- Loading branch information
Showing
2 changed files
with
238 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
import _ from 'underscore'; | ||
import Onyx from 'react-native-onyx'; | ||
import Log from '../Log'; | ||
import ONYXKEYS from '../../ONYXKEYS'; | ||
|
||
/** | ||
* @returns {Promise<Object>} | ||
*/ | ||
function getReportActionsFromOnyx() { | ||
return new Promise((resolve) => { | ||
const connectionID = Onyx.connect({ | ||
key: ONYXKEYS.COLLECTION.REPORT_ACTIONS, | ||
waitForCollectionCallback: true, | ||
callback: (allReportActions) => { | ||
Onyx.disconnect(connectionID); | ||
return resolve(allReportActions); | ||
}, | ||
}); | ||
}); | ||
} | ||
|
||
/** | ||
* This migration checks for the 'previousReportActionID' key in the first valid reportAction of a report in Onyx. | ||
* If the key is not found then all reportActions for all reports are removed from Onyx. | ||
* | ||
* @returns {Promise<void>} | ||
*/ | ||
export default function () { | ||
return getReportActionsFromOnyx().then((allReportActions) => { | ||
if (_.isEmpty(allReportActions)) { | ||
Log.info(`[Migrate Onyx] Skipped migration CheckForPreviousReportActionID because there were no reportActions`); | ||
return; | ||
} | ||
|
||
let firstValidValue; | ||
_.some(_.values(allReportActions), (reportActions) => | ||
_.some(_.values(reportActions), (reportActionData) => { | ||
if (_.has(reportActionData, 'reportActionID')) { | ||
firstValidValue = reportActionData; | ||
return true; | ||
} | ||
|
||
return false; | ||
}), | ||
); | ||
|
||
if (_.isUndefined(firstValidValue)) { | ||
Log.info(`[Migrate Onyx] Skipped migration CheckForPreviousReportActionID because there were no valid reportActions`); | ||
return; | ||
} | ||
|
||
if (_.has(firstValidValue, 'previousReportActionID')) { | ||
Log.info(`[Migrate Onyx] CheckForPreviousReportActionID Migration: previousReportActionID found. Migration complete`); | ||
return; | ||
} | ||
|
||
// If previousReportActionID not found: | ||
Log.info(`[Migrate Onyx] CheckForPreviousReportActionID Migration: removing all reportActions because previousReportActionID not found in the first valid reportAction`); | ||
|
||
const onyxData = {}; | ||
_.each(allReportActions, (reportAction, onyxKey) => { | ||
onyxData[onyxKey] = {}; | ||
}); | ||
|
||
return Onyx.multiSet(onyxData); | ||
}); | ||
} |
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