Files
pcpuma_unam_api/src/upload-file/upload-file.controller.ts
T

85 lines
2.3 KiB
TypeScript
Raw Normal View History

2022-06-02 14:22:27 -05:00
import * as fs from 'fs';
2022-04-23 23:55:07 -05:00
import {
2022-06-16 09:02:14 -05:00
BadRequestException,
2022-04-23 23:55:07 -05:00
Controller,
Get,
Post,
Query,
Response,
UploadedFile,
UseInterceptors,
} from '@nestjs/common';
import { FileInterceptor } from '@nestjs/platform-express';
2022-06-02 14:22:27 -05:00
import { ApiTags } from '@nestjs/swagger';
2022-04-23 23:55:07 -05:00
import { UploadFileService } from './upload-file.service';
2022-04-24 12:00:56 -05:00
import { IdInstitucionDto } from '../dto/id-institucion.dto';
2022-04-23 23:55:07 -05:00
@Controller('upload-file')
2022-06-02 14:22:27 -05:00
@ApiTags('upload-file')
2022-04-23 23:55:07 -05:00
export class UploadFileController {
constructor(private uploadFileService: UploadFileService) {}
@Post('carga-masiva-equipos')
2022-04-27 00:47:09 -05:00
@UseInterceptors(FileInterceptor('csv'))
2022-04-23 23:55:07 -05:00
cargaMasivaEquipos(
@UploadedFile() file: Express.Multer.File,
2022-06-06 15:00:36 -05:00
@Query() query: IdInstitucionDto,
2022-04-23 23:55:07 -05:00
) {
2022-06-16 09:02:14 -05:00
const path = file ? `${file.destination}/${file.filename}` : null;
if (!file) throw new BadRequestException('No se mandó ningún archivo.');
2022-06-07 22:47:46 -05:00
return this.uploadFileService
2022-06-16 09:02:14 -05:00
.createEquipos(path, parseInt(query.id_institucion))
2022-06-07 22:47:46 -05:00
.then((res) => {
2022-06-16 09:02:14 -05:00
fs.unlink(path, (err) => {});
return res;
2022-06-07 22:47:46 -05:00
});
2022-04-23 23:55:07 -05:00
}
@Post('carga-masiva-usuarios')
2022-04-27 00:47:09 -05:00
@UseInterceptors(FileInterceptor('csv'))
2022-04-23 23:55:07 -05:00
cargaMasivaUsuarios(
@UploadedFile() file: Express.Multer.File,
@Query() query: IdInstitucionDto,
) {
2022-06-16 09:02:14 -05:00
const path = file ? `${file.destination}/${file.filename}` : null;
if (!file) throw new BadRequestException('No se mandó ningún archivo.');
2022-06-02 14:22:27 -05:00
return this.uploadFileService
2022-06-16 09:02:14 -05:00
.createUsuarios(path, parseInt(query.id_institucion))
2022-06-02 14:22:27 -05:00
.then((res) => {
2022-06-16 09:02:14 -05:00
fs.unlink(path, (err) => {});
return res;
2022-06-02 14:22:27 -05:00
});
2022-04-23 23:55:07 -05:00
}
@Get('download-logo')
downloadLogo(@Response() res, @Query() query: IdInstitucionDto) {
2022-06-16 09:02:14 -05:00
return this.uploadFileService
.downloadLogo(parseInt(query.id_institucion))
.then((logo) => res.download(logo));
2022-04-23 23:55:07 -05:00
}
2022-04-25 11:35:21 -05:00
@Get('download-plantilla-equipos')
2022-05-09 14:49:38 -05:00
downloadPlantillaEquipos(@Response() res) {
2022-04-27 00:47:09 -05:00
return res.download('./upload/plantilla_equipos.csv');
2022-04-25 11:35:21 -05:00
}
@Get('download-plantilla-usuarios')
2022-05-09 14:49:38 -05:00
downloadPlantillaUsuarios(@Response() res) {
2022-04-27 00:47:09 -05:00
return res.download('./upload/plantilla_alumnos.csv');
2022-04-25 11:35:21 -05:00
}
2022-04-23 23:55:07 -05:00
@Post('upload-logo')
2022-06-16 09:02:14 -05:00
@UseInterceptors(FileInterceptor('logo'))
2022-04-23 23:55:07 -05:00
uploadLogo(
@UploadedFile() file: Express.Multer.File,
@Query() query: IdInstitucionDto,
) {
2022-06-16 09:02:14 -05:00
return this.uploadFileService.uploadLogo(
file,
parseInt(query.id_institucion),
);
2022-04-23 23:55:07 -05:00
}
}