Files
api-AT/src/user/user.service.ts
T

126 lines
3.0 KiB
TypeScript
Raw Normal View History

2025-10-08 15:45:21 -06:00
import {
BadRequestException,
Injectable,
NotFoundException,
} from '@nestjs/common';
2025-09-12 20:41:51 -06:00
import { Response } from 'express';
import { changePasswordDto, CreateUserDto, Login } from './dto/create-user.dto';
import { InjectRepository } from '@nestjs/typeorm';
2025-09-24 11:00:56 -06:00
import { Perfil, User } from './entities/user.entity';
import { Repository } from 'typeorm';
2025-09-10 21:03:33 -04:00
import { JwtService } from '@nestjs/jwt';
2025-09-04 21:04:15 -04:00
@Injectable()
export class UserService {
constructor(
@InjectRepository(User)
private userRepository: Repository<User>,
2025-09-24 11:00:56 -06:00
@InjectRepository(Perfil)
private perfilRepository: Repository<Perfil>,
2025-09-18 21:08:19 -06:00
private jwtService: JwtService,
2025-09-10 16:15:36 -06:00
) {}
2025-09-24 11:00:56 -06:00
async findOneByNameandPassword(data: Login): Promise<User> {
2025-09-10 16:15:36 -06:00
const { usuario, password } = data;
const user = await this.userRepository.findOne({
2025-09-10 16:15:36 -06:00
where: { usuario, password },
});
if(user?.activo === 0){
throw new NotFoundException(`El usuario se encuentra desactivado`);
}
if (!user) {
2025-09-18 21:08:19 -06:00
throw new NotFoundException(`El usuario o la contraseña es incorrecta`);
}
return user;
2025-09-04 21:04:15 -04:00
}
2025-09-24 11:00:56 -06:00
async Login(data: Login, res: Response) {
2025-09-10 16:39:44 -04:00
const user = await this.findOneByNameandPassword(data);
2025-09-10 21:03:33 -04:00
2026-01-15 14:01:56 -06:00
const payload = {
id: user.id_usuario,
2026-03-02 18:38:42 -06:00
usuario: user.nombre,
2026-02-19 19:02:13 -06:00
role: user.perfil.id_perfil,
2026-01-15 14:01:56 -06:00
};
2025-09-12 20:41:51 -06:00
const token = await this.jwtService.signAsync(payload);
2025-09-18 21:08:19 -06:00
res.cookie('token', token, {
2025-09-12 20:41:51 -06:00
httpOnly: true,
2026-03-02 18:38:42 -06:00
secure: process.env.NODE_ENV === 'production',
2025-09-12 20:41:51 -06:00
sameSite: 'strict',
path: '/',
});
2025-09-19 17:15:05 -06:00
return res.json({
message: 'Inicio de sesión exitoso',
access_token: token,
});
}
async findOne(id_usuario: number): Promise<User> {
2025-10-08 15:45:21 -06:00
const user = await this.userRepository.findOne({
2025-09-19 17:15:05 -06:00
where: { id_usuario },
});
2025-10-08 15:45:21 -06:00
if (!user) {
2025-09-19 17:15:05 -06:00
throw new NotFoundException(`Student not found`);
}
2025-10-08 15:45:21 -06:00
return user;
}
async findbyUser(usuario: string) {
const user = await this.userRepository.findOne({
where: { usuario },
});
return user;
}
2025-09-24 11:00:56 -06:00
async create(data: CreateUserDto) {
const { id_perfil, ...rest } = data;
2025-10-08 15:45:21 -06:00
const user = await this.findbyUser(data.usuario);
if (user) {
throw new BadRequestException('User found, change the user ');
}
2025-09-24 11:00:56 -06:00
const perfil = await this.perfilRepository.findOne({
where: { id_perfil },
});
if (!perfil) {
throw new NotFoundException('Perfil not found');
}
2026-01-15 14:01:56 -06:00
const fecha_registro = new Date();
2026-01-13 14:51:59 -06:00
2026-01-15 14:01:56 -06:00
const datauser = { ...rest, perfil, fecha_registro };
2025-09-24 11:00:56 -06:00
2025-10-08 15:45:21 -06:00
const usercreate = this.userRepository.create(datauser);
return this.userRepository.save(usercreate);
2025-09-24 11:00:56 -06:00
}
async changePassword(data: changePasswordDto, id_usuario: number) {
2025-09-24 11:04:08 -04:00
const user = await this.findOneByNameandPassword({
usuario: (await this.findOne(id_usuario)).usuario,
password: data.password,
});
user.password = data.newPassword;
return this.userRepository.save(user);
}
2026-01-15 14:01:56 -06:00
async getAll() {
2026-01-13 14:51:59 -06:00
return this.userRepository.find();
}
2026-01-15 14:01:56 -06:00
async getAllPerfil() {
2026-01-13 14:51:59 -06:00
return this.perfilRepository.find();
}
2025-09-04 21:04:15 -04:00
}
2025-09-19 17:15:05 -06:00
//IO