Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- return db.query (
- 'WITH t1 AS (' +
- '(SELECT fecha, texto, estado, contacto_id, \'saliente\' AS bandeja FROM mensaje_saliente WHERE movil_id = $1 ORDER BY fecha DESC LIMIT 10) UNION ' +
- '(SELECT fecha, texto, estado, NULL AS contacto_id, \'entrante\' AS bandeja FROM mensaje_entrante WHERE movil_id = $1 ORDER BY fecha DESC LIMIT 10)' +
- ') SELECT * FROM t1 ORDER BY fecha DESC LIMIT 10',
- [ data.movil_id ]
- );
- }).then (function (mensajes) {
- var entrantes_nuevos = mensajes.filter (function (mensaje) {
- return mensaje.bandeja == 'entrante' && mensaje.estado != 'leido';
- }).map (function (mensaje) {
- var d = mensaje.fecha.toISOString ();
- return d.substr(0, 10) + ' ' + d.substr(11, 8);
- });
- console.log ('entrantes nuevos: ' + JSON.stringify (entrantes_nuevos));
- if (entrantes_nuevos.length) {
- db.query ('UPDATE mensaje_entrante SET estado = \'leido\', contacto_id = $1, fecha_leido = NOW() WHERE movil_id = $2 AND to_char(fecha, \'YYYY-MM-DD HH24:MI:SS\') = ANY($3)', [ app.contacto_id, data.movil_id, entrantes_nuevos ]);
- }
- mensajes.reverse ();
- return defer.resolve ({
- ok: true,
- mensajes: mensajes.map (function (mensaje) {
- mensaje.movil_id = data.movil_id;
- return mensaje;
- })
- });
- });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement