fixed entities
This commit is contained in:
@@ -1,15 +1,23 @@
|
|||||||
import { Body, Controller, Post, UseGuards } from "@nestjs/common";
|
import { Body, Controller, Post, Req, UseGuards } from '@nestjs/common';
|
||||||
import { OperationsService } from "./operations.services";
|
import { OperationsService } from './operations.services';
|
||||||
import { chargePrintDto } from "./dto/operations.dto";
|
import { chargePrintDto } from './dto/operations.dto';
|
||||||
import { JwtAuthGuard } from "src/user/jwt.guard";
|
import { JwtAuthGuard } from 'src/user/jwt.guard';
|
||||||
|
|
||||||
@Controller('operations')
|
@Controller('operations')
|
||||||
export class OperationsController{
|
export class OperationsController {
|
||||||
constructor(private readonly operationsService:OperationsService){}
|
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()
|
@UseGuards(JwtAuthGuard)
|
||||||
async Login(@Body() data:chargePrintDto) {
|
@Post('receipt')
|
||||||
return this.operationsService.chargePrint(data,data.id_cuenta);
|
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 { OperationsService } from './operations.services';
|
||||||
import { DetalleServicioModule } from 'src/detalle_servicio/detalle_servicio.module';
|
import { DetalleServicioModule } from 'src/detalle_servicio/detalle_servicio.module';
|
||||||
import { AlumnoModule } from 'src/alumno/student.module';
|
import { AlumnoModule } from 'src/alumno/student.module';
|
||||||
|
import { UserModule } from 'src/user/user.module';
|
||||||
|
import { ReciboModule } from 'src/recibo/recibo.module';
|
||||||
|
|
||||||
@Module({
|
@Module({
|
||||||
imports: [
|
imports: [
|
||||||
TypeOrmModule.forFeature([Alumno, DetalleServicio]),
|
TypeOrmModule.forFeature([Alumno, DetalleServicio]),
|
||||||
DetalleServicioModule,
|
DetalleServicioModule,
|
||||||
AlumnoModule,
|
AlumnoModule,
|
||||||
|
UserModule,
|
||||||
|
ReciboModule,
|
||||||
],
|
],
|
||||||
controllers: [OperationsController],
|
controllers: [OperationsController],
|
||||||
providers: [OperationsService],
|
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 { AlumnoService } from 'src/alumno/student.service';
|
||||||
import { DetalleServicioService } from 'src/detalle_servicio/detalle_servicio.service';
|
import { DetalleServicioService } from 'src/detalle_servicio/detalle_servicio.service';
|
||||||
import { DataSource } from 'typeorm';
|
import { DataSource } from 'typeorm';
|
||||||
import { chargePrintDto } from './dto/operations.dto';
|
import { chargePrintDto } from './dto/operations.dto';
|
||||||
import { CreateDetalleServicioDto } from 'src/detalle_servicio/dto/create-detalle_servicio.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()
|
@Injectable()
|
||||||
export class OperationsService {
|
export class OperationsService {
|
||||||
@@ -11,9 +19,11 @@ export class OperationsService {
|
|||||||
private readonly dataSource: DataSource,
|
private readonly dataSource: DataSource,
|
||||||
private readonly alumnoService: AlumnoService,
|
private readonly alumnoService: AlumnoService,
|
||||||
private readonly detalleServicioService: DetalleServicioService,
|
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 { monto, id_cuenta } = data;
|
||||||
|
|
||||||
const queryRunner = this.dataSource.createQueryRunner();
|
const queryRunner = this.dataSource.createQueryRunner();
|
||||||
@@ -27,10 +37,14 @@ export class OperationsService {
|
|||||||
throw new BadRequestException('Crédito insuficiente');
|
throw new BadRequestException('Crédito insuficiente');
|
||||||
}
|
}
|
||||||
|
|
||||||
const detalleData: CreateDetalleServicioDto = {
|
const user = await this.userService.findOne(id_usuario);
|
||||||
...data,
|
const alum = await this.alumnoService.findOne(id_cuenta)
|
||||||
id_servicio: 1,
|
|
||||||
id_usuario: userId,
|
const detalleData = {
|
||||||
|
servicio: 1,
|
||||||
|
user,
|
||||||
|
alum,
|
||||||
|
monto,
|
||||||
fecha_operacion: new Date(),
|
fecha_operacion: new Date(),
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -39,7 +53,7 @@ export class OperationsService {
|
|||||||
queryRunner.manager,
|
queryRunner.manager,
|
||||||
);
|
);
|
||||||
|
|
||||||
await this.alumnoService.UpdateCredit(
|
await this.alumnoService.collectCredit(
|
||||||
id_cuenta,
|
id_cuenta,
|
||||||
monto,
|
monto,
|
||||||
queryRunner.manager,
|
queryRunner.manager,
|
||||||
@@ -54,4 +68,37 @@ export class OperationsService {
|
|||||||
await queryRunner.release();
|
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 })
|
@Column({ name: 'generacion', type: 'int', width: 4, nullable: true })
|
||||||
generacion: number | null;
|
generacion: number | null;
|
||||||
|
|
||||||
@OneToMany(() => DetalleServicio, (detalle) => detalle.id_cuenta)
|
@OneToMany(() => DetalleServicio, (detalle) => detalle.alum)
|
||||||
detalles_servicio: DetalleServicio[];
|
detalles_servicio: DetalleServicio[];
|
||||||
|
|
||||||
@ManyToOne(() => Carrera, (carrera) => carrera.estudiantes, {
|
@ManyToOne(() => Carrera, (carrera) => carrera.estudiantes, {
|
||||||
|
|||||||
@@ -36,7 +36,7 @@ export class AlumnoService {
|
|||||||
return student.credito;
|
return student.credito;
|
||||||
}
|
}
|
||||||
|
|
||||||
async UpdateCredit(
|
async collectCredit(
|
||||||
id_cuenta: number,
|
id_cuenta: number,
|
||||||
credit: number,
|
credit: number,
|
||||||
manager: EntityManager,
|
manager: EntityManager,
|
||||||
@@ -49,5 +49,19 @@ export class AlumnoService {
|
|||||||
.where({ id_cuenta })
|
.where({ id_cuenta })
|
||||||
.execute();
|
.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
|
//IO
|
||||||
@@ -5,7 +5,7 @@ import { CreateDetalleServicioDto } from './dto/create-detalle_servicio.dto';
|
|||||||
|
|
||||||
@Injectable()
|
@Injectable()
|
||||||
export class DetalleServicioService {
|
export class DetalleServicioService {
|
||||||
async Create(data: CreateDetalleServicioDto, manager: EntityManager) {
|
async Create(data, manager: EntityManager) {
|
||||||
const repo = manager.getRepository(DetalleServicio);
|
const repo = manager.getRepository(DetalleServicio);
|
||||||
const details = repo.create(data);
|
const details = repo.create(data);
|
||||||
return await repo.save(details);
|
return await repo.save(details);
|
||||||
|
|||||||
@@ -38,17 +38,11 @@ export class DetalleServicio {
|
|||||||
})
|
})
|
||||||
fecha_operacion: Date;
|
fecha_operacion: Date;
|
||||||
|
|
||||||
@Column()
|
@ManyToOne(() => Alumno, (alum) => alum.detalles_servicio, {
|
||||||
id_cuenta:number
|
|
||||||
|
|
||||||
@Column()
|
|
||||||
id_servicio:number
|
|
||||||
|
|
||||||
@ManyToOne(() => Alumno, (id_cuenta) => id_cuenta.detalles_servicio, {
|
|
||||||
eager: true,
|
eager: true,
|
||||||
})
|
})
|
||||||
@JoinColumn({ name: 'id_cuenta' })
|
@JoinColumn({ name: 'id_cuenta' })
|
||||||
cuenta: Alumno;
|
alum: Alumno;
|
||||||
|
|
||||||
@ManyToOne(() => Servicio, (id_servicio) => id_servicio.detalles_servicio, {
|
@ManyToOne(() => Servicio, (id_servicio) => id_servicio.detalles_servicio, {
|
||||||
eager: true,
|
eager: true,
|
||||||
@@ -56,16 +50,16 @@ export class DetalleServicio {
|
|||||||
@JoinColumn({ name: 'id_servicio' })
|
@JoinColumn({ name: 'id_servicio' })
|
||||||
servicio: Servicio;
|
servicio: Servicio;
|
||||||
|
|
||||||
@ManyToOne(() => User, (id_perfil) => id_perfil.detalles_servicio, {
|
@ManyToOne(() => User, (user) => user.detalles_servicio, {
|
||||||
eager: true,
|
eager: true,
|
||||||
})
|
})
|
||||||
@JoinColumn({ name: 'id_usuario' })
|
@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,
|
eager: true,
|
||||||
nullable: true,
|
nullable: true,
|
||||||
})
|
})
|
||||||
@JoinColumn({ name: 'id_periodo' })
|
@JoinColumn({ name: 'id_periodo' })
|
||||||
id_periodo: Periodo;
|
periodo: Periodo;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -24,7 +24,7 @@ export class Periodo {
|
|||||||
|
|
||||||
@OneToMany(
|
@OneToMany(
|
||||||
() => DetalleServicio,
|
() => DetalleServicio,
|
||||||
(id_detalle_servicio) => id_detalle_servicio.id_periodo,
|
(id_detalle_servicio) => id_detalle_servicio.periodo,
|
||||||
)
|
)
|
||||||
detalles_servicio: DetalleServicio[];
|
detalles_servicio: DetalleServicio[];
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -2,8 +2,6 @@ import { Type } from 'class-transformer';
|
|||||||
import { IsDate, IsNumber, IsString } from 'class-validator';
|
import { IsDate, IsNumber, IsString } from 'class-validator';
|
||||||
|
|
||||||
export class CreateReciboDto {
|
export class CreateReciboDto {
|
||||||
@IsNumber()
|
|
||||||
id_cuenta: number;
|
|
||||||
|
|
||||||
@IsNumber()
|
@IsNumber()
|
||||||
monto: number;
|
monto: number;
|
||||||
|
|||||||
@@ -6,18 +6,10 @@ import {
|
|||||||
Req,
|
Req,
|
||||||
} from '@nestjs/common';
|
} from '@nestjs/common';
|
||||||
import { ReciboService } from './recibo.service';
|
import { ReciboService } from './recibo.service';
|
||||||
import { CreateReciboDto } from './dto/create-recibo.dto';
|
|
||||||
import { JwtAuthGuard } from 'src/user/jwt.guard';
|
|
||||||
|
|
||||||
@Controller('recibo')
|
@Controller('recibo')
|
||||||
export class ReciboController {
|
export class ReciboController {
|
||||||
constructor(private readonly reciboService: ReciboService) {}
|
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
|
//IO
|
||||||
|
|||||||
@@ -10,6 +10,7 @@ import { UserModule } from 'src/user/user.module';
|
|||||||
imports: [TypeOrmModule.forFeature([Recibo]), AlumnoModule, UserModule],
|
imports: [TypeOrmModule.forFeature([Recibo]), AlumnoModule, UserModule],
|
||||||
controllers: [ReciboController],
|
controllers: [ReciboController],
|
||||||
providers: [ReciboService],
|
providers: [ReciboService],
|
||||||
|
exports:[ReciboService]
|
||||||
})
|
})
|
||||||
export class ReciboModule {}
|
export class ReciboModule {}
|
||||||
//IO
|
//IO
|
||||||
|
|||||||
@@ -1,20 +1,16 @@
|
|||||||
import { ConflictException, Injectable } from '@nestjs/common';
|
import { ConflictException, Injectable } from '@nestjs/common';
|
||||||
import { InjectRepository } from '@nestjs/typeorm';
|
import { InjectRepository } from '@nestjs/typeorm';
|
||||||
import { Recibo } from './entities/recibo.entity';
|
import { Recibo } from './entities/recibo.entity';
|
||||||
import { Repository } from 'typeorm';
|
import { EntityManager, Repository } from 'typeorm';
|
||||||
import { CreateReciboDto } from './dto/create-recibo.dto';
|
import { CreateReciboDto } from './dto/create-recibo.dto';
|
||||||
import { AlumnoService } from 'src/alumno/student.service';
|
|
||||||
import { UserService } from 'src/user/user.service';
|
|
||||||
|
|
||||||
@Injectable()
|
@Injectable()
|
||||||
export class ReciboService {
|
export class ReciboService {
|
||||||
constructor(
|
constructor(
|
||||||
@InjectRepository(Recibo)
|
@InjectRepository(Recibo)
|
||||||
private readonly reciboRepository:Repository<Recibo>,
|
private readonly reciboRepository: Repository<Recibo>,
|
||||||
private readonly alumnoService:AlumnoService,
|
) {}
|
||||||
private readonly usuarioService:UserService,
|
|
||||||
){}
|
|
||||||
|
|
||||||
async findOne(folio_recibo: string) {
|
async findOne(folio_recibo: string) {
|
||||||
const recibo = await this.reciboRepository.findOne({
|
const recibo = await this.reciboRepository.findOne({
|
||||||
where: { folio_recibo },
|
where: { folio_recibo },
|
||||||
@@ -22,29 +18,10 @@ export class ReciboService {
|
|||||||
return recibo;
|
return recibo;
|
||||||
}
|
}
|
||||||
|
|
||||||
async create(data: CreateReciboDto, id_usuario: number) {
|
async create(data: CreateReciboDto, manager: EntityManager) {
|
||||||
const {id_cuenta,folio_recibo} = data
|
const repo = manager.getRepository(Recibo);
|
||||||
const receipt = await this.findOne(folio_recibo)
|
const details = repo.create(data);
|
||||||
|
return await repo.save(details);
|
||||||
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;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
//IO
|
//IO
|
||||||
|
|||||||
@@ -11,7 +11,7 @@ export class Servicio {
|
|||||||
|
|
||||||
@OneToMany(
|
@OneToMany(
|
||||||
() => DetalleServicio,
|
() => DetalleServicio,
|
||||||
(id_detalle_servicio) => id_detalle_servicio.id_servicio,
|
(id_detalle_servicio) => id_detalle_servicio.servicio,
|
||||||
)
|
)
|
||||||
detalles_servicio: DetalleServicio[];
|
detalles_servicio: DetalleServicio[];
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -56,7 +56,7 @@ export class User {
|
|||||||
|
|
||||||
@OneToMany(
|
@OneToMany(
|
||||||
() => DetalleServicio,
|
() => DetalleServicio,
|
||||||
(id_detalle_servicio) => id_detalle_servicio.id_perfil,
|
(id_detalle_servicio) => id_detalle_servicio.user,
|
||||||
)
|
)
|
||||||
detalles_servicio: DetalleServicio[];
|
detalles_servicio: DetalleServicio[];
|
||||||
|
|
||||||
|
|||||||
@@ -14,7 +14,6 @@ export class JwtStrategy extends PassportStrategy(Strategy) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
async validate(payload: any) {
|
async validate(payload: any) {
|
||||||
console.log('payload:', payload);
|
|
||||||
return { id: payload.id, usuario: payload.usuario };
|
return { id: payload.id, usuario: payload.usuario };
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user