Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[No QA][TS migration] Migrate 'CheckForPreviousReportActionID.js' lib to TypeScript #27652

Merged
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`);
dangrous marked this conversation as resolved.
Show resolved Hide resolved
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>>);
});
fvlvte marked this conversation as resolved.
Show resolved Hide resolved
}
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
Loading