import { VERIFICATION_CODE_STATUS } from 'common/enums' ; import { CodeInvalidError , PasswordInvalidError , UserNotFoundError , } from 'common/errors' ; import { isValidPassword , isValidPaymentPassword } from 'common/utils' ; import { GQLVerificationCodeType , MutationToResetPasswordResolver , } from 'definitions' ; const resolver = async ( _ , { input : { password , codeId : uuid , type } } , { viewer , dataSources : { userService , notificationService } } ) => { const [ code ] = await userService . findVerificationCodes ( { where : { uuid , type : type === 'payment' ? GQLVerificationCodeType . payment_password_reset : GQLVerificationCodeType . password_reset , status : VERIFICATION_CODE_STATUS . verified , } , } ) ; if ( ! code ) { throw new CodeInvalidError ( 'code does not exists' ) ; } const user = await userService . findByEmail ( code . email ) ; if ( ! user ) { throw new UserNotFoundError ( 'target user does not exists' ) ; } if ( type === 'payment' ) { if ( ! isValidPaymentPassword ( password ) ) { throw new PasswordInvalidError ( 'invalid payment password, should be 6 digits.' ) ; } } else { if ( ! isValidPassword ( password ) ) { throw new PasswordInvalidError ( 'invalid user password' ) ; } } await userService . changePassword ( { userId : user . id , password , type } ) ; await userService . markVerificationCodeAs ( { codeId : code . id , status : VERIFICATION_CODE_STATUS . used , } ) ; if ( type === 'payment' ) { notificationService . mail . sendPayment ( { to : user . email , recipient : { displayName : user . displayName , userName : user . userName , } , type : 'passwordChanged' , } ) ; } return true ; } ; export default resolver ;