import { Injectable, NotFoundException } from '@nestjs/common'; import { UserRepository } from '../users/UserRepository'; import * as md5 from 'md5'; @Injectable() export class ChangePasswordService { constructor(private readonly userRepository: UserRepository) {} async execute(userId: number, oldPassword: string, newPassword: string) { const current = await this.userRepository.findByIdAndPassword( userId, md5(oldPassword).toUpperCase(), ); if (!current) { throw new NotFoundException('Usuário não encontrado ou senha inválida'); } const newPasswordHash = md5(newPassword).toUpperCase(); await this.userRepository.updatePassword(userId, newPasswordHash); return current; } }