import 'reflect-metadata' ; import { Post , Body , JsonController , Res , Authorized , Req , Get , QueryParam } from 'routing-controllers' ; import { classToPlain } from 'class-transformer' ; import jwt from 'jsonwebtoken' ; import { MAILService } from '../../../auth/mail.services' ; import { CustomerRegisterRequest } from './requests/CustomerRegisterRequest' ; import { CustomerLogin } from './requests/CustomerLoginRequest' ; import { ChangePassword } from './requests/changePasswordRequest' ; import { Customer } from '../../models/Customer' ; import { CustomerService } from '../../services/CustomerService' ; import { LoginLogService } from '../../services/LoginLogService' ; import { CustomerEditProfileRequest } from './requests/CustomerEditProfileRequest' ; import { env } from '../../../env' ; import { LoginLog } from '../../models/LoginLog' ; import { EmailTemplateService } from '../../services/EmailTemplateService' ; import { ImageService } from '../../services/ImageService' ; import { S3Service } from '../../services/S3Service' ; @ JsonController ( '/customer' ) export class CustomerController { constructor ( private customerService , private s3Service , private imageService , private loginLogService , private emailTemplateService ) { } api apiGroup apiParam apiParam apiParam apiParam apiParam apiParamExample apiSuccessExample apiSampleRequest apiErrorExample @ Post ( '/register' ) public async register ( @ Body ( { validate : true } ) registerParam , @ Req ( ) request , @ Res ( ) response ) { const newUser = new Customer ( ) ; newUser . firstName = registerParam . name ; newUser . password = await Customer . hashPassword ( registerParam . password ) ; newUser . email = registerParam . emailId ; newUser . username = registerParam . emailId ; newUser . mobileNumber = registerParam . phoneNumber ; newUser . isActive = 1 ; newUser . ip = ( request . headers [ 'x-forwarded-for' ] || request . connection . remoteAddress || request . socket . remoteAddress || request . connection . socket . remoteAddress ) . split ( ',' ) [ 0 ] ; const resultUser = await this . customerService . findOne ( { where : { email : registerParam . emailId , deleteFlag : 0 } } ) ; if ( resultUser ) { const successResponse = { status : 1 , message : 'You already registered please login.' , } ; return response . status ( 400 ) . send ( successResponse ) ; } if ( registerParam . password === registerParam . confirmPassword ) { const resultData = await this . customerService . create ( newUser ) ; const emailContent = await this . emailTemplateService . findOne ( 1 ) ; const message = emailContent . content . replace ( '{name}' , resultData . firstName ) ; const sendMailRes = MAILService . registerMail ( message , resultData . email , emailContent . subject ) ; if ( sendMailRes ) { const successResponse = { status : 1 , message : 'Thank you for registering with us. Kindly check your email inbox for further details. ' , data : classToPlain ( resultData ) , } ; return response . status ( 200 ) . send ( successResponse ) ; } else { const errorResponse = { status : 0 , message : 'Registration successful, but unable to send email. ' , } ; return response . status ( 400 ) . send ( errorResponse ) ; } } const errorPasswordResponse = { status : 0 , message : 'A mismatch between password and confirm password. ' , } ; return response . status ( 400 ) . send ( errorPasswordResponse ) ; } api apiGroup apiParam apiParamExample apiSuccessExample apiSampleRequest apiErrorExample @ Post ( '/forgot-password' ) public async forgotPassword ( @ Body ( { validate : true } ) forgotparam , @ Res ( ) response ) { const resultData = await this . customerService . findOne ( { where : { email : forgotparam . emailId , deleteFlag : 0 } } ) ; if ( ! resultData ) { const errorResponse = { status : 0 , message : 'Invalid Email Id' , } ; return response . status ( 400 ) . send ( errorResponse ) ; } const tempPassword = Math . random ( ) . toString ( ) . substr ( 2 , 5 ) ; resultData . password = await Customer . hashPassword ( tempPassword ) ; const updateUserData = await this . customerService . update ( resultData . id , resultData ) ; const emailContent = await this . emailTemplateService . findOne ( 2 ) ; const message = emailContent . content . replace ( '{name}' , updateUserData . username ) . replace ( '{xxxxxx}' , tempPassword ) ; emailContent . content = message ; const sendMailRes = MAILService . passwordForgotMail ( message , updateUserData . 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, Invalid email.' , } ; return response . status ( 400 ) . send ( errorResponse ) ; } } api apiGroup apiParam apiParam apiParamExample apiSuccessExample apiSampleRequest apiErrorExample @ Post ( '/login' ) public async login ( @ Body ( { validate : true } ) loginParam , @ Req ( ) request , @ Res ( ) response ) { const resultData = await this . customerService . findOne ( { select : [ 'id' , 'firstName' , 'email' , 'mobileNumber' , 'password' , 'avatar' , 'avatarPath' , 'isActive' ] , where : { email : loginParam . emailId , deleteFlag : 0 } , } ) ; if ( ! resultData ) { const errorUserNameResponse = { status : 0 , message : 'Invalid EmailId' , } ; return response . status ( 400 ) . send ( errorUserNameResponse ) ; } if ( resultData . isActive === 0 ) { const errorUserInActiveResponse = { status : 0 , message : 'InActive Customer.' , } ; return response . status ( 400 ) . send ( errorUserInActiveResponse ) ; } if ( await Customer . comparePassword ( resultData , loginParam . password ) ) { const token = jwt . sign ( { id : resultData . id } , '123##$$)(***&' ) ; const loginLog = new LoginLog ( ) ; loginLog . customerId = resultData . id ; loginLog . emailId = resultData . email ; loginLog . firstName = resultData . firstName ; loginLog . ipAddress = ( request . headers [ 'x-forwarded-for' ] || request . connection . remoteAddress || request . socket . remoteAddress || request . connection . socket . remoteAddress ) . split ( ',' ) [ 0 ] ; const savedloginLog = await this . loginLogService . create ( loginLog ) ; const customer = await this . customerService . findOne ( { where : { email : loginParam . emailId , deleteFlag : 0 } } ) ; customer . lastLogin = savedloginLog . createdDate ; await this . customerService . create ( customer ) ; const successResponse = { status : 1 , message : 'Loggedin successfully' , data : { token , user : classToPlain ( resultData ) , } , } ; return response . status ( 200 ) . send ( successResponse ) ; } const errorResponse = { status : 0 , message : 'Invalid password' , } ; return response . status ( 400 ) . send ( errorResponse ) ; } api apiGroup apiHeader apiParam apiParam apiParamExample apiSuccessExample apiSampleRequest apiErrorExample @ Post ( '/change-password' ) @ Authorized ( 'customer' ) public async changePassword ( @ Body ( { validate : true } ) changePasswordParam , @ Req ( ) request , @ Res ( ) response ) { const resultData = await this . customerService . findOne ( { where : { id : request . user . id } } ) ; if ( await Customer . comparePassword ( resultData , changePasswordParam . oldPassword ) ) { const val = await Customer . comparePassword ( resultData , changePasswordParam . newPassword ) ; if ( val ) { const errResponse = { status : 0 , message : 'you are given a same password, please try different one' , } ; return response . status ( 400 ) . send ( errResponse ) ; } resultData . password = await Customer . hashPassword ( changePasswordParam . newPassword ) ; const updateUserData = await this . customerService . update ( resultData . id , resultData ) ; if ( updateUserData ) { 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 ( '/get-profile' ) @ Authorized ( 'customer' ) public async getProfile ( @ Req ( ) request , @ Res ( ) response ) { const resultData = await this . customerService . findOne ( { where : { id : request . user . id } } ) ; const successResponse = { status : 1 , message : 'Successfully Get the Profile.' , data : resultData , } ; return response . status ( 200 ) . send ( successResponse ) ; } api apiGroup apiHeader apiParam apiParam apiParam apiParam apiParam apiParam apiParamExample apiSuccessExample apiSampleRequest apiErrorExample @ Post ( '/edit-profile' ) @ Authorized ( 'customer' ) public async editProfile ( @ Body ( { validate : true } ) customerEditProfileRequest , @ Req ( ) request , @ Res ( ) response ) { const image = customerEditProfileRequest . image ; let name ; const resultData = await this . customerService . findOne ( { select : [ 'id' , 'firstName' , 'lastName' , 'email' , 'mobileNumber' , 'address' , 'zoneId' , 'countryId' , 'pincode' , 'avatar' , 'avatarPath' , 'password' ] , where : { id : request . user . id } , } ) ; if ( image ) { const base64Data = new Buffer ( image . replace ( /^data:image\/\w+;base64,/ , '' ) , 'base64' ) ; const type = image . split ( ';' ) [ 0 ] . split ( '/' ) [ 1 ] ; name = 'Img_' + Date . now ( ) + '.' + type ; const path = 'customer/' ; let val ; if ( env . imageserver === 's3' ) { val = await this . s3Service . imageUpload ( ( path + name ) , base64Data , type ) ; } else { val = await this . imageService . imageUpload ( ( path + name ) , base64Data ) ; } console . log ( val ) ; resultData . avatar = name ; resultData . avatarPath = path ; } resultData . firstName = customerEditProfileRequest . firstName ; resultData . lastName = customerEditProfileRequest . lastName ; resultData . email = customerEditProfileRequest . emailId ; resultData . mobileNumber = customerEditProfileRequest . phoneNumber ; resultData . username = customerEditProfileRequest . emailId ; if ( customerEditProfileRequest . password ) { resultData . password = await Customer . hashPassword ( customerEditProfileRequest . password ) ; const updateUserData = await this . customerService . update ( resultData . id , resultData ) ; if ( updateUserData ) { const successResponseResult = { status : 1 , message : 'Your profile Update Successfully.' , data : classToPlain ( updateUserData ) , } ; return response . status ( 200 ) . send ( successResponseResult ) ; } } const updateuserData = await this . customerService . update ( resultData . id , resultData ) ; const successResponse = { status : 1 , message : 'Your profile Update Successfully.' , data : classToPlain ( updateuserData ) , } ; return response . status ( 200 ) . send ( successResponse ) ; } api apiGroup apiParam apiSuccessExample apiSampleRequest apiErrorExample @ Get ( '/login-log-list' ) public async LogList ( @ QueryParam ( 'limit' ) limit , @ Res ( ) response ) { const loginLogList = await this . loginLogService . logList ( limit ) ; const promise = loginLogList . map ( async ( result ) => { const moment = require ( 'moment' ) ; const createdDate = moment . utc ( result . createdDate ) . local ( ) . format ( 'YYYY-MM-DD' ) ; const temp = result ; temp . createdDate = createdDate ; return temp ; } ) ; const finalResult = await Promise . all ( promise ) ; const successResponse = { status : 1 , message : 'Successfully get login Log list' , data : finalResult , } ; return response . status ( 200 ) . send ( successResponse ) ; } }