import { Permissions , combinePermissions , hasPermissions } from './Permissions' ; import type { PermissionsData } from './PermissionsData' ; export enum Role { ADMIN = 'admin' , EXTERNAL = 'partner' , MEMBER = 'member' , OWNER = 'owner' } export const roleToPermissions = ( role ) : Permissions => { switch ( role . toLowerCase ( ) ) { case Role . OWNER : { return combinePermissions ( [ roleToPermissions ( Role . ADMIN ) , Permissions . DELETE_TEAM , Permissions . GET_BILLING , Permissions . SET_BILLING , ] ) ; } case Role . ADMIN : { return combinePermissions ( [ roleToPermissions ( Role . MEMBER ) , Permissions . ADD_TEAM_MEMBER , Permissions . REMOVE_TEAM_MEMBER , Permissions . SET_MEMBER_PERMISSIONS , Permissions . SET_TEAM_DATA , ] ) ; } case Role . MEMBER : { return combinePermissions ( [ roleToPermissions ( Role . EXTERNAL ) , Permissions . ADD_CONVERSATION_MEMBER , Permissions . DELETE_CONVERSATION , Permissions . GET_MEMBER_PERMISSIONS , Permissions . REMOVE_CONVERSATION_MEMBER , ] ) ; } case Role . EXTERNAL : { return combinePermissions ( [ Permissions . CREATE_CONVERSATION , Permissions . GET_TEAM_CONVERSATIONS ] ) ; } default : { return 0 ; } } } ; export const permissionsToRole = ( permissions ) : Role | undefined => { if ( hasPermissions ( permissions . self , roleToPermissions ( Role . OWNER ) ) ) { return Role . OWNER ; } if ( hasPermissions ( permissions . self , roleToPermissions ( Role . ADMIN ) ) ) { return Role . ADMIN ; } if ( hasPermissions ( permissions . self , roleToPermissions ( Role . MEMBER ) ) ) { return Role . MEMBER ; } if ( hasPermissions ( permissions . self , roleToPermissions ( Role . EXTERNAL ) ) ) { return Role . EXTERNAL ; } return undefined ; } ; export const isPartner = ( permissions ) : boolean => { return ! ! ( permissions && permissionsToRole ( permissions ) === Role . EXTERNAL ) ; } ; export const isMember = ( permissions ) : boolean => { return ! ! ( permissions && permissionsToRole ( permissions ) === Role . MEMBER ) ; } ; export const isAdmin = ( permissions ) : boolean => { return ! ! ( permissions && permissionsToRole ( permissions ) === Role . ADMIN ) ; } ; export const isOwner = ( permissions ) : boolean => { return ! ! ( permissions && permissionsToRole ( permissions ) === Role . OWNER ) ; } ; export const isAtLeastPartner = ( permissions ) : boolean => { return isPartner ( permissions ) || isMember ( permissions ) || isAdmin ( permissions ) || isOwner ( permissions ) ; } ; export const isAtLeastMember = ( permissions ) : boolean => { return isMember ( permissions ) || isAdmin ( permissions ) || isOwner ( permissions ) ; } ; export const isAtLeastAdmin = ( permissions ) : boolean => { return isAdmin ( permissions ) || isOwner ( permissions ) ; } ; export const isAtLeastEqual = ( permissions , otherPermissions ) : boolean => { return ( ( isOwner ( permissions ) && isOwner ( otherPermissions ) ) || ( isAtLeastAdmin ( permissions ) && isAdmin ( otherPermissions ) ) || ( isAtLeastMember ( permissions ) && isMember ( otherPermissions ) ) || ( isAtLeastPartner ( permissions ) && isPartner ( otherPermissions ) ) ) ; } ;