add folder operation and use createQueryRunner

This commit is contained in:
IO420
2025-09-16 22:37:25 -06:00
parent 7024bfdeac
commit 3e4b3b1b18
15 changed files with 150 additions and 76 deletions
+18
View File
@@ -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;
}
+9
View File
@@ -0,0 +1,9 @@
import { Controller } from "@nestjs/common";
import { OperationsService } from "./operations.services";
@Controller('operations')
export class OperationsController{
constructor(private readonly operationsService:OperationsService){}
}
+15
View File
@@ -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 {}
+54
View File
@@ -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();
}
}
}