123 lines
3.5 KiB
TypeScript
123 lines
3.5 KiB
TypeScript
import {
|
|
BadRequestException,
|
|
Controller,
|
|
Get,
|
|
Post,
|
|
Query,
|
|
Response,
|
|
Request,
|
|
UploadedFile,
|
|
UseGuards,
|
|
UseInterceptors,
|
|
} from '@nestjs/common';
|
|
import { AuthGuard } from '@nestjs/passport';
|
|
import { FileInterceptor } from '@nestjs/platform-express';
|
|
import { ApiBearerAuth, ApiTags } from '@nestjs/swagger';
|
|
import { UploadFileService } from './upload-file.service';
|
|
import { Operador } from '../operador/entity/operador.entity';
|
|
import { ValidarUsuarioService } from '../validar-usuario/validar-usuario.service';
|
|
import { IdInstitucionDto } from '../dto/id-institucion.dto';
|
|
|
|
@Controller('upload-file')
|
|
@ApiTags('upload-file')
|
|
export class UploadFileController {
|
|
constructor(
|
|
private uploadFileService: UploadFileService,
|
|
private validarUsuarioService: ValidarUsuarioService,
|
|
) {}
|
|
|
|
@Post('carga-masiva-equipos')
|
|
@UseGuards(AuthGuard('jwt'))
|
|
@UseInterceptors(FileInterceptor('csv'))
|
|
@ApiBearerAuth('jwt')
|
|
cargaMasivaEquipos(
|
|
@Request() req,
|
|
@UploadedFile() file: Express.Multer.File,
|
|
@Query() query: IdInstitucionDto,
|
|
) {
|
|
const admin: Operador = req.user.operador;
|
|
const path = file ? `${file.destination}/${file.filename}` : null;
|
|
|
|
this.validarUsuarioService.validarAdmin(admin);
|
|
if (!file) throw new BadRequestException('No se mandó ningún archivo.');
|
|
return this.uploadFileService.createEquipos(
|
|
path,
|
|
parseInt(query.id_institucion),
|
|
);
|
|
}
|
|
|
|
@Post('carga-masiva-usuarios')
|
|
@UseGuards(AuthGuard('jwt'))
|
|
@UseInterceptors(FileInterceptor('csv'))
|
|
@ApiBearerAuth('jwt')
|
|
cargaMasivaUsuarios(
|
|
@Request() req,
|
|
@UploadedFile() file: Express.Multer.File,
|
|
@Query() query: IdInstitucionDto,
|
|
) {
|
|
const admin: Operador = req.user.operador;
|
|
const path = file ? `${file.destination}/${file.filename}` : null;
|
|
|
|
this.validarUsuarioService.validarAdmin(admin);
|
|
if (!file) throw new BadRequestException('No se mandó ningún archivo.');
|
|
return this.uploadFileService.createUsuarios(
|
|
path,
|
|
parseInt(query.id_institucion),
|
|
);
|
|
}
|
|
|
|
// @Get('download-logo')
|
|
// @UseGuards(AuthGuard('jwt'))
|
|
// @ApiBearerAuth('jwt')
|
|
// downloadLogo(
|
|
// @Request() req,
|
|
// @Response() res,
|
|
// @Query() query: IdInstitucionDto,
|
|
// ) {
|
|
// const operador: Operador = req.user.operador;
|
|
|
|
// this.validarUsuarioService.validarAdminOperador(operador);
|
|
// return this.uploadFileService
|
|
// .downloadLogo(parseInt(query.id_institucion))
|
|
// .then((logo) => res.download(logo));
|
|
// }
|
|
|
|
@Get('download-plantilla-equipos')
|
|
@UseGuards(AuthGuard('jwt'))
|
|
@ApiBearerAuth('jwt')
|
|
downloadPlantillaEquipos(@Request() req, @Response() res) {
|
|
const admin: Operador = req.user.operador;
|
|
|
|
this.validarUsuarioService.validarAdmin(admin);
|
|
return res.download('./upload/plantilla_equipos.csv');
|
|
}
|
|
|
|
@Get('download-plantilla-usuarios')
|
|
@UseGuards(AuthGuard('jwt'))
|
|
@ApiBearerAuth('jwt')
|
|
downloadPlantillaUsuarios(@Request() req, @Response() res) {
|
|
const admin: Operador = req.user.operador;
|
|
|
|
this.validarUsuarioService.validarAdmin(admin);
|
|
return res.download('./upload/plantilla_alumnos.csv');
|
|
}
|
|
|
|
@Post('upload-logo')
|
|
@UseGuards(AuthGuard('jwt'))
|
|
@ApiBearerAuth('jwt')
|
|
@UseInterceptors(FileInterceptor('logo'))
|
|
uploadLogo(
|
|
@Request() req,
|
|
@UploadedFile() file: Express.Multer.File,
|
|
@Query() query: IdInstitucionDto,
|
|
) {
|
|
const admin: Operador = req.user.operador;
|
|
|
|
this.validarUsuarioService.validarAdmin(admin);
|
|
return this.uploadFileService.uploadLogo(
|
|
file,
|
|
parseInt(query.id_institucion),
|
|
);
|
|
}
|
|
}
|