From 815c55fc6ccc9d2e145784267851690bb3bc02a1 Mon Sep 17 00:00:00 2001 From: Nikki Wines Date: Wed, 24 Mar 2021 10:35:11 -0700 Subject: [PATCH 1/3] ensure we're always including timezone in myPersonalDetails add timezone migration --- src/libs/DateUtils.js | 8 +---- src/libs/actions/PersonalDetails.js | 11 +++++-- src/libs/migrateOnyx.js | 2 ++ src/libs/migrations/ReformatTimezone.js | 41 +++++++++++++++++++++++++ 4 files changed, 52 insertions(+), 10 deletions(-) create mode 100644 src/libs/migrations/ReformatTimezone.js diff --git a/src/libs/DateUtils.js b/src/libs/DateUtils.js index f07011613eb5..ce32d4939663 100644 --- a/src/libs/DateUtils.js +++ b/src/libs/DateUtils.js @@ -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'; @@ -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; }, }); /** diff --git a/src/libs/actions/PersonalDetails.js b/src/libs/actions/PersonalDetails.js index 4faccd19f686..cf81a5ff8cd8 100644 --- a/src/libs/actions/PersonalDetails.js +++ b/src/libs/actions/PersonalDetails.js @@ -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'], ''); diff --git a/src/libs/migrateOnyx.js b/src/libs/migrateOnyx.js index 8b0d428076c2..5ee060ea0bc2 100644 --- a/src/libs/migrateOnyx.js +++ b/src/libs/migrateOnyx.js @@ -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(); @@ -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 diff --git a/src/libs/migrations/ReformatTimezone.js b/src/libs/migrations/ReformatTimezone.js new file mode 100644 index 000000000000..5cefd077643e --- /dev/null +++ b/src/libs/migrations/ReformatTimezone.js @@ -0,0 +1,41 @@ +import Onyx from 'react-native-onyx'; +import _ from 'underscore'; +import ONYXKEYS from '../../ONYXKEYS'; +import CONST from '../../CONST'; + +// 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: 'myPersonalDetails', + callback: (myPersonalDetails) => { + Onyx.disconnect(connectionID); + + if (_.isUndefined(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(); + } + + // Replace the old timezone with the default timezone + const details = myPersonalDetails; + details.timezone = CONST.DEFAULT_TIME_ZONE; + Onyx.set({ + [ONYXKEYS.MY_PERSONAL_DETAILS]: details, + }) + .then(() => { + console.debug('[Migrate Onyx] Ran migration ReformatTimezone'); + resolve(); + }); + }, + }); + }); +} From 1fa1e209abff1fb83a6e19fb48480593446c6491 Mon Sep 17 00:00:00 2001 From: Nikki Wines Date: Wed, 24 Mar 2021 15:11:16 -0700 Subject: [PATCH 2/3] skip migration is personalDetails key is empty --- src/libs/migrations/ReformatTimezone.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/libs/migrations/ReformatTimezone.js b/src/libs/migrations/ReformatTimezone.js index 5cefd077643e..ebd042e16513 100644 --- a/src/libs/migrations/ReformatTimezone.js +++ b/src/libs/migrations/ReformatTimezone.js @@ -14,7 +14,7 @@ export default function () { callback: (myPersonalDetails) => { Onyx.disconnect(connectionID); - if (_.isUndefined(myPersonalDetails)) { + if (_.isUndefined(myPersonalDetails) || _.isEmpty(myPersonalDetails)) { console.debug('[Migrate Onyx] Skipped migration ReformatTimezone: No myPersonalDetails key found'); return resolve(); } From f3cfc168ab2496928a1e53374471cb97dbd4bc82 Mon Sep 17 00:00:00 2001 From: Nikki Wines Date: Wed, 24 Mar 2021 18:23:32 -0700 Subject: [PATCH 3/3] use users old timezone selection and update to use constants where applicable --- src/libs/migrations/ReformatTimezone.js | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/libs/migrations/ReformatTimezone.js b/src/libs/migrations/ReformatTimezone.js index ebd042e16513..24221f3f0780 100644 --- a/src/libs/migrations/ReformatTimezone.js +++ b/src/libs/migrations/ReformatTimezone.js @@ -1,7 +1,6 @@ import Onyx from 'react-native-onyx'; import _ from 'underscore'; import ONYXKEYS from '../../ONYXKEYS'; -import CONST from '../../CONST'; // This migration changes the format of the timezone in the Onyx key MY_PERSONAL_DETAILS from a string to an object export default function () { @@ -10,7 +9,7 @@ export default function () { // then update the timezone to be the default timezone and set the myPersonalDetails // key with the updated values const connectionID = Onyx.connect({ - key: 'myPersonalDetails', + key: ONYXKEYS.MY_PERSONAL_DETAILS, callback: (myPersonalDetails) => { Onyx.disconnect(connectionID); @@ -25,9 +24,10 @@ export default function () { return resolve(); } - // Replace the old timezone with the default timezone + // 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 = CONST.DEFAULT_TIME_ZONE; + details.timezone = {selected: details.timezone, automatic: false}; Onyx.set({ [ONYXKEYS.MY_PERSONAL_DETAILS]: details, })