Skip to content

Commit

Permalink
Merge pull request #27652 from fvlvte/24865-migrate-CheckForPreviousR…
Browse files Browse the repository at this point in the history
…eportActionID.js
  • Loading branch information
dangrous authored Nov 16, 2023
2 parents a615c41 + 172f1c7 commit 96f5127
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 39 deletions.
Original file line number Diff line number Diff line change
@@ -1,12 +1,9 @@
import Onyx from 'react-native-onyx';
import _ from 'underscore';
import Onyx, {OnyxCollection} from 'react-native-onyx';
import Log from '@libs/Log';
import ONYXKEYS from '@src/ONYXKEYS';
import * as OnyxTypes from '@src/types/onyx';

/**
* @returns {Promise<Object>}
*/
function getReportActionsFromOnyx() {
function getReportActionsFromOnyx(): Promise<OnyxCollection<OnyxTypes.ReportActions>> {
return new Promise((resolve) => {
const connectionID = Onyx.connect({
key: ONYXKEYS.COLLECTION.REPORT_ACTIONS,
Expand All @@ -22,20 +19,19 @@ function getReportActionsFromOnyx() {
/**
* 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 () {
export default function (): Promise<void> {
return getReportActionsFromOnyx().then((allReportActions) => {
if (_.isEmpty(allReportActions)) {
if (Object.keys(allReportActions ?? {}).length === 0) {
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')) {
let firstValidValue: OnyxTypes.ReportAction | undefined;

Object.values(allReportActions ?? {}).some((reportActions) =>
Object.values(reportActions ?? {}).some((reportActionData) => {
if ('reportActionID' in reportActionData) {
firstValidValue = reportActionData;
return true;
}
Expand All @@ -44,24 +40,25 @@ export default function () {
}),
);

if (_.isUndefined(firstValidValue)) {
if (!firstValidValue) {
Log.info(`[Migrate Onyx] Skipped migration CheckForPreviousReportActionID because there were no valid reportActions`);
return;
}

if (_.has(firstValidValue, 'previousReportActionID')) {
if (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) => {
const onyxData: OnyxCollection<OnyxTypes.ReportActions> = {};

Object.keys(allReportActions ?? {}).forEach((onyxKey) => {
onyxData[onyxKey] = {};
});

return Onyx.multiSet(onyxData);
return Onyx.multiSet(onyxData as Record<`${typeof ONYXKEYS.COLLECTION.REPORT_ACTIONS}`, Record<string, never>>);
});
}
40 changes: 20 additions & 20 deletions tests/unit/MigrationTest.js
Original file line number Diff line number Diff line change
Expand Up @@ -464,10 +464,10 @@ describe('Migrations', () => {
Onyx.multiSet({
[`${ONYXKEYS.COLLECTION.REPORT_ACTIONS}1`]: {
1: {
reportActionID: 1,
reportActionID: '1',
},
2: {
reportActionID: 2,
reportActionID: '2',
},
},
})
Expand All @@ -491,12 +491,12 @@ describe('Migrations', () => {
Onyx.multiSet({
[`${ONYXKEYS.COLLECTION.REPORT_ACTIONS}1`]: {
1: {
reportActionID: 1,
previousReportActionID: 0,
reportActionID: '1',
previousReportActionID: '0',
},
2: {
reportActionID: 2,
previousReportActionID: 1,
reportActionID: '2',
previousReportActionID: '1',
},
},
})
Expand All @@ -510,12 +510,12 @@ describe('Migrations', () => {
Onyx.disconnect(connectionID);
const expectedReportAction = {
1: {
reportActionID: 1,
previousReportActionID: 0,
reportActionID: '1',
previousReportActionID: '0',
},
2: {
reportActionID: 2,
previousReportActionID: 1,
reportActionID: '2',
previousReportActionID: '1',
},
};
expect(allReportActions[`${ONYXKEYS.COLLECTION.REPORT_ACTIONS}1`]).toMatchObject(expectedReportAction);
Expand All @@ -530,10 +530,10 @@ describe('Migrations', () => {
[`${ONYXKEYS.COLLECTION.REPORT_ACTIONS}3`]: null,
[`${ONYXKEYS.COLLECTION.REPORT_ACTIONS}4`]: {
1: {
reportActionID: 1,
reportActionID: '1',
},
2: {
reportActionID: 2,
reportActionID: '2',
},
},
})
Expand Down Expand Up @@ -563,12 +563,12 @@ describe('Migrations', () => {
[`${ONYXKEYS.COLLECTION.REPORT_ACTIONS}3`]: null,
[`${ONYXKEYS.COLLECTION.REPORT_ACTIONS}4`]: {
1: {
reportActionID: 1,
previousReportActionID: 10,
reportActionID: '1',
previousReportActionID: '10',
},
2: {
reportActionID: 2,
previousReportActionID: 23,
reportActionID: '2',
previousReportActionID: '23',
},
},
})
Expand All @@ -583,12 +583,12 @@ describe('Migrations', () => {
const expectedReportAction1 = {};
const expectedReportAction4 = {
1: {
reportActionID: 1,
previousReportActionID: 10,
reportActionID: '1',
previousReportActionID: '10',
},
2: {
reportActionID: 2,
previousReportActionID: 23,
reportActionID: '2',
previousReportActionID: '23',
},
};
expect(allReportActions[`${ONYXKEYS.COLLECTION.REPORT_ACTIONS}1`]).toMatchObject(expectedReportAction1);
Expand Down

0 comments on commit 96f5127

Please sign in to comment.