added modified programs
This commit is contained in:
@@ -1,5 +1,6 @@
|
|||||||
import {
|
import {
|
||||||
BadRequestException,
|
BadRequestException,
|
||||||
|
HttpException,
|
||||||
Injectable,
|
Injectable,
|
||||||
InternalServerErrorException,
|
InternalServerErrorException,
|
||||||
} from '@nestjs/common';
|
} from '@nestjs/common';
|
||||||
@@ -65,7 +66,12 @@ export class OperationsService {
|
|||||||
return { message: 'correct' };
|
return { message: 'correct' };
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
await queryRunner.rollbackTransaction();
|
await queryRunner.rollbackTransaction();
|
||||||
throw new InternalServerErrorException(error.message);
|
|
||||||
|
if (error instanceof HttpException) {
|
||||||
|
throw error;
|
||||||
|
}
|
||||||
|
|
||||||
|
throw new InternalServerErrorException('Error al realizar el cobro');
|
||||||
} finally {
|
} finally {
|
||||||
await queryRunner.release();
|
await queryRunner.release();
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,4 +1,11 @@
|
|||||||
import { PartialType } from '@nestjs/mapped-types';
|
import { PartialType } from '@nestjs/mapped-types';
|
||||||
import { CreateProgramaEquipoDto } from './create-programa_equipo.dto';
|
import { CreateProgramaEquipoDto } from './create-programa_equipo.dto';
|
||||||
|
|
||||||
export class UpdateProgramaEquipoDto extends PartialType(CreateProgramaEquipoDto) {}
|
export class UpdateProgramaEquipoDto {
|
||||||
|
programas: number[];
|
||||||
|
}
|
||||||
|
|
||||||
|
export class UpdateProgramasSalaDto {
|
||||||
|
sala: number;
|
||||||
|
programas: number[];
|
||||||
|
}
|
||||||
@@ -1,6 +1,6 @@
|
|||||||
import { Controller, Get, Body, Patch, Param } from '@nestjs/common';
|
import { Controller, Get, Body, Patch, Param } from '@nestjs/common';
|
||||||
import { ProgramaEquipoService } from './programa_equipo.service';
|
import { ProgramaEquipoService } from './programa_equipo.service';
|
||||||
import { UpdateProgramaEquipoDto } from './dto/update-programa_equipo.dto';
|
import { UpdateProgramaEquipoDto, UpdateProgramasSalaDto } from './dto/update-programa_equipo.dto';
|
||||||
|
|
||||||
@Controller('programa-equipo')
|
@Controller('programa-equipo')
|
||||||
export class ProgramaEquipoController {
|
export class ProgramaEquipoController {
|
||||||
@@ -11,16 +11,29 @@ export class ProgramaEquipoController {
|
|||||||
return this.programaEquipoService.findAllProgramByNumber(+id);
|
return this.programaEquipoService.findAllProgramByNumber(+id);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Get('ubicacion/:id')
|
||||||
|
findAllProgramByUbicacion(@Param('id') id: string) {
|
||||||
|
return this.programaEquipoService.findAllProgramByUbication(id);
|
||||||
|
}
|
||||||
|
|
||||||
@Get(':id')
|
@Get(':id')
|
||||||
findOne(@Param('id') id: string) {
|
findOne(@Param('id') id: string) {
|
||||||
return this.programaEquipoService.findOne(id);
|
return this.programaEquipoService.findOne(id);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Patch(':id')
|
@Patch('equipo/:id')
|
||||||
update(
|
updateMachine(
|
||||||
@Param('id') id: string,
|
@Param('id') id: string,
|
||||||
@Body() updateProgramaEquipoDto: UpdateProgramaEquipoDto,
|
@Body() updateProgramaEquipoDto: UpdateProgramaEquipoDto,
|
||||||
) {
|
) {
|
||||||
return this.programaEquipoService.update(+id, updateProgramaEquipoDto);
|
return this.programaEquipoService.updateMachine(
|
||||||
|
Number(id),
|
||||||
|
updateProgramaEquipoDto.programas,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Patch('sala')
|
||||||
|
updateBySala(@Body() dto: UpdateProgramasSalaDto) {
|
||||||
|
return this.programaEquipoService.updateBySala(dto.sala, dto.programas);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -3,9 +3,10 @@ import { ProgramaEquipoService } from './programa_equipo.service';
|
|||||||
import { ProgramaEquipoController } from './programa_equipo.controller';
|
import { ProgramaEquipoController } from './programa_equipo.controller';
|
||||||
import { TypeOrmModule } from '@nestjs/typeorm';
|
import { TypeOrmModule } from '@nestjs/typeorm';
|
||||||
import { ProgramaEquipo } from './entities/programa_equipo.entity';
|
import { ProgramaEquipo } from './entities/programa_equipo.entity';
|
||||||
|
import { Equipo } from 'src/equipo/entities/equipo.entity';
|
||||||
|
|
||||||
@Module({
|
@Module({
|
||||||
imports:[TypeOrmModule.forFeature([ProgramaEquipo])],
|
imports:[TypeOrmModule.forFeature([ProgramaEquipo,Equipo])],
|
||||||
controllers: [ProgramaEquipoController],
|
controllers: [ProgramaEquipoController],
|
||||||
providers: [ProgramaEquipoService],
|
providers: [ProgramaEquipoService],
|
||||||
})
|
})
|
||||||
|
|||||||
@@ -1,24 +1,29 @@
|
|||||||
import { Injectable, NotFoundException } from '@nestjs/common';
|
import { Injectable, NotFoundException } from '@nestjs/common';
|
||||||
import { CreateProgramaEquipoDto } from './dto/create-programa_equipo.dto';
|
|
||||||
import { UpdateProgramaEquipoDto } from './dto/update-programa_equipo.dto';
|
|
||||||
import { InjectRepository } from '@nestjs/typeorm';
|
import { InjectRepository } from '@nestjs/typeorm';
|
||||||
import { ProgramaEquipo } from './entities/programa_equipo.entity';
|
import { ProgramaEquipo } from './entities/programa_equipo.entity';
|
||||||
import { Repository } from 'typeorm';
|
import { In, Repository } from 'typeorm';
|
||||||
|
import { DataSource } from 'typeorm';
|
||||||
|
import { Equipo } from 'src/equipo/entities/equipo.entity';
|
||||||
|
|
||||||
@Injectable()
|
@Injectable()
|
||||||
export class ProgramaEquipoService {
|
export class ProgramaEquipoService {
|
||||||
constructor(
|
constructor(
|
||||||
@InjectRepository(ProgramaEquipo)
|
@InjectRepository(ProgramaEquipo)
|
||||||
private readonly ProgramaEquipoRepository: Repository<ProgramaEquipo>,
|
private readonly programaEquipoRepository: Repository<ProgramaEquipo>,
|
||||||
|
private readonly dataSource: DataSource,
|
||||||
) {}
|
) {}
|
||||||
|
|
||||||
findAllProgramByNumber(id_equipo: number) {
|
findAllProgramByNumber(id_equipo: number) {
|
||||||
return this.ProgramaEquipoRepository.find({ where: { id_equipo } });
|
return this.programaEquipoRepository.find({ where: { id_equipo } });
|
||||||
|
}
|
||||||
|
|
||||||
|
findAllProgramByUbication(ubicacion: string) {
|
||||||
|
return this.programaEquipoRepository.find();
|
||||||
}
|
}
|
||||||
|
|
||||||
async findOne(ubicacion: string) {
|
async findOne(ubicacion: string) {
|
||||||
const equipo = await this.ProgramaEquipoRepository.find({
|
const equipo = await this.programaEquipoRepository.find({
|
||||||
where: { equipo:{ubicacion} },
|
where: { equipo: { ubicacion } },
|
||||||
});
|
});
|
||||||
|
|
||||||
if (equipo.length === 0) {
|
if (equipo.length === 0) {
|
||||||
@@ -28,7 +33,104 @@ export class ProgramaEquipoService {
|
|||||||
return equipo;
|
return equipo;
|
||||||
}
|
}
|
||||||
|
|
||||||
update(id: number, updateProgramaEquipoDto: UpdateProgramaEquipoDto) {
|
async updateMachine(id_equipo: number, programasSeleccionados: number[]) {
|
||||||
return `This action updates a #${id} programaEquipo`;
|
const actuales = await this.programaEquipoRepository.find({
|
||||||
|
where: { id_equipo },
|
||||||
|
});
|
||||||
|
|
||||||
|
const actualesIds = actuales.map((p) => p.id_programa);
|
||||||
|
|
||||||
|
const aCrear = programasSeleccionados.filter(
|
||||||
|
(id) => !actualesIds.includes(id),
|
||||||
|
);
|
||||||
|
|
||||||
|
const aEliminar = actualesIds.filter(
|
||||||
|
(id) => !programasSeleccionados.includes(id),
|
||||||
|
);
|
||||||
|
|
||||||
|
if (aCrear.length > 0) {
|
||||||
|
const nuevos = aCrear.map((id_programa) =>
|
||||||
|
this.programaEquipoRepository.create({
|
||||||
|
id_equipo,
|
||||||
|
id_programa,
|
||||||
|
}),
|
||||||
|
);
|
||||||
|
await this.programaEquipoRepository.save(nuevos);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (aEliminar.length > 0) {
|
||||||
|
await this.programaEquipoRepository.delete({
|
||||||
|
id_equipo,
|
||||||
|
id_programa: In(aEliminar),
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
return {
|
||||||
|
message: 'Programas del equipo actualizados correctamente',
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
async updateBySala(
|
||||||
|
id_area_ubicacion: number,
|
||||||
|
programasSeleccionados: number[],
|
||||||
|
) {
|
||||||
|
const queryRunner = this.dataSource.createQueryRunner();
|
||||||
|
await queryRunner.connect();
|
||||||
|
await queryRunner.startTransaction();
|
||||||
|
|
||||||
|
try {
|
||||||
|
const equipos = await queryRunner.manager.find(Equipo, {
|
||||||
|
where: { id_area_ubicacion },
|
||||||
|
});
|
||||||
|
|
||||||
|
if (equipos.length === 0) {
|
||||||
|
throw new Error('No hay equipos en la sala');
|
||||||
|
}
|
||||||
|
|
||||||
|
for (const equipo of equipos) {
|
||||||
|
const actuales = await queryRunner.manager.find(ProgramaEquipo, {
|
||||||
|
where: { id_equipo: equipo.id_equipo },
|
||||||
|
});
|
||||||
|
|
||||||
|
const actualesIds = actuales.map(p => p.id_programa);
|
||||||
|
|
||||||
|
const aCrear = programasSeleccionados.filter(
|
||||||
|
id => !actualesIds.includes(id),
|
||||||
|
);
|
||||||
|
|
||||||
|
const aEliminar = actualesIds.filter(
|
||||||
|
id => !programasSeleccionados.includes(id),
|
||||||
|
);
|
||||||
|
|
||||||
|
if (aCrear.length > 0) {
|
||||||
|
const nuevos = aCrear.map(id_programa =>
|
||||||
|
queryRunner.manager.create(ProgramaEquipo, {
|
||||||
|
id_equipo: equipo.id_equipo,
|
||||||
|
id_programa,
|
||||||
|
}),
|
||||||
|
);
|
||||||
|
await queryRunner.manager.save(nuevos);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (aEliminar.length > 0) {
|
||||||
|
await queryRunner.manager.delete(ProgramaEquipo, {
|
||||||
|
id_equipo: equipo.id_equipo,
|
||||||
|
id_programa: In(aEliminar),
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
await queryRunner.commitTransaction();
|
||||||
|
|
||||||
|
return {
|
||||||
|
message: 'Programas actualizados correctamente en la sala',
|
||||||
|
equiposActualizados: equipos.length,
|
||||||
|
};
|
||||||
|
} catch (error) {
|
||||||
|
await queryRunner.rollbackTransaction();
|
||||||
|
throw error;
|
||||||
|
} finally {
|
||||||
|
await queryRunner.release();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user