2025-10-16 14:04:30 -06:00
|
|
|
import {
|
|
|
|
|
Controller,
|
|
|
|
|
Post,
|
|
|
|
|
Put,
|
|
|
|
|
Get,
|
|
|
|
|
Body,
|
|
|
|
|
Query,
|
|
|
|
|
UploadedFile,
|
|
|
|
|
UseGuards,
|
|
|
|
|
UseInterceptors,
|
|
|
|
|
BadRequestException,
|
|
|
|
|
Param,
|
2025-11-27 23:31:50 +00:00
|
|
|
ParseIntPipe,
|
2025-10-16 14:04:30 -06:00
|
|
|
} from '@nestjs/common';
|
|
|
|
|
import { FileInterceptor } from '@nestjs/platform-express';
|
|
|
|
|
import { diskStorage } from 'multer';
|
|
|
|
|
import { extname } from 'path';
|
|
|
|
|
import * as fs from 'fs';
|
|
|
|
|
import { AuthGuard } from '@nestjs/passport';
|
|
|
|
|
|
2025-05-20 15:53:57 -06:00
|
|
|
import { ProgramaService } from './programa.service';
|
2025-10-16 14:04:30 -06:00
|
|
|
import { IsEmail, isEmail } from 'class-validator';
|
|
|
|
|
|
|
|
|
|
class Email {
|
|
|
|
|
@IsEmail()
|
|
|
|
|
correo: string;
|
|
|
|
|
}
|
2025-05-20 15:53:57 -06:00
|
|
|
|
|
|
|
|
@Controller('programa')
|
|
|
|
|
export class ProgramaController {
|
|
|
|
|
constructor(private readonly programaService: ProgramaService) {}
|
|
|
|
|
|
2025-10-16 14:04:30 -06:00
|
|
|
// ==================== POST /carga_masiva ====================
|
|
|
|
|
@Post('carga_masiva')
|
|
|
|
|
@UseGuards(AuthGuard('jwt'))
|
|
|
|
|
@UseInterceptors(
|
|
|
|
|
FileInterceptor('csv', {
|
|
|
|
|
storage: diskStorage({
|
|
|
|
|
destination: './server/uploads',
|
|
|
|
|
filename: (req, file, cb) => {
|
|
|
|
|
const uniqueName = `${Date.now()}${extname(file.originalname)}`;
|
|
|
|
|
cb(null, uniqueName);
|
|
|
|
|
},
|
|
|
|
|
}),
|
|
|
|
|
}),
|
|
|
|
|
)
|
|
|
|
|
async cargaMasiva(@UploadedFile() file: Express.Multer.File) {
|
|
|
|
|
if (!file) {
|
|
|
|
|
throw new BadRequestException(
|
|
|
|
|
'No se envio un archivo csv para la carga masiva.',
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
try {
|
|
|
|
|
const data = await this.programaService.cargaMasiva(file.filename);
|
|
|
|
|
return {
|
|
|
|
|
statusCode: 201,
|
|
|
|
|
message: 'Carga masiva realizada con éxito',
|
|
|
|
|
data,
|
|
|
|
|
};
|
|
|
|
|
} catch (err) {
|
|
|
|
|
// Eliminar archivo si ocurre error
|
|
|
|
|
if (file && file.filename) {
|
|
|
|
|
fs.unlinkSync(`./server/uploads/${file.filename}`);
|
|
|
|
|
}
|
|
|
|
|
throw new BadRequestException(err.message);
|
|
|
|
|
}
|
2025-05-20 15:53:57 -06:00
|
|
|
}
|
|
|
|
|
|
2025-10-16 14:04:30 -06:00
|
|
|
// ==================== PUT /reasignar_programas ====================
|
|
|
|
|
@Put('reasignar_programas/:idUsuario')
|
|
|
|
|
@UseGuards(AuthGuard('jwt'))
|
|
|
|
|
async reasignarProgramas(
|
|
|
|
|
@Body() email: Email,
|
2025-11-27 23:31:50 +00:00
|
|
|
@Param('idUsuario', ParseIntPipe) idUsuario: number,
|
2025-10-16 14:04:30 -06:00
|
|
|
) {
|
2025-11-27 23:31:50 +00:00
|
|
|
|
|
|
|
|
console.log('BODY:', email);
|
|
|
|
|
console.log('PARAM:', idUsuario);
|
|
|
|
|
|
2025-10-16 14:04:30 -06:00
|
|
|
try {
|
|
|
|
|
const data = await this.programaService.reasignarProgramas(
|
|
|
|
|
idUsuario,
|
|
|
|
|
email.correo,
|
|
|
|
|
);
|
2025-11-27 23:31:50 +00:00
|
|
|
|
2025-10-16 14:04:30 -06:00
|
|
|
return {
|
|
|
|
|
statusCode: 200,
|
|
|
|
|
message: 'Programas reasignados con éxito',
|
|
|
|
|
data,
|
|
|
|
|
};
|
|
|
|
|
} catch (err) {
|
|
|
|
|
throw new BadRequestException(err.message);
|
|
|
|
|
}
|
2025-05-20 15:53:57 -06:00
|
|
|
}
|
|
|
|
|
|
2025-10-16 14:04:30 -06:00
|
|
|
// ==================== GET /programas_admin ====================
|
|
|
|
|
@Get('programas_admin/:idUsuario')
|
|
|
|
|
@UseGuards(AuthGuard('jwt'))
|
|
|
|
|
async programasAdmin(@Param('idUsuario') query: number) {
|
|
|
|
|
try {
|
|
|
|
|
const data = await this.programaService.programasAdmin(query);
|
|
|
|
|
return {
|
|
|
|
|
statusCode: 200,
|
|
|
|
|
data,
|
|
|
|
|
};
|
|
|
|
|
} catch (err) {
|
|
|
|
|
throw new BadRequestException(err.message);
|
|
|
|
|
}
|
2025-05-20 15:53:57 -06:00
|
|
|
}
|
|
|
|
|
|
2025-10-16 14:04:30 -06:00
|
|
|
// ==================== GET /programas_responsable ====================
|
|
|
|
|
@Get('programas_responsable/:idUsuario')
|
|
|
|
|
@UseGuards(AuthGuard('jwt'))
|
|
|
|
|
async programasResponsable(@Param(`idUsuario`) idUsuario: number) {
|
|
|
|
|
try {
|
|
|
|
|
const data = await this.programaService.programasResponsable(idUsuario);
|
|
|
|
|
return {
|
|
|
|
|
statusCode: 200,
|
|
|
|
|
data,
|
|
|
|
|
};
|
|
|
|
|
} catch (err) {
|
|
|
|
|
throw new BadRequestException(err.message);
|
|
|
|
|
}
|
2025-05-20 15:53:57 -06:00
|
|
|
}
|
|
|
|
|
}
|