124 lines
3.5 KiB
TypeScript
124 lines
3.5 KiB
TypeScript
import { ConflictException, forwardRef, Inject, Injectable, NotFoundException } from '@nestjs/common';
|
|
import { CreateStudentDto } from './dto/create-student.dto';
|
|
import { Alumno } from './entities/student.entity';
|
|
import { EntityManager, Repository } from 'typeorm';
|
|
import { InjectRepository } from '@nestjs/typeorm';
|
|
import { Carrera } from 'src/carrera/entities/carrera.entity';
|
|
import { AlumnoSancionService } from 'src/alumno_sancion/alumno_sancion.service';
|
|
import { ExceptionsHandler } from '@nestjs/core/exceptions/exceptions-handler';
|
|
|
|
@Injectable()
|
|
export class AlumnoService {
|
|
constructor(
|
|
@InjectRepository(Alumno)
|
|
private readonly studentRepository: Repository<Alumno>,
|
|
@InjectRepository(Carrera)
|
|
private readonly carreraRepository: Repository<Carrera>,
|
|
|
|
@Inject(forwardRef(() => AlumnoSancionService))
|
|
private readonly alumnoSancionService: AlumnoSancionService,
|
|
) { }
|
|
|
|
async findAll(): Promise<Alumno[]> {
|
|
return this.studentRepository.find({
|
|
take: 100,
|
|
});
|
|
}
|
|
|
|
async create(data: CreateStudentDto): Promise<Alumno> {
|
|
const { id_carrera, ...rest } = data;
|
|
|
|
const carrera = await this.carreraRepository.findOne({
|
|
where: { id_carrera: data.id_carrera },
|
|
});
|
|
|
|
if (!carrera) {
|
|
throw new NotFoundException(`Carrera not found`);
|
|
}
|
|
|
|
const currenStudent = await this.studentRepository.findOne({where:{id_cuenta:data.id_cuenta}})
|
|
|
|
if(currenStudent){
|
|
throw new ConflictException('El usuario ya existe')
|
|
}
|
|
|
|
const createStudent = { ...rest, carrera, fecha_registro: new Date() };
|
|
|
|
const student = this.studentRepository.create(createStudent);
|
|
return await this.studentRepository.save(student);
|
|
}
|
|
|
|
async findOne(id_cuenta: number): Promise<Alumno> {
|
|
const student = await this.studentRepository.findOne({
|
|
where: { id_cuenta },
|
|
select: { id_cuenta: true, nombre: true, credito: true },
|
|
});
|
|
|
|
if (!student) {
|
|
throw new NotFoundException(`Numero de cuenta ${id_cuenta} no existente`);
|
|
}
|
|
|
|
return student;
|
|
}
|
|
|
|
async findOneWhitSancion(id_cuenta: number): Promise<Alumno> {
|
|
const student = await this.studentRepository.findOne({
|
|
where: { id_cuenta },
|
|
select: { id_cuenta: true, nombre: true, credito: true },
|
|
});
|
|
|
|
if (!student) {
|
|
throw new NotFoundException(`Numero de cuenta ${id_cuenta} no existente`);
|
|
}
|
|
const sancion = await this.alumnoSancionService.findSancion(id_cuenta)
|
|
|
|
if (sancion) {
|
|
throw new NotFoundException('El usuario se encuentra sancionado')
|
|
}
|
|
|
|
return student;
|
|
}
|
|
|
|
async findOneByName(nombre: string): Promise<Alumno> {
|
|
const student = await this.studentRepository.findOne({
|
|
where: { nombre },
|
|
});
|
|
|
|
if (!student) {
|
|
throw new NotFoundException(`Student not found`);
|
|
}
|
|
|
|
return student;
|
|
}
|
|
|
|
async GetCredit(id_cuenta: number): Promise<Alumno['credito']> {
|
|
const student = await this.findOne(id_cuenta);
|
|
return student.credito;
|
|
}
|
|
|
|
async collectCredit(
|
|
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();
|
|
}
|
|
|
|
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
|