Se agregaron funciones y cambios en los dto
This commit is contained in:
@@ -1,34 +1,119 @@
|
||||
import { Controller, Get, Post, Body, Patch, Param, Delete } from '@nestjs/common';
|
||||
import {
|
||||
Controller,
|
||||
Post,
|
||||
Put,
|
||||
Get,
|
||||
Body,
|
||||
Query,
|
||||
UploadedFile,
|
||||
UseGuards,
|
||||
UseInterceptors,
|
||||
BadRequestException,
|
||||
Param,
|
||||
} 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';
|
||||
|
||||
import { ProgramaService } from './programa.service';
|
||||
import { CreateProgramaDto } from './dto/create-programa.dto';
|
||||
import { UpdateProgramaDto } from './dto/update-programa.dto';
|
||||
import { IsEmail, isEmail } from 'class-validator';
|
||||
|
||||
class Email {
|
||||
@IsEmail()
|
||||
correo: string;
|
||||
}
|
||||
|
||||
@Controller('programa')
|
||||
export class ProgramaController {
|
||||
constructor(private readonly programaService: ProgramaService) {}
|
||||
|
||||
@Post()
|
||||
create(@Body() createProgramaDto: CreateProgramaDto) {
|
||||
return this.programaService.create(createProgramaDto);
|
||||
// ==================== 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);
|
||||
}
|
||||
}
|
||||
|
||||
@Get()
|
||||
findAll() {
|
||||
return this.programaService.findAll();
|
||||
// ==================== PUT /reasignar_programas ====================
|
||||
@Put('reasignar_programas/:idUsuario')
|
||||
@UseGuards(AuthGuard('jwt'))
|
||||
async reasignarProgramas(
|
||||
@Body() email: Email,
|
||||
@Param('idUsuario') idUsuario: number,
|
||||
) {
|
||||
try {
|
||||
const data = await this.programaService.reasignarProgramas(
|
||||
idUsuario,
|
||||
email.correo,
|
||||
);
|
||||
return {
|
||||
statusCode: 200,
|
||||
message: 'Programas reasignados con éxito',
|
||||
data,
|
||||
};
|
||||
} catch (err) {
|
||||
throw new BadRequestException(err.message);
|
||||
}
|
||||
}
|
||||
|
||||
@Get(':id')
|
||||
findOne(@Param('id') id: string) {
|
||||
return this.programaService.findOne(+id);
|
||||
// ==================== 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);
|
||||
}
|
||||
}
|
||||
|
||||
@Patch(':id')
|
||||
update(@Param('id') id: string, @Body() updateProgramaDto: UpdateProgramaDto) {
|
||||
return this.programaService.update(+id, updateProgramaDto);
|
||||
}
|
||||
|
||||
@Delete(':id')
|
||||
remove(@Param('id') id: string) {
|
||||
return this.programaService.remove(+id);
|
||||
// ==================== 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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user