Exportar datos de osp de la db en un excel
This commit is contained in:
@@ -5,6 +5,11 @@ import { NodePgDatabase } from 'drizzle-orm/node-postgres';
|
||||
import { DRIZZLE_PROVIDER } from 'src/database/drizzle-provider';
|
||||
import * as schema from 'src/database/index';
|
||||
import { municipalities, parishes, states, trainingSurveys } from 'src/database/index';
|
||||
import * as fs from 'fs';
|
||||
import * as path from 'path';
|
||||
// @ts-ignore
|
||||
import XlsxPopulate from 'xlsx-populate';
|
||||
|
||||
|
||||
import { PaginationDto } from '../../common/dto/pagination.dto';
|
||||
import { CreateTrainingDto } from './dto/create-training.dto';
|
||||
@@ -98,7 +103,7 @@ export class TrainingService {
|
||||
if (municipalityId)
|
||||
filters.push(eq(trainingSurveys.municipality, municipalityId));
|
||||
if (parishId) filters.push(eq(trainingSurveys.parish, parishId));
|
||||
if (ospType) filters.push(eq(trainingSurveys.ospType, ospType));
|
||||
if (ospType && ospType !== 'all') filters.push(eq(trainingSurveys.ospType, ospType));
|
||||
|
||||
const whereCondition = filters.length > 0 ? and(...filters) : undefined;
|
||||
|
||||
@@ -864,4 +869,178 @@ export class TrainingService {
|
||||
|
||||
// return book.outputAsync();
|
||||
// }
|
||||
async exportAll(filterDto: TrainingStatisticsFilterDto) {
|
||||
try {
|
||||
const { startDate, endDate, stateId, municipalityId, parishId, ospType } =
|
||||
filterDto;
|
||||
|
||||
const filters: SQL[] = [];
|
||||
|
||||
if (startDate)
|
||||
filters.push(gte(trainingSurveys.visitDate, new Date(startDate)));
|
||||
if (endDate)
|
||||
filters.push(lte(trainingSurveys.visitDate, new Date(endDate)));
|
||||
if (stateId) filters.push(eq(trainingSurveys.state, stateId));
|
||||
if (municipalityId)
|
||||
filters.push(eq(trainingSurveys.municipality, municipalityId));
|
||||
if (parishId) filters.push(eq(trainingSurveys.parish, parishId));
|
||||
if (ospType && ospType !== 'all')
|
||||
filters.push(eq(trainingSurveys.ospType, ospType));
|
||||
|
||||
const whereCondition = filters.length > 0 ? and(...filters) : undefined;
|
||||
|
||||
const records = await this.drizzle
|
||||
.select({
|
||||
coorFullName: trainingSurveys.coorFullName,
|
||||
visitDate: trainingSurveys.visitDate,
|
||||
stateName: states.name,
|
||||
municipalityName: municipalities.name,
|
||||
parishName: parishes.name,
|
||||
communeName: trainingSurveys.communeName,
|
||||
siturCodeCommune: trainingSurveys.siturCodeCommune,
|
||||
communalCouncil: trainingSurveys.communalCouncil,
|
||||
siturCodeCommunalCouncil: trainingSurveys.siturCodeCommunalCouncil,
|
||||
productiveActivity: trainingSurveys.productiveActivity,
|
||||
ospName: trainingSurveys.ospName,
|
||||
ospAddress: trainingSurveys.ospAddress,
|
||||
ospRif: trainingSurveys.ospRif,
|
||||
ospType: trainingSurveys.ospType,
|
||||
currentStatus: trainingSurveys.currentStatus,
|
||||
companyConstitutionYear: trainingSurveys.companyConstitutionYear,
|
||||
ospResponsibleFullname: trainingSurveys.ospResponsibleFullname,
|
||||
ospResponsibleCedula: trainingSurveys.ospResponsibleCedula,
|
||||
ospResponsibleRif: trainingSurveys.ospResponsibleRif,
|
||||
ospResponsiblePhone: trainingSurveys.ospResponsiblePhone,
|
||||
ospResponsibleEmail: trainingSurveys.ospResponsibleEmail,
|
||||
civilState: trainingSurveys.civilState,
|
||||
familyBurden: trainingSurveys.familyBurden,
|
||||
numberOfChildren: trainingSurveys.numberOfChildren,
|
||||
generalObservations: trainingSurveys.generalObservations,
|
||||
paralysisReason: trainingSurveys.paralysisReason,
|
||||
productList: trainingSurveys.productList,
|
||||
infrastructureMt2: trainingSurveys.infrastructureMt2,
|
||||
photo1: trainingSurveys.photo1,
|
||||
photo2: trainingSurveys.photo2,
|
||||
photo3: trainingSurveys.photo3,
|
||||
})
|
||||
.from(trainingSurveys)
|
||||
.leftJoin(states, eq(trainingSurveys.state, states.id))
|
||||
.leftJoin(
|
||||
municipalities,
|
||||
eq(trainingSurveys.municipality, municipalities.id),
|
||||
)
|
||||
.leftJoin(parishes, eq(trainingSurveys.parish, parishes.id))
|
||||
.where(whereCondition)
|
||||
.execute();
|
||||
|
||||
const workbook: any = await XlsxPopulate.fromBlankAsync();
|
||||
const sheet = workbook.sheet(0);
|
||||
|
||||
const headers = [
|
||||
'Coordinador',
|
||||
'Fecha',
|
||||
'Hora',
|
||||
'Estado',
|
||||
'Municipio',
|
||||
'Parroquia',
|
||||
'Comuna',
|
||||
'Código SITUR Comuna',
|
||||
'Consejo Comunal',
|
||||
'Código SITUR C.C.',
|
||||
'Actividad Productiva',
|
||||
'Nombre OSP',
|
||||
'Dirección OSP',
|
||||
'RIF OSP',
|
||||
'Tipo OSP',
|
||||
'Estatus Actual',
|
||||
'Año Constitución',
|
||||
'Total Productores',
|
||||
'Productos',
|
||||
'Infraestructura (mt2)',
|
||||
'Motivo Paralización',
|
||||
'Responsable',
|
||||
'Cédula',
|
||||
'RIF Responsable',
|
||||
'Teléfono',
|
||||
'Email',
|
||||
'Estado Civil',
|
||||
'Carga Familiar',
|
||||
'Nro Hijos',
|
||||
'Observaciones',
|
||||
// 'Foto 1',
|
||||
// 'Foto 2',
|
||||
// 'Foto 3',
|
||||
];
|
||||
|
||||
// Configurar encabezados
|
||||
sheet.range('A1:AG1').value([headers]).style({
|
||||
bold: true,
|
||||
fill: 'BFBFBF',
|
||||
});
|
||||
|
||||
let currentRow = 2;
|
||||
|
||||
for (const record of records) {
|
||||
const date = new Date(record.visitDate);
|
||||
const dateStr = date.toLocaleDateString('es-VE');
|
||||
const timeStr = date.toLocaleTimeString('es-VE');
|
||||
|
||||
const products = (record.productList as any[]) || [];
|
||||
const totalProducers = products.reduce(
|
||||
(sum, p) =>
|
||||
sum + (Number(p.menCount) || 0) + (Number(p.womenCount) || 0),
|
||||
0,
|
||||
);
|
||||
const productsDesc = products.map((p) => p.name).join(', ');
|
||||
|
||||
const rowData = [
|
||||
record.coorFullName,
|
||||
dateStr,
|
||||
timeStr,
|
||||
record.stateName || '',
|
||||
record.municipalityName || '',
|
||||
record.parishName || '',
|
||||
record.communeName,
|
||||
record.siturCodeCommune,
|
||||
record.communalCouncil,
|
||||
record.siturCodeCommunalCouncil,
|
||||
record.productiveActivity,
|
||||
record.ospName,
|
||||
record.ospAddress,
|
||||
record.ospRif,
|
||||
record.ospType,
|
||||
record.currentStatus,
|
||||
record.companyConstitutionYear,
|
||||
totalProducers,
|
||||
productsDesc,
|
||||
record.infrastructureMt2,
|
||||
record.paralysisReason || '',
|
||||
record.ospResponsibleFullname,
|
||||
record.ospResponsibleCedula,
|
||||
record.ospResponsibleRif,
|
||||
record.ospResponsiblePhone,
|
||||
record.ospResponsibleEmail,
|
||||
record.civilState,
|
||||
record.familyBurden,
|
||||
record.numberOfChildren,
|
||||
record.generalObservations || '',
|
||||
// record.photo1 || '',
|
||||
// record.photo2 || '',
|
||||
// record.photo3 || '',
|
||||
];
|
||||
|
||||
sheet.cell(`A${currentRow}`).value([rowData]);
|
||||
currentRow++;
|
||||
}
|
||||
|
||||
return await workbook.outputAsync();
|
||||
} catch (error: any) {
|
||||
console.error('Export Error:', error);
|
||||
throw new HttpException(
|
||||
'Error al generar el archivo Excel: ' + error.message,
|
||||
HttpStatus.INTERNAL_SERVER_ERROR,
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user