import 'reflect-metadata' ; import { Post , Body , JsonController , Res , Get , Authorized , QueryParam , Put , Param , Delete , Req } from 'routing-controllers' ; import { classToPlain } from 'class-transformer' ; import jwt from 'jsonwebtoken' ; import { env } from '../../env' ; import { ForgotPassword as ForgotPassword } from './requests/ForgotPasswordRequest' ; import { UserLogin as LoginRequest } from './requests/UserLoginRequest' ; import { CreateUser as CreateRequest } from './requests/CreateUserRequest' ; import { UpdateUserRequest as updateUserRequest } from './requests/UpdateUserRequest' ; import { User } from '../models/User' ; import { AccessToken } from '../models/AccessTokenModel' ; import { UserService } from '../services/UserService' ; import { UserGroupService } from '../services/UserGroupService' ; import { ChangePassword } from './requests/ChangePasswordRequest' ; import { EditProfileRequest } from './requests/EditProfileRequest' ; import { AccessTokenService } from '../services/AccessTokenService' ; import { EmailTemplateService } from '../services/EmailTemplateService' ; import { MAILService } from '../../auth/mail.services' ; import { ImageService } from '../services/ImageService' ; import { S3Service } from '../services/S3Service' ; @ JsonController ( '/auth' ) export class UserController { constructor ( private userService , private userGroupService , private accessTokenService , private emailTemplateService , private s3Service , private imageService ) { } api apiGroup apiParam apiParam apiParamExample apiSuccessExample apiSampleRequest apiErrorExample @ Post ( '/login' ) public async login ( @ Body ( { validate : true } ) loginParam , @ Res ( ) response ) { console . log ( loginParam . username ) ; console . log ( loginParam . password ) ; const user = await this . userService . findOne ( { where : { username : loginParam . username , deleteFlag : 0 , } , relations : [ 'usergroup' ] , } ) ; if ( user ) { if ( await User . comparePassword ( user , loginParam . password ) ) { const token = jwt . sign ( { id : user . userId } , '123##$$)(***&' ) ; if ( user . usergroup . isActive === 0 ) { const errorResponse = { status : 0 , message : 'InActive Role' , } ; return response . status ( 400 ) . send ( errorResponse ) ; } if ( token ) { const newToken = new AccessToken ( ) ; newToken . userId = user . userId ; newToken . token = token ; const tokenSave = await this . accessTokenService . create ( newToken ) ; console . log ( tokenSave ) ; } const successResponse = { status : 1 , message : 'Loggedin successful' , data : { token , user : classToPlain ( user ) , } , } ; return response . status ( 200 ) . send ( successResponse ) ; } else { const errorResponse = { status : 0 , message : 'Invalid password' , } ; return response . status ( 400 ) . send ( errorResponse ) ; } } else { const errorResponse = { status : 0 , message : 'Invalid username' , } ; return response . status ( 400 ) . send ( errorResponse ) ; } } api apiGroup apiParam apiParam apiParam apiParam apiHeader apiSuccessExample apiSampleRequest apiErrorExample @ Get ( '/userlist' ) @ Authorized ( ) public async findAll ( @ QueryParam ( 'limit' ) limit , @ QueryParam ( 'offset' ) offset , @ QueryParam ( 'keyword' ) keyword , @ QueryParam ( 'count' ) count , @ Res ( ) response ) { console . log ( keyword ) ; const relation = [ 'usergroup' ] ; const WhereConditions = [ { name : 'deleteFlag' , value : 0 , } ] ; const user = await this . userService . list ( limit , offset , [ 'userId' , 'username' , 'firstName' , 'lastName' , 'email' , 'address' , 'phoneNumber' , 'avatar' , 'avatarPath' , 'password' , 'createdDate' ] , relation , WhereConditions , keyword , count ) ; const successResponse = { status : 1 , data : classToPlain ( user ) , message : 'Successfully get All user List' , } ; return response . status ( 200 ) . send ( successResponse ) ; } api apiGroup apiParam apiParam apiParam apiParam apiParam apiParam apiHeader apiParamExample apiSuccessExample apiSampleRequest apiErrorExample @ Post ( '/create-user' ) @ Authorized ( ) public async createUser ( @ Body ( { validate : true } ) createParam , @ Res ( ) response ) { console . log ( createParam ) ; const userGroupExistWhereCondition = [ { name : 'id' , value : createParam . userGroupId , } , ] ; const userGroupExistRecord = await this . userGroupService . list ( 0 , 0 , [ ] , userGroupExistWhereCondition , 0 ) ; if ( userGroupExistRecord . length === 0 ) { const errorResponse = { status : 0 , message : 'Invalid userGroupId' , } ; return response . status ( 400 ) . send ( errorResponse ) ; } const user = await this . userService . findOne ( { where : { username : createParam . username , deleteFlag : 0 , } , } ) ; console . log ( user ) ; if ( user ) { const errorResponse = { status : 0 , message : 'this user already saved' , } ; return response . status ( 400 ) . send ( errorResponse ) ; } console . log ( createParam . password ) ; const newUserPassword = await User . hashPassword ( createParam . password ) ; const newUserParams = new User ( ) ; newUserParams . username = createParam . username ; newUserParams . password = newUserPassword ; newUserParams . firstName = createParam . firstName ; newUserParams . lastName = createParam . lastName ; newUserParams . email = createParam . email ; newUserParams . deleteFlag = 0 ; newUserParams . userGroupId = createParam . userGroupId ; newUserParams . isActive = 1 ; const userSaveResponse = await this . userService . create ( newUserParams ) ; if ( userSaveResponse ) { const emailContent = await this . emailTemplateService . findOne ( 7 ) ; const message = emailContent . content . replace ( '{name}' , createParam . username ) . replace ( '{username}' , createParam . email ) . replace ( '{password}' , createParam . password ) ; MAILService . customerLoginMail ( message , createParam . email , emailContent . subject ) ; const successResponse = { status : 1 , message : 'User saved successfully' , data : userSaveResponse , } ; return response . status ( 200 ) . send ( successResponse ) ; } } api apiGroup apiParam apiParam apiParam apiParam apiParam apiParam apiHeader apiParamExample apiSuccessExample apiSampleRequest apiErrorExample @ Put ( '/update-user/:id' ) @ Authorized ( ) public async updateUser ( @ Param ( 'id' ) id , @ Body ( { validate : true } ) createParam , @ Req ( ) request , @ Res ( ) response ) { console . log ( createParam ) ; if ( request . user . userId === id ) { const errorResponse = { status : 0 , message : 'You cannot Edit loggedin User' , } ; return response . status ( 400 ) . send ( errorResponse ) ; } const userGroupExistWhereCondition = [ { name : 'id' , value : createParam . userGroupId , } , ] ; const userGroupExistRecord = await this . userGroupService . list ( 0 , 0 , [ ] , userGroupExistWhereCondition , 0 ) ; if ( userGroupExistRecord . length === 0 ) { const errorResponse = { status : 0 , message : 'Invalid userGroupId' , } ; return response . status ( 400 ) . send ( errorResponse ) ; } console . log ( createParam . password ) ; const newUserPassword = await User . hashPassword ( createParam . password ) ; const newUserParams = new User ( ) ; newUserParams . username = createParam . username ; if ( createParam . password ) { newUserParams . password = newUserPassword ; } newUserParams . firstName = createParam . firstName ; newUserParams . lastName = createParam . lastName ; newUserParams . email = createParam . email ; newUserParams . userGroupId = createParam . userGroupId ; newUserParams . isActive = 1 ; const userSaveResponse = await this . userService . update ( id , newUserParams ) ; if ( userSaveResponse ) { const successResponse = { status : 1 , message : 'User updated successfully' , } ; return response . status ( 200 ) . send ( successResponse ) ; } else { const errorResponse = { status : 0 , message : 'Unable to update user' , } ; return response . status ( 400 ) . send ( errorResponse ) ; } } api apiGroup apiParam apiHeader apiSuccessExample apiSampleRequest apiErrorExample @ Delete ( '/delete-user/:id' ) @ Authorized ( ) public async remove ( @ Param ( 'id' ) id , @ Req ( ) request , @ Res ( ) response ) { console . log ( request . user . userId ) ; if ( request . user . userId === id ) { const errorResponse = { status : 0 , message : 'You cannot delete loggedin user' , } ; return response . status ( 400 ) . send ( errorResponse ) ; } const user = await this . userService . findOne ( { where : { userId : id , } , } ) ; user . deleteFlag = 1 ; const deleteUser = await this . userService . create ( user ) ; if ( deleteUser ) { const successResponse = { status : 1 , message : 'User Deleted successfully' , } ; return response . status ( 200 ) . send ( successResponse ) ; } else { const errorResponse = { status : 0 , message : 'Unable to delete user' , } ; return response . status ( 400 ) . send ( errorResponse ) ; } } api apiGroup apiParam apiParamExample apiSuccessExample apiSampleRequest apiErrorExample @ Post ( '/forgot-password' ) public async forgotPassword ( @ Body ( { validate : true } ) forgotPassword , @ Res ( ) response ) { console . log ( 'emailId' + forgotPassword . email ) ; const user = await this . userService . findOne ( { where : { email : forgotPassword . email , } , } ) ; if ( ! user ) { const errorResponse = { status : 0 , message : 'Invalid email Id' , } ; return response . status ( 400 ) . send ( errorResponse ) ; } const tempPassword = Math . random ( ) . toString ( ) . substr ( 2 , 5 ) ; console . log ( tempPassword ) ; const password = await User . hashPassword ( tempPassword ) ; user . password = password ; console . log ( password ) ; await this . userService . create ( user ) ; const emailContent = await this . emailTemplateService . findOne ( 2 ) ; const message = emailContent . content . replace ( '{name}' , user . firstName ) . replace ( '{xxxxxx}' , tempPassword ) ; const sendMailRes = MAILService . passwordForgotMail ( message , user . email , emailContent . subject ) ; if ( sendMailRes ) { const successResponse = { status : 1 , message : 'Your password has been sent to your email inbox.' , } ; return response . status ( 200 ) . send ( successResponse ) ; } else { const errorResponse = { status : 0 , message : 'error in sending email' , } ; return response . status ( 400 ) . send ( errorResponse ) ; } } api apiGroup apiHeader apiParam apiParam apiParamExample apiSuccessExample apiSampleRequest apiErrorExample @ Put ( '/change-password' ) @ Authorized ( ) public async changePassword ( @ Body ( { validate : true } ) changePasswordParam , @ Req ( ) request , @ Res ( ) response ) { console . log ( request . user . userId ) ; const user = await this . userService . findOne ( { where : { userId : request . user . userId , } , } ) ; if ( ! user ) { const errResponse = { status : 0 , message : 'Invalid userId' , } ; return response . status ( 400 ) . send ( errResponse ) ; } if ( await User . comparePassword ( user , changePasswordParam . oldPassword ) ) { const val = await User . comparePassword ( user , changePasswordParam . newPassword ) ; if ( val ) { const errResponse = { status : 0 , message : 'Existing password and New password should not match' , } ; return response . status ( 400 ) . send ( errResponse ) ; } user . password = await User . hashPassword ( changePasswordParam . newPassword ) ; const updateUser = await this . userService . update ( user . userId , user ) ; if ( updateUser ) { const successResponse = { status : 1 , message : 'Your password changed successfully' , } ; return response . status ( 200 ) . send ( successResponse ) ; } } const errorResponse = { status : 0 , message : 'Your old password is wrong' , } ; return response . status ( 400 ) . send ( errorResponse ) ; } api apiGroup apiHeader apiSuccessExample apiSampleRequest apiErrorExample @ Get ( '/logout' ) @ Authorized ( ) public async logout ( @ Req ( ) request , @ Res ( ) response ) { console . log ( 'logout' ) ; console . log ( request . user . userId ) ; const user = await this . accessTokenService . findOne ( { where : { userId : request . user . userId , } , } ) ; if ( ! user ) { const errorResponse = { status : 0 , message : 'Invalid token' , } ; return response . status ( 400 ) . send ( errorResponse ) ; } const deleteToken = await this . accessTokenService . delete ( user ) ; console . log ( 'token' + deleteToken ) ; if ( ! deleteToken ) { const successResponse = { status : 1 , message : 'Successfully Logout' , } ; return response . status ( 200 ) . send ( successResponse ) ; } } api apiGroup apiHeader apiParam apiParam apiParam apiParam apiParam apiParamExample apiSuccessExample apiSampleRequest apiErrorExample @ Post ( '/edit-profile' ) @ Authorized ( ) public async editProfile ( @ Body ( { validate : true } ) editProfileParam , @ Res ( ) response , @ Req ( ) request ) { const user = await this . userService . findOne ( { where : { userId : request . user . userId , } , } ) ; const avatar = editProfileParam . avatar ; if ( avatar ) { const type = avatar . split ( ';' ) [ 0 ] . split ( '/' ) [ 1 ] ; const name = 'Img_' + Date . now ( ) + '.' + type ; const path = 'user/' ; const base64Data = new Buffer ( avatar . replace ( /^data:image\/\w+;base64,/ , '' ) , 'base64' ) ; if ( env . imageserver === 's3' ) { await this . s3Service . imageUpload ( ( path + name ) , base64Data , type ) ; } else { await this . imageService . imageUpload ( ( path + name ) , base64Data ) ; } user . avatar = name ; user . avatarPath = path ; } user . username = editProfileParam . username ; user . email = editProfileParam . email ; user . phoneNumber = editProfileParam . phoneNumber ; user . address = editProfileParam . address ; const userSave = await this . userService . create ( user ) ; if ( userSave ) { const successResponse = { status : 1 , message : 'Successfully updated profile' , data : userSave , } ; return response . status ( 200 ) . send ( successResponse ) ; } else { const errorResponse = { status : 0 , message : 'unable to edit profile' , } ; return response . status ( 400 ) . send ( errorResponse ) ; } } }