From a5a6b6416283a95af46ccc1b06f73a53d9a2f83e Mon Sep 17 00:00:00 2001 From: tienifr Date: Wed, 19 Jul 2023 17:22:58 +0700 Subject: [PATCH 1/8] fix: cannot remove rbr when invite an invalid workspace member --- src/libs/actions/Policy.js | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/src/libs/actions/Policy.js b/src/libs/actions/Policy.js index 4b585da62ba5..c378c4d60203 100644 --- a/src/libs/actions/Policy.js +++ b/src/libs/actions/Policy.js @@ -7,11 +7,13 @@ import {escapeRegExp} from 'lodash'; import * as API from '../API'; import ONYXKEYS from '../../ONYXKEYS'; import CONST from '../../CONST'; +import * as LocalePhoneNumber from '../LocalePhoneNumber'; import * as OptionsListUtils from '../OptionsListUtils'; import * as ErrorUtils from '../ErrorUtils'; import * as ReportUtils from '../ReportUtils'; import Log from '../Log'; import Permissions from '../Permissions'; +import * as UserUtils from '../UserUtils'; const allPolicies = {}; Onyx.connect({ @@ -364,6 +366,21 @@ function addMembersToWorkspace(invitedEmailsToAccountIDs, welcomeNote, policyID, value: _.object(accountIDs, Array(accountIDs.length).fill({pendingAction: CONST.RED_BRICK_ROAD_PENDING_ACTION.ADD})), }, ...membersChats.onyxOptimisticData, + { + onyxMethod: Onyx.METHOD.MERGE, + key: ONYXKEYS.PERSONAL_DETAILS_LIST, + value: _.object( + _.map(invitedEmailsToAccountIDs, (accountID, memberLogin) => [ + accountID, + { + accountID, + avatar: UserUtils.getDefaultAvatarURL(accountID), + displayName: LocalePhoneNumber.formatPhoneNumber(memberLogin), + login: OptionsListUtils.addSMSDomainIfPhoneNumber(memberLogin), + }, + ]), + ), + }, ]; const successData = [ @@ -376,6 +393,11 @@ function addMembersToWorkspace(invitedEmailsToAccountIDs, welcomeNote, policyID, value: _.object(accountIDs, Array(accountIDs.length).fill({pendingAction: null, errors: null})), }, ...membersChats.onyxSuccessData, + { + onyxMethod: Onyx.METHOD.MERGE, + key: ONYXKEYS.PERSONAL_DETAILS_LIST, + value: _.object(accountIDs, Array(accountIDs.length).fill(null)), + }, ]; const failureData = [ From 304a5e68e7ac6dd346c8f13cdcabfcbe17c07742 Mon Sep 17 00:00:00 2001 From: tienifr Date: Wed, 19 Jul 2023 23:28:21 +0700 Subject: [PATCH 2/8] exclude current account id --- src/libs/actions/Policy.js | 40 ++++++++++++++++++++++++++------------ 1 file changed, 28 insertions(+), 12 deletions(-) diff --git a/src/libs/actions/Policy.js b/src/libs/actions/Policy.js index c378c4d60203..05e946779c02 100644 --- a/src/libs/actions/Policy.js +++ b/src/libs/actions/Policy.js @@ -357,6 +357,25 @@ function addMembersToWorkspace(invitedEmailsToAccountIDs, welcomeNote, policyID, // create onyx data for policy expense chats for each new member const membersChats = createPolicyExpenseChats(policyID, invitedEmailsToAccountIDs, betas); + // Optimistic personal details for the new accounts invited + const optimisticPersonalDetails = _.object( + _.compact( + _.map( + invitedEmailsToAccountIDs, + (accountID, memberLogin) => + !_.has(allPersonalDetails, accountID) && [ + accountID, + { + accountID, + avatar: UserUtils.getDefaultAvatarURL(accountID), + displayName: LocalePhoneNumber.formatPhoneNumber(memberLogin), + login: OptionsListUtils.addSMSDomainIfPhoneNumber(memberLogin), + }, + ], + ), + ), + ); + const optimisticData = [ { onyxMethod: Onyx.METHOD.MERGE, @@ -369,17 +388,7 @@ function addMembersToWorkspace(invitedEmailsToAccountIDs, welcomeNote, policyID, { onyxMethod: Onyx.METHOD.MERGE, key: ONYXKEYS.PERSONAL_DETAILS_LIST, - value: _.object( - _.map(invitedEmailsToAccountIDs, (accountID, memberLogin) => [ - accountID, - { - accountID, - avatar: UserUtils.getDefaultAvatarURL(accountID), - displayName: LocalePhoneNumber.formatPhoneNumber(memberLogin), - login: OptionsListUtils.addSMSDomainIfPhoneNumber(memberLogin), - }, - ]), - ), + value: optimisticPersonalDetails, }, ]; @@ -396,7 +405,7 @@ function addMembersToWorkspace(invitedEmailsToAccountIDs, welcomeNote, policyID, { onyxMethod: Onyx.METHOD.MERGE, key: ONYXKEYS.PERSONAL_DETAILS_LIST, - value: _.object(accountIDs, Array(accountIDs.length).fill(null)), + value: _.object(_.keys(optimisticPersonalDetails), Array(_.size(optimisticPersonalDetails)).fill(null)), }, ]; @@ -765,6 +774,13 @@ function clearAddMemberError(policyID, accountID) { Onyx.merge(`${ONYXKEYS.COLLECTION.POLICY_MEMBERS}${policyID}`, { [accountID]: null, }); + + // Remove draft accountID + if (accountID.toString().length === 16) { + Onyx.merge(`${ONYXKEYS.PERSONAL_DETAILS_LIST}`, { + [accountID]: null, + }); + } } /** From 012b2aa6466f4060a94ee52970099cd480c92461 Mon Sep 17 00:00:00 2001 From: tienifr Date: Wed, 19 Jul 2023 23:38:37 +0700 Subject: [PATCH 3/8] refactor using chain --- src/libs/actions/Policy.js | 32 +++++++++++++++----------------- 1 file changed, 15 insertions(+), 17 deletions(-) diff --git a/src/libs/actions/Policy.js b/src/libs/actions/Policy.js index 05e946779c02..e9ac0bf50b06 100644 --- a/src/libs/actions/Policy.js +++ b/src/libs/actions/Policy.js @@ -358,23 +358,21 @@ function addMembersToWorkspace(invitedEmailsToAccountIDs, welcomeNote, policyID, const membersChats = createPolicyExpenseChats(policyID, invitedEmailsToAccountIDs, betas); // Optimistic personal details for the new accounts invited - const optimisticPersonalDetails = _.object( - _.compact( - _.map( - invitedEmailsToAccountIDs, - (accountID, memberLogin) => - !_.has(allPersonalDetails, accountID) && [ - accountID, - { - accountID, - avatar: UserUtils.getDefaultAvatarURL(accountID), - displayName: LocalePhoneNumber.formatPhoneNumber(memberLogin), - login: OptionsListUtils.addSMSDomainIfPhoneNumber(memberLogin), - }, - ], - ), - ), - ); + const optimisticPersonalDetails = _.chain(invitedEmailsToAccountIDs) + .map( + (accountID, memberLogin) => !_.has(allPersonalDetails, accountID) && [ + accountID, + { + accountID, + avatar: UserUtils.getDefaultAvatarURL(accountID), + displayName: LocalePhoneNumber.formatPhoneNumber(memberLogin), + login: OptionsListUtils.addSMSDomainIfPhoneNumber(memberLogin), + }, + ], + ) + .compact() + .object() + .value(); const optimisticData = [ { From 9d734b988a754b37190decbc4a882e0e88490aa9 Mon Sep 17 00:00:00 2001 From: tienifr Date: Wed, 19 Jul 2023 23:46:03 +0700 Subject: [PATCH 4/8] dummy account ID length --- src/libs/actions/Policy.js | 19 ++++++++++--------- 1 file changed, 10 insertions(+), 9 deletions(-) diff --git a/src/libs/actions/Policy.js b/src/libs/actions/Policy.js index e9ac0bf50b06..7c71097ebb76 100644 --- a/src/libs/actions/Policy.js +++ b/src/libs/actions/Policy.js @@ -360,15 +360,16 @@ function addMembersToWorkspace(invitedEmailsToAccountIDs, welcomeNote, policyID, // Optimistic personal details for the new accounts invited const optimisticPersonalDetails = _.chain(invitedEmailsToAccountIDs) .map( - (accountID, memberLogin) => !_.has(allPersonalDetails, accountID) && [ - accountID, - { + (accountID, memberLogin) => + !_.has(allPersonalDetails, accountID) && [ accountID, - avatar: UserUtils.getDefaultAvatarURL(accountID), - displayName: LocalePhoneNumber.formatPhoneNumber(memberLogin), - login: OptionsListUtils.addSMSDomainIfPhoneNumber(memberLogin), - }, - ], + { + accountID, + avatar: UserUtils.getDefaultAvatarURL(accountID), + displayName: LocalePhoneNumber.formatPhoneNumber(memberLogin), + login: OptionsListUtils.addSMSDomainIfPhoneNumber(memberLogin), + }, + ], ) .compact() .object() @@ -774,7 +775,7 @@ function clearAddMemberError(policyID, accountID) { }); // Remove draft accountID - if (accountID.toString().length === 16) { + if (accountID.toString().length >= 15) { Onyx.merge(`${ONYXKEYS.PERSONAL_DETAILS_LIST}`, { [accountID]: null, }); From ca996a6f05b32fce5766ae6dc9a3df63a1f3db9c Mon Sep 17 00:00:00 2001 From: tienifr Date: Sat, 22 Jul 2023 02:08:06 +0700 Subject: [PATCH 5/8] isDummy account --- src/libs/actions/Policy.js | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/libs/actions/Policy.js b/src/libs/actions/Policy.js index 7c71097ebb76..85613e329790 100644 --- a/src/libs/actions/Policy.js +++ b/src/libs/actions/Policy.js @@ -368,6 +368,7 @@ function addMembersToWorkspace(invitedEmailsToAccountIDs, welcomeNote, policyID, avatar: UserUtils.getDefaultAvatarURL(accountID), displayName: LocalePhoneNumber.formatPhoneNumber(memberLogin), login: OptionsListUtils.addSMSDomainIfPhoneNumber(memberLogin), + isDummy: true, }, ], ) @@ -775,7 +776,7 @@ function clearAddMemberError(policyID, accountID) { }); // Remove draft accountID - if (accountID.toString().length >= 15) { + if (lodashGet(allPersonalDetails, [accountID, 'isDummy'])) { Onyx.merge(`${ONYXKEYS.PERSONAL_DETAILS_LIST}`, { [accountID]: null, }); From 78f8ba1140aa2ca80b8e9679ad06dd37ad20fa98 Mon Sep 17 00:00:00 2001 From: tienifr Date: Wed, 26 Jul 2023 11:06:03 +0700 Subject: [PATCH 6/8] change isDummy to isOptimisticData --- src/libs/actions/Policy.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/libs/actions/Policy.js b/src/libs/actions/Policy.js index 85613e329790..a55aff0b11fc 100644 --- a/src/libs/actions/Policy.js +++ b/src/libs/actions/Policy.js @@ -368,7 +368,7 @@ function addMembersToWorkspace(invitedEmailsToAccountIDs, welcomeNote, policyID, avatar: UserUtils.getDefaultAvatarURL(accountID), displayName: LocalePhoneNumber.formatPhoneNumber(memberLogin), login: OptionsListUtils.addSMSDomainIfPhoneNumber(memberLogin), - isDummy: true, + isOptimisticData: true, }, ], ) @@ -776,7 +776,7 @@ function clearAddMemberError(policyID, accountID) { }); // Remove draft accountID - if (lodashGet(allPersonalDetails, [accountID, 'isDummy'])) { + if (lodashGet(allPersonalDetails, [accountID, 'isOptimisticData'])) { Onyx.merge(`${ONYXKEYS.PERSONAL_DETAILS_LIST}`, { [accountID]: null, }); From b3998dc03c6ba9154411e0766a3da0b09a088477 Mon Sep 17 00:00:00 2001 From: tienifr Date: Thu, 27 Jul 2023 01:32:56 +0700 Subject: [PATCH 7/8] remove isOptimisticData --- src/libs/actions/Policy.js | 10 +++------- 1 file changed, 3 insertions(+), 7 deletions(-) diff --git a/src/libs/actions/Policy.js b/src/libs/actions/Policy.js index a55aff0b11fc..af986492d674 100644 --- a/src/libs/actions/Policy.js +++ b/src/libs/actions/Policy.js @@ -774,13 +774,9 @@ function clearAddMemberError(policyID, accountID) { Onyx.merge(`${ONYXKEYS.COLLECTION.POLICY_MEMBERS}${policyID}`, { [accountID]: null, }); - - // Remove draft accountID - if (lodashGet(allPersonalDetails, [accountID, 'isOptimisticData'])) { - Onyx.merge(`${ONYXKEYS.PERSONAL_DETAILS_LIST}`, { - [accountID]: null, - }); - } + Onyx.merge(`${ONYXKEYS.PERSONAL_DETAILS_LIST}`, { + [accountID]: null, + }); } /** From 9eba1ddbe07aabf82f8b15f7bbc82dc44197ffcf Mon Sep 17 00:00:00 2001 From: tienifr Date: Thu, 27 Jul 2023 01:48:32 +0700 Subject: [PATCH 8/8] remove isOptimisticData --- src/libs/actions/Policy.js | 1 - 1 file changed, 1 deletion(-) diff --git a/src/libs/actions/Policy.js b/src/libs/actions/Policy.js index af986492d674..231bce5aae0c 100644 --- a/src/libs/actions/Policy.js +++ b/src/libs/actions/Policy.js @@ -368,7 +368,6 @@ function addMembersToWorkspace(invitedEmailsToAccountIDs, welcomeNote, policyID, avatar: UserUtils.getDefaultAvatarURL(accountID), displayName: LocalePhoneNumber.formatPhoneNumber(memberLogin), login: OptionsListUtils.addSMSDomainIfPhoneNumber(memberLogin), - isOptimisticData: true, }, ], )