Errores arreglados
This commit is contained in:
+2
-2
@@ -1,5 +1,4 @@
|
||||
import { Module } from '@nestjs/common';
|
||||
import { AppController } from './app.controller';
|
||||
import { AppService } from './app.service';
|
||||
import { ConfigModule } from '@nestjs/config';
|
||||
import { TypeOrmModule } from '@nestjs/typeorm';
|
||||
@@ -24,10 +23,11 @@ import { AuthModule } from './auth/auth.module';
|
||||
username: process.env.DB_USER,
|
||||
password: process.env.DB_PASS,
|
||||
database: process.env.DB_NAME,
|
||||
autoLoadEntities: true,
|
||||
//autoLoadEntities: true,
|
||||
synchronize: true,
|
||||
}),
|
||||
UsuariosModule,
|
||||
|
||||
MovimientoModule,
|
||||
EquipoModule,
|
||||
AuthModule
|
||||
|
||||
@@ -7,11 +7,14 @@ import { LoginDto } from './dto/login.dto';
|
||||
@Controller('auth')
|
||||
export class AuthController {
|
||||
constructor( private readonly authService:AuthService){}
|
||||
|
||||
@Post("registro")
|
||||
registro(@Body() registroDto:CreateUsuarioDto ){
|
||||
return this.authService.registro(registroDto);
|
||||
|
||||
}
|
||||
|
||||
|
||||
@Post("login")
|
||||
login(@Body() loginDto:LoginDto){
|
||||
return this.authService.login(loginDto);
|
||||
|
||||
@@ -2,13 +2,13 @@ import { Module } from '@nestjs/common';
|
||||
import { AuthController } from './auth.controller';
|
||||
import { AuthService } from './auth.service';
|
||||
import { UsuariosModule } from 'src/usuarios/usuarios.module';
|
||||
import { JwtModule, JwtService } from '@nestjs/jwt';
|
||||
import { UsuariosService } from 'src/usuarios/usuarios.service';
|
||||
import { JwtModule } from '@nestjs/jwt';
|
||||
|
||||
|
||||
|
||||
@Module({
|
||||
imports:[UsuariosModule,
|
||||
imports:[
|
||||
UsuariosModule,
|
||||
JwtModule,
|
||||
JwtModule.register({
|
||||
global:true,
|
||||
@@ -18,6 +18,7 @@ import { UsuariosService } from 'src/usuarios/usuarios.service';
|
||||
}),
|
||||
],
|
||||
controllers: [AuthController],
|
||||
providers: [AuthService,UsuariosService, JwtService]
|
||||
providers: [AuthService],
|
||||
exports:[AuthService]
|
||||
})
|
||||
export class AuthModule {}
|
||||
|
||||
+24
-13
@@ -1,9 +1,12 @@
|
||||
import { BadRequestException, Injectable, UnauthorizedException } from '@nestjs/common';
|
||||
import { BadRequestException, Injectable, UnauthorizedException, InternalServerErrorException } from '@nestjs/common';
|
||||
import { UsuariosService } from 'src/usuarios/usuarios.service';
|
||||
import * as argon2 from 'argon2';
|
||||
import { LoginDto } from './dto/login.dto';
|
||||
import { CreateUsuarioDto } from 'src/usuarios/dto/create-usuario.dto';
|
||||
import { JwtService } from '@nestjs/jwt';
|
||||
import { Repository } from 'typeorm';
|
||||
import { Usuario } from 'src/usuarios/entities/usuario.entity';
|
||||
import { InjectRepository } from '@nestjs/typeorm';
|
||||
|
||||
@Injectable()
|
||||
export class AuthService {
|
||||
@@ -11,22 +14,30 @@ export class AuthService {
|
||||
private readonly usuarioService:UsuariosService,
|
||||
private readonly jwtService:JwtService
|
||||
){}
|
||||
//Pendiente
|
||||
//Pendiente tipoUsuario
|
||||
async registro({nombre, contraseña,tipoUsuario}: CreateUsuarioDto){
|
||||
const usuario= await this.usuarioService.findOneByName(nombre)
|
||||
|
||||
const usuario = await this.usuarioService.findOneByName(nombre);
|
||||
if(usuario){
|
||||
throw new BadRequestException("Nombre de usuario ya existente");
|
||||
}
|
||||
const hashedContraseña= await argon2.hash(contraseña) ;
|
||||
|
||||
await this.usuarioService.create({
|
||||
nombre,
|
||||
contraseña:hashedContraseña,
|
||||
tipoUsuario,
|
||||
});
|
||||
|
||||
return{
|
||||
message:"Usuario registrado exitosamente " };
|
||||
|
||||
try{
|
||||
const hashedContraseña = await argon2.hash(contraseña);
|
||||
|
||||
await this.usuarioService.create({
|
||||
nombre,
|
||||
contraseña: hashedContraseña,
|
||||
tipoUsuario,
|
||||
});
|
||||
|
||||
return {
|
||||
message: "Usuario registrado exitosamente"
|
||||
};
|
||||
}catch(error){
|
||||
|
||||
throw new InternalServerErrorException('Error al crear el usuario');
|
||||
}
|
||||
}
|
||||
|
||||
async login({nombre, contraseña}:LoginDto){
|
||||
|
||||
@@ -32,6 +32,8 @@ export class Equipo {
|
||||
|
||||
@Column({ length: 100, nullable: true })
|
||||
modelo: string;
|
||||
|
||||
|
||||
//Relaciones
|
||||
@OneToMany(()=>Movimiento, mov => mov.equipo)
|
||||
mov:Movimiento[]
|
||||
|
||||
@@ -1,9 +1,14 @@
|
||||
import { Module } from '@nestjs/common';
|
||||
import { EquipoService } from './equipo.service';
|
||||
import { EquipoController } from './equipo.controller';
|
||||
import { TypeOrmModule } from '@nestjs/typeorm';
|
||||
import { Equipo } from './entities/equipo.entity';
|
||||
import { Estado, Marca, Uso, Adscripcion, TipoEquipo, SistemaOperativo, Procesador } from './entities/catalogo.entities';
|
||||
|
||||
@Module({
|
||||
imports:[TypeOrmModule.forFeature([Equipo, Estado, Marca, Uso, Adscripcion, TipoEquipo, SistemaOperativo, Procesador])],
|
||||
controllers: [EquipoController],
|
||||
providers: [EquipoService],
|
||||
exports:[EquipoService],
|
||||
})
|
||||
export class EquipoModule {}
|
||||
|
||||
@@ -1,8 +1,11 @@
|
||||
import { NestFactory } from '@nestjs/core';
|
||||
import { AppModule } from './app.module';
|
||||
import { ValidationPipe } from '@nestjs/common';
|
||||
|
||||
async function bootstrap() {
|
||||
const app = await NestFactory.create(AppModule);
|
||||
// Habilita validación global para que DTOs invalidos devuelvan 400 en lugar de 500
|
||||
app.useGlobalPipes(new ValidationPipe({ whitelist: true, transform: true }));
|
||||
await app.listen(process.env.PORT ?? 3000);
|
||||
}
|
||||
bootstrap();
|
||||
|
||||
@@ -9,7 +9,7 @@ export class CreateMovimientoDto {
|
||||
@IsNumber()
|
||||
idEquipo: number;
|
||||
|
||||
@IsOptional()
|
||||
@IsNotEmpty()
|
||||
@IsString()
|
||||
observaciones?: string;
|
||||
observaciones: string;
|
||||
}
|
||||
|
||||
@@ -10,5 +10,6 @@ import { Equipo } from 'src/equipo/entities/equipo.entity';
|
||||
imports: [TypeOrmModule.forFeature([Movimiento, Usuario, Equipo])],
|
||||
controllers: [MovimientoController],
|
||||
providers: [MovimientoService],
|
||||
exports:[MovimientoService]
|
||||
})
|
||||
export class MovimientoModule {}
|
||||
|
||||
@@ -5,41 +5,27 @@ import { Movimiento } from './entities/movimiento.entity';
|
||||
import { Usuario } from 'src/usuarios/entities/usuario.entity';
|
||||
import { Equipo } from 'src/equipo/entities/equipo.entity';
|
||||
import { CreateMovimientoDto } from './dto/create-movimiento.dto';
|
||||
import { UpdateMovimientoDto } from './dto/update-movimiento.dto';
|
||||
|
||||
|
||||
@Injectable()
|
||||
export class MovimientoService {
|
||||
constructor(
|
||||
@InjectRepository(Movimiento)
|
||||
private readonly movimientoRepo: Repository<Movimiento>,
|
||||
@InjectRepository(Usuario)
|
||||
private readonly usuarioRepo: Repository<Usuario>,
|
||||
|
||||
@InjectRepository(Equipo)
|
||||
private readonly equipoRepo: Repository<Equipo>,
|
||||
) {}
|
||||
|
||||
async crear(dto: CreateMovimientoDto) {
|
||||
const usuario = await this.usuarioRepo.findOne({
|
||||
where: { id_usuario: dto.idUsuario },
|
||||
});
|
||||
|
||||
const equipo = await this.equipoRepo.findOne({
|
||||
where: { id_equipo: dto.idEquipo },
|
||||
});
|
||||
|
||||
if (!usuario || !equipo) {
|
||||
throw new Error('Usuario o equipo no encontrado');
|
||||
}
|
||||
/*
|
||||
const movimiento = this.movimientoRepo.create({
|
||||
usuario,
|
||||
equipo,
|
||||
observaciones: dto.observaciones || undefined,
|
||||
const movimiento = this.movimientoRepo.create({
|
||||
user:{id_usuario:dto.idUsuario},
|
||||
equipo:{id_equipo:dto.idEquipo},
|
||||
fechaMovimiento:new Date(),
|
||||
observaciones:dto.observaciones
|
||||
});
|
||||
|
||||
return await this.movimientoRepo.save(movimiento);
|
||||
}
|
||||
*/
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -1,17 +1,22 @@
|
||||
import { DefaultValuePipe } from "@nestjs/common"
|
||||
import { Transform } from "class-transformer"
|
||||
import { IsInt, IsString, MinLength} from "class-validator"
|
||||
import { IsInt, IsNotEmpty, IsOptional, IsString, MinLength} from "class-validator"
|
||||
|
||||
export class CreateUsuarioDto {
|
||||
|
||||
@IsString()
|
||||
@IsNotEmpty()
|
||||
nombre:string
|
||||
|
||||
@IsString()
|
||||
@MinLength(5)
|
||||
@Transform(({value})=>value.trim())
|
||||
@IsNotEmpty()
|
||||
contraseña:string
|
||||
|
||||
@IsInt()
|
||||
tipoUsuario:number
|
||||
@IsOptional()
|
||||
tipoUsuario?:number
|
||||
|
||||
|
||||
}
|
||||
|
||||
@@ -2,6 +2,7 @@ import { Movimiento } from 'src/movimiento/entities/movimiento.entity';
|
||||
import {Column, Entity, ManyToOne, OneToMany, PrimaryGeneratedColumn} from 'typeorm';
|
||||
|
||||
|
||||
|
||||
@Entity()
|
||||
export class Tipo_Usuario {
|
||||
@PrimaryGeneratedColumn()
|
||||
@@ -27,7 +28,7 @@ export class Usuario {
|
||||
contraseña:string
|
||||
|
||||
|
||||
@ManyToOne(()=>Tipo_Usuario,tipoUsuario=>tipoUsuario.usuarios )
|
||||
@ManyToOne(()=>Tipo_Usuario,tipoUsuario=>tipoUsuario.usuarios)
|
||||
tipoUsuario:Tipo_Usuario;
|
||||
|
||||
@OneToMany(()=>Movimiento, movimiento=>movimiento.id_movimiento)
|
||||
|
||||
@@ -1,9 +1,31 @@
|
||||
import { Module } from '@nestjs/common';
|
||||
import { UsuariosService } from './usuarios.service';
|
||||
import { UsuariosController } from './usuarios.controller';
|
||||
import { Tipo_Usuario, Usuario } from './entities/usuario.entity';
|
||||
import { TypeOrmModule } from '@nestjs/typeorm';
|
||||
|
||||
@Module({
|
||||
imports:[TypeOrmModule.forFeature([Usuario, Tipo_Usuario])],
|
||||
controllers: [UsuariosController],
|
||||
providers: [UsuariosService],
|
||||
exports:[UsuariosService]
|
||||
|
||||
})
|
||||
export class UsuariosModule {}
|
||||
/*
|
||||
import { Module } from '@nestjs/common';
|
||||
import { TypeOrmModule } from '@nestjs/typeorm';
|
||||
import { Usuario } from './entities/usuario.entity';
|
||||
import { UsuariosService } from './usuarios.service';
|
||||
import { UsuariosController } from './usuarios.controller';
|
||||
|
||||
@Module({
|
||||
controllers: [UsuariosController],
|
||||
imports: [TypeOrmModule.forFeature([Usuario])],
|
||||
providers: [UsuariosService],
|
||||
controllers: [UsuariosController],
|
||||
exports: [UsuariosService],
|
||||
})
|
||||
export class UsuariosModule {}
|
||||
|
||||
|
||||
*/
|
||||
@@ -8,7 +8,7 @@ import { Repository } from 'typeorm';
|
||||
export class UsuariosService {
|
||||
constructor(
|
||||
@InjectRepository(Usuario)
|
||||
private readonly usuarioRepository: Repository <Usuario>
|
||||
private readonly usuarioRepository: Repository<Usuario>
|
||||
){}
|
||||
async create(createUsuarioDto: CreateUsuarioDto) {
|
||||
|
||||
|
||||
Reference in New Issue
Block a user