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
2 changes: 1 addition & 1 deletion src/ONYXKEYS.ts
Original file line number Diff line number Diff line change
Expand Up @@ -386,7 +386,7 @@ type OnyxValues = {
[ONYXKEYS.COLLECTION.WORKSPACE_INVITE_MEMBERS_DRAFT]: Record<string, number>;
[ONYXKEYS.COLLECTION.REPORT]: OnyxTypes.Report;
[ONYXKEYS.COLLECTION.REPORT_METADATA]: OnyxTypes.ReportMetadata;
[ONYXKEYS.COLLECTION.REPORT_ACTIONS]: OnyxTypes.ReportAction;
[ONYXKEYS.COLLECTION.REPORT_ACTIONS]: OnyxTypes.ReportActions;
fvlvte marked this conversation as resolved.
Show resolved Hide resolved
[ONYXKEYS.COLLECTION.REPORT_ACTIONS_DRAFTS]: string;
[ONYXKEYS.COLLECTION.REPORT_ACTIONS_REACTIONS]: OnyxTypes.ReportActionReactions;
[ONYXKEYS.COLLECTION.REPORT_DRAFT_COMMENT]: string;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,17 +1,14 @@
import _ from 'underscore';
import Onyx from 'react-native-onyx';
import Log from '../Log';
import Onyx, {OnyxCollection} from 'react-native-onyx';
import ONYXKEYS from '../../ONYXKEYS';
import * as OnyxTypes from '../../types/onyx';
import Log from '../Log';

/**
* @returns {Promise<Object>}
*/
function getReportActionsFromOnyx() {
function getReportActionsFromOnyx(): Promise<OnyxCollection<OnyxTypes.ReportActions>> {
return new Promise((resolve) => {
const connectionID = Onyx.connect({
key: ONYXKEYS.COLLECTION.REPORT_ACTIONS,
waitForCollectionCallback: true,
callback: (allReportActions) => {
callback: (allReportActions: OnyxCollection<OnyxTypes.ReportActions>) => {
Onyx.disconnect(connectionID);
fvlvte marked this conversation as resolved.
Show resolved Hide resolved
return resolve(allReportActions);
},
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 (reportActionData?.reportActionID) {
firstValidValue = reportActionData;
fvlvte marked this conversation as resolved.
Show resolved Hide resolved
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
}
4 changes: 4 additions & 0 deletions src/types/onyx/ReportAction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,8 +85,12 @@ type ReportActionBase = {
childVisibleActionCount?: number;

pendingAction?: OnyxCommon.PendingAction;
errors?: OnyxCommon.Errors;
fvlvte marked this conversation as resolved.
Show resolved Hide resolved
};

type ReportAction = ReportActionBase & OriginalMessage;

type ReportActions = Record<string, ReportAction>;

export default ReportAction;
export type {ReportActions};
3 changes: 2 additions & 1 deletion src/types/onyx/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ import Policy from './Policy';
import PolicyCategory from './PolicyCategory';
import Report from './Report';
import ReportMetadata from './ReportMetadata';
import ReportAction from './ReportAction';
import ReportAction, {ReportActions} from './ReportAction';
import ReportActionReactions from './ReportActionReactions';
import SecurityGroup from './SecurityGroup';
import Transaction from './Transaction';
Expand Down Expand Up @@ -87,6 +87,7 @@ export type {
Report,
ReportMetadata,
ReportAction,
ReportActions,
ReportActionReactions,
SecurityGroup,
Transaction,
Expand Down
52 changes: 26 additions & 26 deletions tests/unit/MigrationTest.js
Original file line number Diff line number Diff line change
Expand Up @@ -463,10 +463,10 @@ describe('Migrations', () => {
Onyx.multiSet({
[`${ONYXKEYS.COLLECTION.REPORT_ACTIONS}1`]: {
1: {
reportActionID: 1,
reportActionID: '1',
},
2: {
reportActionID: 2,
reportActionID: '2',
},
},
})
Expand All @@ -490,12 +490,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 @@ -509,12 +509,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 @@ -529,10 +529,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 All @@ -548,8 +548,8 @@ describe('Migrations', () => {
Onyx.disconnect(connectionID);
const expectedReportAction = {};
expect(allReportActions[`${ONYXKEYS.COLLECTION.REPORT_ACTIONS}1`]).toMatchObject(expectedReportAction);
expect(allReportActions[`${ONYXKEYS.COLLECTION.REPORT_ACTIONS}2`]).toBeUndefined();
expect(allReportActions[`${ONYXKEYS.COLLECTION.REPORT_ACTIONS}3`]).toBeUndefined();
expect(allReportActions[`${ONYXKEYS.COLLECTION.REPORT_ACTIONS}2`]).toMatchObject(expectedReportAction);
expect(allReportActions[`${ONYXKEYS.COLLECTION.REPORT_ACTIONS}3`]).toMatchObject(expectedReportAction);
expect(allReportActions[`${ONYXKEYS.COLLECTION.REPORT_ACTIONS}4`]).toMatchObject(expectedReportAction);
},
});
Expand All @@ -562,12 +562,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 @@ -582,17 +582,17 @@ 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);
expect(allReportActions[`${ONYXKEYS.COLLECTION.REPORT_ACTIONS}2`]).toBeUndefined();
expect(allReportActions[`${ONYXKEYS.COLLECTION.REPORT_ACTIONS}3`]).toBeUndefined();
expect(allReportActions[`${ONYXKEYS.COLLECTION.REPORT_ACTIONS}2`]).toBeNull();
expect(allReportActions[`${ONYXKEYS.COLLECTION.REPORT_ACTIONS}3`]).toBeNull();
expect(allReportActions[`${ONYXKEYS.COLLECTION.REPORT_ACTIONS}4`]).toMatchObject(expectedReportAction4);
},
});
Expand All @@ -614,10 +614,10 @@ describe('Migrations', () => {
callback: (allReportActions) => {
Onyx.disconnect(connectionID);
const expectedReportAction = {};
expect(allReportActions[`${ONYXKEYS.COLLECTION.REPORT_ACTIONS}1`]).toBeUndefined();
expect(allReportActions[`${ONYXKEYS.COLLECTION.REPORT_ACTIONS}1`]).toBeNull();
expect(allReportActions[`${ONYXKEYS.COLLECTION.REPORT_ACTIONS}2`]).toMatchObject(expectedReportAction);
expect(allReportActions[`${ONYXKEYS.COLLECTION.REPORT_ACTIONS}3`]).toMatchObject(expectedReportAction);
expect(allReportActions[`${ONYXKEYS.COLLECTION.REPORT_ACTIONS}4`]).toBeUndefined();
expect(allReportActions[`${ONYXKEYS.COLLECTION.REPORT_ACTIONS}4`]).toBeNull();
},
});
}));
Expand Down
Loading