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 { AuthGuard } from '@nestjs/passport';
|
||||||
import { ApiBearerAuth, ApiBody, ApiOperation, ApiTags } from '@nestjs/swagger';
|
import { ApiBearerAuth, ApiBody, ApiOperation, ApiTags } from '@nestjs/swagger';
|
||||||
|
import { Operador } from 'src/operador/entity/operador.entity';
|
||||||
import { EquipoProgramaService } from './equipo-programa.service';
|
import { EquipoProgramaService } from './equipo-programa.service';
|
||||||
import { CreateEquipoProgramaDto } from './dto/input/create.dto';
|
import { CreateEquipoProgramaDto } from './dto/input/create.dto';
|
||||||
import { DeleteEquipoProgramaDto } from './dto/input/delete.dto';
|
import { DeleteEquipoProgramaDto } from './dto/input/delete.dto';
|
||||||
@ -36,7 +45,17 @@ export class EquipoProgramaController {
|
|||||||
description: 'La variable id_equipo_programa es obligatoria.',
|
description: 'La variable id_equipo_programa es obligatoria.',
|
||||||
examples: { ejemplo: { value: { id_equipo_programa: 1 } } },
|
examples: { ejemplo: { value: { id_equipo_programa: 1 } } },
|
||||||
})
|
})
|
||||||
delete(@Body() body: DeleteEquipoProgramaDto) {
|
delete(@Request() req, @Body() body: DeleteEquipoProgramaDto) {
|
||||||
return this.equipoProgramaService.delete(body.id_equipo_programa);
|
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 {
|
import {
|
||||||
ConflictException,
|
ConflictException,
|
||||||
|
ForbiddenException,
|
||||||
forwardRef,
|
forwardRef,
|
||||||
Inject,
|
Inject,
|
||||||
Injectable,
|
Injectable,
|
||||||
@ -11,6 +12,7 @@ import { EquipoPrograma } from './entity/equipo-programa.entity';
|
|||||||
import { Programa } from '../institucion-programa/entity/programa.entity';
|
import { Programa } from '../institucion-programa/entity/programa.entity';
|
||||||
import { EquipoService } from '../equipo/equipo.service';
|
import { EquipoService } from '../equipo/equipo.service';
|
||||||
import { InstitucionProgramaService } from '../institucion-programa/institucion-programa.service';
|
import { InstitucionProgramaService } from '../institucion-programa/institucion-programa.service';
|
||||||
|
import { Operador } from 'src/operador/entity/operador.entity';
|
||||||
|
|
||||||
@Injectable()
|
@Injectable()
|
||||||
export class EquipoProgramaService {
|
export class EquipoProgramaService {
|
||||||
@ -22,11 +24,14 @@ export class EquipoProgramaService {
|
|||||||
private institucionProgramaService: InstitucionProgramaService,
|
private institucionProgramaService: InstitucionProgramaService,
|
||||||
) {}
|
) {}
|
||||||
|
|
||||||
countProgramasEquipo(equipo: Equipo) {
|
async create(
|
||||||
return this.repository.count({ equipo });
|
// operador: Operador,
|
||||||
}
|
id_equipo: number | Equipo,
|
||||||
|
id_programa: number | Programa,
|
||||||
async create(id_equipo: number | Equipo, id_programa: number | Programa) {
|
) {
|
||||||
|
const sinPrograma = await this.institucionProgramaService.findProgramaById(
|
||||||
|
1,
|
||||||
|
);
|
||||||
const equipo =
|
const equipo =
|
||||||
typeof id_equipo === 'number'
|
typeof id_equipo === 'number'
|
||||||
? await this.equipoService.findById(id_equipo)
|
? await this.equipoService.findById(id_equipo)
|
||||||
@ -38,53 +43,64 @@ export class EquipoProgramaService {
|
|||||||
|
|
||||||
return this.findEquipoProgramaByEquipoPrograma(equipo, programa)
|
return this.findEquipoProgramaByEquipoPrograma(equipo, programa)
|
||||||
.then((_) =>
|
.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) => ({
|
.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,
|
equipoPrograma,
|
||||||
}));
|
}));
|
||||||
}
|
}
|
||||||
|
|
||||||
async delete(id_equipo_programa: number | EquipoPrograma) {
|
async delete(operador: Operador, id_equipo_programa: number) {
|
||||||
const equipoPrograma =
|
const equipoPrograma = await this.findById(id_equipo_programa);
|
||||||
typeof id_equipo_programa === 'number'
|
|
||||||
? await this.findById(id_equipo_programa)
|
|
||||||
: id_equipo_programa;
|
|
||||||
|
|
||||||
if (typeof id_equipo_programa === 'number')
|
if (
|
||||||
await this.countProgramasEquipo(equipoPrograma.equipo).then((n) => {
|
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 (n === 1) {
|
||||||
if (equipoPrograma.programa.id_programa === 1)
|
if (equipoPrograma.programa.id_programa === 1)
|
||||||
throw new ConflictException('No se puede eliminar esta opción.');
|
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);
|
||||||
return this.repository.remove(equipoPrograma).then((_) => ({
|
})
|
||||||
message: 'Se eliminó correctamente el programa de este equipo.',
|
.then((_) => ({
|
||||||
|
message: 'Se eliminó correctamente el software 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);
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
findById(id_equipo_programa: number) {
|
findById(id_equipo_programa: number) {
|
||||||
return this.repository
|
return this.repository
|
||||||
.findOne({
|
.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 },
|
where: { id_equipo_programa },
|
||||||
})
|
})
|
||||||
.then((equipoPrograma) => {
|
.then((equipoPrograma) => {
|
||||||
if (!equipoPrograma)
|
if (!equipoPrograma)
|
||||||
throw new ConflictException('No existe este id equipo programa.');
|
throw new ConflictException('No existe este id equipo software.');
|
||||||
return equipoPrograma;
|
return equipoPrograma;
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
@ -99,7 +115,7 @@ export class EquipoProgramaService {
|
|||||||
.then((equipoPrograma) => {
|
.then((equipoPrograma) => {
|
||||||
if (validarExistencia && equipoPrograma)
|
if (validarExistencia && equipoPrograma)
|
||||||
throw new ConflictException(
|
throw new ConflictException(
|
||||||
'Este programa ya fue asignado a este equipo.',
|
'Este software ya fue asignado a este equipo.',
|
||||||
);
|
);
|
||||||
return equipoPrograma;
|
return equipoPrograma;
|
||||||
});
|
});
|
||||||
|
@ -238,10 +238,7 @@ export class UploadFileService {
|
|||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
await this.equipoProgramaService
|
await this.equipoProgramaService
|
||||||
.eliminarEquipoSinPrograma(equipo)
|
.create(equipo, programa)
|
||||||
.then((_) =>
|
|
||||||
this.equipoProgramaService.create(equipo, programa),
|
|
||||||
)
|
|
||||||
.then((res) => {
|
.then((res) => {
|
||||||
equipo.programas.push(res.equipoPrograma);
|
equipo.programas.push(res.equipoPrograma);
|
||||||
mensajes.push(res.message);
|
mensajes.push(res.message);
|
||||||
|
Loading…
Reference in New Issue
Block a user