fixed entities
This commit is contained in:
@@ -1,15 +1,23 @@
|
||||
import { Body, Controller, Post, UseGuards } from "@nestjs/common";
|
||||
import { OperationsService } from "./operations.services";
|
||||
import { chargePrintDto } from "./dto/operations.dto";
|
||||
import { JwtAuthGuard } from "src/user/jwt.guard";
|
||||
import { Body, Controller, Post, Req, UseGuards } from '@nestjs/common';
|
||||
import { OperationsService } from './operations.services';
|
||||
import { chargePrintDto } from './dto/operations.dto';
|
||||
import { JwtAuthGuard } from 'src/user/jwt.guard';
|
||||
|
||||
@Controller('operations')
|
||||
export class OperationsController{
|
||||
constructor(private readonly operationsService:OperationsService){}
|
||||
export class OperationsController {
|
||||
constructor(private readonly operationsService: OperationsService) {}
|
||||
|
||||
@UseGuards(JwtAuthGuard)
|
||||
@Post('impressions')
|
||||
async chargePrint(@Body() data: chargePrintDto, @Req() req: any) {
|
||||
const id_usuario = req.user.id;
|
||||
return this.operationsService.chargePrint(data, id_usuario);
|
||||
}
|
||||
|
||||
@Post()
|
||||
async Login(@Body() data:chargePrintDto) {
|
||||
return this.operationsService.chargePrint(data,data.id_cuenta);
|
||||
}
|
||||
}
|
||||
@UseGuards(JwtAuthGuard)
|
||||
@Post('receipt')
|
||||
async addCredit(@Body() data, @Req() req: any) {
|
||||
const id_usuario = req.user.id;
|
||||
return this.operationsService.addCredit(data, id_usuario);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -6,12 +6,16 @@ import { OperationsController } from './operations.controller';
|
||||
import { OperationsService } from './operations.services';
|
||||
import { DetalleServicioModule } from 'src/detalle_servicio/detalle_servicio.module';
|
||||
import { AlumnoModule } from 'src/alumno/student.module';
|
||||
import { UserModule } from 'src/user/user.module';
|
||||
import { ReciboModule } from 'src/recibo/recibo.module';
|
||||
|
||||
@Module({
|
||||
imports: [
|
||||
TypeOrmModule.forFeature([Alumno, DetalleServicio]),
|
||||
DetalleServicioModule,
|
||||
AlumnoModule,
|
||||
UserModule,
|
||||
ReciboModule,
|
||||
],
|
||||
controllers: [OperationsController],
|
||||
providers: [OperationsService],
|
||||
|
||||
@@ -1,9 +1,17 @@
|
||||
import { BadRequestException, Injectable } from '@nestjs/common';
|
||||
import {
|
||||
BadRequestException,
|
||||
Injectable,
|
||||
Req,
|
||||
UseGuards,
|
||||
} from '@nestjs/common';
|
||||
import { AlumnoService } 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';
|
||||
import { UserService } from 'src/user/user.service';
|
||||
import { ReciboService } from 'src/recibo/recibo.service';
|
||||
import { CreateReciboDto } from 'src/recibo/dto/create-recibo.dto';
|
||||
|
||||
@Injectable()
|
||||
export class OperationsService {
|
||||
@@ -11,9 +19,11 @@ export class OperationsService {
|
||||
private readonly dataSource: DataSource,
|
||||
private readonly alumnoService: AlumnoService,
|
||||
private readonly detalleServicioService: DetalleServicioService,
|
||||
private readonly userService: UserService,
|
||||
private readonly reciboService: ReciboService,
|
||||
) {}
|
||||
|
||||
async chargePrint(data: chargePrintDto, userId: number) {
|
||||
async chargePrint(data: chargePrintDto, id_usuario: number) {
|
||||
const { monto, id_cuenta } = data;
|
||||
|
||||
const queryRunner = this.dataSource.createQueryRunner();
|
||||
@@ -27,10 +37,14 @@ export class OperationsService {
|
||||
throw new BadRequestException('Crédito insuficiente');
|
||||
}
|
||||
|
||||
const detalleData: CreateDetalleServicioDto = {
|
||||
...data,
|
||||
id_servicio: 1,
|
||||
id_usuario: userId,
|
||||
const user = await this.userService.findOne(id_usuario);
|
||||
const alum = await this.alumnoService.findOne(id_cuenta)
|
||||
|
||||
const detalleData = {
|
||||
servicio: 1,
|
||||
user,
|
||||
alum,
|
||||
monto,
|
||||
fecha_operacion: new Date(),
|
||||
};
|
||||
|
||||
@@ -39,7 +53,7 @@ export class OperationsService {
|
||||
queryRunner.manager,
|
||||
);
|
||||
|
||||
await this.alumnoService.UpdateCredit(
|
||||
await this.alumnoService.collectCredit(
|
||||
id_cuenta,
|
||||
monto,
|
||||
queryRunner.manager,
|
||||
@@ -54,4 +68,37 @@ export class OperationsService {
|
||||
await queryRunner.release();
|
||||
}
|
||||
}
|
||||
|
||||
async addCredit(data, id_usuario: number) {
|
||||
const { id_cuenta, monto, fecha_recibo, folio_recibo } = data;
|
||||
|
||||
const queryRunner = this.dataSource.createQueryRunner();
|
||||
await queryRunner.connect();
|
||||
await queryRunner.startTransaction();
|
||||
|
||||
const user = await this.userService.findOne(id_usuario);
|
||||
const alum = await this.alumnoService.findOne(id_cuenta);
|
||||
|
||||
try {
|
||||
const detalleData = {
|
||||
folio_recibo,
|
||||
user,
|
||||
alum,
|
||||
fecha_recibo,
|
||||
monto,
|
||||
};
|
||||
|
||||
await this.reciboService.create(detalleData, queryRunner.manager);
|
||||
|
||||
await this.alumnoService.addCredit(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();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user