import { Injectable } from '@nestjs/common' ; import { InjectRepository } from '@nestjs/typeorm' ; import { User } from '../domain/user.entity' ; import { UserDTO } from './dto/user.dto' ; import { UserMapper } from './mapper/user.mapper' ; import { UserRepository } from '../repository/user.repository' ; import { FindManyOptions , FindOneOptions } from 'typeorm' ; import { transformPassword } from '../security' ; @ Injectable ( ) export class UserService { constructor ( @ InjectRepository ( UserRepository ) private userRepository ) { } async findById ( id ) { const result = await this . userRepository . findOne ( id ) ; return UserMapper . fromEntityToDTO ( this . flatAuthorities ( result ) ) ; } async findByFields ( options ) { options . relations = [ 'authorities' ] ; const result = await this . userRepository . findOne ( options ) ; return UserMapper . fromEntityToDTO ( this . flatAuthorities ( result ) ) ; } async find ( options ) { const result = await this . userRepository . findOne ( options ) ; return UserMapper . fromEntityToDTO ( this . flatAuthorities ( result ) ) ; } async findAndCount ( options ) { options . relations = [ 'authorities' ] ; const resultList = await this . userRepository . findAndCount ( options ) ; const usersDTO = [ ] ; if ( resultList && resultList [ 0 ] ) { resultList [ 0 ] . forEach ( user => usersDTO . push ( UserMapper . fromEntityToDTO ( this . flatAuthorities ( user ) ) ) ) ; resultList [ 0 ] = usersDTO ; } return resultList ; } async save ( userDTO , creator ? , updatePassword = false ) { const user = this . convertInAuthorities ( UserMapper . fromDTOtoEntity ( userDTO ) ) ; if ( updatePassword ) { await transformPassword ( user ) ; } if ( creator ) { if ( ! user . createdBy ) { user . createdBy = creator ; } user . lastModifiedBy = creator ; } const result = await this . userRepository . save ( user ) ; return UserMapper . fromEntityToDTO ( this . flatAuthorities ( result ) ) ; } async update ( userDTO , updater ? ) { return this . save ( userDTO , updater ) ; } async delete ( userDTO ) { const user = UserMapper . fromDTOtoEntity ( userDTO ) ; const result = await this . userRepository . remove ( user ) ; return UserMapper . fromEntityToDTO ( result ) ; } private flatAuthorities ( user ) { if ( user && user . authorities ) { const authorities = [ ] ; user . authorities . forEach ( authority => authorities . push ( authority . name ) ) ; user . authorities = authorities ; } return user ; } private convertInAuthorities ( user ) { if ( user && user . authorities ) { const authorities = [ ] ; user . authorities . forEach ( authority => authorities . push ( { name : authority } ) ) ; user . authorities = authorities ; } return user ; } }