import { Request , Response , NextFunction } from 'express' ; import { getRepository } from 'typeorm' ; import { User } from 'typeorm/entities/users/User' ; import { CustomError } from 'utils/response/custom-error/CustomError' ; export const register = async ( req , res , next ) => { const { email , password } = req . body ; const userRepository = getRepository ( User ) ; try { const user = await userRepository . findOne ( { where : { email } } ) ; if ( user ) { const customError = new CustomError ( 400 , 'General' , 'User already exists' , [ `Email '${ user . email }' already exists` , ] ) ; return next ( customError ) ; } try { const newUser = new User ( ) ; newUser . email = email ; newUser . password = password ; newUser . hashPassword ( ) ; await userRepository . save ( newUser ) ; res . customSuccess ( 200 , 'User successfully created.' ) ; } catch ( err ) { const customError = new CustomError ( 400 , 'Raw' , `User '${ email }' can't be created` , null , err ) ; return next ( customError ) ; } } catch ( err ) { const customError = new CustomError ( 400 , 'Raw' , 'Error' , null , err ) ; return next ( customError ) ; } } ;