From 3e48e5075735f0ca10335aa50bb96afe5b99e77d Mon Sep 17 00:00:00 2001 From: Josh Harvey Date: Fri, 6 May 2016 04:28:15 +0300 Subject: [PATCH] WIP --- lib/plugins.js | 36 ++++++++++++++++++++++-------- lib/postgresql_interface.js | 41 +++++++++++++++++++++++++++++++++-- lib/routes.js | 20 +++++++++++++++++ migrations/008-add-two-way.js | 13 +++++++++++ todo.txt | 11 ++++++++++ 5 files changed, 110 insertions(+), 11 deletions(-) create mode 100644 migrations/008-add-two-way.js diff --git a/lib/plugins.js b/lib/plugins.js index f9b88546..78033fd9 100644 --- a/lib/plugins.js +++ b/lib/plugins.js @@ -1,6 +1,6 @@ 'use strict' -var _ = require('lodash') +var R = require('ramda') var async = require('async') var BigNumber = require('bignumber.js') @@ -362,12 +362,6 @@ exports.trade = function trade (session, rawTrade, cb) { }) } - if (!rawTrade.toAddress) { - var newRawTrade = _.cloneDeep(rawTrade) - newRawTrade.toAddress = 'remit' - return db.recordBill(session, newRawTrade, cb) - } - async.parallel([ async.apply(db.addOutgoingPending, session, rawTrade.currency, rawTrade.cryptoCode, rawTrade.toAddress), async.apply(db.recordBill, session, rawTrade) @@ -412,8 +406,7 @@ exports.cashOut = function cashOut (session, tx, cb) { walletPlugin.newAddress(tmpInfo, function (err, address) { if (err) return cb(err) - var newTx = _.clone(tx) - newTx.toAddress = address + const newTx = R.assoc('toAddress', address, tx) db.addInitialIncoming(session, newTx, function (_err) { cb(_err, address) }) @@ -729,3 +722,28 @@ exports.getPhoneCode = function getPhoneCode (phone) { return smsPlugin.sendMessage(rec) .then(() => code) } + +exports.updatePhone = function updatePhone (session, tx) { + db.addIncomingPhone(session, tx, err => { + if (err) return Promise.reject(err) + return Promise.resolve() + }) +} + +exports.fetchPhoneTx = function fetchPhoneTx (phone) { + db.fetchPhoneTxs(phone) + .then(txs => { + const authorizedTxs = txs.filter(tx => tx.authorized) + if (authorizedTxs.length > 0) { + return R.reduce((acc, val) => { + !acc || val.cryptoAtoms.gt(acc.cryptoAtoms) ? val : acc + }, null, authorizedTxs) + } + + if (txs.length > 0) { + // pending txs + } + + // no txs for this number + }) +} diff --git a/lib/postgresql_interface.js b/lib/postgresql_interface.js index 59ff5215..3ddb48f5 100644 --- a/lib/postgresql_interface.js +++ b/lib/postgresql_interface.js @@ -264,8 +264,7 @@ function insertOutgoingCompleteTx (client, session, tx, cb) { insertOutgoing(client, session, tx, satoshis, fiat, stage, authority, cb) } -function insertIncoming (client, session, tx, satoshis, fiat, stage, authority, - cb) { +function insertIncoming (client, session, tx, satoshis, fiat, stage, authority, cb) { var realSatoshis = satoshis || new BigNumber(0) insertTx(client, session, true, tx, realSatoshis, fiat, stage, authority, cb) } @@ -289,6 +288,7 @@ function insertTx (client, session, incoming, tx, satoshis, fiat, stage, 'crypto_code', 'fiat', 'tx_hash', + 'phone', 'error' ] @@ -304,6 +304,7 @@ function insertTx (client, session, incoming, tx, satoshis, fiat, stage, tx.cryptoCode, fiat, tx.txHash, + tx.phone, tx.error ] @@ -486,11 +487,47 @@ function insertDispense (client, session, tx, cartridges, transactionId, cb) { client.query(sql, values, cb) } +exports.addIncomingPhone = function addIncomingPhone (session, tx, cb) { + var sql = 'UPDATE transactions SET phone=$1 ' + + 'WHERE stage=$2 AND authority=$3 AND device_fingerprint=$4 AND session_id=$5' + connect(function (cerr, client, done) { + if (cerr) return cb(cerr) + var values = [tx.phone, 'initial_request', 'deposit', session.fingerprint, session.id] + query(client, sql, values, function (err) { + done(err) + cb(err) + }) + }) +} + +function updateDispense (client, session, dispensed, cb) { + var sql = 'UPDATE transactions SET dispense=$1 ' + + 'WHERE stage=$2 AND authority=$3 AND device_fingerprint=$4 AND session_id=$5' + var values = [dispensed, 'initial_request', 'deposit', session.fingerprint, session.id] + query(client, sql, values, function (err) { + cb(err) + }) +} + +exports.updateAuthorized = function updateAuthorized (session, cb) { + var sql = 'UPDATE transactions SET dispense=$1 ' + + 'WHERE stage=$2 AND authority=$3 AND device_fingerprint=$4 AND session_id=$5' + connect(function (cerr, client, done) { + if (cerr) return cb(cerr) + var values = [true, 'initial_request', 'deposit', session.fingerprint, session.id] + query(client, sql, values, function (err) { + done(err) + cb(err) + }) + }) +} + exports.addDispense = function addDispense (session, tx, cartridges) { connect(function (cerr, client, done) { if (cerr) return async.waterfall([ + async.apply(updateDispense, client, true), async.apply(insertIncoming, client, session, tx, 0, tx.fiat, 'dispense', 'authorized'), async.apply(insertDispense, client, session, tx, cartridges) diff --git a/lib/routes.js b/lib/routes.js index c2f42474..60eec98e 100644 --- a/lib/routes.js +++ b/lib/routes.js @@ -224,6 +224,24 @@ function phoneCode (req, res) { }) } +function updatePhone (req, res) { + return plugins.updatePhone(session(req), req.body.phone) + .then(code => res.send(200)) + .catch(err => { + logger.error(err) + res.send(500) + }) +} + +function fetchPhoneTx (req, res) { + return plugins.updatePhone(req.query.phone) + .then(code => res.send(200)) + .catch(err => { + logger.error(err) + res.send(500) + }) +} + function init (localConfig) { lamassuConfig = localConfig.lamassuConfig plugins = localConfig.plugins @@ -248,6 +266,8 @@ function init (localConfig) { app.post('/pair', pair) app.post('/phone_code', authMiddleware, phoneCode) + app.post('/update-phone', authMiddleware, updatePhone) + app.get('/phone-tx', authMiddleware, fetchPhoneTx) localApp.get('/pid', function (req, res) { var machineFingerprint = req.query.fingerprint diff --git a/migrations/008-add-two-way.js b/migrations/008-add-two-way.js new file mode 100644 index 00000000..1d96df5d --- /dev/null +++ b/migrations/008-add-two-way.js @@ -0,0 +1,13 @@ +var db = require('./db') + +exports.up = function (next) { + var sql = [ + 'alter table transactions add dispensed boolean NOT NULL DEFAULT false', + 'alter table transactions add authorized boolean NOT NULL DEFAULT false' + ] + db.multi(sql, next) +} + +exports.down = function (next) { + next() +} diff --git a/todo.txt b/todo.txt index e69de29b..21baab50 100644 --- a/todo.txt +++ b/todo.txt @@ -0,0 +1,11 @@ +- need a new table for undispensed cash-out txs +- or add dispensed flag to txs + +- need to update cash-out tx status based on authorization and confirmation + +- add dispensed and authorized booleans to txs +- for fetching phones: fetch all non-dispensed rows for phone + - get max by satoshis of all authorized (why not consolidate all authorized? next version) + - if no authorized, check if any non-dispensed and non-authorized + +- index by phone + dispensed?