diff --git a/src/libs/migrations/PersonalDetailsByAccountID.js b/src/libs/migrations/PersonalDetailsByAccountID.js index a4f97e77a27d..cf421702fd7a 100644 --- a/src/libs/migrations/PersonalDetailsByAccountID.js +++ b/src/libs/migrations/PersonalDetailsByAccountID.js @@ -25,6 +25,22 @@ function getReportActionsFromOnyx() { }); } +/** + * @returns {Promise} + */ +function getReportsFromOnyx() { + return new Promise((resolve) => { + const connectionID = Onyx.connect({ + key: ONYXKEYS.COLLECTION.REPORT, + waitForCollectionCallback: true, + callback: (allReports) => { + Onyx.disconnect(connectionID); + return resolve(allReports); + }, + }); + }); +} + /** * We use the old personalDetails object becuase it is more efficient for this migration since it is keyed by email address. * Also, if the old personalDetails object is not available, that likely means the migration has already run successfully before on this account. @@ -73,12 +89,15 @@ function getDeprecatedPolicyMemberListFromOnyx() { * - whisperedTo -> whisperedToAccountIDs * - childOldestFourEmails -> childOldestFourAccountIDs * - originalMessage.participants -> originalMessage.participantAccountIDs + * - report_ + * - lastActorEmail -> lastActorAccountID + * - participants -> participantAccountIDs * * @returns {Promise} */ export default function () { - return Promise.all([getReportActionsFromOnyx(), getDeprecatedPersonalDetailsFromOnyx(), getDeprecatedPolicyMemberListFromOnyx()]).then( - ([oldReportActions, oldPersonalDetails, oldPolicyMemberList]) => { + return Promise.all([getReportActionsFromOnyx(), getReportsFromOnyx(), getDeprecatedPersonalDetailsFromOnyx(), getDeprecatedPolicyMemberListFromOnyx()]).then( + ([oldReportActions, oldReports, oldPersonalDetails, oldPolicyMemberList]) => { const onyxData = {}; // The personalDetails object has been replaced by personalDetailsList @@ -212,6 +231,37 @@ export default function () { } }); + // For the reports migration, we don't need to look up emails from accountIDs. Instead, + // we will just look for old email data and automatically remove it if it exists. The reason for + // this is that we already stopped sending email based data in reports, and from everywhere else + // in the app by this point (since this is the last data we migrated). + _.each(oldReports, (report, onyxKey) => { + const newReport = report; + + // Delete report key if it's empty + if (_.isEmpty(newReport)) { + onyxData[onyxKey] = null; + return; + } + + let reportWasModified = false; + if (lodashHas(newReport, ['lastActorEmail'])) { + reportWasModified = true; + Log.info(`[Migrate Onyx] PersonalDetailsByAccountID migration: removing lastActorEmail from report ${newReport.reportID}`); + delete newReport.lastActorEmail; + } + + if (lodashHas(newReport, ['participants'])) { + reportWasModified = true; + Log.info(`[Migrate Onyx] PersonalDetailsByAccountID migration: removing participants from report ${newReport.reportID}`); + delete newReport.participants; + } + + if (reportWasModified) { + onyxData[onyxKey] = newReport; + } + }); + return Onyx.multiSet(onyxData); }, ); diff --git a/src/pages/home/sidebar/SidebarLinksData.js b/src/pages/home/sidebar/SidebarLinksData.js index 243ba24cdd00..95264bb33ebb 100644 --- a/src/pages/home/sidebar/SidebarLinksData.js +++ b/src/pages/home/sidebar/SidebarLinksData.js @@ -131,7 +131,6 @@ SidebarLinksData.displayName = 'SidebarLinksData'; const chatReportSelector = (report) => report && { reportID: report.reportID, - participants: report.participants, participantAccountIDs: report.participantAccountIDs, hasDraft: report.hasDraft, isPinned: report.isPinned, diff --git a/src/pages/reportPropTypes.js b/src/pages/reportPropTypes.js index a2c41b5a8147..b70438171a73 100644 --- a/src/pages/reportPropTypes.js +++ b/src/pages/reportPropTypes.js @@ -22,9 +22,6 @@ export default PropTypes.shape({ /** Whether we're waiting on submitter to add a bank account */ isWaitingOnBankAccount: PropTypes.bool, - /** The email of the last message's actor */ - lastActorEmail: PropTypes.string, - /** The accountID of the last message's actor */ lastActorAccountID: PropTypes.number, diff --git a/src/types/onyx/OriginalMessage.ts b/src/types/onyx/OriginalMessage.ts index 369ff44773ab..143b70127de5 100644 --- a/src/types/onyx/OriginalMessage.ts +++ b/src/types/onyx/OriginalMessage.ts @@ -13,7 +13,6 @@ type OriginalMessageIOU = { currency: string; lastModified?: string; participantAccountIDs?: number[]; - participants?: string[]; type: string; }; }; diff --git a/src/types/onyx/Report.ts b/src/types/onyx/Report.ts index 46e51fe41238..15cf1aacd167 100644 --- a/src/types/onyx/Report.ts +++ b/src/types/onyx/Report.ts @@ -18,9 +18,6 @@ type Report = { /** Indicates if the report is pinned to the LHN or not */ isPinned?: boolean; - /** The email of the last message's actor */ - lastActorEmail?: string; - /** The text of the last message on the report */ lastMessageText?: string; @@ -39,9 +36,6 @@ type Report = { /** The email address of the report owner */ ownerEmail?: string; - /** List of primarylogins of participants of the report */ - participants?: string[]; - /** Linked policy's ID */ policyID?: string; diff --git a/tests/unit/MigrationTest.js b/tests/unit/MigrationTest.js index 60b4e5ebe813..7d5911e6419f 100644 --- a/tests/unit/MigrationTest.js +++ b/tests/unit/MigrationTest.js @@ -464,6 +464,56 @@ describe('Migrations', () => { }, }); })); + + it('Should remove any instances of lastActorEmail found in a report', () => + Onyx.multiSet({ + [`${ONYXKEYS.COLLECTION.REPORT}1`]: { + reportID: 1, + lastActorEmail: 'fake@test.com', + lastActorAccountID: 5, + }, + }) + .then(PersonalDetailsByAccountID) + .then(() => { + expect(LogSpy).toHaveBeenCalledWith('[Migrate Onyx] PersonalDetailsByAccountID migration: removing lastActorEmail from report 1'); + const connectionID = Onyx.connect({ + key: ONYXKEYS.COLLECTION.REPORT, + waitForCollectionCallback: true, + callback: (allReports) => { + Onyx.disconnect(connectionID); + const expectedReport = { + reportID: 1, + lastActorAccountID: 5, + }; + expect(allReports[`${ONYXKEYS.COLLECTION.REPORT}1`]).toMatchObject(expectedReport); + }, + }); + })); + + it('Should remove any instances of participants found in a report', () => + Onyx.multiSet({ + [`${ONYXKEYS.COLLECTION.REPORT}1`]: { + reportID: 1, + participants: ['fake@test.com'], + participantAccountIDs: [5], + }, + }) + .then(PersonalDetailsByAccountID) + .then(() => { + expect(LogSpy).toHaveBeenCalledWith('[Migrate Onyx] PersonalDetailsByAccountID migration: removing participants from report 1'); + const connectionID = Onyx.connect({ + key: ONYXKEYS.COLLECTION.REPORT, + waitForCollectionCallback: true, + callback: (allReports) => { + Onyx.disconnect(connectionID); + const expectedReport = { + reportID: 1, + participantAccountIDs: [5], + }; + expect(allReports[`${ONYXKEYS.COLLECTION.REPORT}1`]).toMatchObject(expectedReport); + }, + }); + })); }); describe('CheckForPreviousReportActionID', () => {