-
Notifications
You must be signed in to change notification settings - Fork 0
/
app_2.js
159 lines (126 loc) · 4.73 KB
/
app_2.js
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
var config = require('./config');
var logger = require('winston');
var db = require('./db');
var record = require('./record_epicenter')
function processMessage(records){
// Determinar si la secuencia se trata una consulta o resultados
// provenientes del EpiCenter
var messageType = records[2][0];
switch (messageType){
case "O": processMessageResults(records); break;
case "Q": processMessageQueries(recordr); break;
}
}
function processMessageResults(records){
var record = [];
var resultados = [];
for (var i = 0; i < records.length; i++) {
record = records[i];
switch (record[0]){
case "H": handleHeader(record); break;
case "P": handlePatient(record); break;
case "O": orderRecord = record; break;
case "R": resultados.push([record, orderRecord]); break;
case "C": handleComment(record); break;
case "L": handleTerminator(record); break;
}
}
processResultsAsPromises(resultados);
}
function processResultsAsPromises(resultados){
let chain = Promise.resolve();
for (let rec of resultados) {
chain = chain.then(()=>handleResult(rec[0],rec[1]))
// .then(Wait)
}
return chain;
}
function Wait() {
return new Promise(r => setTimeout(r, 1000))
}
function processMessageQueries(records){
var record = [];
for (var i = 0; i < records.length; i++) {
record = records[i];
switch (record[0]){
case "H": handleHeader(record); break;
case "Q": handleInquiry(record); break;
case "L": handleTerminator(record); break;
}
}
}
function handleHeader(record){}
function handleInquiry(record){}
function handlePatient(record){}
function handleComment(record){}
function handleOrder(order){}
function handleTerminator(record){}
function handleResult(resultRecord, orderRecord){
// Return a Promise
var order = new record.OrderRecord();
order.buildFromRecord(orderRecord);
var result = new record.ResultRecord();
result.buildFromRecord(resultRecord);
switch (result.code){
case "GND": return db.saveResultGND(result, order); break;
case "GND_MGIT": break;
case "GND_PROBETEC": break;
case "AST": return db.saveResultAST(result, order); break;
case "AST_MGIT": break;
case "AST_MIC": return db.saveResultAST(result, order); break;
case "AST_DIA": break;
case "ID": return db.saveResultID(result, order); break;
case "OTHER": break;
}
return Promise.resolve();
}
function composeOrderMessages(protocol){
var header = new record.HeaderRecord();
var patient = createPatientRecordFromProtocol(protocol);
var order = createOrderRecordFromProtocol(protocol);
var termination = new record.TerminationRecord();
console.log(patient.toASTM());
console.log(order.toASTM());
return [[header.toASTM(), patient.toASTM(), order.toASTM(), termination.toASTM()]];
}
function createPatientRecordFromProtocol(protocol){
var patient = new record.PatientRecord();
patient.birthdate = protocol.anioNacimiento;
patient.sex = protocol.sexo;
patient.hospitalService = protocol.sectorSolicitante;
datos_extras = protocol.paciente.split("-");
patient.id = datos_extras[0];
patient.name = datos_extras[1];
return patient;
}
function replaceSpecialCharacters(r) {
r = r.replace(new RegExp(/[àáâãäå]/g),"a");
r = r.replace(new RegExp(/[èéêë]/g),"e");
r = r.replace(new RegExp(/[ìíîï]/g),"i");
r = r.replace(new RegExp(/ñ/g),"n");
r = r.replace(new RegExp(/Ñ/g),"N");
r = r.replace(new RegExp(/[òóôõö]/g),"o");
r = r.replace(new RegExp(/[ùúûü]/g),"u");
return r;
}
function createOrderRecordFromProtocol(protocol){
var order = new record.OrderRecord();
order.accessionNumber = protocol.numeroProtocolo.trim();
order.specimenType = protocol.tipoMuestra;
// // Tipo de muestra
// var tipoMuestra = 1;
// switch (tipoMuestraNombre){
// case "Suero/Plasma": tipoMuestra=1;break;
// case "Orina": tipoMuestra=2;break;
// case "CSF": tipoMuestra=3;break;
// case "Suprnt": tipoMuestra=4;break;
// case "Otros": tipoMuestra=5;break;
// }
return order;
}
module.exports = {
processMessage : processMessage,
processMessageResults : processMessageResults,
processMessageQueries : processMessageQueries,
composeOrderMessages: composeOrderMessages
};