2025-06-13 17:54:38 -06:00
|
|
|
// src/excel/excel.controller.ts
|
2025-06-16 14:14:23 -06:00
|
|
|
import { Controller, Post, Get, UseGuards, Request, Res, UploadedFile, UseInterceptors, HttpStatus } from '@nestjs/common';
|
|
|
|
|
import { AuthGuard } from '@nestjs/passport';
|
|
|
|
|
import { ApiBearerAuth } from '@nestjs/swagger';
|
2025-06-13 17:54:38 -06:00
|
|
|
import { FileInterceptor } from '@nestjs/platform-express';
|
2025-06-16 11:26:17 -06:00
|
|
|
import { Response } from 'express';
|
2025-06-16 14:14:23 -06:00
|
|
|
import { ExcelDocumentation } from './excel.documentation';
|
|
|
|
|
import { ExcelService } from './excel.service';
|
|
|
|
|
import { MovimientoService } from '../movimiento/movimiento.service';
|
2025-06-13 17:54:38 -06:00
|
|
|
|
2025-06-16 14:14:23 -06:00
|
|
|
@UseGuards(AuthGuard('jwt'))
|
|
|
|
|
@ApiBearerAuth('bearer')
|
2025-06-13 17:54:38 -06:00
|
|
|
@Controller('excel')
|
|
|
|
|
export class ExcelController {
|
2025-06-16 11:26:17 -06:00
|
|
|
constructor(
|
|
|
|
|
private readonly excelService: ExcelService,
|
|
|
|
|
private readonly movimientoService: MovimientoService,
|
|
|
|
|
) {}
|
2025-06-13 17:54:38 -06:00
|
|
|
|
|
|
|
|
@Post('verify')
|
|
|
|
|
@UseInterceptors(FileInterceptor('file'))
|
|
|
|
|
@ExcelDocumentation.verifyExcel()
|
2025-06-16 11:26:17 -06:00
|
|
|
async verifyExcel(@UploadedFile() file: Express.Multer.File, @Request() req) {
|
2025-06-16 14:14:23 -06:00
|
|
|
const userId: number = req.user.userId; // ahora sí existe
|
2025-06-13 17:54:38 -06:00
|
|
|
const errors = await this.excelService.validateFile(file.buffer);
|
2025-06-16 11:26:17 -06:00
|
|
|
await this.movimientoService.log(
|
|
|
|
|
userId,
|
|
|
|
|
'VERIFY',
|
|
|
|
|
errors.length ? 'FAILED' : 'SUCCESS',
|
2025-06-16 14:14:23 -06:00
|
|
|
errors.join('; ')
|
2025-06-16 11:26:17 -06:00
|
|
|
);
|
2025-06-13 17:54:38 -06:00
|
|
|
return { valid: errors.length === 0, errors };
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Post('load')
|
|
|
|
|
@UseInterceptors(FileInterceptor('file'))
|
|
|
|
|
@ExcelDocumentation.loadExcel()
|
2025-06-16 11:26:17 -06:00
|
|
|
async loadExcel(@UploadedFile() file: Express.Multer.File, @Request() req) {
|
2025-06-16 14:14:23 -06:00
|
|
|
const userId: number = req.user.userId;
|
2025-06-16 11:26:17 -06:00
|
|
|
try {
|
2025-06-16 14:14:23 -06:00
|
|
|
const result = await this.excelService.loadFile(file.buffer);
|
2025-06-16 11:26:17 -06:00
|
|
|
await this.movimientoService.log(
|
|
|
|
|
userId,
|
|
|
|
|
'LOAD',
|
|
|
|
|
'SUCCESS',
|
|
|
|
|
undefined,
|
2025-06-16 14:14:23 -06:00
|
|
|
`inserted=${result.inserted}`
|
2025-06-16 11:26:17 -06:00
|
|
|
);
|
2025-06-16 14:14:23 -06:00
|
|
|
return result;
|
2025-06-16 11:26:17 -06:00
|
|
|
} catch (err) {
|
2025-06-16 14:14:23 -06:00
|
|
|
await this.movimientoService.log(
|
|
|
|
|
userId,
|
|
|
|
|
'LOAD',
|
|
|
|
|
'FAILED',
|
|
|
|
|
err.message
|
|
|
|
|
);
|
2025-06-16 11:26:17 -06:00
|
|
|
throw err;
|
|
|
|
|
}
|
2025-06-13 17:54:38 -06:00
|
|
|
}
|
2025-06-16 11:26:17 -06:00
|
|
|
|
|
|
|
|
@Get('download')
|
2025-06-16 14:14:23 -06:00
|
|
|
@ExcelDocumentation.downloadExcel()
|
2025-06-16 11:26:17 -06:00
|
|
|
async downloadData(@Request() req, @Res() res: Response) {
|
2025-06-16 14:14:23 -06:00
|
|
|
const userId: number = req.user.userId;
|
2025-06-16 11:26:17 -06:00
|
|
|
try {
|
2025-06-16 14:14:23 -06:00
|
|
|
const csv = await this.excelService.generateCsv();
|
2025-06-16 11:26:17 -06:00
|
|
|
await this.movimientoService.log(
|
2025-06-16 14:14:23 -06:00
|
|
|
userId,
|
2025-06-16 11:26:17 -06:00
|
|
|
'DOWNLOAD',
|
|
|
|
|
'SUCCESS',
|
|
|
|
|
undefined,
|
2025-06-16 14:14:23 -06:00
|
|
|
`size=${csv.length}`
|
2025-06-16 11:26:17 -06:00
|
|
|
);
|
|
|
|
|
return res
|
2025-06-16 14:14:23 -06:00
|
|
|
.status(HttpStatus.OK)
|
2025-06-16 11:26:17 -06:00
|
|
|
.header('Content-Type', 'text/tab-separated-values')
|
|
|
|
|
.header('Content-Disposition', 'attachment; filename="usuarios.tsv"')
|
2025-06-16 14:14:23 -06:00
|
|
|
.send(csv);
|
2025-06-16 11:26:17 -06:00
|
|
|
} catch (err) {
|
|
|
|
|
await this.movimientoService.log(
|
|
|
|
|
userId,
|
|
|
|
|
'DOWNLOAD',
|
|
|
|
|
'FAILED',
|
2025-06-16 14:14:23 -06:00
|
|
|
err.message
|
2025-06-16 11:26:17 -06:00
|
|
|
);
|
|
|
|
|
return res
|
|
|
|
|
.status(HttpStatus.INTERNAL_SERVER_ERROR)
|
|
|
|
|
.json({ statusCode: 500, message: 'Error generando descarga.' });
|
|
|
|
|
}
|
|
|
|
|
}
|
2025-06-13 17:54:38 -06:00
|
|
|
}
|