From d36a76282ef0aab476dc4538f8d523102844c963 Mon Sep 17 00:00:00 2001 From: dukenv0307 Date: Tue, 17 Oct 2023 21:20:02 +0700 Subject: [PATCH 1/4] remove error when all workspace is deleted --- src/libs/actions/Policy.js | 87 +++++++++++++++++++++++++------------- 1 file changed, 57 insertions(+), 30 deletions(-) diff --git a/src/libs/actions/Policy.js b/src/libs/actions/Policy.js index 53753e193fb1..bd234d22a826 100644 --- a/src/libs/actions/Policy.js +++ b/src/libs/actions/Policy.js @@ -66,6 +66,12 @@ Onyx.connect({ callback: (val) => (allPersonalDetails = val), }); +let reimbursementAccount; +Onyx.connect({ + key: ONYXKEYS.REIMBURSEMENT_ACCOUNT, + callback: (val) => (reimbursementAccount = val), +}); + let allRecentlyUsedCategories = {}; Onyx.connect({ key: ONYXKEYS.COLLECTION.POLICY_RECENTLY_USED_CATEGORIES, @@ -81,6 +87,36 @@ function updateLastAccessedWorkspace(policyID) { Onyx.set(ONYXKEYS.LAST_ACCESSED_WORKSPACE_POLICY_ID, policyID); } +/** + * Check if the user has any active free policies (aka workspaces) + * + * @param {Array} policies + * @returns {Boolean} + */ +function hasActiveFreePolicy(policies) { + const adminFreePolicies = _.filter(policies, (policy) => policy && policy.type === CONST.POLICY.TYPE.FREE && policy.role === CONST.POLICY.ROLE.ADMIN); + + if (adminFreePolicies.length === 0) { + return false; + } + + if (_.some(adminFreePolicies, (policy) => !policy.pendingAction)) { + return true; + } + + if (_.some(adminFreePolicies, (policy) => policy.pendingAction === CONST.RED_BRICK_ROAD_PENDING_ACTION.ADD)) { + return true; + } + + if (_.some(adminFreePolicies, (policy) => policy.pendingAction === CONST.RED_BRICK_ROAD_PENDING_ACTION.DELETE)) { + return false; + } + + // If there are no add or delete pending actions the only option left is an update + // pendingAction, in which case we should return true. + return true; +} + /** * Delete the workspace * @@ -89,6 +125,10 @@ function updateLastAccessedWorkspace(policyID) { * @param {String} policyName */ function deleteWorkspace(policyID, reports, policyName) { + const filteredPolicies = _.filter(allPolicies, (policy) => policy.id !== policyID); + const hasActivePolicy = hasActiveFreePolicy(filteredPolicies); + const oldReimbursementAccount = reimbursementAccount; + const optimisticData = [ { onyxMethod: Onyx.METHOD.MERGE, @@ -125,6 +165,18 @@ function deleteWorkspace(policyID, reports, policyName) { value: optimisticReportActions, }; }), + + ...(!hasActivePolicy + ? [ + { + onyxMethod: Onyx.METHOD.MERGE, + key: ONYXKEYS.REIMBURSEMENT_ACCOUNT, + value: { + errors: null, + }, + }, + ] + : []), ]; // Restore the old report stateNum and statusNum @@ -139,6 +191,11 @@ function deleteWorkspace(policyID, reports, policyName) { oldPolicyName, }, })), + { + onyxMethod: Onyx.METHOD.MERGE, + key: ONYXKEYS.REIMBURSEMENT_ACCOUNT, + value: oldReimbursementAccount, + }, ]; // We don't need success data since the push notification will update @@ -162,36 +219,6 @@ function isAdminOfFreePolicy(policies) { return _.some(policies, (policy) => policy && policy.type === CONST.POLICY.TYPE.FREE && policy.role === CONST.POLICY.ROLE.ADMIN); } -/** - * Check if the user has any active free policies (aka workspaces) - * - * @param {Array} policies - * @returns {Boolean} - */ -function hasActiveFreePolicy(policies) { - const adminFreePolicies = _.filter(policies, (policy) => policy && policy.type === CONST.POLICY.TYPE.FREE && policy.role === CONST.POLICY.ROLE.ADMIN); - - if (adminFreePolicies.length === 0) { - return false; - } - - if (_.some(adminFreePolicies, (policy) => !policy.pendingAction)) { - return true; - } - - if (_.some(adminFreePolicies, (policy) => policy.pendingAction === CONST.RED_BRICK_ROAD_PENDING_ACTION.ADD)) { - return true; - } - - if (_.some(adminFreePolicies, (policy) => policy.pendingAction === CONST.RED_BRICK_ROAD_PENDING_ACTION.DELETE)) { - return false; - } - - // If there are no add or delete pending actions the only option left is an update - // pendingAction, in which case we should return true. - return true; -} - /** * Remove the passed members from the policy employeeList * From 1dff6dd3fd3d1582d7eae15f3e9aa668c1c3fe2e Mon Sep 17 00:00:00 2001 From: dukenv0307 Date: Fri, 20 Oct 2023 12:20:53 +0700 Subject: [PATCH 2/4] using lodash instead of underscore --- src/libs/actions/Policy.js | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/src/libs/actions/Policy.js b/src/libs/actions/Policy.js index bd234d22a826..4417d5f00853 100644 --- a/src/libs/actions/Policy.js +++ b/src/libs/actions/Policy.js @@ -1,4 +1,5 @@ import _ from 'underscore'; +import filter from 'lodash/filter'; import Onyx from 'react-native-onyx'; import lodashGet from 'lodash/get'; import lodashUnion from 'lodash/union'; @@ -125,8 +126,7 @@ function hasActiveFreePolicy(policies) { * @param {String} policyName */ function deleteWorkspace(policyID, reports, policyName) { - const filteredPolicies = _.filter(allPolicies, (policy) => policy.id !== policyID); - const hasActivePolicy = hasActiveFreePolicy(filteredPolicies); + const filteredPolicies = filter(allPolicies, (policy) => policy.id !== policyID); const oldReimbursementAccount = reimbursementAccount; const optimisticData = [ @@ -166,7 +166,7 @@ function deleteWorkspace(policyID, reports, policyName) { }; }), - ...(!hasActivePolicy + ...(!hasActiveFreePolicy(filteredPolicies) ? [ { onyxMethod: Onyx.METHOD.MERGE, @@ -194,7 +194,9 @@ function deleteWorkspace(policyID, reports, policyName) { { onyxMethod: Onyx.METHOD.MERGE, key: ONYXKEYS.REIMBURSEMENT_ACCOUNT, - value: oldReimbursementAccount, + value: { + errors: lodashGet(oldReimbursementAccount, 'errors', null), + }, }, ]; From b8a97da49ea9ecbfcfcdf61959ec300be462d825 Mon Sep 17 00:00:00 2001 From: dukenv0307 Date: Tue, 24 Oct 2023 12:27:52 +0700 Subject: [PATCH 3/4] remove un-use variable --- src/libs/actions/Policy.js | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/src/libs/actions/Policy.js b/src/libs/actions/Policy.js index 7f4d3b72e81f..1878ab6e71b3 100644 --- a/src/libs/actions/Policy.js +++ b/src/libs/actions/Policy.js @@ -134,8 +134,6 @@ function hasActiveFreePolicy(policies) { */ function deleteWorkspace(policyID, reports, policyName) { const filteredPolicies = filter(allPolicies, (policy) => policy.id !== policyID); - const oldReimbursementAccount = reimbursementAccount; - const optimisticData = [ { onyxMethod: Onyx.METHOD.MERGE, @@ -202,7 +200,7 @@ function deleteWorkspace(policyID, reports, policyName) { onyxMethod: Onyx.METHOD.MERGE, key: ONYXKEYS.REIMBURSEMENT_ACCOUNT, value: { - errors: lodashGet(oldReimbursementAccount, 'errors', null), + errors: lodashGet(reimbursementAccount, 'errors', null), }, }, ]; From 9b24175227286b36baed7adb9100122f059eba04 Mon Sep 17 00:00:00 2001 From: dukenv0307 Date: Thu, 2 Nov 2023 17:21:25 +0700 Subject: [PATCH 4/4] fix lint --- src/libs/actions/Policy.js | 1 + 1 file changed, 1 insertion(+) diff --git a/src/libs/actions/Policy.js b/src/libs/actions/Policy.js index db264bed5b47..9b33ff9b086e 100644 --- a/src/libs/actions/Policy.js +++ b/src/libs/actions/Policy.js @@ -1,6 +1,7 @@ import {PUBLIC_DOMAINS} from 'expensify-common/lib/CONST'; import Str from 'expensify-common/lib/str'; import {escapeRegExp} from 'lodash'; +import filter from 'lodash/filter'; import lodashGet from 'lodash/get'; import lodashUnion from 'lodash/union'; import Onyx from 'react-native-onyx';