From 30bd4fee8e2e7cbaa07423ddcd7bb2c794de98f3 Mon Sep 17 00:00:00 2001
From: cgeek <cem.moreau@gmail.com>
Date: Sun, 3 Apr 2016 16:45:10 +0200
Subject: [PATCH] Fix #362 Added reception time on pending transactions

---
 app/lib/dal/sqliteDAL/MetaDAL.js      | 3 ++-
 app/lib/dal/sqliteDAL/TxsDAL.js       | 6 +++++-
 app/lib/streams/dtos.js               | 1 +
 doc/HTTP_API.md                       | 5 +++++
 test/dal/dal.js                       | 2 +-
 test/integration/transactions-test.js | 7 +++++++
 6 files changed, 21 insertions(+), 3 deletions(-)

diff --git a/app/lib/dal/sqliteDAL/MetaDAL.js b/app/lib/dal/sqliteDAL/MetaDAL.js
index 3e3954eb4..7a4a8a38a 100644
--- a/app/lib/dal/sqliteDAL/MetaDAL.js
+++ b/app/lib/dal/sqliteDAL/MetaDAL.js
@@ -28,7 +28,8 @@ function MetaDAL(db) {
 
   let migrations = {
     0: 'BEGIN; COMMIT;',
-    1: 'BEGIN; COMMIT;'
+    1: 'BEGIN; COMMIT;',
+    2: 'BEGIN; ALTER TABLE txs ADD COLUMN received INTEGER NULL; COMMIT;'
   };
 
   this.init = () => co(function *() {
diff --git a/app/lib/dal/sqliteDAL/TxsDAL.js b/app/lib/dal/sqliteDAL/TxsDAL.js
index ef36c0b21..05b278fb0 100644
--- a/app/lib/dal/sqliteDAL/TxsDAL.js
+++ b/app/lib/dal/sqliteDAL/TxsDAL.js
@@ -4,6 +4,7 @@
 
 var Q = require('q');
 var co = require('co');
+var moment = require('moment');
 var AbstractSQLite = require('./AbstractSQLite');
 
 module.exports = TxsDAL;
@@ -24,6 +25,7 @@ function TxsDAL(db) {
     'currency',
     'comment',
     'locktime',
+    'received',
     'time',
     'written',
     'removed',
@@ -99,11 +101,13 @@ function TxsDAL(db) {
   };
 
   this.addPending = (tx) => {
+    tx.received = moment().unix();
     tx.written = false;
     tx.removed = false;
     tx.hash = tx.getHash(true);
     tx.recipients = tx.outputs.map(function(out) {
-      return out.match('(.*):')[1];
+      let recipent = out.match('SIG\\((.*)\\)');
+      return (recipent && recipent[1]) || 'UNKNOWN';
     });
     return this.saveEntity(tx);
   };
diff --git a/app/lib/streams/dtos.js b/app/lib/streams/dtos.js
index 3e297b5d7..1971dcb40 100644
--- a/app/lib/streams/dtos.js
+++ b/app/lib/streams/dtos.js
@@ -329,6 +329,7 @@ dtos.TxOfHistory = {
   "outputs": [String],
   "comment": String,
   "locktime": String,
+  "received": Number,
   "signatures": [String],
   "hash": String,
   "block_number": Number,
diff --git a/doc/HTTP_API.md b/doc/HTTP_API.md
index 886b1afaf..8dcfecdc2 100644
--- a/doc/HTTP_API.md
+++ b/doc/HTTP_API.md
@@ -1375,6 +1375,7 @@ The full transaction history for the given `pubkey`
     "sent": [
       {
         "version": 2,
+        "received": null,
         "issuers": [
           "HnFcSms8jzwngtVomTTnzudZx7SHUQY8sVE1y8yBmULk"
         ],
@@ -1397,6 +1398,7 @@ The full transaction history for the given `pubkey`
     "received": [
       {
         "version": 2,
+        "received": null,
         "issuers": [
           "8Fi1VSTbjkXguwThF4v2ZxC5whK7pwG2vcGTkPUPjPGU"
         ],
@@ -1417,6 +1419,7 @@ The full transaction history for the given `pubkey`
       },
       {
         "version": 2,
+        "received": null,
         "issuers": [
           "J78bPUvLjxmjaEkdjxWLeENQtcfXm7iobqB49uT1Bgp3"
         ],
@@ -1439,6 +1442,7 @@ The full transaction history for the given `pubkey`
     "sending": [
 	  {
         "version": 2,
+        "received": 1459691641,
         "issuers": [
           "HnFcSms8jzwngtVomTTnzudZx7SHUQY8sVE1y8yBmULk"
         ],
@@ -1459,6 +1463,7 @@ The full transaction history for the given `pubkey`
     "receiving": [
 	 {
         "version": 2,
+        "received": 1459691641,
         "issuers": [
           "2sq8bBDQGK74f1eD3mAPQVgHCmFdijZr9nbv16FwbokX"
         ],
diff --git a/test/dal/dal.js b/test/dal/dal.js
index 6e64cc3b2..6909f5d0a 100644
--- a/test/dal/dal.js
+++ b/test/dal/dal.js
@@ -173,7 +173,7 @@ describe("DAL", function(){
   it('should have DB version 2', () => co(function *() {
     let version = yield fileDAL.getDBVersion();
     should.exist(version);
-    version.should.equal(2);
+    version.should.equal(3);
   }));
 
   it('should have no peer in a first time', function(){
diff --git a/test/integration/transactions-test.js b/test/integration/transactions-test.js
index 996d589b9..b844f68b1 100644
--- a/test/integration/transactions-test.js
+++ b/test/integration/transactions-test.js
@@ -8,6 +8,8 @@ var constants = require('../../app/lib/constants');
 var node   = require('./tools/node');
 var user   = require('./tools/user');
 var unit   = require('./tools/unit');
+var http   = require('./tools/http');
+var rp     = require('request-promise');
 var MEMORY_MODE = true;
 
 describe("Testing transactions", function() {
@@ -40,6 +42,11 @@ describe("Testing transactions", function() {
           yield node2.commitP();
           yield node2.commitP();
           yield tic.sendP(51, toc);
+          yield http.expectAnswer(rp('http://127.0.0.1:9998/tx/history/DKpQPUL4ckzXYdnDRvCRKAm1gNvSdmAXnTrJZ7LvM5Qo', { json: true }), function(res) {
+            res.should.have.property('pubkey').equal('DKpQPUL4ckzXYdnDRvCRKAm1gNvSdmAXnTrJZ7LvM5Qo');
+            res.should.have.property('history').property('pending').length(1);
+            res.history.pending[0].should.have.property('received').be.a.Number;
+          });
           yield node2.commitP();
         });
       });
-- 
GitLab