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();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -84,7 +84,7 @@ export class Alumno {
|
||||
@Column({ name: 'generacion', type: 'int', width: 4, nullable: true })
|
||||
generacion: number | null;
|
||||
|
||||
@OneToMany(() => DetalleServicio, (detalle) => detalle.id_cuenta)
|
||||
@OneToMany(() => DetalleServicio, (detalle) => detalle.alum)
|
||||
detalles_servicio: DetalleServicio[];
|
||||
|
||||
@ManyToOne(() => Carrera, (carrera) => carrera.estudiantes, {
|
||||
|
||||
@@ -36,7 +36,7 @@ export class AlumnoService {
|
||||
return student.credito;
|
||||
}
|
||||
|
||||
async UpdateCredit(
|
||||
async collectCredit(
|
||||
id_cuenta: number,
|
||||
credit: number,
|
||||
manager: EntityManager,
|
||||
@@ -49,5 +49,19 @@ export class AlumnoService {
|
||||
.where({ id_cuenta })
|
||||
.execute();
|
||||
}
|
||||
|
||||
async addCredit(
|
||||
id_cuenta: number,
|
||||
credit: number,
|
||||
manager: EntityManager,
|
||||
) {
|
||||
const repo = manager.getRepository(Alumno);
|
||||
return await repo
|
||||
.createQueryBuilder()
|
||||
.update()
|
||||
.set({ credito: () => `credito + ${credit}` })
|
||||
.where({ id_cuenta })
|
||||
.execute();
|
||||
}
|
||||
}
|
||||
//IO
|
||||
@@ -5,7 +5,7 @@ import { CreateDetalleServicioDto } from './dto/create-detalle_servicio.dto';
|
||||
|
||||
@Injectable()
|
||||
export class DetalleServicioService {
|
||||
async Create(data: CreateDetalleServicioDto, manager: EntityManager) {
|
||||
async Create(data, manager: EntityManager) {
|
||||
const repo = manager.getRepository(DetalleServicio);
|
||||
const details = repo.create(data);
|
||||
return await repo.save(details);
|
||||
|
||||
@@ -38,17 +38,11 @@ export class DetalleServicio {
|
||||
})
|
||||
fecha_operacion: Date;
|
||||
|
||||
@Column()
|
||||
id_cuenta:number
|
||||
|
||||
@Column()
|
||||
id_servicio:number
|
||||
|
||||
@ManyToOne(() => Alumno, (id_cuenta) => id_cuenta.detalles_servicio, {
|
||||
@ManyToOne(() => Alumno, (alum) => alum.detalles_servicio, {
|
||||
eager: true,
|
||||
})
|
||||
@JoinColumn({ name: 'id_cuenta' })
|
||||
cuenta: Alumno;
|
||||
alum: Alumno;
|
||||
|
||||
@ManyToOne(() => Servicio, (id_servicio) => id_servicio.detalles_servicio, {
|
||||
eager: true,
|
||||
@@ -56,16 +50,16 @@ export class DetalleServicio {
|
||||
@JoinColumn({ name: 'id_servicio' })
|
||||
servicio: Servicio;
|
||||
|
||||
@ManyToOne(() => User, (id_perfil) => id_perfil.detalles_servicio, {
|
||||
@ManyToOne(() => User, (user) => user.detalles_servicio, {
|
||||
eager: true,
|
||||
})
|
||||
@JoinColumn({ name: 'id_usuario' })
|
||||
id_perfil: User;
|
||||
user: User;
|
||||
|
||||
@ManyToOne(() => Periodo, (id_periodo) => id_periodo.detalles_servicio, {
|
||||
@ManyToOne(() => Periodo, (periodo) => periodo.detalles_servicio, {
|
||||
eager: true,
|
||||
nullable: true,
|
||||
})
|
||||
@JoinColumn({ name: 'id_periodo' })
|
||||
id_periodo: Periodo;
|
||||
periodo: Periodo;
|
||||
}
|
||||
|
||||
@@ -24,7 +24,7 @@ export class Periodo {
|
||||
|
||||
@OneToMany(
|
||||
() => DetalleServicio,
|
||||
(id_detalle_servicio) => id_detalle_servicio.id_periodo,
|
||||
(id_detalle_servicio) => id_detalle_servicio.periodo,
|
||||
)
|
||||
detalles_servicio: DetalleServicio[];
|
||||
}
|
||||
|
||||
@@ -2,8 +2,6 @@ import { Type } from 'class-transformer';
|
||||
import { IsDate, IsNumber, IsString } from 'class-validator';
|
||||
|
||||
export class CreateReciboDto {
|
||||
@IsNumber()
|
||||
id_cuenta: number;
|
||||
|
||||
@IsNumber()
|
||||
monto: number;
|
||||
|
||||
@@ -6,18 +6,10 @@ import {
|
||||
Req,
|
||||
} from '@nestjs/common';
|
||||
import { ReciboService } from './recibo.service';
|
||||
import { CreateReciboDto } from './dto/create-recibo.dto';
|
||||
import { JwtAuthGuard } from 'src/user/jwt.guard';
|
||||
|
||||
@Controller('recibo')
|
||||
export class ReciboController {
|
||||
constructor(private readonly reciboService: ReciboService) {}
|
||||
|
||||
@UseGuards(JwtAuthGuard)
|
||||
@Post()
|
||||
async createReceipt(@Body() data: CreateReciboDto, @Req() req: any) {
|
||||
const userId = req.user.id;
|
||||
return this.reciboService.create(data, +userId);
|
||||
}
|
||||
}
|
||||
//IO
|
||||
|
||||
@@ -10,6 +10,7 @@ import { UserModule } from 'src/user/user.module';
|
||||
imports: [TypeOrmModule.forFeature([Recibo]), AlumnoModule, UserModule],
|
||||
controllers: [ReciboController],
|
||||
providers: [ReciboService],
|
||||
exports:[ReciboService]
|
||||
})
|
||||
export class ReciboModule {}
|
||||
//IO
|
||||
|
||||
@@ -1,20 +1,16 @@
|
||||
import { ConflictException, Injectable } from '@nestjs/common';
|
||||
import { InjectRepository } from '@nestjs/typeorm';
|
||||
import { Recibo } from './entities/recibo.entity';
|
||||
import { Repository } from 'typeorm';
|
||||
import { EntityManager, Repository } from 'typeorm';
|
||||
import { CreateReciboDto } from './dto/create-recibo.dto';
|
||||
import { AlumnoService } from 'src/alumno/student.service';
|
||||
import { UserService } from 'src/user/user.service';
|
||||
|
||||
@Injectable()
|
||||
export class ReciboService {
|
||||
constructor(
|
||||
@InjectRepository(Recibo)
|
||||
private readonly reciboRepository:Repository<Recibo>,
|
||||
private readonly alumnoService:AlumnoService,
|
||||
private readonly usuarioService:UserService,
|
||||
){}
|
||||
|
||||
private readonly reciboRepository: Repository<Recibo>,
|
||||
) {}
|
||||
|
||||
async findOne(folio_recibo: string) {
|
||||
const recibo = await this.reciboRepository.findOne({
|
||||
where: { folio_recibo },
|
||||
@@ -22,29 +18,10 @@ export class ReciboService {
|
||||
return recibo;
|
||||
}
|
||||
|
||||
async create(data: CreateReciboDto, id_usuario: number) {
|
||||
const {id_cuenta,folio_recibo} = data
|
||||
const receipt = await this.findOne(folio_recibo)
|
||||
|
||||
if(receipt){
|
||||
throw new ConflictException('Recibo ya existe');
|
||||
}
|
||||
|
||||
const alumno = await this.alumnoService.findOne(id_cuenta)
|
||||
const usuario = await this.usuarioService.findOne(id_usuario)
|
||||
|
||||
console.log(usuario)
|
||||
const recibo = this.reciboRepository.create({
|
||||
folio_recibo: data.folio_recibo,
|
||||
monto: data.monto,
|
||||
fecha_recibo: data.fecha_recibo,
|
||||
alum: alumno,
|
||||
user:usuario
|
||||
});
|
||||
|
||||
console.log(recibo)
|
||||
await this.reciboRepository.save(recibo)
|
||||
return;
|
||||
async create(data: CreateReciboDto, manager: EntityManager) {
|
||||
const repo = manager.getRepository(Recibo);
|
||||
const details = repo.create(data);
|
||||
return await repo.save(details);
|
||||
}
|
||||
}
|
||||
//IO
|
||||
//IO
|
||||
|
||||
@@ -11,7 +11,7 @@ export class Servicio {
|
||||
|
||||
@OneToMany(
|
||||
() => DetalleServicio,
|
||||
(id_detalle_servicio) => id_detalle_servicio.id_servicio,
|
||||
(id_detalle_servicio) => id_detalle_servicio.servicio,
|
||||
)
|
||||
detalles_servicio: DetalleServicio[];
|
||||
}
|
||||
|
||||
@@ -56,7 +56,7 @@ export class User {
|
||||
|
||||
@OneToMany(
|
||||
() => DetalleServicio,
|
||||
(id_detalle_servicio) => id_detalle_servicio.id_perfil,
|
||||
(id_detalle_servicio) => id_detalle_servicio.user,
|
||||
)
|
||||
detalles_servicio: DetalleServicio[];
|
||||
|
||||
|
||||
@@ -14,7 +14,6 @@ export class JwtStrategy extends PassportStrategy(Strategy) {
|
||||
}
|
||||
|
||||
async validate(payload: any) {
|
||||
console.log('payload:', payload);
|
||||
return { id: payload.id, usuario: payload.usuario };
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user