create alumno

This commit is contained in:
2025-09-24 11:00:56 -06:00
parent c39faa513b
commit 684de4808c
9 changed files with 124 additions and 86 deletions
+50 -2
View File
@@ -1,9 +1,57 @@
import { IsString } from 'class-validator';
import {
IsInt,
IsNotEmpty,
IsOptional,
IsString,
MaxLength,
MinLength,
} from 'class-validator';
export class CreateUserDto {
export class Login {
@IsString()
usuario: string;
@IsString()
password: string;
}
export class CreateUserDto {
@IsString()
@IsNotEmpty()
nombre: string;
@IsString()
@IsNotEmpty()
apellido_paterno: string;
@IsString()
@IsOptional()
apellido_materno?: string;
@IsString()
@IsNotEmpty()
usuario: string;
@IsString()
@IsNotEmpty()
@MinLength(6)
@MaxLength(45)
password: string;
@IsInt()
@IsOptional()
activo?: number;
@IsString()
@IsOptional()
@MaxLength(50)
descripcion?: string;
@IsString()
@IsOptional()
fecha_registro?: string;
@IsInt()
@IsNotEmpty()
id_perfil: number;
}
+29 -13
View File
@@ -26,38 +26,54 @@ export class User {
@PrimaryGeneratedColumn({ name: 'id_usuario', type: 'int' })
id_usuario: number;
@Column({ name: 'nombre', type: 'varchar' })
@Column({ name: 'nombre', type: 'varchar', length: 45, nullable: false })
nombre: string;
@Column({ name: 'apellido_paterno', type: 'varchar' })
@Column({
name: 'apellido_paterno',
type: 'varchar',
length: 45,
nullable: true,
})
apellido_paterno: string;
@Column({ name: 'apellido_materno', type: 'varchar' })
@Column({
name: 'apellido_materno',
type: 'varchar',
length: 45,
nullable: true,
})
apellido_materno: string;
@Column({ name: 'usuario', type: 'varchar' })
@Column({ name: 'usuario', type: 'varchar', length: 45, nullable: false })
usuario: string;
@Column({ name: 'password', type: 'varchar', length: 45, nullable: false })
password: string;
@Column({ name: 'activo', type: 'tinyint', nullable: false, default: 1 })
@Column({
name: 'activo',
type: 'tinyint',
nullable: false,
default: () => 1,
})
activo: number;
@Column({ name: 'descripcion', type: 'varchar', length: 50, default: null })
description: boolean;
@Column({ name: 'descripcion', type: 'varchar', length: 50, nullable: true })
descripcion: string;
@Column({ name: 'fecha_registro', type: 'varchar' })
fecha_registro: string;
@Column({
name: 'fecha_registro',
type: 'timestamp',
default: () => 'CURRENT_TIMESTAMP',
})
fecha_registro: Date;
@ManyToOne(() => Perfil, (perfil) => perfil.usuarios, { eager: true })
@JoinColumn({ name: 'id_perfil' })
perfil: Perfil;
@OneToMany(
() => DetalleServicio,
(id_detalle_servicio) => id_detalle_servicio.user,
)
@OneToMany(() => DetalleServicio, (detalleServicio) => detalleServicio.user)
detalles_servicio: DetalleServicio[];
@OneToMany(() => Recibo, (recibo) => recibo.user)
+7 -2
View File
@@ -9,7 +9,7 @@ import {
} from '@nestjs/common';
import type { Response } from 'express';
import { UserService } from './user.service';
import { CreateUserDto } from './dto/create-user.dto';
import { CreateUserDto, Login } from './dto/create-user.dto';
import { AuthGuard } from '@nestjs/passport';
@Controller('user')
@@ -17,7 +17,7 @@ export class UserController {
constructor(private readonly userService: UserService) {}
@Post()
async Login(@Body() data: CreateUserDto, @Res() res: Response) {
async Login(@Body() data: Login, @Res() res: Response) {
return this.userService.Login(data, res);
}
@@ -26,5 +26,10 @@ export class UserController {
getProfile(@Request() req) {
return req.user;
}
@Post('/create')
async createUser(@Body() data: CreateUserDto) {
return this.userService.create(data);
}
}
//IO
+3 -3
View File
@@ -2,7 +2,7 @@ import { Module } from '@nestjs/common';
import { UserService } from './user.service';
import { UserController } from './user.controller';
import { TypeOrmModule } from '@nestjs/typeorm';
import { User } from './entities/user.entity';
import { Perfil, User } from './entities/user.entity';
import { ConfigService } from '@nestjs/config';
import { JwtModule } from '@nestjs/jwt';
import { PassportModule } from '@nestjs/passport';
@@ -10,7 +10,7 @@ import { JwtStrategy } from './jwt.strategy';
@Module({
imports: [
TypeOrmModule.forFeature([User]),
TypeOrmModule.forFeature([User, Perfil]),
PassportModule.register({ defaultStrategy: 'jwt' }),
JwtModule.registerAsync({
inject: [ConfigService],
@@ -25,6 +25,6 @@ import { JwtStrategy } from './jwt.strategy';
],
controllers: [UserController],
providers: [UserService, JwtStrategy],
exports: [UserService,PassportModule,JwtModule],
exports: [UserService, PassportModule, JwtModule],
})
export class UserModule {}
+24 -4
View File
@@ -1,8 +1,8 @@
import { Injectable, NotFoundException } from '@nestjs/common';
import { Response } from 'express';
import { CreateUserDto } from './dto/create-user.dto';
import { CreateUserDto, Login } from './dto/create-user.dto';
import { InjectRepository } from '@nestjs/typeorm';
import { User } from './entities/user.entity';
import { Perfil, User } from './entities/user.entity';
import { Repository } from 'typeorm';
import { JwtService } from '@nestjs/jwt';
@@ -11,10 +11,13 @@ export class UserService {
constructor(
@InjectRepository(User)
private userRepository: Repository<User>,
@InjectRepository(Perfil)
private perfilRepository: Repository<Perfil>,
private jwtService: JwtService,
) {}
async findOneByNameandPassword(data: CreateUserDto): Promise<User> {
async findOneByNameandPassword(data: Login): Promise<User> {
const { usuario, password } = data;
const user = await this.userRepository.findOne({
@@ -28,7 +31,7 @@ export class UserService {
return user;
}
async Login(data: CreateUserDto, res: Response) {
async Login(data: Login, res: Response) {
const user = await this.findOneByNameandPassword(data);
const payload = { id: user.id_usuario, usuario: user.usuario };
@@ -56,5 +59,22 @@ export class UserService {
}
return student;
}
async create(data: CreateUserDto) {
const { id_perfil, ...rest } = data;
const perfil = await this.perfilRepository.findOne({
where: { id_perfil },
});
if (!perfil) {
throw new NotFoundException('Perfil not found');
}
const datauser = { ...rest, perfil };
const user = this.userRepository.create(datauser);
return this.userRepository.save(user);
}
}
//IO