import { IRepository , IService , ForgotPassword , IVerificationGeneratorDependencies , InetLocation , VerifyUuid , ResetPassword } from '@btcp/bootcamp-entities' ; import { BadRequestException } from './exceptions/bad-request.exception' ; import { ForgotPasswordRepository } from '@btcp/bootcamp-data' ; export class ForgotPasswordService implements IService { HOURS_TO_VERIFY = 4 ; private readonly _repository : IRepository < string , ForgotPassword > ; private readonly _forgotPasswordDependencies : IVerificationGeneratorDependencies ; constructor ( repository , forgotPasswordDependencies ) { this . _repository = repository ; this . _forgotPasswordDependencies = forgotPasswordDependencies ; } async save ( refreshToken ) { return await this . _repository . save ( refreshToken ) ; } public async saveForgotPassword ( email , inetLoc ) { const expires = this . _forgotPasswordDependencies . getExpirationDate ( this . HOURS_TO_VERIFY ) ; const a = { email : email , verification : this . _forgotPasswordDependencies . createVerificationCode ( ) , expires : expires , ip : inetLoc . ip , browser : inetLoc . browser , country : inetLoc . country , } ; const forgotPassword = await this . _repository . create ( a ) ; await this . _repository . save ( forgotPassword ) ; } public async findForgotPasswordByUuid ( verification ) { const forgotPassword = await ( this . _repository as ForgotPasswordRepository ) . getByVerification ( verification ) ; if ( ! forgotPassword ) { return null ; } return forgotPassword ; } public async setForgotPasswordFirstUsed ( forgotPassword , inetLoc ) { forgotPassword . firstUsed = true ; forgotPassword . ipChanged = inetLoc . ip ; forgotPassword . browserChanged = inetLoc . browser ; forgotPassword . countryChanged = inetLoc . country ; await this . _repository . save ( forgotPassword ) ; } public async findForgotPasswordByEmail ( email ) { const forgotPassword = await ( this . _repository as ForgotPasswordRepository ) . getByEmail ( email ) ; if ( ! forgotPassword ) { throw new BadRequestException ( 'Bad request.' ) ; } return forgotPassword ; } public async setForgotPasswordFinalUsed ( forgotPassword ) { forgotPassword . finalUsed = true ; await this . _repository . save ( forgotPassword ) ; } public async resetUserPassword ( email , password ) { const user = await this . _repository . getByQuery ( { email : email , verified : true , } ) ; user . pw = password ; await this . _repository . save ( user ) ; } }