Advertisement
Guest User

Untitled

a guest
Nov 21st, 2019
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.27 KB | None | 0 0
  1. return db.query (
  2. 'WITH t1 AS (' +
  3. '(SELECT fecha, texto, estado, contacto_id, \'saliente\' AS bandeja FROM mensaje_saliente WHERE movil_id = $1 ORDER BY fecha DESC LIMIT 10) UNION ' +
  4. '(SELECT fecha, texto, estado, NULL AS contacto_id, \'entrante\' AS bandeja FROM mensaje_entrante WHERE movil_id = $1 ORDER BY fecha DESC LIMIT 10)' +
  5. ') SELECT * FROM t1 ORDER BY fecha DESC LIMIT 10',
  6. [ data.movil_id ]
  7. );
  8. }).then (function (mensajes) {
  9. var entrantes_nuevos = mensajes.filter (function (mensaje) {
  10. return mensaje.bandeja == 'entrante' && mensaje.estado != 'leido';
  11. }).map (function (mensaje) {
  12. var d = mensaje.fecha.toISOString ();
  13. return d.substr(0, 10) + ' ' + d.substr(11, 8);
  14. });
  15.  
  16. console.log ('entrantes nuevos: ' + JSON.stringify (entrantes_nuevos));
  17. if (entrantes_nuevos.length) {
  18. 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 ]);
  19. }
  20. mensajes.reverse ();
  21. return defer.resolve ({
  22. ok: true,
  23. mensajes: mensajes.map (function (mensaje) {
  24. mensaje.movil_id = data.movil_id;
  25. return mensaje;
  26. })
  27. });
  28. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement