Files
cargaMasiva_api/src/excel/excel.documentation.ts
T

138 lines
3.4 KiB
TypeScript
Raw Normal View History

import { applyDecorators } from '@nestjs/common';
import { ApiTags, ApiOperation, ApiConsumes, ApiBody, ApiResponse } from '@nestjs/swagger';
export class ExcelDocumentation {
/**
* Decorators Swagger para el endpoint POST /excel/verify
*/
static verifyExcel() {
return applyDecorators(
ApiTags('Excel'),
ApiOperation({
summary: 'Verificar archivo Excel',
description: 'Valida duplicados, filas en blanco y formato básico de los datos sin realizar carga en la base.'
}),
ApiConsumes('multipart/form-data'),
ApiBody({
schema: {
type: 'object',
properties: {
file: {
type: 'string',
format: 'binary',
description: 'Archivo Excel (.xlsx) con usuarios a validar'
}
}
}
}),
ApiResponse({
status: 200,
description: 'Resultado de la validación',
schema: {
type: 'object',
properties: {
valid: { type: 'boolean', example: false },
errors: {
type: 'array',
items: { type: 'string' },
example: ['Fila 2: cuenta duplicada "42515101".', 'Fila 3: fecha de nacimiento inválida "1988051".']
}
}
}
}),
ApiResponse({ status: 400, description: 'No se recibió archivo o formato inválido.' })
);
}
/**
* Decorators Swagger para el endpoint POST /excel/load
*/
static loadExcel() {
return applyDecorators(
ApiTags('Excel'),
ApiOperation({
summary: 'Cargar archivo Excel',
description: 'Valida y persiste los datos en la base de datos. Retorna el número de registros insertados.'
}),
ApiConsumes('multipart/form-data'),
ApiBody({
schema: {
type: 'object',
properties: {
file: {
type: 'string',
format: 'binary',
description: 'Archivo Excel (.xlsx) con usuarios a cargar'
}
}
}
}),
ApiResponse({
status: 201,
description: 'Usuarios insertados correctamente',
schema: {
type: 'object',
properties: {
inserted: { type: 'number', example: 3 }
}
}
}),
ApiResponse({
status: 400,
description: 'Errores de validación o carga',
schema: {
type: 'object',
properties: {
errors: {
type: 'array',
items: { type: 'string' },
example: ['Fila 5: rfc contiene caracteres inválidos.']
}
}
}
})
);
}
static downloadExcel() {
return applyDecorators(
ApiTags('Excel'),
//ApiBearerAuth('bearer'),
ApiOperation({
summary: 'Descargar usuarios',
description: 'Exporta todos los usuarios en un archivo TSV (o CSV) y registra el movimiento.'
}),
ApiResponse({
status: 200,
description: 'TSV generado correctamente',
content: {
'text/tab-separated-values': {
schema: { type: 'string', example: 'num_cuenta\\tnombre\\t...\\n...' }
}
}
}),
ApiResponse({ status: 401, description: 'No autorizado' }),
ApiResponse({ status: 500, description: 'Error al generar descarga' }),
);
}
}