Commit 92d687df authored by Manuel De la Penna's avatar Manuel De la Penna

se cambia guardado de lecturas y puntos

parent 7120b1fb
......@@ -26,12 +26,111 @@ class AppPointCollector extends backendPlus.AppBackend{
}
mainApp.use(baseUrl+'/webapp',serveContent(be.rootPath+'/webapp',optsGenericForFiles));
mainApp.post(baseUrl+'/save/positions',function(req,res){
var result = {};
var client = null;
return Promise.resolve().then(function(){
return be.getDbClient(req).then(function(cli){
client = cli;
return client.query("BEGIN TRANSACTION").execute();
});
}).then(function(){
var today = date.today();
return client.query("select * " +
"from tareas " +
"where tarea = $1 and vencimiento >= $2"
,[req.body.token.tarea, today.toYmd()]).fetchOneRowIfExists();
}).then(function(queryResult){
if(queryResult.rowCount){
var posiciones = JSON.parse(req.body.position_collection);
if(posiciones.length){
var promiseChain=Promise.resolve();
posiciones.forEach(function(pos){
promiseChain = promiseChain.then(function(){
pos.observaciones = (pos.observaciones)?"'"+pos.observaciones+"'":"null";
pos.puerta = (pos.puerta)?"'"+pos.puerta+"'":"null";
var values = [];
values.push("'" + (req.body.token.token) + "'");
values.push("'" + pos.fecha_relevamiento + "'");
values.push("'" + pos.sector + "'");
values.push("'" + pos.manzana + "'");
values.push("'" + pos.lado + "'");
values.push(pos.secuencia);
values.push(pos.puerta);
values.push(pos.nivel);
values.push(pos.observaciones);
return client.query("INSERT INTO lecturas " +
"(token, fecha_relevamiento, sector, manzana, lado, secuencia, puerta, nivel, observaciones) " +
"VALUES ("+values.join(",")+") returning id" ,[]).fetchUniqueRow();
}).then(function(insertedResult){
var lecturaId = insertedResult.row.id;
var promiseChain2=Promise.resolve();
pos.puntos.forEach(function(punto){
promiseChain2 = promiseChain2.then(function(){
var values = [];
values.push(lecturaId);
values.push("'" + punto.datetime + "'");
values.push(punto.latitud);
values.push(punto.longitud);
return client.query("INSERT INTO puntos " +
"(lectura, fecha, latitud, longitud) " +
"VALUES ("+values.join(",")+") "
,[]).execute();
}).catch(function(err){
throw err;
});
});
return promiseChain2;
}).catch(function(err){
throw err;
});
});
result = { code: 200, message: 'Posiciones guardadas correctamente'};
return promiseChain;
}else{
result = { code: 204, message: 'No se recibieron posiciones nuevas.'};
}
}else{
throw new Error('La tarea no existe o se encuentra vencida. No se guardaron los puntos');
}
}).then(function(){
return client.query("COMMIT").execute();
}).then(function(){
client.done();
}).then(function(){
res.send(result);
}).catch(function(err){
if(client && typeof client.done === "function"){
client.query("ROLLBACK").execute().then(function(){
client.done();
});
}
console.log('ERROR: ',err.message);
result = { code: 500, message: "Error al guardar los puntos. " + err.message};
res.send(result);
});
});
/*mainApp.post(baseUrl+'/save/positions',function(req,res){
var result = {};
var lines = [];
var client = null;
Promise.resolve().then(function(){
JSON.parse(req.body.position_collection).forEach(function(pos){
lines.push('\'' + JSON.stringify(pos) + '\',\'' + (req.body.token.token) + '\'');
pos.observaciones = (pos.observaciones)?"'"+pos.observaciones+"'":null;
pos.puerta = (pos.puerta)?"'"+pos.puerta+"'":null;
lines.push("'" + (req.body.token.token) + "'"
+ ",'" + pos.fecha_relevamiento + "'"
+ ",'" + pos.sector + "'"
+ ",'" + pos.manzana + "'"
+ ",'" + pos.lado + "'"
+ ',' + pos.secuencia
+ "," + pos.puerta
+ ',' + pos.nivel
+ ",'" + JSON.stringify(pos.puntos) + "'"
+ "," + pos.observaciones
);
});
if(lines.length){
return be.getDbClient(req).then(function(cli){
......@@ -43,7 +142,8 @@ class AppPointCollector extends backendPlus.AppBackend{
,[req.body.token.tarea, today.toYmd()]).fetchOneRowIfExists();
}).then(function(result){
if(result.rowCount){
return client.query("INSERT INTO lecturas (posicion, token) " +
return client.query("INSERT INTO lecturas " +
"(token, fecha_relevamiento, sector, manzana, lado, secuencia, puerta, nivel, puntos, observaciones) " +
"VALUES ("+lines.join("),(")+") "
,[]).execute();
}else{
......@@ -66,7 +166,7 @@ class AppPointCollector extends backendPlus.AppBackend{
result = { code: 500, message: "Error. " + err.message};
res.send(result);
});
});
});*/
}
getProcedures(){
var be = this;
......@@ -99,7 +199,8 @@ class AppPointCollector extends backendPlus.AppBackend{
'variables',
'tokens',
'tareas',
'lecturas'
'lecturas',
'puntos'
]);
}
}
......
......@@ -7,13 +7,23 @@ module.exports = function(context){
title:'Lecturas',
editable: admin,
fields:[
{name:'id' , typeName:'integer' , nullable:false, sequence:{name: 'secuencia_lecturas', firstValue: 1}},
{name:'token' , typeName:'text' , nullable:false },
{name:'posicion' , typeName:'text' , nullable:false, width: 900 }
{name:'id' , typeName:'integer' , nullable:false, sequence:{name: 'secuencia_lecturas', firstValue: 1}},
{name:'token' , typeName:'text' , nullable:false },
{name:'fecha_relevamiento' , typeName:'timestamp' , nullable:false },
{name:'sector' , typeName:'text' , nullable:false },
{name:'manzana' , typeName:'text' , nullable:false },
{name:'lado' , typeName:'text' , nullable:false },
{name:'secuencia' , typeName:'integer' , nullable:false },
{name:'puerta' , typeName:'text' },
{name:'nivel' , typeName:'integer' , nullable:false },
{name:'observaciones' , typeName:'text' }
],
primaryKey:['id'],
foreignKeys:[
{references:'tokens', fields:['token']},
],
detailTables:[
{table: 'puntos' , fields:[{source:'id', target:'lectura'}], abr:'P', label:'Puntos'},
]
}, context);
}
"use strict";
module.exports = function(context){
var admin=context.user.rol==='admin';
return context.be.tableDefAdapt({
name:'puntos',
title:'punto',
editable: admin,
fields:[
{name:'id' , typeName:'integer' , nullable:false, sequence:{name: 'secuencia_puntos', firstValue: 1}},
{name:'lectura' , typeName:'integer' , nullable:false },
{name:'fecha' , typeName:'timestamp' , nullable:false },
{name:'latitud' , typeName:'decimal' , nullable:false },
{name:'longitud' , typeName:'decimal' , nullable:false },
],
primaryKey:['id'],
foreignKeys:[
{references:'lecturas', fields:[{source:'lectura', target:'id'}]},
],
}, context);
}
......@@ -18,6 +18,7 @@ module.exports = function(context){
{references:'usuarios', fields:['usuario']},
],
sql:{
isTable: true,
from: `(select * from tokens order by fecha desc)`
},
}, context);
......
......@@ -26,7 +26,7 @@ function calcDatetime(){
ss='0'+ss;
}
return (dd+'/'+mm+'/'+yyyy+'-'+hh+':'+min+':'+ss);
return (yyyy+'-'+mm+'-'+dd+' '+hh+':'+min+':'+ss);
}
......
CACHE MANIFEST
# Version 0.0.14
# Version 0.0.16
#--------------------------- HTMLs ---------------------------------
index.html
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment