add folder operation and use createQueryRunner
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,9 @@
|
||||
import { Controller } from "@nestjs/common";
|
||||
import { OperationsService } from "./operations.services";
|
||||
|
||||
@Controller('operations')
|
||||
export class OperationsController{
|
||||
constructor(private readonly operationsService:OperationsService){}
|
||||
|
||||
|
||||
}
|
||||
@@ -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();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,5 +1,5 @@
|
||||
import { Type } from 'class-transformer';
|
||||
import { IsDate, IsNotEmpty, IsNumber, IsString } from 'class-validator';
|
||||
|
||||
import { IsEmail, IsNotEmpty, IsNumber, IsString } from 'class-validator';
|
||||
|
||||
export class CreateStudentDto {
|
||||
@IsNotEmpty()
|
||||
@@ -17,6 +17,7 @@ export class CreateStudentDto {
|
||||
@IsNumber()
|
||||
id_carrera: number;
|
||||
|
||||
@IsEmail()
|
||||
@IsString()
|
||||
correo: string;
|
||||
}
|
||||
|
||||
@@ -26,7 +26,7 @@ export class Student {
|
||||
scale: 2,
|
||||
default: 0.0,
|
||||
})
|
||||
credito: string;
|
||||
credito: number;
|
||||
|
||||
@Column({
|
||||
name: 'fecha_actualizacion_credito',
|
||||
|
||||
@@ -27,17 +27,7 @@ export class StudentController {
|
||||
}
|
||||
|
||||
@Get(':id')
|
||||
findOne(@Param('id') id: string) {
|
||||
findOne(@Param('id') id: number) {
|
||||
return this.studentService.findOne(+id);
|
||||
}
|
||||
|
||||
@Patch(':id')
|
||||
update(@Param('id') id: string, @Body() updateStudentDto: UpdateStudentDto) {
|
||||
return this.studentService.update(+id, updateStudentDto);
|
||||
}
|
||||
|
||||
@Delete(':id')
|
||||
remove(@Param('id') id: string) {
|
||||
return this.studentService.remove(+id);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2,7 +2,7 @@ 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 { Repository } from 'typeorm';
|
||||
import { EntityManager, Repository } from 'typeorm';
|
||||
import { InjectRepository } from '@nestjs/typeorm';
|
||||
|
||||
@Injectable()
|
||||
@@ -31,11 +31,22 @@ export class StudentService {
|
||||
return student;
|
||||
}
|
||||
|
||||
update(id: number, updateStudentDto: UpdateStudentDto) {
|
||||
return `This action updates a #${id} student`;
|
||||
async GetCredit(id_cuenta: number): Promise<Student['credito']> {
|
||||
const student = await this.findOne(id_cuenta);
|
||||
return student.credito;
|
||||
}
|
||||
|
||||
remove(id: number) {
|
||||
return `This action removes a #${id} student`;
|
||||
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();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,26 +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';
|
||||
import { UpdateDetalleServicioDto } from './dto/update-detalle_servicio.dto';
|
||||
|
||||
@Injectable()
|
||||
export class DetalleServicioService {
|
||||
create(createDetalleServicioDto: CreateDetalleServicioDto) {
|
||||
return 'This action adds a new detalleServicio';
|
||||
}
|
||||
|
||||
findAll() {
|
||||
return `This action returns all detalleServicio`;
|
||||
}
|
||||
|
||||
findOne(id: number) {
|
||||
return `This action returns a #${id} detalleServicio`;
|
||||
}
|
||||
|
||||
update(id: number, updateDetalleServicioDto: UpdateDetalleServicioDto) {
|
||||
return `This action updates a #${id} detalleServicio`;
|
||||
}
|
||||
|
||||
remove(id: number) {
|
||||
return `This action removes a #${id} detalleServicio`;
|
||||
async Create(data: CreateDetalleServicioDto, manager: EntityManager) {
|
||||
const repo = manager.getRepository(DetalleServicio);
|
||||
const details = repo.create(data);
|
||||
return await repo.save(details);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,9 +1,15 @@
|
||||
import { Type } from 'class-transformer';
|
||||
import { IsDate, IsInt, IsNotEmpty, IsOptional, Min } from 'class-validator';
|
||||
import { IsDate, IsIn, IsInt, IsNotEmpty, IsOptional, Min } from 'class-validator';
|
||||
|
||||
export class CreateDetalleServicioDto {
|
||||
|
||||
@IsInt()
|
||||
@Min(0)
|
||||
@IsNotEmpty()
|
||||
@Min(1)
|
||||
monto:number
|
||||
|
||||
@IsInt()
|
||||
@Min(1)
|
||||
@IsNotEmpty()
|
||||
numero_hojas: number;
|
||||
|
||||
@@ -23,8 +29,4 @@ export class CreateDetalleServicioDto {
|
||||
@IsInt()
|
||||
@IsNotEmpty()
|
||||
id_usuario: number;
|
||||
|
||||
@IsOptional()
|
||||
@IsInt()
|
||||
id_periodo?: number;
|
||||
}
|
||||
|
||||
@@ -1,9 +0,0 @@
|
||||
export class DetalleServicioDto {
|
||||
id_detalle_servicio: number;
|
||||
numero_hojas: number;
|
||||
fecha_operacion: Date;
|
||||
id_cuenta: number;
|
||||
id_servicio: number;
|
||||
id_usuario: number;
|
||||
id_periodo: number | null;
|
||||
}
|
||||
@@ -15,7 +15,7 @@ export class DetalleServicio {
|
||||
scale: 2,
|
||||
nullable: false,
|
||||
})
|
||||
monto: string;
|
||||
monto: number;
|
||||
|
||||
@Column({ name: 'numero_hojas', type: 'int', nullable: false, default: 0 })
|
||||
numero_hojas: number;
|
||||
@@ -39,13 +39,4 @@ export class DetalleServicio {
|
||||
|
||||
@Column({ name: 'id_perido', type: 'int', default: null })
|
||||
id_periodo: number;
|
||||
}
|
||||
|
||||
/* `id_detalle_servicio` int(11) NOT NULL AUTO_INCREMENT,
|
||||
`monto` decimal(10,2) NOT NULL,
|
||||
`numero_hojas` int(11) NOT NULL DEFAULT 0,
|
||||
`fecha_operacion` timestamp NOT NULL DEFAULT current_timestamp(),
|
||||
`id_cuenta` int(9) unsigned zerofill NOT NULL,
|
||||
`id_servicio` int(11) NOT NULL,
|
||||
`id_usuario` int(11) NOT NULL,
|
||||
`id_periodo` int(11) DEFAULT NULL,*/
|
||||
}
|
||||
@@ -49,7 +49,6 @@ export class Perfil {
|
||||
@Column({ name: 'perfil', type: 'varchar', length: 45, nullable: false })
|
||||
perfil: string;
|
||||
|
||||
// Relación uno a muchos con usuario
|
||||
@OneToMany(() => User, (user) => user.perfil)
|
||||
usuarios: User[];
|
||||
}
|
||||
+16
-10
@@ -1,4 +1,12 @@
|
||||
import { Controller, Get, Post, Body, UseGuards, Request, Res } 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';
|
||||
@@ -6,19 +14,17 @@ import { AuthGuard } from '@nestjs/passport';
|
||||
|
||||
@Controller('user')
|
||||
export class UserController {
|
||||
constructor(private readonly userService: UserService) { }
|
||||
|
||||
@Post()
|
||||
async Login(@Body() data: CreateUserDto, @Res() res: Response) {
|
||||
return this.userService.Login(data, res);
|
||||
}
|
||||
constructor(private readonly userService: UserService) {}
|
||||
|
||||
@Post()
|
||||
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;
|
||||
return req.user;
|
||||
}
|
||||
|
||||
}
|
||||
//IO
|
||||
//IO
|
||||
|
||||
@@ -10,7 +10,7 @@ import { JwtService } from '@nestjs/jwt';
|
||||
export class UserService {
|
||||
constructor(
|
||||
@InjectRepository(User)
|
||||
private userRepository: Repository<User>,
|
||||
private readonly userRepository: Repository<User>,
|
||||
|
||||
private jwtService: JwtService
|
||||
) { }
|
||||
|
||||
Reference in New Issue
Block a user