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

Ensure we're always including timezone in myPersonalDetails #2040

Merged
merged 4 commits into from
Mar 25, 2021
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
8 changes: 1 addition & 7 deletions src/libs/DateUtils.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import moment from 'moment';
import 'moment-timezone';
import _ from 'underscore';
import Onyx from 'react-native-onyx';
import Str from 'expensify-common/lib/str';
import ONYXKEYS from '../ONYXKEYS';
Expand All @@ -9,12 +8,7 @@ import CONST from '../CONST';
let timezone;
Onyx.connect({
key: ONYXKEYS.MY_PERSONAL_DETAILS,
callback: (val) => {
timezone = val ? val.timezone : CONST.DEFAULT_TIME_ZONE.selected;
if (_.isObject(timezone)) {
timezone = val.timezone.selected;
}
},
callback: (val) => { timezone = val ? val.timezone.selected : CONST.DEFAULT_TIME_ZONE.selected; },
});

/**
Expand Down
11 changes: 8 additions & 3 deletions src/libs/actions/PersonalDetails.js
Original file line number Diff line number Diff line change
Expand Up @@ -111,11 +111,16 @@ function fetch() {
returnValueList: 'personalDetailsList',
})
.then((data) => {
const allPersonalDetails = formatPersonalDetails(data.personalDetailsList);
let myPersonalDetails = {};

// If personalDetailsList is empty, ensure we set the personal details for the current user
const personalDetailsList = _.isEmpty(data.personalDetailsList)
? {[currentUserEmail]: myPersonalDetails}
: data.personalDetailsList;
const allPersonalDetails = formatPersonalDetails(personalDetailsList);
Onyx.merge(ONYXKEYS.PERSONAL_DETAILS, allPersonalDetails);

const myPersonalDetails = allPersonalDetails[currentUserEmail]
|| {avatar: getAvatar(undefined, currentUserEmail)};
myPersonalDetails = allPersonalDetails[currentUserEmail];

// Add the first and last name to the current user's MY_PERSONAL_DETAILS key
myPersonalDetails.firstName = lodashGet(data.personalDetailsList, [currentUserEmail, 'firstName'], '');
Expand Down
2 changes: 2 additions & 0 deletions src/libs/migrateOnyx.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import RenameActiveClientsKey from './migrations/RenameActiveClientsKey';
import RenamePriorityModeKey from './migrations/RenamePriorityModeKey';
import ReformatTimezone from './migrations/ReformatTimezone';

export default function () {
const startTime = Date.now();
Expand All @@ -10,6 +11,7 @@ export default function () {
const migrationPromises = [
RenameActiveClientsKey,
RenamePriorityModeKey,
ReformatTimezone,
];

// Reduce all promises down to a single promise. All promises run in a linear fashion, waiting for the
Expand Down
41 changes: 41 additions & 0 deletions src/libs/migrations/ReformatTimezone.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import Onyx from 'react-native-onyx';
import _ from 'underscore';
import ONYXKEYS from '../../ONYXKEYS';

// This migration changes the format of the timezone in the Onyx key MY_PERSONAL_DETAILS from a string to an object
export default function () {
return new Promise((resolve) => {
// Connect to the old key in Onyx to get the old value of myPersonalDetails timezone
// then update the timezone to be the default timezone and set the myPersonalDetails
// key with the updated values
const connectionID = Onyx.connect({
key: ONYXKEYS.MY_PERSONAL_DETAILS,
callback: (myPersonalDetails) => {
Onyx.disconnect(connectionID);

if (_.isUndefined(myPersonalDetails) || _.isEmpty(myPersonalDetails)) {
console.debug('[Migrate Onyx] Skipped migration ReformatTimezone: No myPersonalDetails key found');
return resolve();
}

// Fail early here because there is nothing to migrate, the timezone is already in the expected format
if (_.isObject(myPersonalDetails.timezone)) {
console.debug('[Migrate Onyx] Skipped migration ReformatTimezone');
return resolve();
}

// Update the timezone with the user's old timezone selection and set "automatic" to false
// because we don't know if their old timezone was set automatically or not
const details = myPersonalDetails;
details.timezone = {selected: details.timezone, automatic: false};
Onyx.set({
[ONYXKEYS.MY_PERSONAL_DETAILS]: details,
})
.then(() => {
console.debug('[Migrate Onyx] Ran migration ReformatTimezone');
resolve();
});
},
});
});
}