-
-
Notifications
You must be signed in to change notification settings - Fork 37
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1 from PatrickOtero/feature/redefinePassUser
Feature/redefine pass user
- Loading branch information
Showing
11 changed files
with
248 additions
and
25 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
import { | ||
registerDecorator, | ||
ValidationArguments, | ||
ValidationOptions, | ||
ValidatorConstraint, | ||
ValidatorConstraintInterface, | ||
} from 'class-validator'; | ||
|
||
export function Match(property: string, validationOptions?: ValidationOptions) { | ||
return (object: any, propertyName: string) => { | ||
registerDecorator({ | ||
target: object.constructor, | ||
propertyName, | ||
options: validationOptions, | ||
constraints: [property], | ||
validator: MatchConstraint, | ||
}); | ||
}; | ||
} | ||
|
||
@ValidatorConstraint({ name: 'Match' }) | ||
export class MatchConstraint implements ValidatorConstraintInterface { | ||
validate(value: any, args: ValidationArguments) { | ||
const [relatedPropertyName] = args.constraints; | ||
const relatedValue = (args.object as any)[relatedPropertyName]; | ||
return value === relatedValue; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
import { Injectable } from '@nestjs/common'; | ||
import * as bcrypt from 'bcrypt'; | ||
import { MailService } from 'src/modules/mails/mail.service'; | ||
import { UpdateMyPasswordDto } from '../dtos/update-my-password.dto'; | ||
import { CompanyRepository } from '../repository/company-repository'; | ||
import { CompaniesEntity } from 'src/database/entities/companies.entity'; | ||
|
||
@Injectable() | ||
export class UpdateCompanyPassword { | ||
constructor( | ||
private companyRepository: CompanyRepository, | ||
private mailService: MailService, | ||
) {} | ||
|
||
async execute( | ||
company: CompaniesEntity, | ||
{ oldPassword, password, confirmNewPassword }: UpdateMyPasswordDto, | ||
) { | ||
const user = await this.companyRepository.findCompanyById(company.id); | ||
|
||
const isOldPassCorrect = await bcrypt.compare(oldPassword, user.password); | ||
|
||
if (!isOldPassCorrect) { | ||
return { | ||
status: 400, | ||
data: { message: 'A senha atual está incorreta.' }, | ||
}; | ||
} | ||
|
||
if (password != confirmNewPassword) { | ||
return { | ||
status: 400, | ||
data: { message: 'As senhas não conferem!' }, | ||
}; | ||
} | ||
const passwordHash = await bcrypt.hash(password, 10); | ||
|
||
const companyUpdated = await this.companyRepository.updatePassword( | ||
company.id, | ||
passwordHash, | ||
); | ||
|
||
await this.mailService.sendCompanyConfirmation(companyUpdated); | ||
|
||
return { | ||
status: 200, | ||
data: { message: 'Senha redefinida com sucesso!' }, | ||
}; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
import { Injectable } from '@nestjs/common'; | ||
import * as bcrypt from 'bcrypt'; | ||
import { MailService } from 'src/modules/mails/mail.service'; | ||
import { UpdateMyPasswordDto } from '../dtos/update-my-password.dto'; | ||
import { UserRepository } from '../repository/user.repository'; | ||
import { UsersEntity } from 'src/database/entities/users.entity'; | ||
|
||
@Injectable() | ||
export class UpdatePasswordService { | ||
constructor( | ||
private userRepository: UserRepository, | ||
private mailService: MailService, | ||
) {} | ||
|
||
async execute( | ||
user: UsersEntity, | ||
{ oldPassword, password, confirmNewPassword }: UpdateMyPasswordDto, | ||
) { | ||
const userExists = await this.userRepository.findOneById(user.id); | ||
|
||
if (!userExists) { | ||
return { | ||
status: 400, | ||
data: { message: 'usuário não encontrado ou não autenticado.' }, | ||
}; | ||
} | ||
|
||
const isOldPassCorrect = await bcrypt.compare( | ||
oldPassword, | ||
userExists.password, | ||
); | ||
|
||
if (!isOldPassCorrect) { | ||
return { | ||
status: 400, | ||
data: { message: 'A senha atual está incorreta.' }, | ||
}; | ||
} | ||
|
||
if (password != confirmNewPassword) { | ||
return { | ||
status: 400, | ||
data: { message: 'As senhas não conferem!' }, | ||
}; | ||
} | ||
const passwordHash = await bcrypt.hash(password, 10); | ||
|
||
const userUpdated = await this.userRepository.updatePassword( | ||
user.id, | ||
passwordHash, | ||
); | ||
|
||
await this.mailService.sendUserConfirmation(userUpdated); | ||
|
||
return { | ||
status: 200, | ||
data: { message: 'Senha redefinida com sucesso!' }, | ||
}; | ||
} | ||
} |
Oops, something went wrong.