This commit is contained in:
Josh Harvey 2016-05-06 04:28:15 +03:00
parent cd1e9bdb66
commit 3e48e50757
5 changed files with 110 additions and 11 deletions

View file

@ -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
})
}

View file

@ -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)

View file

@ -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

View file

@ -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()
}

View file

@ -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?