This commit is contained in:
Josh Harvey 2016-11-06 20:07:49 +00:00
parent 1771467474
commit 855546f886
7 changed files with 157 additions and 131 deletions

View file

@ -2,8 +2,8 @@
'use strict'
const BigNumber = require('bignumber.js')
const db = require('./db')
const pgp = require('pg-promise')()
var psqlUrl = require('../lib/options').postgresql
const logger = require('./logger')
@ -28,10 +28,6 @@ function getInsertQuery (tableName, fields) {
return query
}
function connect () {
return pgp(psqlUrl)
}
// logs inputted bill and overall tx status (if available)
exports.recordBill = function recordBill (deviceId, rec) {
console.log('DEBUG10: %j', rec)
@ -61,8 +57,6 @@ exports.recordBill = function recordBill (deviceId, rec) {
console.log('DEBUG11: %j', values)
const db = connect()
return db.none(getInsertQuery('bills', fields), values)
.catch(err => {
if (isUniqueViolation(err)) return logger.warn('Attempt to report bill twice')
@ -75,7 +69,6 @@ exports.recordDeviceEvent = function recordDeviceEvent (deviceId, event) {
'note, device_time) VALUES ($1, $2, $3, $4)'
const values = [deviceId, event.eventType, event.note,
event.deviceTime]
const db = connect()
return db.none(sql, values)
}
@ -102,13 +95,11 @@ exports.addOutgoingTx = function addOutgoingTx (deviceId, tx) {
tx.error
]
const db = connect()
return db.none(getInsertQuery('cash_in_txs', fields), values)
}
exports.sentCoins = function sentCoins (tx, toSend, fee, error, txHash) {
const sql = 'update cash_in_txs set tx_hash=$1, error=$2 where id=$3'
const db = connect()
return db.none(sql, [txHash, error, tx.id])
}
@ -131,7 +122,6 @@ exports.addInitialIncoming = function addInitialIncoming (deviceId, tx) {
tx.error
]
const db = connect()
return db.none(getInsertQuery('cash_out_txs', fields), values)
}
@ -159,7 +149,6 @@ function insertDispense (deviceId, tx, cartridges) {
false, tx.error
]
const db = connect()
return db.none(sql, values)
}
@ -168,7 +157,6 @@ exports.addIncomingPhone = function addIncomingPhone (tx, notified) {
WHERE id=$3
AND phone IS NULL`
const values = [tx.phone, notified, tx.id]
const db = connect()
return db.result(sql, values)
.then(results => {
@ -210,7 +198,6 @@ exports.fetchPhoneTxs = function fetchPhoneTxs (phone, dispenseTimeout) {
'AND (EXTRACT(EPOCH FROM (COALESCE(confirmation_time, now()) - created))) * 1000 < $3'
const values = [phone, false, dispenseTimeout]
const db = connect()
return db.any(sql, values)
.then(rows => normalizeTxs(rows))
@ -218,7 +205,6 @@ exports.fetchPhoneTxs = function fetchPhoneTxs (phone, dispenseTimeout) {
exports.fetchTx = function fetchTx (txId) {
const sql = 'SELECT * FROM cash_out_txs WHERE id=$1'
const db = connect()
return db.one(sql, [txId])
.then(row => normalizeTx(row))
@ -227,7 +213,6 @@ exports.fetchTx = function fetchTx (txId) {
exports.addDispenseRequest = function addDispenseRequest (tx) {
const sql = 'update cash_out_txs set dispensed=$1 where id=$2 and dispensed=$3'
const values = [true, tx.id, false]
const db = connect()
return db.result(sql, values)
.then(results => {
@ -245,7 +230,6 @@ exports.addDispense = function addDispense (deviceId, tx, cartridges) {
return insertDispense(deviceId, tx, cartridges)
.then(() => {
const sql2 = 'insert into cash_out_actions (cash_out_txs_id, action) values ($1, $2)'
const db = connect()
return db.none(sql2, [tx.id, 'dispensed'])
})
@ -255,7 +239,6 @@ exports.cartridgeCounts = function cartridgeCounts (deviceId) {
const sql = 'SELECT id, count1, count2 FROM dispenses ' +
'WHERE device_id=$1 AND refill=$2 ' +
'ORDER BY id DESC LIMIT 1'
const db = connect()
return db.oneOrNone(sql, [deviceId, true])
.then(row => {
@ -272,7 +255,6 @@ exports.machineEvent = function machineEvent (rec) {
const values = [rec.id, rec.deviceId, rec.eventType, rec.note, rec.deviceTime]
const deleteSql = 'DELETE FROM machine_events WHERE (EXTRACT(EPOCH FROM (now() - created))) * 1000 > $1'
const deleteValues = [TTL]
const db = connect()
return db.none(sql, values)
.then(() => db.none(deleteSql, deleteValues))
@ -280,14 +262,12 @@ exports.machineEvent = function machineEvent (rec) {
exports.devices = function devices () {
const sql = 'SELECT device_id, name FROM devices'
const db = connect()
return db.any(sql)
}
exports.machineEvents = function machineEvents () {
const sql = 'SELECT *, (EXTRACT(EPOCH FROM (now() - created))) * 1000 AS age FROM machine_events'
const db = connect()
return db.any(sql, [])
}
@ -301,7 +281,6 @@ exports.fetchOpenTxs = function fetchOpenTxs (statuses, age) {
'FROM cash_out_txs ' +
'WHERE ((EXTRACT(EPOCH FROM (now() - created))) * 1000)<$1 ' +
'AND status IN ' + _statuses
const db = connect()
return db.any(sql, [age])
.then(rows => normalizeTxs(rows))
@ -315,7 +294,6 @@ exports.fetchUnnotifiedTxs = function fetchUnnotifiedTxs (age, waitPeriod) {
AND phone IS NOT NULL
AND status IN ('instant', 'confirmed')
AND (redeem=$4 OR ((EXTRACT(EPOCH FROM (now() - created))) * 1000)>$5)`
const db = connect()
return db.any(sql, [age, false, false, true, waitPeriod])
.then(rows => normalizeTxs(rows))
@ -336,7 +314,6 @@ exports.updateTxStatus = function updateTxStatus (tx, status) {
const TransactionMode = pgp.txMode.TransactionMode
const isolationLevel = pgp.txMode.isolationLevel
const tmSRD = new TransactionMode({tiLevel: isolationLevel.serializable})
const db = connect()
function transaction (t) {
const sql = 'select status, confirmation_time from cash_out_txs where id=$1'
@ -381,7 +358,6 @@ exports.updateTxStatus = function updateTxStatus (tx, status) {
exports.updateRedeem = function updateRedeem (txId) {
const sql = 'UPDATE cash_out_txs SET redeem=$1 WHERE id=$2'
const values = [true, txId]
const db = connect()
return db.none(sql, values)
.then(() => {
@ -393,7 +369,6 @@ exports.updateRedeem = function updateRedeem (txId) {
exports.updateNotify = function updateNotify (tx) {
const sql = 'UPDATE cash_out_txs SET notified=$1 WHERE id=$2'
const values = [true, tx.id]
const db = connect()
return db.none(sql, values)
.then(() => {
@ -410,7 +385,6 @@ function insertCachedRequest (deviceId, txId, path, method, body) {
'method',
'body'
]
const db = connect()
const sql = getInsertQuery('cached_responses', fields)
return db.none(sql, [deviceId, txId, path, method, body])
@ -424,7 +398,6 @@ exports.cachedResponse = function (deviceId, txId, path, method) {
and method=$4`
const values = [deviceId, txId, path, method]
const db = connect()
return insertCachedRequest(deviceId, txId, path, method, {pendingRequest: true})
.then(() => ({}))
@ -441,7 +414,6 @@ function pruneCachedResponses () {
where (EXTRACT(EPOCH FROM (now() - created))) * 1000 < $1`
const values = [CACHED_SESSION_TTL]
const db = connect()
return db.none(sql, values)
}
@ -455,7 +427,6 @@ exports.cacheResponse = function (deviceId, txId, path, method, body) {
and method=$5`
const values = [body, deviceId, txId, path, method]
const db = connect()
return db.none(sql, values)
}
@ -463,7 +434,6 @@ exports.cacheResponse = function (deviceId, txId, path, method, body) {
exports.nextCashOutSerialHD = function nextCashOutSerialHD (txId, cryptoCode) {
const sql = `select hd_serial from cash_out_hds
where crypto_code=$1 order by hd_serial desc limit 1`
const db = connect()
const attempt = () => db.oneOrNone(sql, [cryptoCode])
.then(row => {
@ -486,7 +456,6 @@ exports.fetchLiveHD = function fetchLiveHD () {
((extract(epoch from (now() - cash_out_txs.created))) * 1000)<$3`
const values = ['confirmed', false, LIVE_SWEEP_TTL]
const db = connect()
return db.any(sql, values)
}
@ -496,14 +465,12 @@ exports.fetchOldHD = function fetchLiveHD () {
where confirmed
order by last_checked
limit 10`
const db = connect()
return db.any(sql)
}
exports.markSwept = function markSwept (txId) {
const sql = 'update cash_out_hds set swept=$1 where id=$2'
const db = connect()
return db.none(sql, [true, txId])
}