import * as Joi from 'joi' ; import { Response , Request } from 'express' ; import { inject , injectable } from 'inversify' ; import { controller , httpPost , httpGet , httpPut } from 'inversify-express-utils' ; import { responseWith } from '../helpers/responses' ; import { AuthenticatedRequest } from '../interfaces' ; import { UserCommonApplicationType , UserCommonApplication } from '../services/app/user/user.common.app' ; import { UserAccountApplicationType , UserAccountApplication } from '../services/app/user/user.account.app' ; import { UserPasswordApplicationType , UserPasswordApplication } from '../services/app/user/user.password.app' ; import { commonFlowRequestMiddleware , ethereumAddressValidator , verificationValidateSchema } from '../middlewares/request.validation' ; const passwordRegex = /^[a-zA-Z0\d!"#$%&'()*+,-./:;<=>?@[\]^_`{|}~]{8,}$/ ; /**
 * UserController
 */ @ controller ( '/user' , 'ThrottlerMiddleware' ) export class UserController { constructor ( @ inject ( UserCommonApplicationType ) private userCommonApp , @ inject ( UserAccountApplicationType ) private userAccountApp , @ inject ( UserPasswordApplicationType ) private userPasswordApp ) { } param req param res @ httpPost ( '/' , ( req , res , next ) => { commonFlowRequestMiddleware ( Joi . object ( ) . keys ( { name : Joi . string ( ) . min ( 3 ) . required ( ) , email : Joi . string ( ) . email ( ) . required ( ) , password : Joi . string ( ) . required ( ) . regex ( passwordRegex ) , paymentPassword : Joi . string ( ) . required ( ) . regex ( passwordRegex ) , agreeTos : Joi . boolean ( ) . only ( true ) . required ( ) } ) , req , res , next ) ; } ) async create ( req , res ) { res . json ( await this . userAccountApp . create ( req . body ) ) ; } param req param res @ httpPost ( '/activate' , 'VerificationRequiredValidation' ) async activate ( req , res ) { res . json ( await this . userAccountApp . activate ( req . body ) ) ; } param req param res @ httpPost ( '/login/initiate' , ( req , res , next ) => { commonFlowRequestMiddleware ( Joi . object ( ) . keys ( { email : Joi . string ( ) . email ( ) . required ( ) , password : Joi . string ( ) . required ( ) } ) , req , res , next ) ; } ) async initiateLogin ( req , res ) { res . json ( await this . userAccountApp . initiateLogin ( req . body , req . app . locals . remoteIp ) ) ; } param req param res @ httpPost ( '/login/verify' , 'VerificationRequiredValidation' ) async validateLogin ( req , res ) { res . status ( 200 ) . send ( await this . userAccountApp . verifyLogin ( req . body ) ) ; } param req param res @ httpGet ( '/me' , 'AuthMiddleware' ) async getMe ( req , res ) { res . json ( await this . userCommonApp . getUserInfo ( req . app . locals . user ) ) ; } param req param res @ httpPost ( '/me/erc20token/register' , 'AuthMiddleware' , ( req , res , next ) => { commonFlowRequestMiddleware ( Joi . object ( ) . keys ( { walletAddress : ethereumAddressValidator . optional ( ) , contractAddress : ethereumAddressValidator . required ( ) , symbol : Joi . string ( ) . required ( ) , name : Joi . string ( ) . optional ( ) , decimals : Joi . number ( ) . valid ( 0 , 3 , 6 , 9 , 12 , 15 , 18 , 21 , 24 , 27 , 30 ) . required ( ) } ) , req , res , next ) ; } ) async registerErc20Token ( req , res ) { res . json ( await this . userCommonApp . registerToken ( req . app . locals . user , req . body . walletAddress , req . body ) ) ; } param req param res @ httpPost ( '/me/changePaymentPassword/initiate' , 'AuthMiddleware' , ( req , res , next ) => { commonFlowRequestMiddleware ( Joi . object ( ) . keys ( { oldPaymentPassword : Joi . string ( ) . required ( ) , newPaymentPassword : Joi . string ( ) . regex ( passwordRegex ) . disallow ( Joi . ref ( 'oldPaymentPassword' ) ) . required ( ) } ) , req , res , next ) ; } ) async initiateChangePaymentPassword ( req , res ) { res . json ( await this . userPasswordApp . initiateChangePaymentPassword ( req . app . locals . user , req . body ) ) ; } param req param res @ httpPost ( '/me/changePaymentPassword/verify' , 'AuthMiddleware' , 'VerificationRequiredValidation' ) async verifyChangePaymentPassword ( req , res ) { res . json ( await this . userPasswordApp . verifyChangePaymentPassword ( req . app . locals . user , req . body ) ) ; } param req param res @ httpPost ( '/me/changePassword/initiate' , 'AuthMiddleware' , ( req , res , next ) => { commonFlowRequestMiddleware ( Joi . object ( ) . keys ( { oldPassword : Joi . string ( ) . required ( ) , newPassword : Joi . string ( ) . regex ( passwordRegex ) . disallow ( Joi . ref ( 'oldPassword' ) ) . required ( ) } ) , req , res , next ) ; } ) async initiateChangePassword ( req , res ) { res . json ( await this . userPasswordApp . initiateChangePassword ( req . app . locals . user , req . body ) ) ; } param req param res @ httpPost ( '/me/changePassword/verify' , 'AuthMiddleware' , 'VerificationRequiredValidation' ) async verifyChangePassword ( req , res ) { res . json ( await this . userPasswordApp . verifyChangePassword ( req . app . locals . user , req . body ) ) ; } param req param res @ httpPost ( '/resetPassword/initiate' , ( req , res , next ) => { commonFlowRequestMiddleware ( Joi . object ( ) . keys ( { email : Joi . string ( ) . required ( ) . email ( ) } ) , req , res , next ) ; } ) async initiateResetPassword ( req , res ) { res . json ( await this . userPasswordApp . initiateResetPassword ( req . body ) ) ; } param req param res @ httpPost ( '/resetPassword/verify' , ( req , res , next ) => { commonFlowRequestMiddleware ( Joi . object ( ) . keys ( { email : Joi . string ( ) . required ( ) . email ( ) , verification : verificationValidateSchema } ) , req , res , next ) ; } ) async verifyResetPassword ( req , res ) { res . json ( await this . userPasswordApp . verifyResetPassword ( req . body ) ) ; } param req param res @ httpPost ( '/resetPassword/enter' , ( req , res , next ) => { commonFlowRequestMiddleware ( Joi . object ( ) . keys ( { email : Joi . string ( ) . email ( ) . required ( ) , resetId : Joi . string ( ) . required ( ) , password : Joi . string ( ) . required ( ) . regex ( passwordRegex ) } ) , req , res , next ) ; } ) async enterResetPassword ( req , res ) { res . json ( await this . userPasswordApp . resetPasswordEnter ( req . body ) ) ; } param req param res @ httpGet ( '/enable2fa/initiate' , 'AuthMiddleware' ) async enable2faInitiate ( req , res ) { res . json ( await this . userAccountApp . initiateEnableGoogleAuth ( req . app . locals . user ) ) ; } param req param res @ httpPost ( '/enable2fa/verify' , 'AuthMiddleware' , 'VerificationRequiredValidation' ) async enable2faVerify ( req , res ) { res . json ( await this . userAccountApp . verifyEnableGoogleAuth ( req . app . locals . user , req . body ) ) ; } param req param res @ httpGet ( '/disable2fa/initiate' , 'AuthMiddleware' ) async disable2faInitiate ( req , res ) { res . json ( await this . userAccountApp . initiateDisableGoogleAuth ( req . app . locals . user ) ) ; } param req param res @ httpPost ( '/disable2fa/verify' , 'AuthMiddleware' , 'VerificationRequiredValidation' ) async disable2faVerify ( req , res ) { res . json ( await this . userAccountApp . verifyDisableGoogleAuth ( req . app . locals . user , req . body ) ) ; } param req param res @ httpPost ( '/preferences/notifications' , 'AuthMiddleware' , ( req , res , next ) => { commonFlowRequestMiddleware ( Joi . object ( ) . keys ( { notifications : Joi . object ( ) . required ( ) } ) , req , res , next ) ; } ) async setNotification ( req , res ) { res . json ( await this . userAccountApp . setNotifications ( req . app . locals . user , req . body . notifications ) ) ; } param req param res @ httpPost ( '/preferences/verifications/initiate' , 'AuthMiddleware' , ( req , res , next ) => { commonFlowRequestMiddleware ( Joi . object ( ) . keys ( { verifications : Joi . object ( ) . required ( ) } ) , req , res , next ) ; } ) async setVerificationInitiate ( req , res ) { res . json ( await this . userAccountApp . initiateSetVerifications ( req . app . locals . user , req . body . verifications ) ) ; } param req param res @ httpPost ( '/preferences/verifications/verify' , 'AuthMiddleware' , 'VerificationRequiredValidation' ) async setVerificationVerify ( req , res ) { res . json ( await this . userAccountApp . verifySetVerifications ( req . app . locals . user , req . body ) ) ; } param req param res @ httpPost ( '/me/wallets' , 'AuthMiddleware' , ( req , res , next ) => { commonFlowRequestMiddleware ( Joi . object ( ) . keys ( { type : Joi . string ( ) . valid ( 'ETH' ) . required ( ) , paymentPassword : Joi . string ( ) . required ( ) . regex ( passwordRegex ) , name : Joi . string ( ) . required ( ) , color : Joi . number ( ) . required ( ) , } ) , req , res , next ) ; } ) async createNewWallet ( req , res ) { res . json ( await this . userAccountApp . createAndAddNewWallet ( req . app . locals . user , req . body . type , req . body . paymentPassword , { name : req . body . name , color : req . body . color } ) ) ; } param req param res @ httpPut ( '/me/wallets' , 'AuthMiddleware' , ( req , res , next ) => { commonFlowRequestMiddleware ( Joi . object ( ) . keys ( { address : Joi . string ( ) . required ( ) , name : Joi . string ( ) . when ( 'color' , { is : null , then : Joi . required ( ) , otherwise : Joi . optional ( ) } ) , color : Joi . number ( ) . when ( 'name' , { is : null , then : Joi . required ( ) , otherwise : Joi . optional ( ) } ) , } ) , req , res , next ) ; } ) async updateWallet ( req , res ) { res . json ( await this . userAccountApp . updateWallet ( req . app . locals . user , { address : req . body . address , name : req . body . name || null , color : req . body . color || null } ) ) ; } }