Skip to content

Instantly share code, notes, and snippets.

@akkikumar72
Created October 18, 2021 11:46
Show Gist options
  • Save akkikumar72/d440b7d31080c10bac276a0bf86377a1 to your computer and use it in GitHub Desktop.
Save akkikumar72/d440b7d31080c10bac276a0bf86377a1 to your computer and use it in GitHub Desktop.
FOr Testing
import { getUserName } from '../../../../../utils/user-info';
// Virt doesn't have any group
const checkIfVirtHasGroup = (virtGroups, deploymentID) => {
const findMatchingGroupAsPerVirt = virtGroups.filter((p) => p.deployments.includes(Number(deploymentID)));
return findMatchingGroupAsPerVirt.length === 0;
};
const checkForNormalUser = (virtGroups) => {
return virtGroups?.every((p) => p.users.includes(getUserName()));
};
// this case when virt belongs to logged-in user but its grouped and that group doesn't belong to user.
const checkIfVirtAssignedGroupHasUser = (virtGroups, deploymentID) => {
const findMatchingGroupAsPerVirt = virtGroups?.filter((p) => p.deployments.includes(Number(deploymentID)));
return findMatchingGroupAsPerVirt?.some((value) => value.users.includes(getUserName()));
};
const checkIfUserIdFromVirtIsSameAsLoggedIn = (userId) => userId === getUserName();
export const checkForDisable = (userId, deploymentID, virtGroups, isAdmin) => {
if (isAdmin) {
return false;
} else {
// For virt who doesn't have any group
// It all active too
if (checkIfVirtHasGroup(virtGroups, deploymentID)) {
return false;
// For virt which is assigned to a group and logged in user is part of that group
} else if (checkIfVirtHasGroup(virtGroups, deploymentID) && checkIfUserIdFromVirtIsSameAsLoggedIn(userId)) {
return !checkIfVirtAssignedGroupHasUser(virtGroups, deploymentID);
}
// For Virt which has group assigned and are not owner but member of group
else if (
!checkIfVirtHasGroup(virtGroups, deploymentID) &&
checkIfVirtAssignedGroupHasUser(virtGroups, deploymentID)
) {
return false;
}
// For normal users
else if (checkForNormalUser(virtGroups)) {
return false;
}
// Otherwise disabled
else {
return true;
}
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment