origen desconocido, falta registrar los origenes y relacionarlos
This commit is contained in:
@@ -1,27 +1,19 @@
|
||||
// src/excel/excel.controller.ts
|
||||
import {
|
||||
Controller,
|
||||
Post,
|
||||
UploadedFile,
|
||||
UseInterceptors,
|
||||
BadRequestException,
|
||||
Request,
|
||||
Get,
|
||||
Res,
|
||||
HttpStatus,
|
||||
} from '@nestjs/common';
|
||||
|
||||
import { Controller, Post, Get, UseGuards, Request, Res, UploadedFile, UseInterceptors, HttpStatus } from '@nestjs/common';
|
||||
import { AuthGuard } from '@nestjs/passport';
|
||||
import { ApiBearerAuth } from '@nestjs/swagger';
|
||||
import { FileInterceptor } from '@nestjs/platform-express';
|
||||
import { ExcelService } from './excel.service';
|
||||
import { ExcelDocumentation } from './excel.documentation';
|
||||
import { MovimientoService } from 'src/movimiento/movimiento.service';
|
||||
import { Response } from 'express';
|
||||
import { ExcelDocumentation } from './excel.documentation';
|
||||
import { ExcelService } from './excel.service';
|
||||
import { MovimientoService } from '../movimiento/movimiento.service';
|
||||
|
||||
@UseGuards(AuthGuard('jwt'))
|
||||
@ApiBearerAuth('bearer')
|
||||
@Controller('excel')
|
||||
export class ExcelController {
|
||||
constructor(
|
||||
private readonly excelService: ExcelService,
|
||||
|
||||
private readonly movimientoService: MovimientoService,
|
||||
) {}
|
||||
|
||||
@@ -29,18 +21,14 @@ export class ExcelController {
|
||||
@UseInterceptors(FileInterceptor('file'))
|
||||
@ExcelDocumentation.verifyExcel()
|
||||
async verifyExcel(@UploadedFile() file: Express.Multer.File, @Request() req) {
|
||||
if (!file) throw new BadRequestException('Se requiere un archivo .xlsx');
|
||||
const userId = req.user.userId;
|
||||
|
||||
const userId: number = req.user.userId; // ahora sí existe
|
||||
const errors = await this.excelService.validateFile(file.buffer);
|
||||
|
||||
await this.movimientoService.log(
|
||||
userId,
|
||||
'VERIFY',
|
||||
errors.length ? 'FAILED' : 'SUCCESS',
|
||||
errors.join('; '),
|
||||
errors.join('; ')
|
||||
);
|
||||
|
||||
return { valid: errors.length === 0, errors };
|
||||
}
|
||||
|
||||
@@ -48,77 +36,56 @@ export class ExcelController {
|
||||
@UseInterceptors(FileInterceptor('file'))
|
||||
@ExcelDocumentation.loadExcel()
|
||||
async loadExcel(@UploadedFile() file: Express.Multer.File, @Request() req) {
|
||||
const userId = req.user.userId;
|
||||
const userId: number = req.user.userId;
|
||||
try {
|
||||
const { inserted } = await this.excelService.loadFile(file.buffer);
|
||||
const result = await this.excelService.loadFile(file.buffer);
|
||||
await this.movimientoService.log(
|
||||
userId,
|
||||
'LOAD',
|
||||
'SUCCESS',
|
||||
undefined,
|
||||
`inserted=${inserted}`,
|
||||
`inserted=${result.inserted}`
|
||||
);
|
||||
return { inserted };
|
||||
return result;
|
||||
} catch (err) {
|
||||
await this.movimientoService.log(userId, 'LOAD', 'FAILED', err.message);
|
||||
await this.movimientoService.log(
|
||||
userId,
|
||||
'LOAD',
|
||||
'FAILED',
|
||||
err.message
|
||||
);
|
||||
throw err;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@ExcelDocumentation.downloadExcel() // <-- decorador correcto
|
||||
@Get('download')
|
||||
@ExcelDocumentation.downloadExcel()
|
||||
async downloadData(@Request() req, @Res() res: Response) {
|
||||
const userId: number = req.user.userId; // asegúrate de que sea primitivo
|
||||
|
||||
const userId: number = req.user.userId;
|
||||
try {
|
||||
// 1) Genera los datos
|
||||
const data = await this.excelService.generateCsv();
|
||||
|
||||
// 2) Log SUCCESS
|
||||
const csv = await this.excelService.generateCsv();
|
||||
await this.movimientoService.log(
|
||||
userId, // <-- solo el valor, no userId()
|
||||
userId,
|
||||
'DOWNLOAD',
|
||||
'SUCCESS',
|
||||
undefined,
|
||||
`size=${data.length}`,
|
||||
`size=${csv.length}`
|
||||
);
|
||||
|
||||
// 3) Devuelve la respuesta
|
||||
return res
|
||||
.status(HttpStatus.OK) // <-- HttpStatus importado
|
||||
.status(HttpStatus.OK)
|
||||
.header('Content-Type', 'text/tab-separated-values')
|
||||
.header('Content-Disposition', 'attachment; filename="usuarios.tsv"')
|
||||
.send(data);
|
||||
.send(csv);
|
||||
} catch (err) {
|
||||
// 4) Log FAILED
|
||||
await this.movimientoService.log(
|
||||
userId,
|
||||
'DOWNLOAD',
|
||||
'FAILED',
|
||||
err.message,
|
||||
err.message
|
||||
);
|
||||
|
||||
// 5) Responde el error
|
||||
return res
|
||||
.status(HttpStatus.INTERNAL_SERVER_ERROR)
|
||||
.json({ statusCode: 500, message: 'Error generando descarga.' });
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user