const randtoken = require ( 'rand-token' ) ; import CONFIG from '../../../config' ; import log from './../../../utils/logger' ; import { UserRepository } from '../../auth/repositories/user.repository' ; import { InMemoryUserRepository } from '../../auth/repositories/in-memory/in-memory-user.repository' ; import { UserRole , Id } from '../../../models/types' ; import { User } from '../../../models/user' ; const userRepository = new InMemoryUserRepository ( ) ; const PATCHABLE_PROPS = [ 'tfa' ] ; export class AccountService { getUsers ( accountId ) { return userRepository . getUsers ( accountId ) . then ( users => users . map ( u => User . toSafeUser ( u ) ) ) ; } createUser ( userEmail , role , accountId ) { const confirmationCode = randtoken . uid ( 256 ) ; return userRepository . createUser ( { accountId : accountId , email : userEmail , password : undefined , role : role , confirmed : false , confirmationCode , createdWith : 'password' } ) . then ( ( ) => { log . info ( 'settings.create_user_successful' , { email : userEmail } ) ; this . sendConfirmationEmail ( userEmail , confirmationCode ) ; return Promise . resolve ( ) ; } ) . catch ( error => { log . error ( 'settings.create_user_failed' , { email : userEmail } ) ; throw error ; } ) ; } patchUser ( userId , data , session ? ) { const filteredUser = Object . keys ( data ) . filter ( property => PATCHABLE_PROPS . includes ( property ) ) . reduce ( ( user , property ) => { user [ property ] = data [ property ] ; return user ; } , { } ) ; return userRepository . patchUser ( userId , filteredUser ) . then ( patchedUser => { if ( session && session . user && session . user . id === userId ) { session . user = patchedUser ; } } ) ; } deleteUser ( userId ) { return userRepository . deleteUser ( userId ) ; } private sendConfirmationEmail ( email , code ) { const link = `${ CONFIG . clientUrl }/password?email=${ email }&code=${ code }` ; console . log ( `>>> LINK >>>: ${ link }` ) ; log . info ( 'settings.create_user_confirmation_email_sent' , { email } ) ; } }