@mikezimm/fps-core-v7
Version:
Library of reusable core interfaces, types and constants migrated from fps-library-v2
94 lines • 4.07 kB
JavaScript
import { sortObjectArrayByNumberKey } from "../../../logic/Arrays/sorting/numbers";
import { sortObjectArrayByStringKey } from "../../../logic/Arrays/sorting/objects";
import { PermNames, UnknownPerm } from "./Constants";
/**
* 2025-01-04: Migrated from PivotTiles/Permissions
*
* @param myPermissions
* @returns
*/
export function createSortedPermissions(myPermissions) {
let limtedPermissions = false;
const complexity = 'complex';
let sortedPermissions = [];
// let groupedPermissions: IGroupedPermission[] = [];
const allUserPermissions = myPermissions.allUserPermissions;
// export interface IAllUserPermission {
// userId: number;
// permissions: any[];
// userInfo: IThisPermissionUser;
// highPriority: number;
// allPriorities: number[];
// }
//Loop through all users
allUserPermissions.map(user => {
//Loop through this user's permissions and get highest level
let onlyLimited = true;
let common = false;
let known = 'Unknown';
user.permissions.map(userPerm => {
//Add highest level to the user
let thisPerm = null;
PermNames.map(permName => {
if (permName.Name === userPerm.Name) {
thisPerm = JSON.parse(JSON.stringify(permName));
}
});
if (thisPerm === null) { //Check for similarly named permission levels.
PermNames.map(permName => {
if (userPerm.Name.toLowerCase().indexOf(permName.Name.toLowerCase()) > -1) {
thisPerm = JSON.parse(JSON.stringify(permName));
// thisPerm.Name = userPerm.Name;
thisPerm.common = false;
thisPerm.teams = false;
thisPerm.known = 'Similar';
}
});
if (thisPerm === null) { //Check for similarly named permission levels.
thisPerm = JSON.parse(JSON.stringify(UnknownPerm));
// thisPerm.Name = userPerm.Name;
}
}
if (thisPerm[complexity] > user.highPriority) {
user.highPriority = thisPerm[complexity];
}
user.allPriorities.push(thisPerm[complexity]);
if (userPerm.Name.toLowerCase().indexOf('limited') > -1) {
limtedPermissions = true;
}
else {
onlyLimited = false;
}
//Add all thisPerm props to userPerm except Name
Object.keys(thisPerm).map(key => {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const thisPermAny = thisPerm;
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const userPermAny = userPerm;
if (key !== 'Name') {
userPermAny[key] = thisPermAny[key];
}
});
if (thisPerm.common === true) {
common = true;
}
if (thisPerm.known === 'Exact') {
known = 'Exact';
}
else if (thisPerm.known === 'Similar' && known === 'Unknown') {
known = 'Similar';
}
});
user.onlyLimited = onlyLimited;
user.known = known;
user.common = common;
//Push User info to Sorted Users group
sortedPermissions.push(user);
});
sortedPermissions = sortObjectArrayByStringKey(sortedPermissions, 'asc', 'userTitle'); // https://github.com/mikezimm/pivottiles7/issues/142
sortedPermissions = sortObjectArrayByNumberKey(sortedPermissions, 'desc', 'highPriority');
myPermissions.sortedPermissions = sortedPermissions;
myPermissions.limtedPermissions = limtedPermissions;
return myPermissions;
}
//# sourceMappingURL=createSortedPermissions.js.map