From b45e9ef148adad81f60e5d8617e9fd4676c3aded Mon Sep 17 00:00:00 2001 From: Josh Harvey Date: Thu, 28 Jul 2016 18:12:04 +0300 Subject: [PATCH] WIP --- lib/postgresql_interface.js | 49 +++++++++++++++++-------------------- 1 file changed, 23 insertions(+), 26 deletions(-) diff --git a/lib/postgresql_interface.js b/lib/postgresql_interface.js index c4423043..922abb8f 100644 --- a/lib/postgresql_interface.js +++ b/lib/postgresql_interface.js @@ -48,9 +48,9 @@ exports.recordBill = function recordBill (deviceId, rec) { 'currency_code', 'crypto_code', 'to_address', - 'tx_id', + 'cash_in_txs_id', 'device_time', - 'satoshis', + 'crypto_atoms', 'denomination' ] @@ -84,7 +84,7 @@ exports.recordDeviceEvent = function recordDeviceEvent (deviceId, event) { // NOTE: This will fail if we have already sent coins because there will be // a unique cash_in_txs record in the table already keyed by txId. exports.addOutgoingTx = function addOutgoingTx (deviceId, tx) { - const fields = ['tx_id', 'device_id', 'to_address', + const fields = ['id', 'device_id', 'to_address', 'crypto_atoms', 'crypto_code', 'currency_code', 'fiat', 'tx_hash', 'fee', 'phone', 'error' ] @@ -107,12 +107,12 @@ exports.addOutgoingTx = function addOutgoingTx (deviceId, tx) { } exports.sentCoins = function sentCoins (tx, toSend, fee, error, txHash) { - const sql = `update cash_in_txs set tx_hash=$1, error=$2 where tx_id=$3` + const sql = `update cash_in_txs set tx_hash=$1, error=$2 where id=$3` return db.none(sql, [txHash, error, tx.id]) } exports.addInitialIncoming = function addInitialIncoming (deviceId, tx) { - const fields = ['tx_id', 'device_id', 'to_address', + const fields = ['id', 'device_id', 'to_address', 'crypto_atoms', 'crypto_code', 'currency_code', 'fiat', 'tx_hash', 'phone', 'error' ] @@ -135,7 +135,7 @@ exports.addInitialIncoming = function addInitialIncoming (deviceId, tx) { function insertDispense (deviceId, tx, cartridges) { const fields = [ - 'device_id', 'tx_id', + 'device_id', 'cash_out_txs_id', 'dispense1', 'reject1', 'count1', 'dispense2', 'reject2', 'count2', 'refill', 'error' @@ -160,14 +160,14 @@ function insertDispense (deviceId, tx, cartridges) { exports.addIncomingPhone = function addIncomingPhone (tx, notified) { const sql = `UPDATE cash_out_txs SET phone=$1, notified=$2 - WHERE tx_id=$3 + WHERE id=$3 AND phone IS NULL` const values = [tx.phone, notified, tx.id] return db.result(sql, values) .then(results => { const noPhone = results.rowCount === 0 - const sql2 = 'insert into cash_out_actions (tx_id, action) values ($1, $2)' + const sql2 = 'insert into cash_out_actions (cash_out_txs_id, action) values ($1, $2)' if (noPhone) return {noPhone: noPhone} @@ -182,14 +182,11 @@ function normalizeTx (tx) { tx.txHash = tx.tx_hash tx.cryptoCode = tx.crypto_code tx.cryptoAtoms = new BigNumber(tx.crypto_atoms) - tx.id = tx.tx_id tx.to_address = undefined tx.currency_code = undefined tx.tx_hash = undefined tx.crypto_code = undefined - tx.satoshis = undefined - tx.tx_id = undefined // Eventually turn this into BigDecimal, for now, integer tx.fiat = parseInt(tx.fiat, 10) @@ -213,14 +210,14 @@ exports.fetchPhoneTxs = function fetchPhoneTxs (phone, dispenseTimeout) { } exports.fetchTx = function fetchTx (txId) { - const sql = 'SELECT * FROM cash_out_txs WHERE tx_id=$1' + const sql = 'SELECT * FROM cash_out_txs WHERE id=$1' return db.one(sql, [txId]) .then(row => normalizeTx(row)) } exports.addDispenseRequest = function addDispenseRequest (tx) { - const sql = 'update cash_out_txs set dispensed=$1 where tx_id=$2 and dispensed=$3' + const sql = 'update cash_out_txs set dispensed=$1 where id=$2 and dispensed=$3' const values = [true, tx.id, false] return db.result(sql, values) @@ -228,7 +225,7 @@ exports.addDispenseRequest = function addDispenseRequest (tx) { const alreadyDispensed = results.rowCount === 0 if (alreadyDispensed) return {dispense: false, reason: 'alreadyDispensed'} - const sql2 = 'insert into cash_out_actions (tx_id, action) values ($1, $2)' + const sql2 = 'insert into cash_out_actions (cash_out_txs_id, action) values ($1, $2)' return db.none(sql2, [tx.id, 'dispenseRequested']) .then(() => ({dispense: true})) @@ -238,7 +235,7 @@ exports.addDispenseRequest = function addDispenseRequest (tx) { exports.addDispense = function addDispense (deviceId, tx, cartridges) { return insertDispense(deviceId, tx, cartridges) .then(() => { - const sql2 = 'insert into cash_out_actions (tx_id, action) values ($1, $2)' + const sql2 = 'insert into cash_out_actions (cash_out_txs_id, action) values ($1, $2)' return db.none(sql2, [tx.id, 'dispensed']) }) @@ -321,7 +318,7 @@ exports.updateTxStatus = function updateTxStatus (tx, status) { const tmSRD = new TransactionMode({tiLevel: isolationLevel.serializable}) function transaction (t) { - const sql = 'select status, confirmation_time from cash_out_txs where tx_id=$1' + const sql = 'select status, confirmation_time from cash_out_txs where id=$1' return t.one(sql, [tx.id]) .then(row => { const newStatus = ratchetStatus(row.status, status) @@ -331,8 +328,8 @@ exports.updateTxStatus = function updateTxStatus (tx, status) { (newStatus === 'instant' || newStatus === 'confirmed') const sql2 = setConfirmationTime - ? 'UPDATE cash_out_txs SET status=$1, confirmation_time=now() WHERE tx_id=$2' - : 'UPDATE cash_out_txs SET status=$1 WHERE tx_id=$2' + ? 'UPDATE cash_out_txs SET status=$1, confirmation_time=now() WHERE id=$2' + : 'UPDATE cash_out_txs SET status=$1 WHERE id=$2' const values2 = [newStatus, tx.id] @@ -349,11 +346,11 @@ exports.updateTxStatus = function updateTxStatus (tx, status) { .then(r => { if (!r) return - const sql3 = 'insert into cash_out_actions (tx_id, action) values ($1, $2)' + const sql3 = 'insert into cash_out_actions (cash_out_txs_id, action) values ($1, $2)' return db.none(sql3, [tx.id, r.status]) .then(() => { if (r.status === 'confirmed') { - const sql4 = `update cash_out_hds set confirmed=true where tx_id=$1` + const sql4 = `update cash_out_hds set confirmed=true where id=$1` return db.none(sql4, [tx.id]) } }) @@ -366,18 +363,18 @@ exports.updateRedeem = function updateRedeem (txId) { return db.none(sql, values) .then(() => { - const sql2 = 'insert into cash_out_actions (tx_id, action) values ($1, $2)' + const sql2 = 'insert into cash_out_actions (cash_out_txs_id, action) values ($1, $2)' return db.none(sql2, [txId, 'redeem']) }) } exports.updateNotify = function updateNotify (tx) { - const sql = 'UPDATE cash_out_txs SET notified=$1 WHERE tx_id=$2' + const sql = 'UPDATE cash_out_txs SET notified=$1 WHERE id=$2' const values = [true, tx.id] return db.none(sql, values) .then(() => { - const sql2 = 'insert into cash_out_actions (tx_id, action) values ($1, $2)' + const sql2 = 'insert into cash_out_actions (cash_out_txs_id, action) values ($1, $2)' return db.none(sql2, [tx.id, 'notified']) }) } @@ -441,7 +438,7 @@ exports.nextCashOutSerialHD = function nextCashOutSerialHD (txId, cryptoCode) { const attempt = () => db.oneOrNone(sql, [cryptoCode]) .then(row => { const serialNumber = row ? row.hd_serial + 1 : 0 - const fields2 = ['tx_id', 'crypto_code', 'hd_serial'] + const fields2 = ['id', 'crypto_code', 'hd_serial'] const sql2 = getInsertQuery('cash_out_hds', fields2) const values2 = [txId, cryptoCode, serialNumber] return db.none(sql2, values2) @@ -453,7 +450,7 @@ exports.nextCashOutSerialHD = function nextCashOutSerialHD (txId, cryptoCode) { exports.fetchLiveHD = function fetchLiveHD () { const sql = `select * from cash_out_txs, cash_out_hds - where cash_out_txs.tx_id=cash_out_hds.tx_id + where cash_out_txs.id=cash_out_hds.id and status=$1 and swept=$2 and ((extract(epoch from (now() - cash_out_txs.created))) * 1000)<$3` @@ -472,6 +469,6 @@ exports.fetchOldHD = function fetchLiveHD () { } exports.markSwept = function markSwept (txId) { - const sql = `update cash_out_hds set swept=$1 where tx_id=$2` + const sql = `update cash_out_hds set swept=$1 where id=$2` return db.none(sql, [true, txId]) }