diff --git a/src/libs/actions/User.ts b/src/libs/actions/User.ts index 2e2e7897e136..5e0b83f8521d 100644 --- a/src/libs/actions/User.ts +++ b/src/libs/actions/User.ts @@ -36,7 +36,7 @@ import Visibility from '@libs/Visibility'; import CONST from '@src/CONST'; import ONYXKEYS from '@src/ONYXKEYS'; import ROUTES from '@src/ROUTES'; -import type {BlockedFromConcierge, CustomStatusDraft, Policy} from '@src/types/onyx'; +import type {BlockedFromConcierge, CustomStatusDraft, LoginList, Policy} from '@src/types/onyx'; import type Login from '@src/types/onyx/Login'; import type {OnyxServerUpdate} from '@src/types/onyx/OnyxUpdatesFromServer'; import type OnyxPersonalDetails from '@src/types/onyx/PersonalDetails'; @@ -372,7 +372,7 @@ function validateLogin(accountID: number, validateCode: string) { /** * Validates a secondary login / contact method */ -function validateSecondaryLogin(contactMethod: string, validateCode: string) { +function validateSecondaryLogin(loginList: OnyxEntry, contactMethod: string, validateCode: string) { const optimisticData: OnyxUpdate[] = [ { onyxMethod: Onyx.METHOD.MERGE, @@ -427,6 +427,70 @@ function validateSecondaryLogin(contactMethod: string, validateCode: string) { }, }, ]; + // If the primary login isn't validated yet, set the secondary login as the primary login + if (!loginList?.[currentEmail].validatedDate) { + successData.push( + ...[ + { + onyxMethod: Onyx.METHOD.MERGE, + key: ONYXKEYS.ACCOUNT, + value: { + primaryLogin: contactMethod, + }, + }, + { + onyxMethod: Onyx.METHOD.MERGE, + key: ONYXKEYS.SESSION, + value: { + email: contactMethod, + }, + }, + { + onyxMethod: Onyx.METHOD.MERGE, + key: ONYXKEYS.PERSONAL_DETAILS_LIST, + value: { + [currentUserAccountID]: { + login: contactMethod, + displayName: PersonalDetailsUtils.createDisplayName(contactMethod, myPersonalDetails), + }, + }, + }, + ], + ); + + Object.values(allPolicies ?? {}).forEach((policy) => { + if (!policy) { + return; + } + + let optimisticPolicyDataValue; + + if (policy.employeeList) { + const currentEmployee = policy.employeeList[currentEmail]; + optimisticPolicyDataValue = { + employeeList: { + [currentEmail]: null, + [contactMethod]: currentEmployee, + }, + }; + } + + if (policy.ownerAccountID === currentUserAccountID) { + optimisticPolicyDataValue = { + ...optimisticPolicyDataValue, + owner: contactMethod, + }; + } + + if (optimisticPolicyDataValue) { + successData.push({ + onyxMethod: Onyx.METHOD.MERGE, + key: `${ONYXKEYS.COLLECTION.POLICY}${policy.id}`, + value: optimisticPolicyDataValue, + }); + } + }); + } const failureData: OnyxUpdate[] = [ { diff --git a/src/pages/settings/Profile/Contacts/ValidateCodeForm/BaseValidateCodeForm.tsx b/src/pages/settings/Profile/Contacts/ValidateCodeForm/BaseValidateCodeForm.tsx index 556d341d049f..e0b7a23d5df3 100644 --- a/src/pages/settings/Profile/Contacts/ValidateCodeForm/BaseValidateCodeForm.tsx +++ b/src/pages/settings/Profile/Contacts/ValidateCodeForm/BaseValidateCodeForm.tsx @@ -166,8 +166,8 @@ function BaseValidateCodeForm({account = {}, contactMethod, hasMagicCodeBeenSent } setFormError({}); - User.validateSecondaryLogin(contactMethod, validateCode); - }, [validateCode, contactMethod]); + User.validateSecondaryLogin(loginList, contactMethod, validateCode); + }, [loginList, validateCode, contactMethod]); return ( <>