diff --git a/src/alumno/student.controller.ts b/src/alumno/student.controller.ts index 73b7cd3..0d40542 100644 --- a/src/alumno/student.controller.ts +++ b/src/alumno/student.controller.ts @@ -12,14 +12,9 @@ export class AlumnoController { return this.alumnoService.create(createStudentDto); } - @Get() - async findAll(): Promise { - return this.alumnoService.findAll(); - } - @Get(':id') findOne(@Param('id') id: number) { return this.alumnoService.findOne(+id); } } -//IO \ No newline at end of file +//IO diff --git a/src/alumno/student.service.ts b/src/alumno/student.service.ts index 787aa9c..2cf7fdd 100644 --- a/src/alumno/student.service.ts +++ b/src/alumno/student.service.ts @@ -17,17 +17,15 @@ export class AlumnoService { return await this.studentRepository.save(student); } - findAll(): Promise { - return this.studentRepository.find({ skip: 5000, take: 50 }); - } - async findOne(id_cuenta: number): Promise { const student = await this.studentRepository.findOne({ - where: { id_cuenta }, + where: { id_cuenta: id_cuenta }, }); + if (!student) { throw new NotFoundException(`Student not found`); } + return student; } @@ -50,11 +48,7 @@ export class AlumnoService { .execute(); } - async addCredit( - id_cuenta: number, - credit: number, - manager: EntityManager, - ) { + async addCredit(id_cuenta: number, credit: number, manager: EntityManager) { const repo = manager.getRepository(Alumno); return await repo .createQueryBuilder() @@ -64,4 +58,4 @@ export class AlumnoService { .execute(); } } -//IO \ No newline at end of file +//IO diff --git a/src/sancion/sancion.controller.ts b/src/sancion/sancion.controller.ts index ef78a21..c84fa0a 100644 --- a/src/sancion/sancion.controller.ts +++ b/src/sancion/sancion.controller.ts @@ -1,34 +1,11 @@ -import { Controller, Get, Post, Body, Patch, Param, Delete } from '@nestjs/common'; +import { Controller, Get, Param } from '@nestjs/common'; import { SancionService } from './sancion.service'; -import { CreateSancionDto } from './dto/create-sancion.dto'; -import { UpdateSancionDto } from './dto/update-sancion.dto'; - @Controller('sancion') export class SancionController { constructor(private readonly sancionService: SancionService) {} - @Post() - create(@Body() createSancionDto: CreateSancionDto) { - return this.sancionService.create(createSancionDto); - } - - @Get() - findAll() { - return this.sancionService.findAll(); - } - @Get(':id') - findOne(@Param('id') id: string) { + findOne(@Param('id') id: number) { return this.sancionService.findOne(+id); } - - @Patch(':id') - update(@Param('id') id: string, @Body() updateSancionDto: UpdateSancionDto) { - return this.sancionService.update(+id, updateSancionDto); - } - - @Delete(':id') - remove(@Param('id') id: string) { - return this.sancionService.remove(+id); - } } diff --git a/src/sancion/sancion.service.ts b/src/sancion/sancion.service.ts index 50f6e77..fd14e81 100644 --- a/src/sancion/sancion.service.ts +++ b/src/sancion/sancion.service.ts @@ -1,6 +1,4 @@ import { Injectable, NotFoundException } from '@nestjs/common'; -import { CreateSancionDto } from './dto/create-sancion.dto'; -import { UpdateSancionDto } from './dto/update-sancion.dto'; import { Sancion } from './entities/sancion.entity'; import { InjectRepository } from '@nestjs/typeorm'; import { Repository } from 'typeorm'; @@ -9,15 +7,8 @@ import { Repository } from 'typeorm'; export class SancionService { constructor( @InjectRepository(Sancion) - private readonly sancionRepository: Repository - ){} - create(createSancionDto: CreateSancionDto) { - return 'This action adds a new sancion'; - } - - findAll() { - return `This action returns all sancion`; - } + private readonly sancionRepository: Repository, + ) {} async findOne(id_sancion: number): Promise { const sancion = await this.sancionRepository.findOne({ @@ -28,13 +19,5 @@ export class SancionService { } return sancion; } - - update(id: number, updateSancionDto: UpdateSancionDto) { - return `This action updates a #${id} sancion`; - } - - remove(id: number) { - return `This action removes a #${id} sancion`; - } } -//IO \ No newline at end of file +//IO diff --git a/src/user/dto/create-user.dto.ts b/src/user/dto/create-user.dto.ts index 172f9f9..c2376f1 100644 --- a/src/user/dto/create-user.dto.ts +++ b/src/user/dto/create-user.dto.ts @@ -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; +} diff --git a/src/user/entities/user.entity.ts b/src/user/entities/user.entity.ts index d12377d..078634f 100644 --- a/src/user/entities/user.entity.ts +++ b/src/user/entities/user.entity.ts @@ -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) diff --git a/src/user/user.controller.ts b/src/user/user.controller.ts index 37bab3d..967904c 100644 --- a/src/user/user.controller.ts +++ b/src/user/user.controller.ts @@ -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 diff --git a/src/user/user.module.ts b/src/user/user.module.ts index c2fcb49..8608164 100644 --- a/src/user/user.module.ts +++ b/src/user/user.module.ts @@ -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 {} diff --git a/src/user/user.service.ts b/src/user/user.service.ts index 1b5d42e..d58107b 100644 --- a/src/user/user.service.ts +++ b/src/user/user.service.ts @@ -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, + + @InjectRepository(Perfil) + private perfilRepository: Repository, private jwtService: JwtService, ) {} - async findOneByNameandPassword(data: CreateUserDto): Promise { + async findOneByNameandPassword(data: Login): Promise { 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