hora excepcion endpoints restringidos a usuario
This commit is contained in:
parent
24609a3131
commit
59bbb1218e
@ -1,11 +1,13 @@
|
||||
import {
|
||||
Body,
|
||||
ConflictException,
|
||||
Controller,
|
||||
Delete,
|
||||
Get,
|
||||
Post,
|
||||
Put,
|
||||
Query,
|
||||
Request,
|
||||
UseGuards,
|
||||
} from '@nestjs/common';
|
||||
import { AuthGuard } from '@nestjs/passport';
|
||||
@ -18,6 +20,7 @@ import {
|
||||
} from '@nestjs/swagger';
|
||||
import { Serealize } from '../interceptors/serialize.interceptor';
|
||||
import { HoraExcepcionService } from './hora-excepcion.service';
|
||||
import { Operador } from 'src/operador/entity/operador.entity';
|
||||
import { CreateHoraExcepcionDto } from './dto/input/create.dto';
|
||||
import { DeleteHoraExcepcionDto } from './dto/input/delete.dto';
|
||||
import { GetHoraExcepcionDto } from './dto/input/get.dto';
|
||||
@ -47,8 +50,13 @@ export class HoraExcepcionController {
|
||||
},
|
||||
},
|
||||
})
|
||||
create(@Body() body: CreateHoraExcepcionDto) {
|
||||
create(@Request() req, @Body() body: CreateHoraExcepcionDto) {
|
||||
const admin: Operador = req.user.operador;
|
||||
|
||||
if (admin.tipoUsuario.id_tipo_usuario != 3)
|
||||
throw new ConflictException('No tienes permiso de realizar esta acción.');
|
||||
return this.horaExcepcionService.create(
|
||||
admin,
|
||||
body.id_institucion_dia,
|
||||
body.hora_inicio,
|
||||
body.hora_fin,
|
||||
@ -65,8 +73,12 @@ export class HoraExcepcionController {
|
||||
description: 'Es obligatorio mandar la variable id_hora_excepcion.',
|
||||
examples: { ejemplo: { value: { id_hora_excepcion: 1 } } },
|
||||
})
|
||||
delete(@Body() body: DeleteHoraExcepcionDto) {
|
||||
return this.horaExcepcionService.delete(body.id_hora_excepcion);
|
||||
delete(@Request() req, @Body() body: DeleteHoraExcepcionDto) {
|
||||
const admin: Operador = req.user.operador;
|
||||
|
||||
if (admin.tipoUsuario.id_tipo_usuario != 3)
|
||||
throw new ConflictException('No tienes permiso de realizar esta acción.');
|
||||
return this.horaExcepcionService.delete(admin, body.id_hora_excepcion);
|
||||
}
|
||||
|
||||
@Serealize(HoraExcepcionOutputDto)
|
||||
@ -103,7 +115,11 @@ export class HoraExcepcionController {
|
||||
},
|
||||
},
|
||||
})
|
||||
update(@Body() body: UpdateHoraExcepcionDto) {
|
||||
return this.horaExcepcionService.update(body);
|
||||
update(@Request() req, @Body() body: UpdateHoraExcepcionDto) {
|
||||
const admin: Operador = req.user.operador;
|
||||
|
||||
if (admin.tipoUsuario.id_tipo_usuario != 3)
|
||||
throw new ConflictException('No tienes permiso de realizar esta acción.');
|
||||
return this.horaExcepcionService.update(admin, body);
|
||||
}
|
||||
}
|
||||
|
@ -12,6 +12,7 @@ import {
|
||||
} from 'typeorm';
|
||||
import { HoraExcepcion } from './entity/hora-excepcion.entity';
|
||||
import { InstitucionDia } from '../institucion-dia/entity/institucion-dia.entity';
|
||||
import { Operador } from 'src/operador/entity/operador.entity';
|
||||
import { InstitucionDiaService } from '../institucion-dia/institucion-dia.service';
|
||||
|
||||
@Injectable()
|
||||
@ -23,6 +24,7 @@ export class HoraExcepcionService {
|
||||
) {}
|
||||
|
||||
async create(
|
||||
admin: Operador,
|
||||
id_institucion_dia: number,
|
||||
hora_inicio: string,
|
||||
hora_fin: string,
|
||||
@ -31,6 +33,13 @@ export class HoraExcepcionService {
|
||||
id_institucion_dia,
|
||||
);
|
||||
|
||||
if (
|
||||
admin.institucion.id_institucion !=
|
||||
institucionDia.institucion.id_institucion
|
||||
)
|
||||
throw new ConflictException(
|
||||
'No puedes crear una horario sin servicio en este día porque no le corresponde a tu institución.',
|
||||
);
|
||||
if (hora_inicio > hora_fin)
|
||||
throw new ConflictException(
|
||||
'La hora inicio no puede ser más mayor que la hora fin.',
|
||||
@ -42,15 +51,24 @@ export class HoraExcepcionService {
|
||||
),
|
||||
)
|
||||
.then((_) => ({
|
||||
message: 'Se creó correctamente una nueva hora excepción.',
|
||||
message: 'Se creó correctamente un nuevo horario sin servicio.',
|
||||
}));
|
||||
}
|
||||
|
||||
delete(id_hora_excepcion: number) {
|
||||
delete(admin: Operador, id_hora_excepcion: number) {
|
||||
return this.findById(id_hora_excepcion)
|
||||
.then((horaExcepcion) => this.repository.delete(horaExcepcion))
|
||||
.then((horaExcepcion) => {
|
||||
if (
|
||||
admin.institucion.id_institucion !=
|
||||
horaExcepcion.institucionDia.institucion.id_institucion
|
||||
)
|
||||
throw new ConflictException(
|
||||
'No puedes eliminar esta horario sin servicio porque no pertenece a tu institución.',
|
||||
);
|
||||
this.repository.delete(horaExcepcion);
|
||||
})
|
||||
.then((_) => ({
|
||||
message: 'Se eliminó correctamente esta hora excepción.',
|
||||
message: 'Se eliminó correctamente este horario sin servicio.',
|
||||
}));
|
||||
}
|
||||
|
||||
@ -62,17 +80,30 @@ export class HoraExcepcionService {
|
||||
|
||||
findById(id_hora_excepcion: number) {
|
||||
return this.repository
|
||||
.findOne({ id_hora_excepcion })
|
||||
.findOne({
|
||||
join: {
|
||||
alias: 'he',
|
||||
innerJoinAndSelect: { id: 'he.institucionDia', i: 'id.institucion' },
|
||||
},
|
||||
where: { id_hora_excepcion },
|
||||
})
|
||||
.then((horaExcepcion) => {
|
||||
if (!horaExcepcion)
|
||||
throw new NotFoundException('No existe esta id hora excepción.');
|
||||
throw new NotFoundException('No existe este id hora excepcion.');
|
||||
return horaExcepcion;
|
||||
});
|
||||
}
|
||||
|
||||
update(attrs: Partial<HoraExcepcion>) {
|
||||
update(admin: Operador, attrs: Partial<HoraExcepcion>) {
|
||||
return this.findById(attrs.id_hora_excepcion)
|
||||
.then(async (horaExecpcion) => {
|
||||
if (
|
||||
admin.institucion.id_institucion !=
|
||||
horaExecpcion.institucionDia.institucion.id_institucion
|
||||
)
|
||||
throw new ConflictException(
|
||||
'No puedes actualizar la información de este tipo de carrito porque no le corresponde a tu institución.',
|
||||
);
|
||||
if (
|
||||
(attrs.hora_inicio &&
|
||||
!attrs.hora_fin &&
|
||||
@ -133,9 +164,9 @@ export class HoraExcepcionService {
|
||||
existeHoraExcepcion.hora_fin === hora_fin &&
|
||||
existeHoraExcepcion.hora_inicio === hora_inicio
|
||||
)
|
||||
throw new ConflictException('Ya existe esta hora excepción.');
|
||||
throw new ConflictException('Ya existe este horario sin servicio.');
|
||||
throw new ConflictException(
|
||||
'Las horas escogídas se sobrelapan con otra hora excepción del mismo día.',
|
||||
'Las horas escogídas se sobrelapan con otro horario sin servicio del mismo día.',
|
||||
);
|
||||
}
|
||||
});
|
||||
|
@ -35,7 +35,6 @@ export class InstitucionDiaService {
|
||||
innerJoinAndSelect: {
|
||||
d: 'id.dia',
|
||||
i: 'id.institucion',
|
||||
he: 'id.horasExcepcion',
|
||||
},
|
||||
},
|
||||
where: { id_institucion_dia },
|
||||
|
Loading…
Reference in New Issue
Block a user