chore: server code formatting
This commit is contained in:
parent
aedabcbdee
commit
68517170e2
234 changed files with 9824 additions and 6195 deletions
|
|
@ -7,73 +7,91 @@ const { anonymousCustomer } = require('../constants')
|
|||
const toDb = helper.toDb
|
||||
const toObj = helper.toObj
|
||||
|
||||
const UPDATEABLE_FIELDS = ['txHash', 'txVersion', 'status', 'dispense', 'dispenseConfirmed',
|
||||
'notified', 'redeem', 'phone', 'error', 'swept', 'publishedAt', 'confirmedAt', 'errorCode',
|
||||
'receivedCryptoAtoms', 'walletScore', 'customerId' ]
|
||||
const UPDATEABLE_FIELDS = [
|
||||
'txHash',
|
||||
'txVersion',
|
||||
'status',
|
||||
'dispense',
|
||||
'dispenseConfirmed',
|
||||
'notified',
|
||||
'redeem',
|
||||
'phone',
|
||||
'error',
|
||||
'swept',
|
||||
'publishedAt',
|
||||
'confirmedAt',
|
||||
'errorCode',
|
||||
'receivedCryptoAtoms',
|
||||
'walletScore',
|
||||
'customerId',
|
||||
]
|
||||
|
||||
module.exports = {upsert, update, insert}
|
||||
module.exports = { upsert, update, insert }
|
||||
|
||||
function upsert (t, oldTx, tx) {
|
||||
function upsert(t, oldTx, tx) {
|
||||
if (!oldTx) {
|
||||
return insert(t, tx)
|
||||
.then(newTx => [oldTx, newTx])
|
||||
return insert(t, tx).then(newTx => [oldTx, newTx])
|
||||
}
|
||||
|
||||
return update(t, tx, diff(oldTx, tx))
|
||||
.then(newTx => [oldTx, newTx, tx.justAuthorized])
|
||||
return update(t, tx, diff(oldTx, tx)).then(newTx => [
|
||||
oldTx,
|
||||
newTx,
|
||||
tx.justAuthorized,
|
||||
])
|
||||
}
|
||||
|
||||
function insert (t, tx) {
|
||||
function insert(t, tx) {
|
||||
const dbTx = toDb(tx)
|
||||
|
||||
const sql = pgp.helpers.insert(dbTx, null, 'cash_out_txs') + ' returning *'
|
||||
return t.one(sql)
|
||||
.then(toObj)
|
||||
return t.one(sql).then(toObj)
|
||||
}
|
||||
|
||||
function update (t, tx, changes) {
|
||||
function update(t, tx, changes) {
|
||||
if (_.isEmpty(changes)) return Promise.resolve(tx)
|
||||
|
||||
const dbChanges = toDb(changes)
|
||||
const sql = pgp.helpers.update(dbChanges, null, 'cash_out_txs') +
|
||||
const sql =
|
||||
pgp.helpers.update(dbChanges, null, 'cash_out_txs') +
|
||||
pgp.as.format(' where id=$1', [tx.id])
|
||||
|
||||
const newTx = _.merge(tx, changes)
|
||||
|
||||
return t.none(sql)
|
||||
.then(() => newTx)
|
||||
return t.none(sql).then(() => newTx)
|
||||
}
|
||||
|
||||
function diff (oldTx, newTx) {
|
||||
function diff(oldTx, newTx) {
|
||||
let updatedTx = {}
|
||||
|
||||
UPDATEABLE_FIELDS.forEach(fieldKey => {
|
||||
if (oldTx && _.isEqualWith(nilEqual, oldTx[fieldKey], newTx[fieldKey])) return
|
||||
if (oldTx && _.isEqualWith(nilEqual, oldTx[fieldKey], newTx[fieldKey]))
|
||||
return
|
||||
|
||||
// We never null out an existing field
|
||||
if (oldTx && _.isNil(newTx[fieldKey])) return updatedTx[fieldKey] = oldTx[fieldKey]
|
||||
if (oldTx && _.isNil(newTx[fieldKey]))
|
||||
return (updatedTx[fieldKey] = oldTx[fieldKey])
|
||||
|
||||
switch (fieldKey) {
|
||||
case 'customerId':
|
||||
if (oldTx.customerId === anonymousCustomer.uuid) {
|
||||
return updatedTx['customerId'] = newTx['customerId']
|
||||
return (updatedTx['customerId'] = newTx['customerId'])
|
||||
}
|
||||
return
|
||||
// prevent dispense changing from 'true' to 'false'
|
||||
case 'dispense':
|
||||
if (!oldTx.dispense) {
|
||||
return updatedTx[fieldKey] = newTx[fieldKey]
|
||||
return (updatedTx[fieldKey] = newTx[fieldKey])
|
||||
}
|
||||
return
|
||||
default:
|
||||
return updatedTx[fieldKey] = newTx[fieldKey]
|
||||
return (updatedTx[fieldKey] = newTx[fieldKey])
|
||||
}
|
||||
})
|
||||
|
||||
return updatedTx
|
||||
}
|
||||
|
||||
function nilEqual (a, b) {
|
||||
function nilEqual(a, b) {
|
||||
if (_.isNil(a) && _.isNil(b)) return true
|
||||
|
||||
return undefined
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue