-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.ts
More file actions
329 lines (295 loc) · 11.1 KB
/
index.ts
File metadata and controls
329 lines (295 loc) · 11.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
import * as configPrivate from './config.private';
import { resolve } from 'path';
import { PacienteMpi } from './paciente.service';
import * as pacienteSumar from './paciente-sumar.service';
import * as sql from 'mssql';
import * as moment from 'moment';
var async = require('async');
const fs = require('fs');
const MongoClient = require('mongodb').MongoClient;
const chalk = require('chalk');
const clear = require('clear');
const figlet = require('figlet');
let inquirer = require('inquirer');
const url = configPrivate.mongoDB.host;
const coleccion = configPrivate.mongoDB.collection;
const directory = 'export/PacientesCuilificados';
inicio();
function inicio() {
clear();
let arrayFiles: String[] = [];
console.log(
chalk.red.bold(
figlet.textSync('Rayo Cuilificador', {
horizontalLayout: 'full'
})
)
);
const questions = [{
name: 'cuilificar',
type: 'rawlist',
message: 'Seleccione una opción: ',
choices: [{
key: "1",
name: "Importar Pacientes cuilificados",
value: "importar"
},
{
key: "2",
name: "Exportar Pacientes para cuilificar de mongodb a un archivo intermedio exportPatient.txt",
value: "exportar"
},
{
key: "3",
name: "Exportar pacientes Anses",
value: "exportar_anses"
},
{
key: "4",
name: "Ninguna",
value: "ninguna"
}
],
validate: function (value: any) {
if (value.length) {
return true;
} else {
return 'Debe seleccionar una opción';
}
}
}];
inquirer.prompt(questions).then(answers => {
if (answers.cuilificar === 'importar') {
let opcion = [{
name: 'pacienteMPI',
type: 'list',
message: 'Seleccione cuilificación con MPI o sin MPI: ',
choices: [
{
name: 'Con MPI', value: 1
},
{
name: 'Sin MPI', value: 2
}]
}]
inquirer.prompt(opcion).then(answers => {
let mpi;
if (answers.pacienteMPI === 1) {
mpi = true;
} else {
mpi = false;
}
readFilesfromFS()
.then(function (files: any) {
if (files.length) {
let menu = [{
name: 'archivoElegido',
type: 'list',
message: 'Seleccionar el archivo a procesar:',
choices: files,
validate: function (value: any) {
if (value.length) {
return true
} else {
return 'Debe seleccionar al menos un archivo';
}
}
}]
inquirer.prompt(menu)
.then(fileSelected => {
console.log('Inicio parseo... con archivo seleccionado: ', fileSelected.archivoElegido);
importarPacientesCuilificados(fileSelected.archivoElegido, mpi)
.then(function (resultado) {
return true;
})
.catch(function (err) {
console.log('err: ', err)
})
})
}
})
});
} else if (answers.cuilificar === 'exportar') {
exportPacientes();
} else if (answers.cuilificar === 'exportar_anses') {
pacienteSumar.exportarPacientesAnses();
} else {
console.log("Saliendo...");
}
});
function readFilesfromFS() {
return new Promise((resolve, reject) => {
let count = 1;
let archivos: any[] = []
fs.readdir(directory, function (err, files) {
if (err) {
return reject(err);
}
files.forEach(f => {
let elem = {
key: count,
name: f.toString(),
value: f.toString()
}
count++;
archivos.push(elem);
});
return resolve(archivos);
})
})
};
}
async function importarPacientesCuilificados(arch: any, mpi: boolean) {
return new Promise((resolve, reject) => {
let pathFile = directory + '/' + arch;
fs.exists(pathFile, function (exists) {
if (exists) {
fs.readFile(pathFile, {
encoding: "utf8"
}, function (err, data) {
if (err) {
reject(err)
}
let pacientesCuilificados: any[] = data;
if (mpi) {
procesarDatos(pacientesCuilificados)
} else {
guardaSipsCuilificado(pacientesCuilificados);
}
resolve(true);
})
}
});
})
}
async function guardaSipsCuilificado(data) {
let remaining = '';
remaining += data;
let index = remaining.indexOf('\n');
let x = 0;
let pacCuil: any = [];
let paciente: any;
while (index > -1) {
let linea = remaining.substring(0, index);
console.log(linea);
x++;
console.log("Total: ", x)
let nombreCompleto = linea.substring(10, 49);
let dni = linea.substring(2, 10);
let sexo = linea.substring(50, 51);
let cuil = linea.substring(149, 160);
let fechaNacimiento = linea.substring(51, 59);
paciente = {
nombreCompleto: nombreCompleto,
documento: dni,
sexo: sexo,
cuil: cuil,
fechaNacimiento: fechaNacimiento
}
pacCuil.push(paciente);
remaining = remaining.substring(index + 1);
index = remaining.indexOf('\n');
}
var total_entries = pacCuil.length;
MongoClient.connect(url, function (err, db) {
var col = db.collection(coleccion);
var bulk = col.initializeOrderedBulkOp();
var counter = 0;
for (let i = 0; i < pacCuil.length; i++) {
bulk.insert(pacCuil[i]);
}
bulk.execute(function (err, result) {
console.dir(err);
console.dir(result);
db.close();
});
});
console.log("Cant Pac.: ", pacCuil.length);
}
async function procesarDatos(data) {
let remaining = '';
remaining += data;
let index = remaining.indexOf('\n');
while (index > -1) {
let linea = remaining.substring(0, index);
console.log(linea);
await func(linea);
remaining = remaining.substring(index + 1);
index = remaining.indexOf('\n');
}
function func(data) {
return new Promise((resolve, reject) => {
let nombreCompleto = data.substring(10, 49);
let dni = data.substring(2, 10);
let sexo = data.substring(50, 51);
let cuil = data.substring(149, 160);
MongoClient.connect(url, function (err: any, db: any) {
if (err) {
db.close();
reject(err);
}
let query = {
'documento': dni.substring(0, 1) !== '0' ? dni : dni.substring(1, 9),
'sexo': (sexo === 'F') ? 'femenino' : 'masculino'
}
db.collection(coleccion).findOne(query, async function (err, pac) {
// console.log("Procesando: ", pac)
if (err) {
console.log('err: ', err);
reject(err);
}
if (pac && pac.documento && !pac.cuil) {
pac['cuil'] = cuil;
let op = new PacienteMpi();
await op.cuilificaPaciente(pac, configPrivate.secret.token);
}
db.close();
resolve(true);
});
})
})
}
}
// Exporta todos los pacientes de mongo a un archivo .txt para enviar a sips.
function exportPacientes() {
MongoClient.connect(url, function (err: any, dbMongo: any) {
if (err) {
console.log('Error conectando a mongoClient', err);
dbMongo.close();
}
dbMongo.collection(coleccion).find({}).toArray(function (err, docs) {
console.log("Found the following records: ", docs.length);
let writer = fs.createWriteStream('exportPatient.txt', {
flags: 'a' // 'a' means appending (old data will be preserved)
})
docs.forEach(element => {
let sexo = (element.sexo == 'femenino') ? 'F' : 'M';
// Formateamos nombre de acuerdo a especificaciones
let longNombre = element.apellido.length + element.nombre.length;
let nombreCompleto = element.apellido + ' ' + element.nombre;
for (let i = longNombre + 1; i < 40; i++) {
nombreCompleto = nombreCompleto + ' ';
}
if (longNombre > 39) {
nombreCompleto = nombreCompleto.substring(0, 40);
}
let doc = element.documento;
// formateamos la fecha "aaammdd"
let mes = (((element.fechaNacimiento.getMonth() + 1).toString()).length === 1) ? ('0' + (element.fechaNacimiento.getMonth() + 1).toString()) : (element.fechaNacimiento.getMonth() + 1).toString();
let dia = ((element.fechaNacimiento.getDate().toString()).length === 1) ? ('0' + (element.fechaNacimiento.getDate().toString())) : element.fechaNacimiento.getDate().toString();
let fechaNac = element.fechaNacimiento.getFullYear().toString() + mes + dia;
// formateamos DNI
let longDocumento = element.documento.length;
for (let i = longDocumento; i < 8; i++) {
doc = '0' + doc;
}
if (longDocumento > 8) {
doc = doc.substring(0, 8);
}
writer.write('29' + doc + nombreCompleto + sexo + fechaNac + ' ');
writer.write('\n');
});
dbMongo.close();
});
})
}