import { plainToClass } from 'class-transformer' ; import { UserClaims , UserCredentials , UserData } from '../types' ; const DEFAULT_CREDENTIALS = 'default' ; export class User implements UserData { public enabled : boolean ; public claims : UserClaims ; public credentials : [ UserCredentials ] ; public criteria : string [ ] ; static extractUserFromData ( users , condition ) { const userData = users . find ( condition ) ; const currentUser = plainToClass ( User , userData ) ; return currentUser ; } static extractUserFromDataByCriteria ( users , criteria ) { const condition = ( user ) => criteria . every ( ( criterion ) => user . criteria . includes ( criterion ) ) ; return User . extractUserFromData ( users , condition ) ; } static extractEnabledUserFromDataByIdpId ( users , idpId ) { const condition = ( user ) => user . enabled === true && user . credentials . some ( ( credentials ) : boolean => credentials . idpId === idpId ) ; return User . extractUserFromData ( users , condition ) ; } getCredentials ( idpId , useDefault = true ) { let idpCredentials = this . credentials . find ( ( credentials ) => credentials . idpId === idpId ) ; if ( ! idpCredentials && useDefault ) { idpCredentials = this . credentials . find ( ( credentials ) => credentials . idpId === DEFAULT_CREDENTIALS ) ; } return idpCredentials ; } }