Skip to content

Commit

Permalink
set secondary login as primary if primary login isn't validated yet
Browse files Browse the repository at this point in the history
  • Loading branch information
bernhardoj committed Aug 9, 2024
1 parent 384728a commit fa728fa
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 4 deletions.
68 changes: 66 additions & 2 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 @@ -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<LoginList>, contactMethod: string, validateCode: string) {
const optimisticData: OnyxUpdate[] = [
{
onyxMethod: Onyx.METHOD.MERGE,
Expand Down Expand Up @@ -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[] = [
{
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 fa728fa

Please sign in to comment.