-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcountModule.js
74 lines (56 loc) · 2.05 KB
/
countModule.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
var dbManager = require("./database_manager.js");
var crypto = require("./crypto.js");
function recontarVotacion(idVotacion) {
var votacion = dbManager.getPolls(true, idVotacion);
if(!votacion) {
throw "La votación indicada no existe";
}
votacion = votacion[0];
var now = new Date();
if(new Date(votacion.fecha_cierre) > now) {
throw "No se puede recontar esta votación ya que aún no ha terminado";
}
var numVotos = dbManager.getCantidadVotos(idVotacion);
console.log(numVotos);
if(numVotos < 5) {
throw "La votación no se puede recontar porque tiene menos de 5 votos (" + numVotos + ")";
}
var resultados = [];
for(var i = 0; i < votacion.preguntas.length; i++) {
var pregunta = votacion.preguntas[i];
var obj = {id_pregunta: pregunta.id_pregunta, texto_pregunta: pregunta.texto_pregunta, opciones: []};
var votos = dbManager.getVotosPregunta(pregunta.id_pregunta);
var votosDesencriptados = desencriptarVotos(votos);
var recuento = opcionesRecontadas(votosDesencriptados);
for(var j = 0; j < pregunta.opciones.length; j++) {
var opcion = pregunta.opciones[j];
if(!(opcion.id_opcion in recuento)) {
var votos = 0;
} else {
var votos = recuento[opcion.id_opcion];
}
obj.opciones.push({id_opcion: opcion.id_opcion, texto_opcion: opcion.texto_opcion, votos: votos});
}
resultados.push(obj)
}
return resultados;
}
function desencriptarVotos(votos) {
var res = [];
for(var i = 0; i < votos.length; i++) {
res.push(crypto.decrypt(votos[i].opcion));
}
return res;
}
function opcionesRecontadas(votos) {
var res = {};
for(var i = 0; i < votos.length; i++) {
var voto = votos[i];
if(!(voto in res)) {
res[voto] = 0;
}
res[voto]++;
}
return res;
}
exports.recontarVotacion = recontarVotacion;