Skip to content

Commit

Permalink
Merge pull request #29781 from dukenv0307/fix/29235
Browse files Browse the repository at this point in the history
Remove error when all workspace is deleted
  • Loading branch information
luacmartins authored Nov 3, 2023
2 parents bc9cb8f + 9b24175 commit 509d352
Showing 1 changed file with 57 additions and 30 deletions.
87 changes: 57 additions & 30 deletions src/libs/actions/Policy.js
Original file line number Diff line number Diff line change
@@ -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';
Expand Down Expand Up @@ -74,6 +75,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,
Expand All @@ -96,6 +103,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
*
Expand All @@ -104,6 +141,7 @@ function updateLastAccessedWorkspace(policyID) {
* @param {String} policyName
*/
function deleteWorkspace(policyID, reports, policyName) {
const filteredPolicies = filter(allPolicies, (policy) => policy.id !== policyID);
const optimisticData = [
{
onyxMethod: Onyx.METHOD.MERGE,
Expand Down Expand Up @@ -146,6 +184,18 @@ function deleteWorkspace(policyID, reports, policyName) {
value: optimisticReportActions,
};
}),

...(!hasActiveFreePolicy(filteredPolicies)
? [
{
onyxMethod: Onyx.METHOD.MERGE,
key: ONYXKEYS.REIMBURSEMENT_ACCOUNT,
value: {
errors: null,
},
},
]
: []),
];

// Restore the old report stateNum and statusNum
Expand All @@ -160,6 +210,13 @@ function deleteWorkspace(policyID, reports, policyName) {
oldPolicyName,
},
})),
{
onyxMethod: Onyx.METHOD.MERGE,
key: ONYXKEYS.REIMBURSEMENT_ACCOUNT,
value: {
errors: lodashGet(reimbursementAccount, 'errors', null),
},
},
];

// We don't need success data since the push notification will update
Expand All @@ -183,36 +240,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
*
Expand Down

0 comments on commit 509d352

Please sign in to comment.