-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathdb.js
More file actions
142 lines (126 loc) · 4.09 KB
/
db.js
File metadata and controls
142 lines (126 loc) · 4.09 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
var config = require('./config.private');
var winston = require('winston');
const models = require('./models')
const srv = require('./service/laboratorio');
// SQL Server config settings
// var dbConfig = {
// "name": "default",
// "host": config.dbServer,
// "user": config.dbUser,
// "password": config.dbPassword,
// "database": config.dbDatabase
// };
// sql.setDefault(dbConfig);
const logger = winston.createLogger({
level: config.logLevel,
format: winston.format.json(),
transports: [
new winston.transports.File({ filename: 'logs/db-error.log', level: config.logLevel }),
new winston.transports.File({ filename: 'logs/combined.log' })
]
});
function isString(value) { return typeof value === 'string'; }
function saveResult(result, order) {
var logTime = new Date();
var tipoMuestra = "Suero/Plasma";
switch (parseInt(order.biomaterial)) {
case 1: tipoMuestra = "Suero/Plasma"; break;
case 2: tipoMuestra = "Orina"; break;
case 3: tipoMuestra = "CSF"; break;
case 4: tipoMuestra = "Suprnt"; break;
case 5: tipoMuestra = "Otros"; break;
}
models.ejecuciones.findOne({
where: {
numeroProtocolo: order.sampleId,
test: result.test
}
}).then(ejecucion => {
ejecucion.valor = result.value;
ejecucion.estado = 2;
ejecucion.save().then(res => {
console.log('actualizado:', res);
// Aqui hacer la llamada al PATCH de la API
runPatch(ejecucion._id, ejecucion);
}).catch(error => {
console.log(error);
});
}).catch(error => {
console.log('Error en saveResult: ', error);
});
}
async function runPatch(prestaId, registro) {
srv.patchCobasC311(prestaId, registro).then((data) => {
console.log('runPatch data:', data);
}).catch(err => {
console.log(err);
})
};
function hasProtocolsToSend() {
return models.ejecuciones.count({ where: { 'estado': '0' } }).then(cantidad => {
return cantidad > 0;
})
}
function getNextProtocolToSend() {
return models.ejecuciones.findOne({ where: { 'estado': '0' } }).then(ejecucion => {
return ejecucion ? ejecucion : null;
}).catch(error => {
console.log(error);
});
// return sql.execute({
// query: "SELECT TOP 1 * FROM LAB_TempProtocoloEnvio WHERE equipo = @equipo",
// params: {
// equipo: {
// type: sql.NVARCHAR,
// val: config.analyzer,
// }
// }
// })
}
function removeLastProtocolSent() {
getNextProtocolToSend().then(function (results) {
for (var i = 0; i < results.length; i++) { // Always only 1 iteration
var protocol = results[i];
removeProtocol(protocol);
}
}, function (err) {
logger.error("Something bad happened:", err);
});
}
function removeProtocol(ejecucion) {
ejecucion.estado = '1';
ejecucion.save().then(res => {
console.log('removeProtocol :', res);
}).catch(error => {
console.log(error);
});
// return sql.execute({
// query: "DELETE FROM LAB_TempProtocoloEnvio WHERE idTempProtocoloEnvio = @_id",
// params: {
// _id: {
// type: sql.INT,
// val: idTempProtocolo,
// }
// }
// })
}
function logMessages(logMessage, logTime) {
// sql.execute({
// query: "INSERT INTO Temp_Mensaje(mensaje,fechaRegistro) VALUES (@_mensaje,@_fechaRegistro)",
// params: {
// _mensaje: { type: sql.NVARCHAR, val: logMessage },
// _fechaRegistro: { type: sql.DATETIME, val: logTime },
// }
// }).then(function (results) {
// logger.info(results);
// }, function (err) {
// logger.error("Something bad happened:", err);
// });
}
module.exports = {
saveResult: saveResult,
hasProtocolsToSend: hasProtocolsToSend,
getNextProtocolToSend: getNextProtocolToSend,
removeProtocol: removeProtocol,
removeLastProtocolSent: removeLastProtocolSent
};