119 lines
3.0 KiB
TypeScript
119 lines
3.0 KiB
TypeScript
import { BadRequestException, Injectable, NotFoundException } from '@nestjs/common';
|
|
import { InjectRepository } from '@nestjs/typeorm';
|
|
import { Periodo } from './entities/periodo.entity';
|
|
import { Between, Repository } from 'typeorm';
|
|
import { CreatePeriodoDto, ModifiedDateDto } from './dto/create-periodo.dto';
|
|
|
|
@Injectable()
|
|
export class PeriodoService {
|
|
constructor(
|
|
@InjectRepository(Periodo)
|
|
private readonly periodoRepository: Repository<Periodo>,
|
|
) { }
|
|
|
|
async findOne(semestre: string) {
|
|
return this.periodoRepository.findOne({ where: { semestre } });
|
|
}
|
|
|
|
async findAll() {
|
|
return this.periodoRepository.find({ order: { 'id_periodo': "DESC" } });
|
|
}
|
|
|
|
async getPeriodoActivo(): Promise<Periodo> {
|
|
const periodo = await this.periodoRepository.findOne({
|
|
where: { activo: true },
|
|
order: { id_periodo: 'DESC' },
|
|
});
|
|
|
|
if (!periodo) {
|
|
throw new NotFoundException('No hay periodos activos');
|
|
}
|
|
|
|
return periodo;
|
|
}
|
|
|
|
async getPeriodoActivoWhitoutError(): Promise<Periodo | null> {
|
|
const periodo = await this.periodoRepository.findOne({
|
|
where: { activo: true },
|
|
order: { id_periodo: 'DESC' },
|
|
});
|
|
|
|
return periodo;
|
|
}
|
|
|
|
async delete(id_periodo: number) {
|
|
await this.periodoRepository.delete(id_periodo)
|
|
}
|
|
|
|
async periodoDisable(id_periodo: number) {
|
|
await this.periodoRepository.update(id_periodo, { activo: false })
|
|
}
|
|
|
|
async create(data: CreatePeriodoDto) {
|
|
const periodo = await this.findOne(data.semestre)
|
|
|
|
if (periodo) {
|
|
throw new BadRequestException("El periodo ya existe")
|
|
}
|
|
|
|
const periodoActive = await this.getPeriodoActivoWhitoutError();
|
|
|
|
if (periodoActive) {
|
|
await this.periodoDisable(periodoActive.id_periodo)
|
|
}
|
|
|
|
const create = this.periodoRepository.create(data)
|
|
return this.periodoRepository.save(create)
|
|
}
|
|
|
|
async modifiedDate(data: ModifiedDateDto, id_periodo) {
|
|
return this.periodoRepository.update(id_periodo, data)
|
|
}
|
|
|
|
async comparePeriodos(id1: number, id2: number) {
|
|
const periodo1 = await this.periodoRepository.findOne({
|
|
where: { id_periodo: id1 },
|
|
});
|
|
|
|
const periodo2 = await this.periodoRepository.findOne({
|
|
where: { id_periodo: id2 },
|
|
});
|
|
|
|
if (!periodo1 || !periodo2) {
|
|
throw new NotFoundException('Uno de los periodos no existe');
|
|
}
|
|
|
|
return {
|
|
periodo1: {
|
|
id: periodo1.id_periodo,
|
|
nombre: periodo1.semestre,
|
|
fecha_inicio: periodo1.fecha_inicio_servicio,
|
|
fecha_fin: periodo1.fecha_fin_servicio,
|
|
},
|
|
periodo2: {
|
|
id: periodo2.id_periodo,
|
|
nombre: periodo2.semestre,
|
|
fecha_inicio: periodo2.fecha_inicio_servicio,
|
|
fecha_fin: periodo2.fecha_fin_servicio,
|
|
},
|
|
};
|
|
}
|
|
|
|
async getPeriodosEnRango(id1: number, id2: number) {
|
|
const min = Math.min(id1, id2);
|
|
const max = Math.max(id1, id2);
|
|
|
|
return this.periodoRepository.find({
|
|
where: {
|
|
id_periodo: Between(min, max),
|
|
},
|
|
order: {
|
|
id_periodo: 'ASC',
|
|
},
|
|
});
|
|
}
|
|
|
|
}
|
|
|
|
|