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

Update migration for report.participants and report.lastActorEmail #28555

Merged
merged 5 commits into from
Oct 4, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 52 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 @@ -73,12 +89,15 @@ function getDeprecatedPolicyMemberListFromOnyx() {
* - whisperedTo -> whisperedToAccountIDs
* - childOldestFourEmails -> childOldestFourAccountIDs
* - originalMessage.participants -> originalMessage.participantAccountIDs
* - report_
* - lastActorEmail -> lastActorAccountID
* - participants -> participantAccountIDs
*
* @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 +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);
},
);
Expand Down
1 change: 0 additions & 1 deletion src/pages/home/sidebar/SidebarLinksData.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
3 changes: 0 additions & 3 deletions src/pages/reportPropTypes.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,

Expand Down
1 change: 0 additions & 1 deletion src/types/onyx/OriginalMessage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ type OriginalMessageIOU = {
currency: string;
lastModified?: string;
participantAccountIDs?: number[];
participants?: string[];
type: string;
};
};
Expand Down
6 changes: 0 additions & 6 deletions src/types/onyx/Report.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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[];
puneetlath marked this conversation as resolved.
Show resolved Hide resolved

/** Linked policy's ID */
policyID?: string;

Expand Down
50 changes: 50 additions & 0 deletions tests/unit/MigrationTest.js
Original file line number Diff line number Diff line change
Expand Up @@ -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', () => {
Expand Down
Loading