endpoint equipos programa validados por token
This commit is contained in:
parent
94243e544e
commit
35432e5406
@ -1,6 +1,15 @@
|
||||
import { Body, Controller, Delete, Post, UseGuards } from '@nestjs/common';
|
||||
import {
|
||||
Body,
|
||||
Controller,
|
||||
Delete,
|
||||
ForbiddenException,
|
||||
Post,
|
||||
Request,
|
||||
UseGuards,
|
||||
} from '@nestjs/common';
|
||||
import { AuthGuard } from '@nestjs/passport';
|
||||
import { ApiBearerAuth, ApiBody, ApiOperation, ApiTags } from '@nestjs/swagger';
|
||||
import { Operador } from 'src/operador/entity/operador.entity';
|
||||
import { EquipoProgramaService } from './equipo-programa.service';
|
||||
import { CreateEquipoProgramaDto } from './dto/input/create.dto';
|
||||
import { DeleteEquipoProgramaDto } from './dto/input/delete.dto';
|
||||
@ -36,7 +45,17 @@ export class EquipoProgramaController {
|
||||
description: 'La variable id_equipo_programa es obligatoria.',
|
||||
examples: { ejemplo: { value: { id_equipo_programa: 1 } } },
|
||||
})
|
||||
delete(@Body() body: DeleteEquipoProgramaDto) {
|
||||
return this.equipoProgramaService.delete(body.id_equipo_programa);
|
||||
delete(@Request() req, @Body() body: DeleteEquipoProgramaDto) {
|
||||
const operador: Operador = req.user.operador;
|
||||
|
||||
if (
|
||||
!operador ||
|
||||
(operador.tipoUsuario.id_tipo_usuario != 3 &&
|
||||
operador.tipoUsuario.id_tipo_usuario != 4)
|
||||
)
|
||||
throw new ForbiddenException(
|
||||
'No tienes los permisos necesarios para realizar esta acción.',
|
||||
);
|
||||
return this.equipoProgramaService.delete(operador, body.id_equipo_programa);
|
||||
}
|
||||
}
|
||||
|
@ -1,5 +1,6 @@
|
||||
import {
|
||||
ConflictException,
|
||||
ForbiddenException,
|
||||
forwardRef,
|
||||
Inject,
|
||||
Injectable,
|
||||
@ -11,6 +12,7 @@ import { EquipoPrograma } from './entity/equipo-programa.entity';
|
||||
import { Programa } from '../institucion-programa/entity/programa.entity';
|
||||
import { EquipoService } from '../equipo/equipo.service';
|
||||
import { InstitucionProgramaService } from '../institucion-programa/institucion-programa.service';
|
||||
import { Operador } from 'src/operador/entity/operador.entity';
|
||||
|
||||
@Injectable()
|
||||
export class EquipoProgramaService {
|
||||
@ -22,11 +24,14 @@ export class EquipoProgramaService {
|
||||
private institucionProgramaService: InstitucionProgramaService,
|
||||
) {}
|
||||
|
||||
countProgramasEquipo(equipo: Equipo) {
|
||||
return this.repository.count({ equipo });
|
||||
}
|
||||
|
||||
async create(id_equipo: number | Equipo, id_programa: number | Programa) {
|
||||
async create(
|
||||
// operador: Operador,
|
||||
id_equipo: number | Equipo,
|
||||
id_programa: number | Programa,
|
||||
) {
|
||||
const sinPrograma = await this.institucionProgramaService.findProgramaById(
|
||||
1,
|
||||
);
|
||||
const equipo =
|
||||
typeof id_equipo === 'number'
|
||||
? await this.equipoService.findById(id_equipo)
|
||||
@ -38,53 +43,64 @@ export class EquipoProgramaService {
|
||||
|
||||
return this.findEquipoProgramaByEquipoPrograma(equipo, programa)
|
||||
.then((_) =>
|
||||
this.repository.save(this.repository.create({ equipo, programa })),
|
||||
this.findEquipoProgramaByEquipoPrograma(equipo, sinPrograma, false),
|
||||
)
|
||||
.then(async (existeEquipoSinPrograma) => {
|
||||
if (existeEquipoSinPrograma)
|
||||
await this.repository.remove(existeEquipoSinPrograma);
|
||||
return this.repository.save(
|
||||
this.repository.create({ equipo, programa }),
|
||||
);
|
||||
})
|
||||
.then((equipoPrograma) => ({
|
||||
message: `Se asignó el programa: ${equipoPrograma.programa.programa}, al equipo con número de inventario: ${equipo.numero_inventario}.`,
|
||||
message: `Se asignó el software: ${equipoPrograma.programa.programa}, al equipo con número de inventario: ${equipo.numero_inventario}.`,
|
||||
equipoPrograma,
|
||||
}));
|
||||
}
|
||||
|
||||
async delete(id_equipo_programa: number | EquipoPrograma) {
|
||||
const equipoPrograma =
|
||||
typeof id_equipo_programa === 'number'
|
||||
? await this.findById(id_equipo_programa)
|
||||
: id_equipo_programa;
|
||||
async delete(operador: Operador, id_equipo_programa: number) {
|
||||
const equipoPrograma = await this.findById(id_equipo_programa);
|
||||
|
||||
if (typeof id_equipo_programa === 'number')
|
||||
await this.countProgramasEquipo(equipoPrograma.equipo).then((n) => {
|
||||
if (
|
||||
operador.institucion.id_institucion !=
|
||||
equipoPrograma.equipo.carrito.modulo.institucion.id_institucion
|
||||
)
|
||||
throw new ForbiddenException(
|
||||
'No puedes eliminar el software de este equipo porque no pertenece a tu institución.',
|
||||
);
|
||||
return this.repository
|
||||
.count({ equipo: equipoPrograma.equipo })
|
||||
.then(async (n) => {
|
||||
if (n === 1) {
|
||||
if (equipoPrograma.programa.id_programa === 1)
|
||||
throw new ConflictException('No se puede eliminar esta opción.');
|
||||
return this.create(equipoPrograma.equipo, 1);
|
||||
await this.create(equipoPrograma.equipo, 1);
|
||||
}
|
||||
});
|
||||
return this.repository.remove(equipoPrograma).then((_) => ({
|
||||
message: 'Se eliminó correctamente el programa de este equipo.',
|
||||
}));
|
||||
}
|
||||
|
||||
eliminarEquipoSinPrograma(equipo: Equipo) {
|
||||
return this.institucionProgramaService
|
||||
.findProgramaById(1)
|
||||
.then((programa) =>
|
||||
this.findEquipoProgramaByEquipoPrograma(equipo, programa, false),
|
||||
)
|
||||
.then((equipoPrograma) => {
|
||||
if (equipoPrograma) return this.delete(equipoPrograma);
|
||||
});
|
||||
return this.repository.remove(equipoPrograma);
|
||||
})
|
||||
.then((_) => ({
|
||||
message: 'Se eliminó correctamente el software de este equipo.',
|
||||
}));
|
||||
}
|
||||
|
||||
findById(id_equipo_programa: number) {
|
||||
return this.repository
|
||||
.findOne({
|
||||
join: { alias: 'ep', innerJoinAndSelect: { e: 'ep.equipo' } },
|
||||
join: {
|
||||
alias: 'ep',
|
||||
innerJoinAndSelect: {
|
||||
e: 'ep.equipo',
|
||||
p: 'ep.programa',
|
||||
c: 'e.carrito',
|
||||
m: 'c.modulo',
|
||||
i: 'm.institucion',
|
||||
},
|
||||
},
|
||||
where: { id_equipo_programa },
|
||||
})
|
||||
.then((equipoPrograma) => {
|
||||
if (!equipoPrograma)
|
||||
throw new ConflictException('No existe este id equipo programa.');
|
||||
throw new ConflictException('No existe este id equipo software.');
|
||||
return equipoPrograma;
|
||||
});
|
||||
}
|
||||
@ -99,7 +115,7 @@ export class EquipoProgramaService {
|
||||
.then((equipoPrograma) => {
|
||||
if (validarExistencia && equipoPrograma)
|
||||
throw new ConflictException(
|
||||
'Este programa ya fue asignado a este equipo.',
|
||||
'Este software ya fue asignado a este equipo.',
|
||||
);
|
||||
return equipoPrograma;
|
||||
});
|
||||
|
@ -238,10 +238,7 @@ export class UploadFileService {
|
||||
continue;
|
||||
}
|
||||
await this.equipoProgramaService
|
||||
.eliminarEquipoSinPrograma(equipo)
|
||||
.then((_) =>
|
||||
this.equipoProgramaService.create(equipo, programa),
|
||||
)
|
||||
.create(equipo, programa)
|
||||
.then((res) => {
|
||||
equipo.programas.push(res.equipoPrograma);
|
||||
mensajes.push(res.message);
|
||||
|
Loading…
Reference in New Issue
Block a user