Merge branch 'Lino' of https://github.com/IO420/api-nexus into Carlos
This commit is contained in:
@@ -0,0 +1,18 @@
|
||||
import { IsInt, IsNotEmpty, Min } from "class-validator";
|
||||
|
||||
export class chargePrintDto {
|
||||
|
||||
@IsInt()
|
||||
@IsNotEmpty()
|
||||
@Min(1)
|
||||
monto:number
|
||||
|
||||
@IsInt()
|
||||
@Min(1)
|
||||
@IsNotEmpty()
|
||||
numero_hojas: number;
|
||||
|
||||
@IsInt()
|
||||
@IsNotEmpty()
|
||||
id_cuenta: number;
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
import { Body, Controller, Post } from "@nestjs/common";
|
||||
import { OperationsService } from "./operations.services";
|
||||
|
||||
@Controller('operations')
|
||||
export class OperationsController{
|
||||
constructor(private readonly operationsService:OperationsService){}
|
||||
|
||||
@Post()
|
||||
async Login(@Body() data) {
|
||||
return this.operationsService.chargePrint(data,data.userid);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,15 @@
|
||||
import { Module } from '@nestjs/common';
|
||||
import { TypeOrmModule } from '@nestjs/typeorm';
|
||||
import { Student } from 'src/alumno/entities/student.entity';
|
||||
import { DetalleServicio } from 'src/detalle_servicio/entities/detalle_servicio.entity';
|
||||
import { OperationsController } from './operations.controller';
|
||||
import { OperationsService } from './operations.services';
|
||||
|
||||
@Module({
|
||||
imports: [
|
||||
TypeOrmModule.forFeature([Student, DetalleServicio]), // entidades que usarán las transacciones
|
||||
],
|
||||
controllers: [OperationsController],
|
||||
providers: [OperationsService],
|
||||
})
|
||||
export class OperationsModule {}
|
||||
@@ -0,0 +1,54 @@
|
||||
import { BadRequestException, Injectable } from '@nestjs/common';
|
||||
import { StudentService } from 'src/alumno/student.service';
|
||||
import { DetalleServicioService } from 'src/detalle_servicio/detalle_servicio.service';
|
||||
import { DataSource } from 'typeorm';
|
||||
import { chargePrintDto } from './dto/operations.dto';
|
||||
import { CreateDetalleServicioDto } from 'src/detalle_servicio/dto/create-detalle_servicio.dto';
|
||||
|
||||
@Injectable()
|
||||
export class OperationsService {
|
||||
constructor(
|
||||
private readonly dataSource: DataSource,
|
||||
private readonly studentService: StudentService,
|
||||
private readonly detalleServicioService: DetalleServicioService,
|
||||
) {}
|
||||
|
||||
async chargePrint(data: chargePrintDto, userId: number) {
|
||||
const { monto, id_cuenta } = data;
|
||||
|
||||
const queryRunner = this.dataSource.createQueryRunner();
|
||||
await queryRunner.connect();
|
||||
await queryRunner.startTransaction();
|
||||
|
||||
try {
|
||||
const credit = await this.studentService.GetCredit(id_cuenta);
|
||||
|
||||
if (credit < monto) {
|
||||
throw new BadRequestException('Crédito insuficiente');
|
||||
}
|
||||
|
||||
const detalleData: CreateDetalleServicioDto = {
|
||||
...data,
|
||||
id_servicio: 1,
|
||||
id_usuario: userId,
|
||||
fecha_operacion: new Date(),
|
||||
};
|
||||
|
||||
await this.detalleServicioService.Create(detalleData, queryRunner.manager);
|
||||
|
||||
await this.studentService.UpdateCredit(
|
||||
id_cuenta,
|
||||
monto,
|
||||
queryRunner.manager,
|
||||
);
|
||||
|
||||
await queryRunner.commitTransaction();
|
||||
return { message: 'correct' };
|
||||
} catch (error) {
|
||||
await queryRunner.rollbackTransaction();
|
||||
return { message: 'error', error: error.message };
|
||||
} finally {
|
||||
await queryRunner.release();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,23 @@
|
||||
|
||||
import { IsEmail, IsNotEmpty, IsNumber, IsString } from 'class-validator';
|
||||
|
||||
export class CreateStudentDto {
|
||||
@IsNotEmpty()
|
||||
@IsNumber()
|
||||
id_cuenta: number;
|
||||
|
||||
@IsNotEmpty()
|
||||
@IsString()
|
||||
nombre: string;
|
||||
|
||||
@IsString()
|
||||
fecha_nacimiento: string;
|
||||
|
||||
@IsNotEmpty()
|
||||
@IsNumber()
|
||||
id_carrera: number;
|
||||
|
||||
@IsEmail()
|
||||
@IsString()
|
||||
correo: string;
|
||||
}
|
||||
@@ -0,0 +1,30 @@
|
||||
import {
|
||||
Controller,
|
||||
Get,
|
||||
Post,
|
||||
Body,
|
||||
Param,
|
||||
} from '@nestjs/common';
|
||||
import { StudentService } from './student.service';
|
||||
import { CreateStudentDto } from './dto/create-student.dto';
|
||||
import { Student } from './entities/student.entity';
|
||||
|
||||
@Controller('student')
|
||||
export class StudentController {
|
||||
constructor(private readonly studentService: StudentService) {}
|
||||
|
||||
@Post()
|
||||
async create(@Body() createStudentDto: CreateStudentDto): Promise<Student> {
|
||||
return this.studentService.create(createStudentDto);
|
||||
}
|
||||
|
||||
@Get()
|
||||
async findAll(): Promise<Student[]> {
|
||||
return this.studentService.findAll();
|
||||
}
|
||||
|
||||
@Get(':id')
|
||||
findOne(@Param('id') id: number) {
|
||||
return this.studentService.findOne(+id);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,52 @@
|
||||
import { Injectable, NotFoundException } from '@nestjs/common';
|
||||
import { CreateStudentDto } from './dto/create-student.dto';
|
||||
import { UpdateStudentDto } from './dto/update-student.dto';
|
||||
import { Student } from './entities/student.entity';
|
||||
import { EntityManager, Repository } from 'typeorm';
|
||||
import { InjectRepository } from '@nestjs/typeorm';
|
||||
|
||||
@Injectable()
|
||||
export class StudentService {
|
||||
constructor(
|
||||
@InjectRepository(Student)
|
||||
private readonly studentRepository: Repository<Student>,
|
||||
) {}
|
||||
|
||||
async create(createStudentDto: CreateStudentDto): Promise<Student> {
|
||||
const student = this.studentRepository.create(createStudentDto);
|
||||
return await this.studentRepository.save(student);
|
||||
}
|
||||
|
||||
findAll(): Promise<Student[]> {
|
||||
return this.studentRepository.find({ skip: 5000, take: 50 });
|
||||
}
|
||||
|
||||
async findOne(id_cuenta: number): Promise<Student> {
|
||||
const student = await this.studentRepository.findOne({
|
||||
where: { id_cuenta },
|
||||
});
|
||||
if (!student) {
|
||||
throw new NotFoundException(`Student with ID ${id_cuenta} not found`);
|
||||
}
|
||||
return student;
|
||||
}
|
||||
|
||||
async GetCredit(id_cuenta: number): Promise<Student['credito']> {
|
||||
const student = await this.findOne(id_cuenta);
|
||||
return student.credito;
|
||||
}
|
||||
|
||||
async UpdateCredit(
|
||||
id_cuenta: number,
|
||||
credit: number,
|
||||
manager: EntityManager,
|
||||
) {
|
||||
const repo = manager.getRepository(Student);
|
||||
return await repo
|
||||
.createQueryBuilder()
|
||||
.update()
|
||||
.set({ credito: () => `credito - ${credit}` })
|
||||
.where({ id_cuenta })
|
||||
.execute();
|
||||
}
|
||||
}
|
||||
+15
-7
@@ -3,11 +3,16 @@ import { ConfigModule, ConfigService } from '@nestjs/config';
|
||||
import { AppController } from './app.controller';
|
||||
import { AppService } from './app.service';
|
||||
import { TypeOrmModule } from '@nestjs/typeorm';
|
||||
import { StudentModule } from './student/student.module';
|
||||
import { StudentModule } from './alumno/student.module';
|
||||
import { UserModule } from './user/user.module';
|
||||
import { User } from './user/entities/user.entity';
|
||||
import { Student } from './student/entities/student.entity';
|
||||
import { TicketModule } from './ticket/ticket.module';
|
||||
import { Perfil, User } from './user/entities/user.entity';
|
||||
import { DetalleServicioModule } from './detalle_servicio/detalle_servicio.module';
|
||||
import { PeriodoModule } from './periodo/periodo.module';
|
||||
import { ServicioModule } from './servicio/servicio.module';
|
||||
import { Student } from './alumno/entities/student.entity';
|
||||
import { DetalleServicio } from './detalle_servicio/entities/detalle_servicio.entity';
|
||||
import { Periodo } from './periodo/entities/periodo.entity';
|
||||
import { Servicio } from './servicio/entities/servicio.entity';
|
||||
|
||||
@Module({
|
||||
imports: [
|
||||
@@ -24,13 +29,16 @@ import { TicketModule } from './ticket/ticket.module';
|
||||
username: configService.get<string>('DB_USER'),
|
||||
password: configService.get<string>('DB_PASSWORD'),
|
||||
database: configService.get<string>('DB_NAME'),
|
||||
entities: [User, Student],
|
||||
synchronize: false,
|
||||
entities: [User, Student, DetalleServicio, Periodo, Servicio, Perfil],
|
||||
synchronize: false, //Never change to true in production!
|
||||
}),
|
||||
}),
|
||||
UserModule,
|
||||
StudentModule,
|
||||
TicketModule,
|
||||
DetalleServicioModule,
|
||||
PeriodoModule,
|
||||
ServicioModule,
|
||||
StudentModule,
|
||||
],
|
||||
controllers: [AppController],
|
||||
providers: [AppService],
|
||||
|
||||
@@ -0,0 +1,9 @@
|
||||
import { Controller } from '@nestjs/common';
|
||||
import { DetalleServicioService } from './detalle_servicio.service';
|
||||
|
||||
|
||||
@Controller('detalle-servicio')
|
||||
export class DetalleServicioController {
|
||||
constructor(private readonly detalleServicioService: DetalleServicioService) {}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
import { Module } from '@nestjs/common';
|
||||
import { DetalleServicioService } from './detalle_servicio.service';
|
||||
import { DetalleServicioController } from './detalle_servicio.controller';
|
||||
|
||||
@Module({
|
||||
controllers: [DetalleServicioController],
|
||||
providers: [DetalleServicioService],
|
||||
})
|
||||
export class DetalleServicioModule {}
|
||||
@@ -0,0 +1,13 @@
|
||||
import { Injectable } from '@nestjs/common';
|
||||
import { DetalleServicio } from './entities/detalle_servicio.entity';
|
||||
import { EntityManager } from 'typeorm';
|
||||
import { CreateDetalleServicioDto } from './dto/create-detalle_servicio.dto';
|
||||
|
||||
@Injectable()
|
||||
export class DetalleServicioService {
|
||||
async Create(data: CreateDetalleServicioDto, manager: EntityManager) {
|
||||
const repo = manager.getRepository(DetalleServicio);
|
||||
const details = repo.create(data);
|
||||
return await repo.save(details);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,32 @@
|
||||
import { Type } from 'class-transformer';
|
||||
import { IsDate, IsIn, IsInt, IsNotEmpty, IsOptional, Min } from 'class-validator';
|
||||
|
||||
export class CreateDetalleServicioDto {
|
||||
|
||||
@IsInt()
|
||||
@IsNotEmpty()
|
||||
@Min(1)
|
||||
monto:number
|
||||
|
||||
@IsInt()
|
||||
@Min(1)
|
||||
@IsNotEmpty()
|
||||
numero_hojas: number;
|
||||
|
||||
@IsOptional()
|
||||
@Type(() => Date)
|
||||
@IsDate()
|
||||
fecha_operacion?: Date;
|
||||
|
||||
@IsInt()
|
||||
@IsNotEmpty()
|
||||
id_cuenta: number;
|
||||
|
||||
@IsInt()
|
||||
@IsNotEmpty()
|
||||
id_servicio: number;
|
||||
|
||||
@IsInt()
|
||||
@IsNotEmpty()
|
||||
id_usuario: number;
|
||||
}
|
||||
@@ -0,0 +1,4 @@
|
||||
import { PartialType } from '@nestjs/mapped-types';
|
||||
import { CreateDetalleServicioDto } from './create-detalle_servicio.dto';
|
||||
|
||||
export class UpdateDetalleServicioDto extends PartialType(CreateDetalleServicioDto) {}
|
||||
@@ -0,0 +1,42 @@
|
||||
import { Column, Entity, PrimaryGeneratedColumn } from 'typeorm';
|
||||
|
||||
@Entity({ name: 'detalle_servicio' })
|
||||
export class DetalleServicio {
|
||||
@PrimaryGeneratedColumn({
|
||||
name: 'id_detalle_servicio',
|
||||
type: 'int',
|
||||
unsigned: true,
|
||||
})
|
||||
id_detalle_servicio: number;
|
||||
|
||||
@Column('decimal', {
|
||||
name: 'monto',
|
||||
precision: 10,
|
||||
scale: 2,
|
||||
nullable: false,
|
||||
})
|
||||
monto: number;
|
||||
|
||||
@Column({ name: 'numero_hojas', type: 'int', nullable: false, default: 0 })
|
||||
numero_hojas: number;
|
||||
|
||||
@Column({
|
||||
name: 'fecha_operacion',
|
||||
type: 'timestamp',
|
||||
default: () => 'CURRENT_TIMESTAMP',
|
||||
nullable: false,
|
||||
})
|
||||
fecha_operacion: Date;
|
||||
|
||||
@Column({ name: 'id_cuenta', type: 'int', nullable: false })
|
||||
id_cuenta: number;
|
||||
|
||||
@Column({ name: 'id_servicio', type: 'int', nullable: false })
|
||||
id_servicio: number;
|
||||
|
||||
@Column({ name: 'id_usuario', type: 'int', nullable: false })
|
||||
id_usuario: number;
|
||||
|
||||
@Column({ name: 'id_perido', type: 'int', default: null })
|
||||
id_periodo: number;
|
||||
}
|
||||
+10
-1
@@ -1,11 +1,20 @@
|
||||
import { NestFactory } from '@nestjs/core';
|
||||
import { AppModule } from './app.module';
|
||||
import { ValidationPipe } from '@nestjs/common';
|
||||
import { ConfigService } from '@nestjs/config';
|
||||
|
||||
async function bootstrap() {
|
||||
const app = await NestFactory.create(AppModule);
|
||||
const configService = app.get(ConfigService);
|
||||
|
||||
app.useGlobalPipes(new ValidationPipe());
|
||||
app.enableCors();
|
||||
|
||||
app.enableCors(
|
||||
{
|
||||
origin: configService.get<string>('Front_URL'),
|
||||
}
|
||||
);
|
||||
|
||||
await app.listen(process.env.PORT ?? 3000);
|
||||
}
|
||||
bootstrap();
|
||||
|
||||
@@ -0,0 +1,23 @@
|
||||
import {
|
||||
IsString,
|
||||
Length,
|
||||
IsDateString,
|
||||
IsOptional,
|
||||
IsBoolean,
|
||||
} from 'class-validator';
|
||||
|
||||
export class CreatePeriodoDto {
|
||||
@IsString()
|
||||
@Length(6, 6)
|
||||
semestre: string;
|
||||
|
||||
@IsDateString()
|
||||
fecha_inicio_servicio: string;
|
||||
|
||||
@IsDateString()
|
||||
fecha_fin_servicio: string;
|
||||
|
||||
@IsOptional()
|
||||
@IsBoolean()
|
||||
activo?: boolean;
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
export class PeriodoDto {
|
||||
id_periodo: number;
|
||||
semestre: string;
|
||||
fecha_inicio_servicio: Date;
|
||||
fecha_fin_servicio: Date;
|
||||
activo: boolean;
|
||||
}
|
||||
@@ -0,0 +1,4 @@
|
||||
import { PartialType } from '@nestjs/mapped-types';
|
||||
import { CreatePeriodoDto } from './create-periodo.dto';
|
||||
|
||||
export class UpdatePeriodoDto extends PartialType(CreatePeriodoDto) {}
|
||||
@@ -0,0 +1,23 @@
|
||||
import { Column, Entity, PrimaryGeneratedColumn } from 'typeorm';
|
||||
|
||||
@Entity({ name: 'periodo' })
|
||||
export class Periodo {
|
||||
@PrimaryGeneratedColumn({ name: 'id_periodo', type: 'int' })
|
||||
id_periodo: number;
|
||||
|
||||
@Column({ type: 'char', length: 6, nullable: false })
|
||||
semestre: string;
|
||||
|
||||
@Column({ name: 'fecha_inicio_servicio', type: 'date', nullable: false })
|
||||
fecha_inicio_servicio: Date;
|
||||
|
||||
@Column({ name: 'fecha_fin_servicio', type: 'date', nullable: false })
|
||||
fecha_fin_servicio: Date;
|
||||
|
||||
@Column({
|
||||
type: 'bit',
|
||||
width: 1,
|
||||
default: () => "b'1'",
|
||||
})
|
||||
activo: boolean;
|
||||
}
|
||||
@@ -0,0 +1,34 @@
|
||||
import { Controller, Get, Post, Body, Patch, Param, Delete } from '@nestjs/common';
|
||||
import { PeriodoService } from './periodo.service';
|
||||
import { CreatePeriodoDto } from './dto/create-periodo.dto';
|
||||
import { UpdatePeriodoDto } from './dto/update-periodo.dto';
|
||||
|
||||
@Controller('periodo')
|
||||
export class PeriodoController {
|
||||
constructor(private readonly periodoService: PeriodoService) {}
|
||||
|
||||
@Post()
|
||||
create(@Body() createPeriodoDto: CreatePeriodoDto) {
|
||||
return this.periodoService.create(createPeriodoDto);
|
||||
}
|
||||
|
||||
@Get()
|
||||
findAll() {
|
||||
return this.periodoService.findAll();
|
||||
}
|
||||
|
||||
@Get(':id')
|
||||
findOne(@Param('id') id: string) {
|
||||
return this.periodoService.findOne(+id);
|
||||
}
|
||||
|
||||
@Patch(':id')
|
||||
update(@Param('id') id: string, @Body() updatePeriodoDto: UpdatePeriodoDto) {
|
||||
return this.periodoService.update(+id, updatePeriodoDto);
|
||||
}
|
||||
|
||||
@Delete(':id')
|
||||
remove(@Param('id') id: string) {
|
||||
return this.periodoService.remove(+id);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
import { Module } from '@nestjs/common';
|
||||
import { PeriodoService } from './periodo.service';
|
||||
import { PeriodoController } from './periodo.controller';
|
||||
|
||||
@Module({
|
||||
controllers: [PeriodoController],
|
||||
providers: [PeriodoService],
|
||||
})
|
||||
export class PeriodoModule {}
|
||||
@@ -0,0 +1,26 @@
|
||||
import { Injectable } from '@nestjs/common';
|
||||
import { CreatePeriodoDto } from './dto/create-periodo.dto';
|
||||
import { UpdatePeriodoDto } from './dto/update-periodo.dto';
|
||||
|
||||
@Injectable()
|
||||
export class PeriodoService {
|
||||
create(createPeriodoDto: CreatePeriodoDto) {
|
||||
return 'This action adds a new periodo';
|
||||
}
|
||||
|
||||
findAll() {
|
||||
return `This action returns all periodo`;
|
||||
}
|
||||
|
||||
findOne(id: number) {
|
||||
return `This action returns a #${id} periodo`;
|
||||
}
|
||||
|
||||
update(id: number, updatePeriodoDto: UpdatePeriodoDto) {
|
||||
return `This action updates a #${id} periodo`;
|
||||
}
|
||||
|
||||
remove(id: number) {
|
||||
return `This action removes a #${id} periodo`;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
import { IsString, MaxLength } from 'class-validator';
|
||||
|
||||
export class CreateServicioDto {
|
||||
@IsString()
|
||||
@MaxLength(45)
|
||||
servicio: string;
|
||||
}
|
||||
@@ -0,0 +1,4 @@
|
||||
export class ServicioDto {
|
||||
id_servicio: number;
|
||||
servicio: string;
|
||||
}
|
||||
@@ -0,0 +1,4 @@
|
||||
import { PartialType } from '@nestjs/mapped-types';
|
||||
import { CreateServicioDto } from './create-servicio.dto';
|
||||
|
||||
export class UpdateServicioDto extends PartialType(CreateServicioDto) {}
|
||||
@@ -0,0 +1,10 @@
|
||||
import { Entity, PrimaryGeneratedColumn, Column } from 'typeorm';
|
||||
|
||||
@Entity({ name: 'servicio' })
|
||||
export class Servicio {
|
||||
@PrimaryGeneratedColumn({ name: 'id_servicio', type: 'int' })
|
||||
id_servicio: number;
|
||||
|
||||
@Column({ type: 'varchar', length: 45, nullable: false })
|
||||
servicio: string;
|
||||
}
|
||||
@@ -0,0 +1,35 @@
|
||||
import { Controller, Get, Post, Body, Patch, Param, Delete } from '@nestjs/common';
|
||||
import { ServicioService } from './servicio.service';
|
||||
import { CreateServicioDto } from './dto/create-servicio.dto';
|
||||
import { UpdateServicioDto } from './dto/update-servicio.dto';
|
||||
|
||||
@Controller('servicio')
|
||||
export class ServicioController {
|
||||
constructor(private readonly servicioService: ServicioService) {}
|
||||
|
||||
@Post()
|
||||
create(@Body() createServicioDto: CreateServicioDto) {
|
||||
return this.servicioService.create(createServicioDto);
|
||||
}
|
||||
|
||||
@Get()
|
||||
findAll() {
|
||||
return this.servicioService.findAll();
|
||||
}
|
||||
|
||||
@Get(':id')
|
||||
findOne(@Param('id') id: string) {
|
||||
return this.servicioService.findOne(+id);
|
||||
}
|
||||
|
||||
@Patch(':id')
|
||||
update(@Param('id') id: string, @Body() updateServicioDto: UpdateServicioDto) {
|
||||
return this.servicioService.update(+id, updateServicioDto);
|
||||
}
|
||||
|
||||
@Delete(':id')
|
||||
remove(@Param('id') id: string) {
|
||||
return this.servicioService.remove(+id);
|
||||
}
|
||||
}
|
||||
//IO
|
||||
@@ -0,0 +1,9 @@
|
||||
import { Module } from '@nestjs/common';
|
||||
import { ServicioService } from './servicio.service';
|
||||
import { ServicioController } from './servicio.controller';
|
||||
|
||||
@Module({
|
||||
controllers: [ServicioController],
|
||||
providers: [ServicioService],
|
||||
})
|
||||
export class ServicioModule {}
|
||||
@@ -0,0 +1,26 @@
|
||||
import { Injectable } from '@nestjs/common';
|
||||
import { CreateServicioDto } from './dto/create-servicio.dto';
|
||||
import { UpdateServicioDto } from './dto/update-servicio.dto';
|
||||
|
||||
@Injectable()
|
||||
export class ServicioService {
|
||||
create(createServicioDto: CreateServicioDto) {
|
||||
return 'This action adds a new servicio';
|
||||
}
|
||||
|
||||
findAll() {
|
||||
return `This action returns all servicio`;
|
||||
}
|
||||
|
||||
findOne(id: number) {
|
||||
return `This action returns a #${id} servicio`;
|
||||
}
|
||||
|
||||
update(id: number, updateServicioDto: UpdateServicioDto) {
|
||||
return `This action updates a #${id} servicio`;
|
||||
}
|
||||
|
||||
remove(id: number) {
|
||||
return `This action removes a #${id} servicio`;
|
||||
}
|
||||
}
|
||||
@@ -1 +0,0 @@
|
||||
export class CreateStudentDto {}
|
||||
@@ -1,6 +0,0 @@
|
||||
import { IsNumber, IsString } from 'class-validator';
|
||||
|
||||
export class studentDto {
|
||||
@IsNumber()
|
||||
id_cuenta: number;
|
||||
}
|
||||
@@ -1,58 +0,0 @@
|
||||
import { Column, Entity, PrimaryGeneratedColumn } from 'typeorm';
|
||||
|
||||
@Entity({ name: 'alumno' })
|
||||
export class Student {
|
||||
@PrimaryGeneratedColumn({ name: 'id_cuenta', type: 'int', unsigned: true })
|
||||
id_cuenta: number;
|
||||
|
||||
@Column({ name: 'nombre', type: 'varchar', length: 300, nullable: false })
|
||||
nombre: string;
|
||||
|
||||
@Column({
|
||||
name: 'fecha_nacimiento',
|
||||
type: 'varchar',
|
||||
length: 8,
|
||||
nullable: true,
|
||||
})
|
||||
fecha_nacimiento: string | null;
|
||||
|
||||
@Column({ name: 'correo', type: 'varchar', length: 100, nullable: true })
|
||||
correo: string | null;
|
||||
|
||||
@Column({
|
||||
name: 'credito',
|
||||
type: 'decimal',
|
||||
precision: 10,
|
||||
scale: 2,
|
||||
default: 0.0,
|
||||
})
|
||||
credito: string;
|
||||
|
||||
@Column({
|
||||
name: 'fecha_actualizacion_credito',
|
||||
type: 'timestamp',
|
||||
default: () => 'CURRENT_TIMESTAMP',
|
||||
onUpdate: 'CURRENT_TIMESTAMP',
|
||||
})
|
||||
fecha_actualizacion_credito: Date;
|
||||
|
||||
@Column({
|
||||
name: 'vigente',
|
||||
type: 'enum',
|
||||
enum: ['si', 'no'],
|
||||
default: 'si',
|
||||
})
|
||||
vigente: 'si' | 'no';
|
||||
|
||||
@Column({ name: 'id_periodo', type: 'int', nullable: true })
|
||||
id_periodo: number | null;
|
||||
|
||||
@Column({ name: 'fecha_registro', type: 'datetime', nullable: false })
|
||||
fecha_registro: Date;
|
||||
|
||||
@Column({ name: 'id_carrera', type: 'int', nullable: false })
|
||||
id_carrera: number;
|
||||
|
||||
@Column({ name: 'generacion', type: 'int', width: 4, nullable: true })
|
||||
generacion: number | null;
|
||||
}
|
||||
@@ -1,23 +0,0 @@
|
||||
import {
|
||||
Controller,
|
||||
Get,
|
||||
Post,
|
||||
Body,
|
||||
Patch,
|
||||
Param,
|
||||
Delete,
|
||||
} from '@nestjs/common';
|
||||
import { StudentService } from './student.service';
|
||||
import { CreateStudentDto } from './dto/create-student.dto';
|
||||
import { UpdateStudentDto } from './dto/update-student.dto';
|
||||
import { studentDto } from './dto/student.dto';
|
||||
|
||||
@Controller('student')
|
||||
export class StudentController {
|
||||
constructor(private readonly studentService: StudentService) {}
|
||||
|
||||
@Post()
|
||||
create(@Body() data: studentDto) {
|
||||
return this.studentService.findOne(data);
|
||||
}
|
||||
}
|
||||
@@ -1,19 +0,0 @@
|
||||
import { Inject, Injectable } from '@nestjs/common';
|
||||
import { CreateStudentDto } from './dto/create-student.dto';
|
||||
import { UpdateStudentDto } from './dto/update-student.dto';
|
||||
import { studentDto } from './dto/student.dto';
|
||||
import { InjectRepository } from '@nestjs/typeorm';
|
||||
import { Student } from './entities/student.entity';
|
||||
import { Repository } from 'typeorm';
|
||||
|
||||
@Injectable()
|
||||
export class StudentService {
|
||||
constructor(
|
||||
@InjectRepository(Student)
|
||||
private studenRepository: Repository<Student>,
|
||||
) {}
|
||||
findOne(data: studentDto) {
|
||||
const { id_cuenta } = data;
|
||||
return this.studenRepository.findOne({ where: { id_cuenta } });
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,20 @@
|
||||
import { Injectable } from '@nestjs/common';
|
||||
import { PassportStrategy } from '@nestjs/passport';
|
||||
import { ExtractJwt, Strategy } from 'passport-jwt';
|
||||
import { UserService } from './user.service';
|
||||
import { ConfigService } from '@nestjs/config';
|
||||
|
||||
@Injectable()
|
||||
export class JwtStrategy extends PassportStrategy(Strategy) {
|
||||
constructor(private configService: ConfigService) {
|
||||
super({
|
||||
jwtFromRequest: ExtractJwt.fromAuthHeaderAsBearerToken(),
|
||||
ignoreExpiration: false,
|
||||
secretOrKey: configService.get('JWT_SECRET')!,
|
||||
});
|
||||
}
|
||||
|
||||
async validate(payload: any) {
|
||||
return { userId: payload.id, username: payload.usuario };
|
||||
}
|
||||
}
|
||||
@@ -1,15 +1,30 @@
|
||||
import { Controller, Get, Post, Body, Patch, Param, Delete } from '@nestjs/common';
|
||||
import {
|
||||
Controller,
|
||||
Get,
|
||||
Post,
|
||||
Body,
|
||||
UseGuards,
|
||||
Request,
|
||||
Res,
|
||||
} from '@nestjs/common';
|
||||
import type { Response } from 'express';
|
||||
import { UserService } from './user.service';
|
||||
import { CreateUserDto } from './dto/create-user.dto';
|
||||
import { UpdateUserDto } from './dto/update-user.dto';
|
||||
import { AuthGuard } from '@nestjs/passport';
|
||||
|
||||
@Controller('user')
|
||||
export class UserController {
|
||||
constructor(private readonly userService: UserService) {}
|
||||
|
||||
@Post()
|
||||
Login(@Body() user: CreateUserDto) {
|
||||
return this.userService.Login(user);
|
||||
async Login(@Body() data: CreateUserDto, @Res() res: Response) {
|
||||
return this.userService.Login(data, res);
|
||||
}
|
||||
|
||||
@UseGuards(AuthGuard('jwt'))
|
||||
@Get('/validate-token')
|
||||
getProfile(@Request() req) {
|
||||
return req.user;
|
||||
}
|
||||
|
||||
}
|
||||
//IO
|
||||
|
||||
+20
-4
@@ -3,13 +3,29 @@ import { UserService } from './user.service';
|
||||
import { UserController } from './user.controller';
|
||||
import { TypeOrmModule } from '@nestjs/typeorm';
|
||||
import { User } from './entities/user.entity';
|
||||
import { ConfigService } from '@nestjs/config';
|
||||
import { JwtModule } from '@nestjs/jwt';
|
||||
import { PassportModule } from '@nestjs/passport';
|
||||
import { JwtStrategy } from './jwt.strategy';
|
||||
|
||||
@Module({
|
||||
imports:[
|
||||
TypeOrmModule.forFeature([User])
|
||||
imports: [
|
||||
TypeOrmModule.forFeature([User]),
|
||||
PassportModule.register({ defaultStrategy: 'jwt' }),
|
||||
JwtModule.registerAsync({
|
||||
inject: [ConfigService],
|
||||
useFactory: async (configService: ConfigService) => {
|
||||
return {
|
||||
global: true,
|
||||
secret: configService.get('JWT_SECRET'),
|
||||
signOptions: { expiresIn: '1h' },
|
||||
};
|
||||
},
|
||||
|
||||
}),
|
||||
],
|
||||
controllers: [UserController],
|
||||
providers: [UserService],
|
||||
providers: [UserService,JwtStrategy],
|
||||
exports: [UserService],
|
||||
})
|
||||
export class UserModule {}
|
||||
export class UserModule { }
|
||||
|
||||
@@ -1,14 +1,10 @@
|
||||
import {
|
||||
Injectable,
|
||||
Logger,
|
||||
NotFoundException,
|
||||
UnauthorizedException,
|
||||
} from '@nestjs/common';
|
||||
import { Injectable, NotFoundException } from '@nestjs/common';
|
||||
import { Response } from 'express';
|
||||
import { CreateUserDto } from './dto/create-user.dto';
|
||||
import { UpdateUserDto } from './dto/update-user.dto';
|
||||
import { InjectRepository } from '@nestjs/typeorm';
|
||||
import { User } from './entities/user.entity';
|
||||
import { Repository } from 'typeorm';
|
||||
import { JwtService } from '@nestjs/jwt';
|
||||
|
||||
@Injectable()
|
||||
export class UserService {
|
||||
@@ -32,6 +28,7 @@ export class UserService {
|
||||
}
|
||||
|
||||
async Login(data: CreateUserDto) {
|
||||
return await this.findOneByNameandPassword(data);
|
||||
const user = await this.findOneByNameandPassword(data);
|
||||
return user;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user