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.
restructure report actions drafts onyx + migration
- Loading branch information
1 parent
732d48e
commit d6504ca
Showing
8 changed files
with
127 additions
and
5 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
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
53 changes: 53 additions & 0 deletions
53
src/libs/migrations/KeyReportActionsDraftByReportActionID.js
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,53 @@ | ||
import _ from 'underscore'; | ||
import Onyx from 'react-native-onyx'; | ||
import Log from '../Log'; | ||
import ONYXKEYS from '../../ONYXKEYS'; | ||
|
||
/** | ||
* This migration updates reportActionsDrafts data to be keyed by reportActionID. | ||
* | ||
* Before: reportActionsDrafts_reportID_reportActionID: value | ||
* After: reportActionsDrafts_reportID: {[reportActionID]: value} | ||
* | ||
* @returns {Promise} | ||
*/ | ||
export default function () { | ||
return new Promise((resolve) => { | ||
const connectionID = Onyx.connect({ | ||
key: ONYXKEYS.COLLECTION.REPORT_ACTIONS_DRAFTS, | ||
waitForCollectionCallback: true, | ||
callback: (allReportActionsDrafts) => { | ||
Onyx.disconnect(connectionID); | ||
|
||
if (!allReportActionsDrafts) { | ||
Log.info('[Migrate Onyx] Skipped migration KeyReportActionsDraftByReportActionID because there were no reportActionsDrafts'); | ||
return resolve(); | ||
} | ||
|
||
const newReportActionsDrafts = {}; | ||
_.each(allReportActionsDrafts, (reportActionDraft, onyxKey) => { | ||
if (!_.isString(reportActionDraft)) { | ||
return; | ||
} | ||
newReportActionsDrafts[onyxKey] = null; | ||
|
||
const reportActionID = onyxKey.split('_').pop(); | ||
const newOnyxKey = onyxKey.replace(`_${reportActionID}`, ''); | ||
newReportActionsDrafts[newOnyxKey] = { | ||
...(newReportActionsDrafts[newOnyxKey] || allReportActionsDrafts[newOnyxKey]), | ||
[reportActionID]: reportActionDraft, | ||
}; | ||
}); | ||
|
||
if (_.isEmpty(newReportActionsDrafts)) { | ||
Log.info('[Migrate Onyx] Skipped migration KeyReportActionsDraftByReportActionID because there are no actions drafts to migrate'); | ||
return resolve(); | ||
} | ||
|
||
Log.info(`[Migrate Onyx] Re-keying reportActionsDrafts by reportActionID for ${_.keys(newReportActionsDrafts).length} actions drafts`); | ||
// eslint-disable-next-line rulesdir/prefer-actions-set-data | ||
return Onyx.multiSet(newReportActionsDrafts).then(resolve); | ||
}, | ||
}); | ||
}); | ||
} |
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,3 @@ | ||
type ReportActionsDrafts = Record<string, string>; | ||
|
||
export default ReportActionsDrafts; |
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