import { Injectable , NotFoundException , HttpException , HttpStatus } from '@nestjs/common' ; import { Repository } from 'typeorm' ; import { InjectRepository } from '@nestjs/typeorm' ; import { Users } from './entities/users.entity' ; import { IUsers } from './interfaces/users.interface' ; import { UserDto } from './dto/user.dto' ; import * as bcrypt from 'bcrypt' ; import { UserProfileDto } from './dto/user-profile.dto' ; @ Injectable ( ) export class UsersService { constructor ( @ InjectRepository ( Users ) private readonly userRepository ) { } public async findByEmail ( email ) { const user = await this . userRepository . findOne ( { where : { email : email , } , } ) ; if ( ! user ) { throw new NotFoundException ( `User ${ email } not found` ) ; } return user ; } public async findById ( userId ) { const user = await this . userRepository . findOne ( { where : { id : userId , } , } ) ; if ( ! user ) { throw new NotFoundException ( `User #${ userId } not found` ) ; } return user ; } public async create ( userDto ) { try { return await this . userRepository . save ( userDto ) ; } catch ( err ) { throw new HttpException ( err , HttpStatus . BAD_REQUEST ) ; } } public async updateByEmail ( email ) { try { const user = await this . userRepository . findOne ( { email : email } ) ; user . password = bcrypt . hashSync ( Math . random ( ) . toString ( 36 ) . slice ( - 8 ) , 8 ) ; return await this . userRepository . save ( user ) ; } catch ( err ) { throw new HttpException ( err , HttpStatus . BAD_REQUEST ) ; } } public async updateByPassword ( email , password ) { try { const user = await this . userRepository . findOne ( { email : email } ) ; user . password = bcrypt . hashSync ( password , 8 ) ; return await this . userRepository . save ( user ) ; } catch ( err ) { throw new HttpException ( err , HttpStatus . BAD_REQUEST ) ; } } public async updateProfileUser ( id , userProfileDto ) { try { const user = await this . userRepository . findOne ( { id : + id } ) ; user . name = userProfileDto . name ; user . email = userProfileDto . email ; user . username = userProfileDto . username ; return await this . userRepository . save ( user ) ; } catch ( err ) { throw new HttpException ( err , HttpStatus . BAD_REQUEST ) ; } } }