177 lines
4.9 KiB
TypeScript
177 lines
4.9 KiB
TypeScript
import {
|
|
Controller,
|
|
Get,
|
|
Post,
|
|
Body,
|
|
Patch,
|
|
Param,
|
|
Delete,
|
|
UseInterceptors,
|
|
ParseIntPipe,
|
|
UploadedFile,
|
|
UnprocessableEntityException,
|
|
UploadedFiles,
|
|
BadRequestException,
|
|
} from '@nestjs/common';
|
|
import { CuestionarioService } from './cuestionario.service';
|
|
import { CreateCuestionarioDto } from './dto/create-cuestionario.dto';
|
|
import { UpdateCuestionarioDto } from './dto/update-cuestionario.dto';
|
|
import { FileInterceptor, FilesInterceptor } from '@nestjs/platform-express';
|
|
import { diskStorage } from 'multer';
|
|
import { extname } from 'path';
|
|
import { CuestionarioApiDocumentation } from './docs/cuestionario.documentation';
|
|
import { CreateEventoWithCuestionarioDto } from './dto/create-evento-cuestionario.dto';
|
|
import * as ExcelJS from 'exceljs';
|
|
import { TipoPreguntaEnum } from 'src/tipo_pregunta/entities/tipo_pregunta.entity';
|
|
import { TiposValidacion } from 'src/pregunta/entities/pregunta.entity';
|
|
|
|
@Controller('cuestionario')
|
|
@CuestionarioApiDocumentation.ApiController
|
|
export class CuestionarioController {
|
|
constructor(private readonly cuestionarioService: CuestionarioService) {}
|
|
|
|
@Post()
|
|
@CuestionarioApiDocumentation.ApiCreate
|
|
create(@Body() createCuestionarioDto: CreateCuestionarioDto) {
|
|
return this.cuestionarioService.create(createCuestionarioDto);
|
|
}
|
|
|
|
@Post('banners/bulk')
|
|
@UseInterceptors(
|
|
FilesInterceptor('banners', 20, { // puedes subir hasta 20 imágenes
|
|
storage: diskStorage({
|
|
destination: './uploads/banners',
|
|
filename: (req, file, cb) => {
|
|
cb(null, file.originalname);
|
|
|
|
|
|
},
|
|
}),
|
|
fileFilter: (req, file, cb) => {
|
|
const allowedTypes = /jpeg|jpg|png|webp/;
|
|
const ext = extname(file.originalname).toLowerCase();
|
|
if (allowedTypes.test(ext)) {
|
|
cb(null, true);
|
|
} else {
|
|
cb(new Error('Tipo de archivo no permitido'), false);
|
|
}
|
|
},
|
|
}),
|
|
)async subirBanners(@UploadedFiles() files: Express.Multer.File[]) {
|
|
if (!files || files.length === 0) {
|
|
throw new UnprocessableEntityException('No se subió ningún archivo');
|
|
}
|
|
|
|
// Retornamos lista de filenames para asociarlos después
|
|
return files.map((file) => ({
|
|
originalName: file.originalname,
|
|
filename: file.filename,
|
|
path: file.path,
|
|
}));
|
|
}
|
|
|
|
@Post('carga-masiva')
|
|
@UseInterceptors(FileInterceptor('file'))
|
|
async cargaMasiva(@UploadedFile() file: Express.Multer.File) {
|
|
if (!file) {
|
|
throw new BadRequestException('No se ha subido ningún archivo');
|
|
}
|
|
|
|
const cuestionarios = await this.cargaMasiva(file);
|
|
|
|
return {
|
|
message: 'Carga masiva exitosa',
|
|
total: cuestionarios.length,
|
|
data: cuestionarios,
|
|
};
|
|
}
|
|
|
|
|
|
|
|
@Post('withEvento')
|
|
@CuestionarioApiDocumentation.ApiCreateWithEvento
|
|
createCuestionarioEvento(
|
|
@Body() createCuestionarioDto: CreateEventoWithCuestionarioDto,
|
|
) {
|
|
return this.cuestionarioService.createCuestionarioEvento(
|
|
createCuestionarioDto,
|
|
);
|
|
}
|
|
|
|
|
|
|
|
|
|
@Post(':id/banner')
|
|
@UseInterceptors(
|
|
FileInterceptor('banner', {
|
|
storage: diskStorage({
|
|
destination: './uploads/banners', // Asegúrate de que esta carpeta exista
|
|
filename: (req, file, cb) => {
|
|
const uniqueSuffix =
|
|
Date.now() + '-' + Math.round(Math.random() * 1e9);
|
|
const ext = extname(file.originalname);
|
|
cb(null, `banner-${uniqueSuffix}${ext}`);
|
|
},
|
|
}),
|
|
fileFilter: (req, file, cb) => {
|
|
const allowedTypes = /jpeg|jpg|png|webp/;
|
|
const ext = extname(file.originalname).toLowerCase();
|
|
if (allowedTypes.test(ext)) {
|
|
cb(null, true);
|
|
} else {
|
|
cb(new Error('Tipo de archivo no permitido'), false);
|
|
}
|
|
},
|
|
}),
|
|
)
|
|
async subirBannerEvento(
|
|
@Param('id', ParseIntPipe) id: number,
|
|
@UploadedFile() file: Express.Multer.File,
|
|
) {
|
|
if (!file) {
|
|
throw new Error('No se ha recibido un archivo válido');
|
|
}
|
|
|
|
return this.cuestionarioService.asociarBanner(id, file.filename);
|
|
}
|
|
|
|
@Get()
|
|
@CuestionarioApiDocumentation.ApiGetAll
|
|
findAll() {
|
|
return this.cuestionarioService.findAll();
|
|
}
|
|
|
|
@Get('recientes')
|
|
@CuestionarioApiDocumentation.ApiGetAll
|
|
findAllRecientes() {
|
|
return this.cuestionarioService.findAllRecientes();
|
|
}
|
|
|
|
@Get(':id')
|
|
@CuestionarioApiDocumentation.ApiGetOne
|
|
findOne(@Param('id') id: string) {
|
|
return this.cuestionarioService.findOne(+id);
|
|
}
|
|
|
|
@Get(':id/formulario')
|
|
@CuestionarioApiDocumentation.ApiGetFormulario
|
|
findFormulario(@Param('id') id: string) {
|
|
return this.cuestionarioService.findCompleto(+id);
|
|
}
|
|
|
|
@Patch(':id')
|
|
@CuestionarioApiDocumentation.ApiUpdate
|
|
update(
|
|
@Param('id') id: string,
|
|
@Body() updateCuestionarioDto: UpdateCuestionarioDto,
|
|
) {
|
|
return this.cuestionarioService.update(+id, updateCuestionarioDto);
|
|
}
|
|
|
|
@Delete(':id')
|
|
@CuestionarioApiDocumentation.ApiRemove
|
|
remove(@Param('id') id: string) {
|
|
return this.cuestionarioService.remove(+id);
|
|
}
|
|
}
|