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 f360f46de279..3788ab803b4d 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..24221f3f0780 --- /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'; + +// 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(); + }); + }, + }); + }); +}