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

[HOLD E/E#294647] Update PersonalDetails migration of reports to remove email-based data #22811

Closed
wants to merge 8 commits into from
69 changes: 67 additions & 2 deletions src/libs/migrations/PersonalDetailsByAccountID.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,22 @@ function getReportActionsFromOnyx() {
});
}

/**
* @returns {Promise<Object>}
*/
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.
Expand Down Expand Up @@ -74,11 +90,17 @@ function getDeprecatedPolicyMemberListFromOnyx() {
* - childOldestFourEmails -> childOldestFourAccountIDs
* - originalMessage.participants -> originalMessage.participantAccountIDs
*
* For reports, delete existing keys with emails
* - ownerEmail
* - managerEmail
* - lastActorEmail
* - participants
*
* @returns {Promise<void>}
*/
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
Expand Down Expand Up @@ -212,6 +234,49 @@ 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, ['ownerEmail'])) {
reportWasModified = true;
Log.info(`[Migrate Onyx] PersonalDetailsByAccountID migration: removing ownerEmail from report ${newReport.reportID}`);
delete newReport.ownerEmail;
}

if (lodashHas(newReport, ['managerEmail'])) {
reportWasModified = true;
Log.info(`[Migrate Onyx] PersonalDetailsByAccountID migration: removing managerEmail from report ${newReport.reportID}`);
delete newReport.managerEmail;
}

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] = report;
Beamanator marked this conversation as resolved.
Show resolved Hide resolved
}
});

return Onyx.multiSet(onyxData);
},
);
Expand Down
100 changes: 100 additions & 0 deletions tests/unit/MigrationTest.js
Original file line number Diff line number Diff line change
Expand Up @@ -638,5 +638,105 @@ describe('Migrations', () => {
},
});
}));

it('Should remove any instances of ownerEmail found in a report', () =>
Onyx.multiSet({
[`${ONYXKEYS.REPORT}1`]: {
Beamanator marked this conversation as resolved.
Show resolved Hide resolved
reportID: 1,
ownerEmail: 'fake@test.com',
ownerAccountID: 5,
},
})
.then(PersonalDetailsByAccountID)
.then(() => {
expect(LogSpy).toHaveBeenCalledWith('[Migrate Onyx] PersonalDetailsByAccountID migration: removing ownerEmail from report 1');
const connectionID = Onyx.connect({
key: ONYXKEYS.COLLECTION.REPORT,
waitForCollectionCallback: true,
callback: (allReports) => {
Onyx.disconnect(connectionID);
const expectedReport = {
reportID: 1,
ownerAccountID: 5,
};
expect(allReports[`${ONYXKEYS.COLLECTION.REPORT}1`][1]).toMatchObject(expectedReport);
Beamanator marked this conversation as resolved.
Show resolved Hide resolved
},
});
}));

it('Should remove any instances of managerEmail found in a report', () =>
Onyx.multiSet({
[`${ONYXKEYS.REPORT}1`]: {
reportID: 1,
managerEmail: 'fake@test.com',
managerID: 5,
},
})
.then(PersonalDetailsByAccountID)
.then(() => {
expect(LogSpy).toHaveBeenCalledWith('[Migrate Onyx] PersonalDetailsByAccountID migration: removing managerEmail from report 1');
const connectionID = Onyx.connect({
key: ONYXKEYS.COLLECTION.REPORT,
waitForCollectionCallback: true,
callback: (allReports) => {
Onyx.disconnect(connectionID);
const expectedReport = {
reportID: 1,
managerID: 5,
};
expect(allReports[`${ONYXKEYS.COLLECTION.REPORT}1`][1]).toMatchObject(expectedReport);
},
});
}));

it('Should remove any instances of lastActorEmail found in a report', () =>
Onyx.multiSet({
[`${ONYXKEYS.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`][1]).toMatchObject(expectedReport);
},
});
}));

it('Should remove any instances of participants found in a report', () =>
Onyx.multiSet({
[`${ONYXKEYS.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`][1]).toMatchObject(expectedReport);
},
});
}));
});
});
Loading