upload casi listo
This commit is contained in:
@@ -1,5 +1,6 @@
|
||||
import * as fs from 'fs';
|
||||
import {
|
||||
BadRequestException,
|
||||
Controller,
|
||||
Get,
|
||||
Post,
|
||||
@@ -24,11 +25,14 @@ export class UploadFileController {
|
||||
@UploadedFile() file: Express.Multer.File,
|
||||
@Query() query: IdInstitucionDto,
|
||||
) {
|
||||
const path = file ? `${file.destination}/${file.filename}` : null;
|
||||
|
||||
if (!file) throw new BadRequestException('No se mandó ningún archivo.');
|
||||
return this.uploadFileService
|
||||
.createEquipos(file, parseInt(query.id_institucion))
|
||||
.createEquipos(path, parseInt(query.id_institucion))
|
||||
.then((res) => {
|
||||
fs.unlink(res.path, (err) => {});
|
||||
return res.response;
|
||||
fs.unlink(path, (err) => {});
|
||||
return res;
|
||||
});
|
||||
}
|
||||
|
||||
@@ -38,17 +42,22 @@ export class UploadFileController {
|
||||
@UploadedFile() file: Express.Multer.File,
|
||||
@Query() query: IdInstitucionDto,
|
||||
) {
|
||||
const path = file ? `${file.destination}/${file.filename}` : null;
|
||||
|
||||
if (!file) throw new BadRequestException('No se mandó ningún archivo.');
|
||||
return this.uploadFileService
|
||||
.createUsuarios(file, parseInt(query.id_institucion))
|
||||
.createUsuarios(path, parseInt(query.id_institucion))
|
||||
.then((res) => {
|
||||
fs.unlink(res.path, (err) => {});
|
||||
return res.response;
|
||||
fs.unlink(path, (err) => {});
|
||||
return res;
|
||||
});
|
||||
}
|
||||
|
||||
@Get('download-logo')
|
||||
downloadLogo(@Response() res, @Query() query: IdInstitucionDto) {
|
||||
return this.uploadFileService.downloadLogo(parseInt(query.id_institucion));
|
||||
return this.uploadFileService
|
||||
.downloadLogo(parseInt(query.id_institucion))
|
||||
.then((logo) => res.download(logo));
|
||||
}
|
||||
|
||||
@Get('download-plantilla-equipos')
|
||||
@@ -62,11 +71,14 @@ export class UploadFileController {
|
||||
}
|
||||
|
||||
@Post('upload-logo')
|
||||
@UseInterceptors(FileInterceptor('file'))
|
||||
@UseInterceptors(FileInterceptor('logo'))
|
||||
uploadLogo(
|
||||
@UploadedFile() file: Express.Multer.File,
|
||||
@Query() query: IdInstitucionDto,
|
||||
) {
|
||||
this.uploadFileService.uploadLogo(file, parseInt(query.id_institucion));
|
||||
return this.uploadFileService.uploadLogo(
|
||||
file,
|
||||
parseInt(query.id_institucion),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -34,17 +34,15 @@ export class UploadFileService {
|
||||
private usuarioService: UsuarioService,
|
||||
) {}
|
||||
|
||||
async createEquipos(file: Express.Multer.File, id_institucion: number) {
|
||||
const path = `${file.destination}/${file.filename}`;
|
||||
async createEquipos(path: string, id_institucion: number) {
|
||||
const institucion = await this.institucionService.findById(id_institucion);
|
||||
const errores: string[] = [];
|
||||
const mensajes: string[] = [];
|
||||
const equiposNuevos: Equipo[] = [];
|
||||
|
||||
if (!file) throw new BadRequestException('No se mando ningún archivo.');
|
||||
return csvtojson()
|
||||
.fromFile(path)
|
||||
.then(async (equipos: [UploadFileCargaMasivaEquipoDto]) => {
|
||||
.then(async (equipos: UploadFileCargaMasivaEquipoDto[]) => {
|
||||
for (let i = 0; i < equipos.length; i++) {
|
||||
let error = this.errorBase(i);
|
||||
|
||||
@@ -235,28 +233,23 @@ export class UploadFileService {
|
||||
}
|
||||
}
|
||||
return {
|
||||
response: {
|
||||
message: 'Se subió y cargó correctamente tu archivo csv.',
|
||||
mensajes,
|
||||
equiposNuevos,
|
||||
errores,
|
||||
},
|
||||
path,
|
||||
message: 'Se subió y cargó correctamente tu archivo csv.',
|
||||
mensajes,
|
||||
equiposNuevos,
|
||||
errores,
|
||||
};
|
||||
});
|
||||
}
|
||||
|
||||
async createUsuarios(file: Express.Multer.File, id_institucion: number) {
|
||||
const path = `${file.destination}/${file.filename}`;
|
||||
async createUsuarios(path: string, id_institucion: number) {
|
||||
const institucion = await this.institucionService.findById(id_institucion);
|
||||
const errores: string[] = [];
|
||||
const mensajes: string[] = [];
|
||||
const usuariosNuevos: Usuario[] = [];
|
||||
|
||||
if (!file) throw new BadRequestException('No se mando ningún archivo.');
|
||||
return csvtojson()
|
||||
.fromFile(path)
|
||||
.then(async (usuarios: [UploadFileCargaMasivaUsuarioDto]) => {
|
||||
.then(async (usuarios: UploadFileCargaMasivaUsuarioDto[]) => {
|
||||
for (let i = 0; i < usuarios.length; i++) {
|
||||
let error = this.errorBase(i);
|
||||
|
||||
@@ -354,24 +347,34 @@ export class UploadFileService {
|
||||
});
|
||||
}
|
||||
return {
|
||||
response: {
|
||||
message: 'Se subió y cargó correctamente tu archivo csv.',
|
||||
mensajes,
|
||||
usuariosNuevos,
|
||||
errores,
|
||||
},
|
||||
path,
|
||||
message: 'Se subió y cargó correctamente tu archivo csv.',
|
||||
mensajes,
|
||||
usuariosNuevos,
|
||||
errores,
|
||||
};
|
||||
});
|
||||
}
|
||||
|
||||
downloadLogo(id_institucion: number) {}
|
||||
downloadLogo(id_institucion: number) {
|
||||
return this.institucionService
|
||||
.findById(id_institucion)
|
||||
.then((institucion) => institucion.logo);
|
||||
}
|
||||
|
||||
errorBase(index) {
|
||||
return `Se saltó la linea ${
|
||||
index + 2
|
||||
} por el siguiente/los siguientes motivos(s):`;
|
||||
} por el siguiente/los siguientes motivo(s):`;
|
||||
}
|
||||
|
||||
uploadLogo(file: Express.Multer.File, id_institucion: number) {}
|
||||
async uploadLogo(file: Express.Multer.File, id_institucion: number) {
|
||||
const path = file ? `${file.destination}/${file.filename}` : null;
|
||||
const institucion = await this.institucionService.findById(id_institucion);
|
||||
|
||||
if (!file) throw new BadRequestException('No se mandó ningún logo.');
|
||||
institucion.logo = path;
|
||||
return this.institucionService
|
||||
.update(institucion)
|
||||
.then((_) => ({ message: 'Se subió correctamente el logo.' }));
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user