import { ReflectMetadata } from '@nestjs/common' ; import { TokenTypeEnum , UserRoleEnum } from './auth.interfaces' ; export const TokenRequirements = ( requiredTokenType , requiredUserRoles ) => ReflectMetadata ( 'tokenrequirements' , new TokenRequirementsHelper ( requiredTokenType , requiredUserRoles ) ) ; export class TokenRequirementsHelper { private requiredTokenType : TokenTypeEnum ; private requiredUserRoles : UserRoleEnum [ ] ; constructor ( requiredTokenType , requiredUserRoles ) { this . requiredTokenType = requiredTokenType ; this . requiredUserRoles = requiredUserRoles ; } public tokenIsOfType ( tokenType ) { return tokenType === this . requiredTokenType ; } public tokenHasAllUserRoles ( userRoles ) { return this . requiredUserRoles . every ( requiredRole => userRoles . indexOf ( requiredRole ) > - 1 ) || this . requiredUserRoles . length === 0 ; } }