logger
This commit is contained in:
+1
-1
@@ -55,4 +55,4 @@ pids
|
||||
# Diagnostic reports (https://nodejs.org/api/report.html)
|
||||
report.[0-9]*.[0-9]*.[0-9]*.[0-9]*.json
|
||||
|
||||
imagenes
|
||||
imagenes
|
||||
|
||||
Generated
+34
@@ -16,6 +16,7 @@
|
||||
"@nestjs/mapped-types": "*",
|
||||
"@nestjs/passport": "^10.0.3",
|
||||
"@nestjs/platform-express": "^10.3.9",
|
||||
"@nestjs/serve-static": "^4.0.2",
|
||||
"@nestjs/typeorm": "^10.0.2",
|
||||
"bcrypt": "^5.1.1",
|
||||
"bcryptjs": "^2.4.3",
|
||||
@@ -2490,6 +2491,39 @@
|
||||
"integrity": "sha512-AilxAyFOAcK5wA1+LeaySVBrHsGQvUFCDWXKpZjzaL0PqW+xfBOttn8GNtWKFWqneyMZj41MWF9Kl6iPWLwgOA==",
|
||||
"dev": true
|
||||
},
|
||||
"node_modules/@nestjs/serve-static": {
|
||||
"version": "4.0.2",
|
||||
"resolved": "https://registry.npmjs.org/@nestjs/serve-static/-/serve-static-4.0.2.tgz",
|
||||
"integrity": "sha512-cT0vdWN5ar7jDI2NKbhf4LcwJzU4vS5sVpMkVrHuyLcltbrz6JdGi1TfIMMatP2pNiq5Ie/uUdPSFDVaZX/URQ==",
|
||||
"license": "MIT",
|
||||
"dependencies": {
|
||||
"path-to-regexp": "0.2.5"
|
||||
},
|
||||
"peerDependencies": {
|
||||
"@fastify/static": "^6.5.0 || ^7.0.0",
|
||||
"@nestjs/common": "^9.0.0 || ^10.0.0",
|
||||
"@nestjs/core": "^9.0.0 || ^10.0.0",
|
||||
"express": "^4.18.1",
|
||||
"fastify": "^4.7.0"
|
||||
},
|
||||
"peerDependenciesMeta": {
|
||||
"@fastify/static": {
|
||||
"optional": true
|
||||
},
|
||||
"express": {
|
||||
"optional": true
|
||||
},
|
||||
"fastify": {
|
||||
"optional": true
|
||||
}
|
||||
}
|
||||
},
|
||||
"node_modules/@nestjs/serve-static/node_modules/path-to-regexp": {
|
||||
"version": "0.2.5",
|
||||
"resolved": "https://registry.npmjs.org/path-to-regexp/-/path-to-regexp-0.2.5.tgz",
|
||||
"integrity": "sha512-l6qtdDPIkmAmzEO6egquYDfqQGPMRNGjYtrU13HAXb3YSRrt7HSb1sJY0pKp6o2bAa86tSB6iwaW2JbthPKr7Q==",
|
||||
"license": "MIT"
|
||||
},
|
||||
"node_modules/@nestjs/testing": {
|
||||
"version": "10.3.9",
|
||||
"resolved": "https://registry.npmjs.org/@nestjs/testing/-/testing-10.3.9.tgz",
|
||||
|
||||
@@ -27,6 +27,7 @@
|
||||
"@nestjs/mapped-types": "*",
|
||||
"@nestjs/passport": "^10.0.3",
|
||||
"@nestjs/platform-express": "^10.3.9",
|
||||
"@nestjs/serve-static": "^4.0.2",
|
||||
"@nestjs/typeorm": "^10.0.2",
|
||||
"bcrypt": "^5.1.1",
|
||||
"bcryptjs": "^2.4.3",
|
||||
|
||||
@@ -1,48 +1,75 @@
|
||||
import { Body, Controller, Delete, Get, Param, ParseIntPipe, Post, Put, Query,UseGuards } from '@nestjs/common';
|
||||
import { ProfesorService } from './profesor.service';
|
||||
import { profesorDto } from './dto/profesorDto.dto';
|
||||
import {Roles} from '../permissions/roles.decorator'
|
||||
import {RolesGuard} from '../permissions/roles.guard'
|
||||
import {Role} from '../permissions/role.enum'
|
||||
import {
|
||||
Body,
|
||||
Controller,
|
||||
Delete,
|
||||
Get,
|
||||
Logger,
|
||||
Param,
|
||||
ParseIntPipe,
|
||||
Post,
|
||||
Put,
|
||||
Query,
|
||||
UseGuards
|
||||
} from "@nestjs/common";
|
||||
import { ProfesorService } from "./profesor.service";
|
||||
import { profesorDto } from "./dto/profesorDto.dto";
|
||||
import { Roles } from "../permissions/roles.decorator";
|
||||
import { RolesGuard } from "../permissions/roles.guard";
|
||||
import { Role } from "../permissions/role.enum";
|
||||
|
||||
@Controller('profesor')
|
||||
@Controller("profesor")
|
||||
@UseGuards(RolesGuard)
|
||||
export class ProfesorController {
|
||||
constructor(private profesorService: ProfesorService) {}
|
||||
constructor(private profesorService: ProfesorService) {
|
||||
}
|
||||
|
||||
@Post()
|
||||
@Roles(Role.Responsable,Role.Admin)
|
||||
@Roles(Role.Responsable, Role.Admin)
|
||||
async register(@Body() data: profesorDto) {
|
||||
Logger.debug("register profesor");
|
||||
return this.profesorService.register(data);
|
||||
}
|
||||
|
||||
@Get()
|
||||
findAll() {
|
||||
Logger.debug("findAllProfesors");
|
||||
return this.profesorService.findAll();
|
||||
}
|
||||
|
||||
@Get('page')
|
||||
findAllPaginated(@Query('page') page: number = 1, @Query('limit') limit: number = 10, @Query() filters: any) {
|
||||
page = page < 1 ? 1 : page;
|
||||
limit = limit > 10 || limit < 1 ? 10 : limit;
|
||||
@Get("page")
|
||||
findAllPaginated(@Query("page") page: number = 1, @Query("limit") limit: number = 10, @Query() filters: any) {
|
||||
try {
|
||||
Logger.debug("Page Profesors");
|
||||
page = page < 1 ? 1 : page;
|
||||
limit = limit > 10 || limit < 1 ? 10 : limit;
|
||||
return this.profesorService.findAllPaginated(page, limit, filters);
|
||||
} catch (err) {
|
||||
Logger.error("profesor controller ", err.error);
|
||||
return [];
|
||||
}
|
||||
|
||||
return this.profesorService.findAllPaginated(page, limit, filters);
|
||||
}
|
||||
|
||||
@Put(':id_profesor')
|
||||
@Roles(Role.Responsable,Role.Admin)
|
||||
async modify(@Param('id_profesor', ParseIntPipe) id_profesor: number, @Body() data: profesorDto) {
|
||||
@Put(":id_profesor")
|
||||
@Roles(Role.Responsable, Role.Admin)
|
||||
async modify(@Param("id_profesor", ParseIntPipe) id_profesor: number, @Body() data: profesorDto) {
|
||||
Logger.debug("put Profesors by id");
|
||||
|
||||
return this.profesorService.modify(id_profesor, data);
|
||||
}
|
||||
|
||||
@Delete(':id_profesor')
|
||||
@Roles(Role.Responsable,Role.Admin)
|
||||
async remove(@Param('id_profesor', ParseIntPipe) id_profesor: number) {
|
||||
@Delete(":id_profesor")
|
||||
@Roles(Role.Responsable, Role.Admin)
|
||||
async remove(@Param("id_profesor", ParseIntPipe) id_profesor: number) {
|
||||
Logger.debug("delete Profesors by id");
|
||||
|
||||
return this.profesorService.remove(id_profesor);
|
||||
}
|
||||
|
||||
@Get(':id_profesor')
|
||||
async profile(@Param('id_profesor', ParseIntPipe) id_profesor: number) {
|
||||
@Get(":id_profesor")
|
||||
async profile(@Param("id_profesor", ParseIntPipe) id_profesor: number) {
|
||||
Logger.debug("get Profesors by id");
|
||||
|
||||
return this.profesorService.profile(id_profesor);
|
||||
}
|
||||
|
||||
|
||||
@@ -1,19 +1,19 @@
|
||||
import { HttpException, Injectable } from '@nestjs/common';
|
||||
import { Profesor } from './entities/profesor.entity';
|
||||
import { DatosAcademicosService } from '../datos_academicos/datos_academicos.service';
|
||||
import { LineasInvestigacionService } from '../lineas_investigacion/lineas_investigacion.service';
|
||||
import { ProyectosAcademicosService } from '../proyectos_academicos/proyectos_academicos.service';
|
||||
import { InjectRepository } from '@nestjs/typeorm';
|
||||
import { Repository } from 'typeorm';
|
||||
import { profesorDto } from './dto/profesorDto.dto';
|
||||
import { ProyectosAcademicos } from '../proyectos_academicos/entities/proyectos_academicos.entity';
|
||||
import { DatosAcademicos } from '../datos_academicos/entities/datos_academicos.entity'
|
||||
import { LineasInvestigacion } from '../lineas_investigacion/entities/lineas_investigacion.entity'
|
||||
import { HttpException, Injectable, Logger } from "@nestjs/common";
|
||||
import { Profesor } from "./entities/profesor.entity";
|
||||
import { DatosAcademicosService } from "../datos_academicos/datos_academicos.service";
|
||||
import { LineasInvestigacionService } from "../lineas_investigacion/lineas_investigacion.service";
|
||||
import { ProyectosAcademicosService } from "../proyectos_academicos/proyectos_academicos.service";
|
||||
import { InjectRepository } from "@nestjs/typeorm";
|
||||
import { Repository } from "typeorm";
|
||||
import { profesorDto } from "./dto/profesorDto.dto";
|
||||
import { ProyectosAcademicos } from "../proyectos_academicos/entities/proyectos_academicos.entity";
|
||||
import { DatosAcademicos } from "../datos_academicos/entities/datos_academicos.entity";
|
||||
import { LineasInvestigacion } from "../lineas_investigacion/entities/lineas_investigacion.entity";
|
||||
|
||||
@Injectable()
|
||||
export class ProfesorService {
|
||||
constructor(
|
||||
@InjectRepository(Profesor)
|
||||
@InjectRepository(Profesor)
|
||||
private readonly profesorRepository: Repository<Profesor>,
|
||||
private readonly datosAcademicosService: DatosAcademicosService,
|
||||
private readonly lineasInvestigacionService: LineasInvestigacionService,
|
||||
@@ -23,15 +23,17 @@ export class ProfesorService {
|
||||
@InjectRepository(LineasInvestigacion)
|
||||
private readonly lineasInvestigacionRepository: Repository<LineasInvestigacion>,
|
||||
@InjectRepository(ProyectosAcademicos)
|
||||
private readonly proyectosAcademicosRepository: Repository<ProyectosAcademicos>,
|
||||
) {}
|
||||
private readonly proyectosAcademicosRepository: Repository<ProyectosAcademicos>
|
||||
) {
|
||||
}
|
||||
|
||||
private async findAvailableId(): Promise<number> {
|
||||
let id = 1;
|
||||
let exists = true;
|
||||
|
||||
|
||||
while (exists) {
|
||||
if (!await this.profesorRepository.findOne({ where: { id_profesor: id } })) {
|
||||
Logger.debug(`id_profesor = ${id}`);
|
||||
exists = false;
|
||||
return id;
|
||||
}
|
||||
@@ -41,14 +43,14 @@ export class ProfesorService {
|
||||
|
||||
//register teacher
|
||||
async register(data: profesorDto): Promise<Profesor | undefined> {
|
||||
const { num_trabajador, rfc, proyectosAcademicos, datosAcademicos, lineasInvestigacion} = data;
|
||||
const { num_trabajador, rfc, proyectosAcademicos, datosAcademicos, lineasInvestigacion } = data;
|
||||
|
||||
if (await this.profesorRepository.findOne({ where: { rfc } })) {
|
||||
throw new HttpException('rfc already exist', 403);
|
||||
throw new HttpException("rfc already exist", 403);
|
||||
}
|
||||
|
||||
if (await this.profesorRepository.findOne({ where: { num_trabajador } })) {
|
||||
throw new HttpException('number of worker already exist', 403);
|
||||
throw new HttpException("number of worker already exist", 403);
|
||||
}
|
||||
|
||||
const availableId = await this.findAvailableId();
|
||||
@@ -61,7 +63,7 @@ export class ProfesorService {
|
||||
const newProyecto = this.proyectosAcademicosRepository.create({
|
||||
...proyecto,
|
||||
profesor: savedProfesor,
|
||||
id_profesor: savedProfesor.id_profesor,
|
||||
id_profesor: savedProfesor.id_profesor
|
||||
});
|
||||
return newProyecto;
|
||||
});
|
||||
@@ -73,7 +75,7 @@ export class ProfesorService {
|
||||
const newLinea = this.lineasInvestigacionRepository.create({
|
||||
...linea,
|
||||
profesor: savedProfesor,
|
||||
id_profesor: savedProfesor.id_profesor,
|
||||
id_profesor: savedProfesor.id_profesor
|
||||
});
|
||||
return newLinea;
|
||||
});
|
||||
@@ -85,7 +87,7 @@ export class ProfesorService {
|
||||
const newDato = this.datosAcademicosRepository.create({
|
||||
...datosAcademicos,
|
||||
profesor: savedProfesor,
|
||||
id_profesor: savedProfesor.id_profesor,
|
||||
id_profesor: savedProfesor.id_profesor
|
||||
});
|
||||
return newDato;
|
||||
});
|
||||
@@ -99,8 +101,8 @@ export class ProfesorService {
|
||||
async findAll(): Promise<Profesor[]> {
|
||||
return this.profesorRepository.find({
|
||||
order: {
|
||||
nombre: 'ASC',
|
||||
},
|
||||
nombre: "ASC"
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
@@ -108,7 +110,7 @@ export class ProfesorService {
|
||||
async modify(id_profesor: number, data: profesorDto) {
|
||||
const profesor = await this.profesorRepository.findOne({ where: { id_profesor } });
|
||||
if (!profesor) {
|
||||
throw new HttpException('Profesor not found', 404);
|
||||
throw new HttpException("Profesor not found", 404);
|
||||
}
|
||||
|
||||
const { proyectosAcademicos, datosAcademicos, lineasInvestigacion, ...profesorData } = data;
|
||||
@@ -122,7 +124,7 @@ export class ProfesorService {
|
||||
const newProyecto = this.proyectosAcademicosRepository.create({
|
||||
...proyecto,
|
||||
profesor: savedProfesor,
|
||||
id_profesor: savedProfesor.id_profesor,
|
||||
id_profesor: savedProfesor.id_profesor
|
||||
});
|
||||
return newProyecto;
|
||||
});
|
||||
@@ -130,12 +132,12 @@ export class ProfesorService {
|
||||
}
|
||||
|
||||
if (lineasInvestigacion) {
|
||||
await this.lineasInvestigacionRepository.delete({ profesor: {id_profesor}});
|
||||
await this.lineasInvestigacionRepository.delete({ profesor: { id_profesor } });
|
||||
const lineas = lineasInvestigacion.map(linea => {
|
||||
const newLinea = this.lineasInvestigacionRepository.create({
|
||||
...linea,
|
||||
profesor: savedProfesor,
|
||||
id_profesor: savedProfesor.id_profesor,
|
||||
id_profesor: savedProfesor.id_profesor
|
||||
});
|
||||
return newLinea;
|
||||
});
|
||||
@@ -148,7 +150,7 @@ export class ProfesorService {
|
||||
const newDato = this.datosAcademicosRepository.create({
|
||||
...datosAcademicos,
|
||||
profesor: savedProfesor,
|
||||
id_profesor: savedProfesor.id_profesor,
|
||||
id_profesor: savedProfesor.id_profesor
|
||||
});
|
||||
return newDato;
|
||||
});
|
||||
@@ -160,28 +162,28 @@ export class ProfesorService {
|
||||
|
||||
//remove teacher
|
||||
async remove(id_profesor: number) {
|
||||
const profesor = await this.profesorRepository.findOne({ where: { id_profesor }, relations: ['datosAcademicos'] });
|
||||
const profesor = await this.profesorRepository.findOne({ where: { id_profesor }, relations: ["datosAcademicos"] });
|
||||
if (!profesor) {
|
||||
throw new HttpException('Profesor not found', 404);
|
||||
throw new HttpException("Profesor not found", 404);
|
||||
}
|
||||
|
||||
await this.datosAcademicosService.removeByProfesorId(id_profesor);
|
||||
await this.lineasInvestigacionService.removeByProfesorId(id_profesor);
|
||||
await this.proyectosAcademicosService.removeByProfesorId(id_profesor);
|
||||
await this.datosAcademicosService.removeByProfesorId(id_profesor);
|
||||
await this.lineasInvestigacionService.removeByProfesorId(id_profesor);
|
||||
await this.proyectosAcademicosService.removeByProfesorId(id_profesor);
|
||||
|
||||
await this.profesorRepository.remove(profesor);
|
||||
return { message: 'Profesor removed successfully' };
|
||||
return { message: "Profesor removed successfully" };
|
||||
}
|
||||
|
||||
//brings teachers by id
|
||||
async profile(id_profesor: number) {
|
||||
const profesor = await this.profesorRepository.findOne({
|
||||
where: { id_profesor },
|
||||
relations: ['proyectosAcademicos', 'datosAcademicos', 'lineasInvestigacion'],
|
||||
relations: ["proyectosAcademicos", "datosAcademicos", "lineasInvestigacion"]
|
||||
});
|
||||
|
||||
|
||||
if (!profesor) {
|
||||
throw new HttpException('Profesor not found', 404);
|
||||
throw new HttpException("Profesor not found", 404);
|
||||
}
|
||||
|
||||
return profesor;
|
||||
@@ -192,43 +194,53 @@ export class ProfesorService {
|
||||
limit: number,
|
||||
filters: any
|
||||
): Promise<{ profesores: Profesor[], total: number, totalPages: number }> {
|
||||
const query = this.profesorRepository.createQueryBuilder('profesor');
|
||||
|
||||
const query = this.profesorRepository.createQueryBuilder("profesor");
|
||||
|
||||
if (filters.nombre) {
|
||||
query.andWhere('profesor.nombre LIKE :nombre', { nombre: `%${filters.nombre}%` });
|
||||
Logger.debug('fiter by name');
|
||||
|
||||
query.andWhere("profesor.nombre LIKE :nombre", { nombre: `%${filters.nombre}%` });
|
||||
}
|
||||
|
||||
|
||||
if (filters.adscripcion) {
|
||||
query.andWhere('profesor.adscripcion = :adscripcion', { adscripcion: filters.adscripcion });
|
||||
Logger.debug("fiter by adscription");
|
||||
|
||||
query.andWhere("profesor.adscripcion = :adscripcion", { adscripcion: filters.adscripcion });
|
||||
}
|
||||
|
||||
|
||||
if (filters.categoria) {
|
||||
query.andWhere('profesor.categoria = :categoria', { categoria: filters.categoria });
|
||||
Logger.debug("fiter by category");
|
||||
|
||||
query.andWhere("profesor.categoria = :categoria", { categoria: filters.categoria });
|
||||
}
|
||||
|
||||
|
||||
if (filters.edificio) {
|
||||
query.andWhere('profesor.edificio = :edificio', { edificio: filters.edificio });
|
||||
Logger.debug("fiter by building");
|
||||
|
||||
query.andWhere("profesor.edificio = :edificio", { edificio: filters.edificio });
|
||||
}
|
||||
|
||||
if (typeof filters.activo !== 'undefined') {
|
||||
query.andWhere('profesor.activo = :activo', { activo: filters.activo });
|
||||
|
||||
if (typeof filters.activo !== "undefined") {
|
||||
Logger.debug("fiter by is active");
|
||||
|
||||
query.andWhere("profesor.activo = :activo", { activo: filters.activo });
|
||||
} else {
|
||||
query.orderBy('profesor.activo', 'DESC');
|
||||
query.orderBy("profesor.activo", "DESC");
|
||||
}
|
||||
|
||||
query.addOrderBy('profesor.nombre', 'ASC');
|
||||
|
||||
|
||||
query.addOrderBy("profesor.nombre", "ASC");
|
||||
|
||||
const [profesores, total] = await query
|
||||
.skip((page - 1) * limit)
|
||||
.take(limit)
|
||||
.getManyAndCount();
|
||||
|
||||
|
||||
const totalPages = Math.ceil(total / limit);
|
||||
|
||||
|
||||
return { profesores, total, totalPages };
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
async updateImageURL(id_profesor: number, imageUrl: string): Promise<void> {
|
||||
await this.profesorRepository.update(id_profesor, { fotografia: imageUrl });
|
||||
}
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
import { Controller, Get } from '@nestjs/common';
|
||||
import { Controller, Get, Logger } from '@nestjs/common';
|
||||
import { AdscripcionService } from './adscripcion.service';
|
||||
|
||||
@Controller('adscripcion')
|
||||
@@ -7,6 +7,7 @@ export class AdscripcionController {
|
||||
|
||||
@Get()
|
||||
findAll() {
|
||||
Logger.debug('find all adscripcion');
|
||||
return this.adscripcionService.findAll();
|
||||
}
|
||||
}
|
||||
|
||||
+23
-14
@@ -11,15 +11,15 @@ import {
|
||||
UseGuards,
|
||||
Delete,
|
||||
ParseIntPipe,
|
||||
Query
|
||||
Query,
|
||||
Logger,
|
||||
} from '@nestjs/common';
|
||||
import { AuthGuard } from './auth.guard';
|
||||
import { AuthService } from './auth.service';
|
||||
import { registerDto } from './dto/registerDto.dto';
|
||||
import {Roles} from '../permissions/roles.decorator'
|
||||
import {RolesGuard} from '../permissions/roles.guard'
|
||||
import {Role} from '../permissions/role.enum'
|
||||
|
||||
import { Roles } from '../permissions/roles.decorator';
|
||||
import { RolesGuard } from '../permissions/roles.guard';
|
||||
import { Role } from '../permissions/role.enum';
|
||||
|
||||
@Controller('auth')
|
||||
@UseGuards(RolesGuard)
|
||||
@@ -29,20 +29,22 @@ export class AuthController {
|
||||
@HttpCode(HttpStatus.OK)
|
||||
@Post('login')
|
||||
async signIn(@Body() data: registerDto) {
|
||||
Logger.debug('signIn');
|
||||
const { rememberMe } = data;
|
||||
return this.authService.signIn(data, rememberMe);
|
||||
}
|
||||
|
||||
//@UseGuards()
|
||||
@Post("register")
|
||||
@Post('register')
|
||||
//@Roles(Role.Admin)
|
||||
async register(@Body() data: registerDto){
|
||||
return this.authService.register(data)
|
||||
async register(@Body() data: registerDto) {
|
||||
Logger.debug('register user');
|
||||
return this.authService.register(data);
|
||||
}
|
||||
|
||||
//@UseGuards(AuthGuard)
|
||||
@Get()
|
||||
findAll() {
|
||||
Logger.debug('find all users');
|
||||
return this.authService.findAll();
|
||||
}
|
||||
|
||||
@@ -50,23 +52,29 @@ export class AuthController {
|
||||
findAllPaginated(
|
||||
@Query('page') page: number = 1,
|
||||
@Query('limit') limit: number = 10,
|
||||
@Query() filters: any
|
||||
@Query() filters: any,
|
||||
) {
|
||||
Logger.debug('find all users max 10');
|
||||
page = page < 1 ? 1 : page;
|
||||
limit = limit > 10 || limit < 1 ? 10 : limit;
|
||||
|
||||
return this.authService.findAllPaginated(page, limit,filters);
|
||||
return this.authService.findAllPaginated(page, limit, filters);
|
||||
}
|
||||
|
||||
@Get(':id_usuario')
|
||||
async profile(@Param('id_usuario', ParseIntPipe) id_usuario: number) {
|
||||
Logger.debug('find one user');
|
||||
return this.authService.profile(id_usuario);
|
||||
}
|
||||
|
||||
//@UseGuards(AuthGuard)
|
||||
@Put(':id_usuario')
|
||||
@Roles(Role.Admin)
|
||||
async update(@Param('id_usuario') id_usuario: number, @Body() data: registerDto) {
|
||||
async update(
|
||||
@Param('id_usuario') id_usuario: number,
|
||||
@Body() data: registerDto,
|
||||
) {
|
||||
Logger.debug('update user');
|
||||
return this.authService.update(id_usuario, data);
|
||||
}
|
||||
|
||||
@@ -74,6 +82,7 @@ export class AuthController {
|
||||
@Delete(':id_usuario')
|
||||
@Roles(Role.Admin)
|
||||
async remove(@Param('id_usuario') id_usuario: number) {
|
||||
Logger.debug('delete user');
|
||||
await this.authService.remove(id_usuario);
|
||||
return { message: 'User successfully deleted' };
|
||||
}
|
||||
@@ -81,7 +90,7 @@ export class AuthController {
|
||||
@HttpCode(HttpStatus.OK)
|
||||
@Post('validate')
|
||||
async validateToken(@Body('token') token: string) {
|
||||
Logger.debug('validate user');
|
||||
return this.authService.validateToken(token);
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2,6 +2,7 @@ import {
|
||||
HttpException,
|
||||
Inject,
|
||||
Injectable,
|
||||
Logger,
|
||||
UnauthorizedException,
|
||||
} from '@nestjs/common';
|
||||
import { UsersService } from '../users/users.service';
|
||||
@@ -131,10 +132,12 @@ export class AuthService {
|
||||
const query = this.userRepository.createQueryBuilder('usuario');
|
||||
|
||||
if (filters.nombre) {
|
||||
Logger.debug('fiter by name user');
|
||||
query.andWhere('usuario.nombre LIKE :nombre', { nombre: `%${filters.nombre}%` });
|
||||
}
|
||||
|
||||
if (filters.id_tipo_usuario) {
|
||||
Logger.debug('fiter by id user');
|
||||
query.andWhere('usuario.id_tipo_usuario = :id_tipo_usuario', { id_tipo_usuario: filters.id_tipo_usuario });
|
||||
}
|
||||
|
||||
@@ -155,6 +158,7 @@ export class AuthService {
|
||||
});
|
||||
|
||||
if (!user) {
|
||||
Logger.debug('user not found');
|
||||
throw new HttpException('user not found', 404);
|
||||
}
|
||||
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
import { Controller, Get } from '@nestjs/common';
|
||||
import { Controller, Get, Logger } from '@nestjs/common';
|
||||
import { CategoriaService } from './categoria.service';
|
||||
|
||||
@Controller('categoria')
|
||||
@@ -7,6 +7,7 @@ export class CategoriaController {
|
||||
|
||||
@Get()
|
||||
findAll() {
|
||||
Logger.debug('find all categoria');
|
||||
return this.categoriaService.findAll();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,12 +1,15 @@
|
||||
import { Controller, Get } from '@nestjs/common';
|
||||
import { Controller, Get, Logger } from '@nestjs/common';
|
||||
import { DatosAcademicosService } from './datos_academicos.service';
|
||||
|
||||
@Controller('datos-academicos')
|
||||
export class DatosAcademicosController {
|
||||
constructor(private readonly datosAcademicosService: DatosAcademicosService) {}
|
||||
constructor(
|
||||
private readonly datosAcademicosService: DatosAcademicosService,
|
||||
) {}
|
||||
|
||||
@Get()
|
||||
findAll() {
|
||||
Logger.debug('find all datos academicos');
|
||||
return this.datosAcademicosService.findAll();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -10,7 +10,6 @@ export class DatosAcademicosService {
|
||||
private readonly datosAcademicosRepository: Repository<DatosAcademicos>,
|
||||
) {}
|
||||
|
||||
// Add methods to manage DatosAcademicos entities as needed
|
||||
async removeByProfesorId(id_profesor: number): Promise<void> {
|
||||
await this.datosAcademicosRepository.delete({ profesor: { id_profesor } });
|
||||
}
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
import { Controller, Get } from '@nestjs/common';
|
||||
import { Controller, Get, Logger } from '@nestjs/common';
|
||||
import { EdificioService } from './edificio.service';
|
||||
|
||||
@Controller('edificio')
|
||||
@@ -7,6 +7,7 @@ export class EdificioController {
|
||||
|
||||
@Get()
|
||||
findAll() {
|
||||
Logger.debug('find all edificio');
|
||||
return this.edificioService.findAll();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,15 +0,0 @@
|
||||
import { extname } from 'path';
|
||||
|
||||
export const renameImage = (req, file, callback) => {
|
||||
const profesorName = req.body.nombre.toLowerCase().replace(/ /g, '_');
|
||||
const fileExtName = extname(file.originalname);
|
||||
const fileName = `${profesorName}${fileExtName}`;
|
||||
callback(null, fileName);
|
||||
};
|
||||
|
||||
export const fileFilter = (req, file, callback) => {
|
||||
if (!file.originalname.match(/\.(jpg|jpeg|png)$/)) {
|
||||
return callback(new Error('Invalid format type'), false);
|
||||
}
|
||||
callback(null, true);
|
||||
};
|
||||
@@ -1,9 +1,4 @@
|
||||
import {
|
||||
Controller,
|
||||
Post,
|
||||
Body,
|
||||
Put,
|
||||
} from '@nestjs/common';
|
||||
import { Controller, Post, Body, Put, Logger } from '@nestjs/common';
|
||||
import { ImagenService } from './images.service';
|
||||
|
||||
@Controller('imagen')
|
||||
@@ -12,8 +7,9 @@ export class ImagenController {
|
||||
|
||||
@Post()
|
||||
async upload(@Body() body) {
|
||||
Logger.debug('upload image');
|
||||
const { fotografia, nombre } = body;
|
||||
|
||||
|
||||
const imageUrl = await this.imagenService.saveImage(fotografia, nombre);
|
||||
|
||||
return { message: 'Image uploaded successfully', imageUrl };
|
||||
@@ -21,11 +17,11 @@ export class ImagenController {
|
||||
|
||||
@Put()
|
||||
async modify(@Body() body) {
|
||||
Logger.debug('modify image');
|
||||
const { fotografia, nombre } = body;
|
||||
|
||||
const imageUrl = await this.imagenService.modify(fotografia, nombre);
|
||||
|
||||
return { message: 'Image uploaded successfully', imageUrl };
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
import { Injectable, BadRequestException, NotFoundException } from '@nestjs/common';
|
||||
import { Injectable, BadRequestException, NotFoundException, Logger } from '@nestjs/common';
|
||||
import * as fs from 'fs';
|
||||
import * as path from 'path';
|
||||
import * as sharp from 'sharp';
|
||||
@@ -37,6 +37,7 @@ export class ImagenService {
|
||||
}
|
||||
|
||||
async deleteImage(nombre: string): Promise<void> {
|
||||
Logger.debug('delete image');
|
||||
const sanitizedFileName = nombre.replace(/\s+/g, '_').toLowerCase();
|
||||
const imageDir = path.join(process.cwd(), 'imagenes');
|
||||
const imagePath = path.join(imageDir, sanitizedFileName);
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
import { Controller, Get } from '@nestjs/common';
|
||||
import { Controller, Get, Logger } from '@nestjs/common';
|
||||
import { LineasInvestigacionService } from './lineas_investigacion.service';
|
||||
|
||||
@Controller('lineas-investigacion')
|
||||
@@ -7,6 +7,7 @@ export class LineasInvestigacionController {
|
||||
|
||||
@Get()
|
||||
findAll() {
|
||||
Logger.debug('find all lineas de investigacion');
|
||||
return this.lineasInvestigacionService.findAll();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,4 +1,10 @@
|
||||
import { Injectable, CanActivate, ExecutionContext, UnauthorizedException } from '@nestjs/common';
|
||||
import {
|
||||
Injectable,
|
||||
CanActivate,
|
||||
ExecutionContext,
|
||||
UnauthorizedException,
|
||||
Logger,
|
||||
} from '@nestjs/common';
|
||||
import { Reflector } from '@nestjs/core';
|
||||
import { JwtService } from '@nestjs/jwt';
|
||||
import { Role } from './role.enum';
|
||||
@@ -10,17 +16,18 @@ export class RolesGuard implements CanActivate {
|
||||
constructor(
|
||||
private reflector: Reflector,
|
||||
private jwtService: JwtService,
|
||||
private configService: ConfigService
|
||||
private configService: ConfigService,
|
||||
) {}
|
||||
|
||||
async canActivate(context: ExecutionContext): Promise<boolean> {
|
||||
Logger.debug('verify roles');
|
||||
const requiredRoles = this.reflector.getAllAndOverride<Role[]>(ROLES_KEY, [
|
||||
context.getHandler(),
|
||||
context.getClass(),
|
||||
]);
|
||||
|
||||
if (!requiredRoles) {
|
||||
return true; // Permitir acceso si no se especifican roles requeridos
|
||||
return true;
|
||||
}
|
||||
|
||||
const request = context.switchToHttp().getRequest();
|
||||
@@ -30,18 +37,15 @@ export class RolesGuard implements CanActivate {
|
||||
throw new UnauthorizedException('No authorization header provided');
|
||||
}
|
||||
|
||||
const [, token] = authHeader.split(' ');
|
||||
const [, token] = authHeader.split(' ');
|
||||
if (!token) {
|
||||
throw new UnauthorizedException('No token provided');
|
||||
}
|
||||
|
||||
try {
|
||||
const userPayload = await this.jwtService.verifyAsync(
|
||||
token,
|
||||
{
|
||||
secret: this.configService.get<string>('JWT_SECRET'),
|
||||
}
|
||||
);
|
||||
const userPayload = await this.jwtService.verifyAsync(token, {
|
||||
secret: this.configService.get<string>('JWT_SECRET'),
|
||||
});
|
||||
|
||||
const userRole: Role = userPayload.id_tipo_usuario;
|
||||
const userId: number = userPayload.id_usuario;
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
import { Controller,Get } from '@nestjs/common';
|
||||
import { Controller,Get, Logger } from '@nestjs/common';
|
||||
import { ProyectosAcademicosService } from './proyectos_academicos.service';
|
||||
|
||||
@Controller('proyectos-academicos')
|
||||
@@ -7,6 +7,7 @@ export class ProyectosAcademicosController {
|
||||
|
||||
@Get()
|
||||
findAll() {
|
||||
Logger.debug('find all proyectos academicos');
|
||||
return this.proyectosAcademicosService.findAll();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -5,7 +5,7 @@ import { Profesor } from 'src/Profesor/entities/profesor.entity';
|
||||
@Entity({ name: 'usuario' })
|
||||
export class User {
|
||||
|
||||
@PrimaryGeneratedColumn({ name: 'PK_usuario' })
|
||||
@PrimaryGeneratedColumn()
|
||||
id_usuario: number;
|
||||
|
||||
@Column({ type: 'int', nullable: false })
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
import { Injectable } from '@nestjs/common';
|
||||
import { Injectable, Logger } from '@nestjs/common';
|
||||
import { InjectRepository } from '@nestjs/typeorm';
|
||||
import { Repository } from 'typeorm';
|
||||
import { User } from './entities/user.entity';
|
||||
@@ -14,12 +14,14 @@ export class UsersService {
|
||||
async findOne(usuario: string): Promise<User | undefined> {
|
||||
return this.users.findOne({ where: { usuario } });
|
||||
}
|
||||
|
||||
async updateTokenAndDates(
|
||||
id: number,
|
||||
token: string,
|
||||
lastLogin: Date,
|
||||
jwtExpiry: Date,
|
||||
): Promise<void> {
|
||||
Logger.debug('update token and date user');
|
||||
await this.users.update(id, {
|
||||
jwt: token,
|
||||
lastlog: lastLogin,
|
||||
|
||||
Reference in New Issue
Block a user