fixing update auto id
This commit is contained in:
@@ -10,12 +10,7 @@ import { ProyectosAcademicos } from 'src/proyectos_academicos/entities/proyectos
|
||||
import { DatosAcademicos } from 'src/datos_academicos/entities/datos_academicos.entity'
|
||||
import { LineasInvestigacion } from 'src/lineas_investigacion/entities/lineas_investigacion.entity'
|
||||
|
||||
import { ProyectosAcademicosService } from 'src/proyectos_academicos/proyectos_academicos.service';
|
||||
import { DatosAcademicosService } from 'src/datos_academicos/datos_academicos.service';
|
||||
import { LineasInvestigacionService } from 'src/lineas_investigacion/lineas_investigacion.service';
|
||||
|
||||
import { JwtModule } from '@nestjs/jwt';
|
||||
import { RolesGuard } from '../permissions/roles.guard';
|
||||
|
||||
@Module({
|
||||
imports: [
|
||||
|
||||
@@ -13,6 +13,7 @@ import {
|
||||
ParseIntPipe,
|
||||
Query,
|
||||
Logger,
|
||||
Req,
|
||||
} from '@nestjs/common';
|
||||
import { AuthGuard } from './auth.guard';
|
||||
import { AuthService } from './auth.service';
|
||||
@@ -36,7 +37,7 @@ export class AuthController {
|
||||
|
||||
//@UseGuards()
|
||||
@Post('register')
|
||||
//@Roles(Role.Admin)
|
||||
@Roles(Role.Admin)
|
||||
async register(@Body() data: registerDto) {
|
||||
Logger.debug('register user');
|
||||
return this.authService.register(data);
|
||||
@@ -73,9 +74,12 @@ export class AuthController {
|
||||
async update(
|
||||
@Param('id_usuario') id_usuario: number,
|
||||
@Body() data: registerDto,
|
||||
@Req() req: any
|
||||
) {
|
||||
Logger.debug('update user');
|
||||
return this.authService.update(id_usuario, data);
|
||||
const authHeader = req.headers['authorization'];
|
||||
const [, token] = authHeader.split(' ');
|
||||
|
||||
return this.authService.update(id_usuario, data, token);
|
||||
}
|
||||
|
||||
//@UseGuards(AuthGuard)
|
||||
|
||||
+38
-13
@@ -26,7 +26,7 @@ export class AuthService {
|
||||
|
||||
//register users
|
||||
async register(data: registerDto) {
|
||||
const { usuario, contraseña} = data;
|
||||
const { usuario, contraseña } = data;
|
||||
|
||||
if (await this.usersService.findOne(usuario)) {
|
||||
throw new HttpException('User already exist', 403);
|
||||
@@ -74,17 +74,29 @@ export class AuthService {
|
||||
}
|
||||
|
||||
//update user
|
||||
async update(id_usuario: number, data: registerDto) {
|
||||
const { contraseña } = data;
|
||||
async update(
|
||||
id_usuario: number,
|
||||
data: registerDto,
|
||||
token: string,
|
||||
): Promise<any> {
|
||||
const { contraseña, id_tipo_usuario } = data;
|
||||
|
||||
const tokenUserId = this.getTokenUserId(token);
|
||||
|
||||
if (tokenUserId == id_usuario && id_tipo_usuario !== undefined) {
|
||||
Logger.debug('Cannot modify id_tipo_usuario')
|
||||
delete data.id_tipo_usuario;
|
||||
}
|
||||
|
||||
const user = await this.userRepository.findOne({ where: { id_usuario } });
|
||||
if (!user) {
|
||||
throw new HttpException('User not found', 404);
|
||||
}
|
||||
|
||||
if (contraseña) {
|
||||
const hashedpassword = await hash(contraseña, 10);
|
||||
data = { ...data, contraseña: hashedpassword };
|
||||
} else {
|
||||
const user = await this.userRepository.findOne({ where: { id_usuario } });
|
||||
if (!user) {
|
||||
throw new HttpException('User not found', 404);
|
||||
}
|
||||
data = { ...data, contraseña: user.contraseña };
|
||||
}
|
||||
|
||||
@@ -114,7 +126,7 @@ export class AuthService {
|
||||
throw new UnauthorizedException('Token has expired');
|
||||
}
|
||||
|
||||
return ;
|
||||
return;
|
||||
} catch (error) {
|
||||
throw new UnauthorizedException('validation token failed');
|
||||
}
|
||||
@@ -127,18 +139,22 @@ export class AuthService {
|
||||
async findAllPaginated(
|
||||
page: number,
|
||||
limit: number,
|
||||
filters: any
|
||||
): Promise<{ usuarios: User[], total: number, totalPages: number }> {
|
||||
filters: any,
|
||||
): Promise<{ usuarios: User[]; total: number; totalPages: number }> {
|
||||
const query = this.userRepository.createQueryBuilder('usuario');
|
||||
|
||||
if (filters.nombre) {
|
||||
Logger.debug('fiter by name user');
|
||||
query.andWhere('usuario.nombre LIKE :nombre', { nombre: `%${filters.nombre}%` });
|
||||
query.andWhere('usuario.nombre LIKE :nombre', {
|
||||
nombre: `%${filters.nombre}%`,
|
||||
});
|
||||
}
|
||||
|
||||
if (filters.id_tipo_usuario) {
|
||||
Logger.debug('fiter by id user');
|
||||
query.andWhere('usuario.id_tipo_usuario = :id_tipo_usuario', { id_tipo_usuario: filters.id_tipo_usuario });
|
||||
query.andWhere('usuario.id_tipo_usuario = :id_tipo_usuario', {
|
||||
id_tipo_usuario: filters.id_tipo_usuario,
|
||||
});
|
||||
}
|
||||
|
||||
const [usuarios, total] = await query
|
||||
@@ -156,7 +172,7 @@ export class AuthService {
|
||||
const user = await this.userRepository.findOne({
|
||||
where: { id_usuario },
|
||||
});
|
||||
|
||||
|
||||
if (!user) {
|
||||
Logger.debug('user not found');
|
||||
throw new HttpException('user not found', 404);
|
||||
@@ -164,4 +180,13 @@ export class AuthService {
|
||||
|
||||
return user;
|
||||
}
|
||||
|
||||
getTokenUserId(token: string): number {
|
||||
try {
|
||||
const decoded = this.jwtService.verify(token);
|
||||
return decoded['id_usuario'];
|
||||
} catch (error) {
|
||||
throw new UnauthorizedException('Invalid token');
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user