Skip to content

Commit

Permalink
Merge pull request #47132 from bernhardoj/fix/46350-set-primary-login
Browse files Browse the repository at this point in the history
  • Loading branch information
roryabraham authored Aug 11, 2024
2 parents 93dc1c9 + fa728fa commit 5dd0d8b
Show file tree
Hide file tree
Showing 3 changed files with 80 additions and 19 deletions.
79 changes: 75 additions & 4 deletions src/libs/actions/User.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -70,6 +70,13 @@ Onyx.connect({
},
});

let allPolicies: OnyxCollection<Policy>;
Onyx.connect({
key: ONYXKEYS.COLLECTION.POLICY,
waitForCollectionCallback: true,
callback: (value) => (allPolicies = value),
});

/**
* Attempt to close the user's accountt
*/
Expand Down Expand Up @@ -365,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<LoginList>, contactMethod: string, validateCode: string) {
const optimisticData: OnyxUpdate[] = [
{
onyxMethod: Onyx.METHOD.MERGE,
Expand Down Expand Up @@ -420,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[] = [
{
Expand Down Expand Up @@ -765,7 +836,7 @@ function generateStatementPDF(period: string) {
/**
* Sets a contact method / secondary login as the user's "Default" contact method.
*/
function setContactMethodAsDefault(newDefaultContactMethod: string, policies: OnyxCollection<Pick<Policy, 'id' | 'ownerAccountID' | 'owner' | 'employeeList'>>) {
function setContactMethodAsDefault(newDefaultContactMethod: string) {
const oldDefaultContactMethod = currentEmail;
const optimisticData: OnyxUpdate[] = [
{
Expand Down Expand Up @@ -858,7 +929,7 @@ function setContactMethodAsDefault(newDefaultContactMethod: string, policies: On
},
];

Object.values(policies ?? {}).forEach((policy) => {
Object.values(allPolicies ?? {}).forEach((policy) => {
if (!policy) {
return;
}
Expand Down
16 changes: 3 additions & 13 deletions src/pages/settings/Profile/Contacts/ContactMethodDetailsPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import type {StackScreenProps} from '@react-navigation/stack';
import {Str} from 'expensify-common';
import React, {useCallback, useEffect, useMemo, useRef, useState} from 'react';
import {InteractionManager, Keyboard, View} from 'react-native';
import type {OnyxEntry} from 'react-native-onyx';
import {useOnyx} from 'react-native-onyx';
import FullPageNotFoundView from '@components/BlockingViews/FullPageNotFoundView';
import ConfirmModal from '@components/ConfirmModal';
Expand All @@ -28,19 +27,11 @@ import CONST from '@src/CONST';
import ONYXKEYS from '@src/ONYXKEYS';
import ROUTES from '@src/ROUTES';
import type SCREENS from '@src/SCREENS';
import type {Policy} from '@src/types/onyx';
import {isEmptyObject} from '@src/types/utils/EmptyObject';
import isLoadingOnyxValue from '@src/types/utils/isLoadingOnyxValue';
import ValidateCodeForm from './ValidateCodeForm';
import type {ValidateCodeFormHandle} from './ValidateCodeForm/BaseValidateCodeForm';

const policiesSelector = (policy: OnyxEntry<Policy>): Pick<Policy, 'id' | 'ownerAccountID' | 'owner' | 'employeeList'> => ({
id: policy?.id ?? '-1',
ownerAccountID: policy?.ownerAccountID,
owner: policy?.owner ?? '',
employeeList: policy?.employeeList,
});

type ContactMethodDetailsPageProps = StackScreenProps<SettingsNavigatorParamList, typeof SCREENS.SETTINGS.PROFILE.CONTACT_METHOD_DETAILS>;

function ContactMethodDetailsPage({route}: ContactMethodDetailsPageProps) {
Expand All @@ -49,9 +40,8 @@ function ContactMethodDetailsPage({route}: ContactMethodDetailsPageProps) {
const [myDomainSecurityGroups, myDomainSecurityGroupsResult] = useOnyx(ONYXKEYS.MY_DOMAIN_SECURITY_GROUPS);
const [securityGroups, securityGroupsResult] = useOnyx(ONYXKEYS.COLLECTION.SECURITY_GROUP);
const [isLoadingReportData, isLoadingReportDataResult] = useOnyx(ONYXKEYS.IS_LOADING_REPORT_DATA, {initialValue: true});
const [policies, policiesResult] = useOnyx(ONYXKEYS.COLLECTION.POLICY, {selector: policiesSelector});

const isLoadingOnyxValues = isLoadingOnyxValue(loginListResult, sessionResult, myDomainSecurityGroupsResult, securityGroupsResult, isLoadingReportDataResult, policiesResult);
const isLoadingOnyxValues = isLoadingOnyxValue(loginListResult, sessionResult, myDomainSecurityGroupsResult, securityGroupsResult, isLoadingReportDataResult);

const {formatPhoneNumber, translate} = useLocalize();
const theme = useTheme();
Expand Down Expand Up @@ -88,8 +78,8 @@ function ContactMethodDetailsPage({route}: ContactMethodDetailsPageProps) {
* Attempt to set this contact method as user's "Default contact method"
*/
const setAsDefault = useCallback(() => {
User.setContactMethodAsDefault(contactMethod, policies);
}, [contactMethod, policies]);
User.setContactMethodAsDefault(contactMethod);
}, [contactMethod]);

/**
* Checks if the user is allowed to change their default contact method. This should only be allowed if:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
<>
Expand Down

0 comments on commit 5dd0d8b

Please sign in to comment.