Merge branch 'v5' of github.com:lamassu/lamassu-server into origin-v5
This commit is contained in:
commit
c860df1576
3 changed files with 56 additions and 2 deletions
|
|
@ -73,7 +73,7 @@ function isMonotonic (oldField, newField, fieldKey) {
|
||||||
|
|
||||||
function ensureRatchet (oldField, newField, fieldKey) {
|
function ensureRatchet (oldField, newField, fieldKey) {
|
||||||
const monotonic = ['cryptoAtoms', 'fiat', 'cashInFeeCrypto', 'send', 'sendConfirmed', 'operatorCompleted', 'timedout', 'txVersion']
|
const monotonic = ['cryptoAtoms', 'fiat', 'cashInFeeCrypto', 'send', 'sendConfirmed', 'operatorCompleted', 'timedout', 'txVersion']
|
||||||
const free = ['sendPending', 'error', 'errorCode']
|
const free = ['sendPending', 'error', 'errorCode', 'customerId']
|
||||||
|
|
||||||
if (_.isNil(oldField)) return true
|
if (_.isNil(oldField)) return true
|
||||||
if (_.includes(fieldKey, monotonic)) return isMonotonic(oldField, newField, fieldKey)
|
if (_.includes(fieldKey, monotonic)) return isMonotonic(oldField, newField, fieldKey)
|
||||||
|
|
|
||||||
35
lib/customers.js
Normal file
35
lib/customers.js
Normal file
|
|
@ -0,0 +1,35 @@
|
||||||
|
const db = require('./db')
|
||||||
|
const uuid = require('uuid')
|
||||||
|
const _ = require('lodash/fp')
|
||||||
|
const BN = require('./bn')
|
||||||
|
|
||||||
|
function add (customer) {
|
||||||
|
const sql = 'insert into customers (id, phone, phone_at) values ($1, $2, now()) returning *'
|
||||||
|
return db.one(sql, [uuid.v4(), customer.phone])
|
||||||
|
}
|
||||||
|
|
||||||
|
function get (phone) {
|
||||||
|
const sql = 'select id, phone from customers where phone=$1'
|
||||||
|
return db.oneOrNone(sql, [phone])
|
||||||
|
.then(customer => {
|
||||||
|
if (!customer) return
|
||||||
|
return getDailyVolume(customer.id).then(dailyVolume => {
|
||||||
|
return _.set('dailyVolume', dailyVolume, customer)
|
||||||
|
})
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
function getDailyVolume (id) {
|
||||||
|
return Promise.all([
|
||||||
|
db.one(`select coalesce(sum(fiat), 0) as total from cash_in_txs
|
||||||
|
where customer_id=$1
|
||||||
|
and created > now() - interval '1 day'`, [id]),
|
||||||
|
db.one(`select COALESCE(sum(fiat), 0) as total from cash_out_txs
|
||||||
|
where customer_id=$1
|
||||||
|
and created > now() - interval '1 day'`, [id])
|
||||||
|
]).then(([cashInTotal, cashOutTotal]) => {
|
||||||
|
return BN(cashInTotal.total).add(cashOutTotal.total)
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
module.exports = { add, get }
|
||||||
|
|
@ -16,6 +16,7 @@ const helpers = require('./route-helpers')
|
||||||
const poller = require('./poller')
|
const poller = require('./poller')
|
||||||
const Tx = require('./tx')
|
const Tx = require('./tx')
|
||||||
const E = require('./error')
|
const E = require('./error')
|
||||||
|
const customers = require('./customers')
|
||||||
|
|
||||||
const argv = require('minimist')(process.argv.slice(2))
|
const argv = require('minimist')(process.argv.slice(2))
|
||||||
|
|
||||||
|
|
@ -143,6 +144,24 @@ function verifyTx (req, res, next) {
|
||||||
.catch(next)
|
.catch(next)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function getCustomerWithPhoneCode (req, res, next) {
|
||||||
|
const pi = plugins(req.settings, req.deviceId)
|
||||||
|
const phone = req.body.phone
|
||||||
|
return pi.getPhoneCode(phone)
|
||||||
|
.then(code => {
|
||||||
|
return customers.get(phone.phone).then(customer => {
|
||||||
|
if (customer) return respond(req, res, {code, customer})
|
||||||
|
return customers.add(req.body)
|
||||||
|
.then(customer => respond(req, res, {code, customer}))
|
||||||
|
})
|
||||||
|
})
|
||||||
|
.catch(err => {
|
||||||
|
if (err.name === 'BadNumberError') throw httpError('Bad number', 410)
|
||||||
|
throw err
|
||||||
|
})
|
||||||
|
.catch(next)
|
||||||
|
}
|
||||||
|
|
||||||
function ca (req, res) {
|
function ca (req, res) {
|
||||||
const token = req.query.token
|
const token = req.query.token
|
||||||
|
|
||||||
|
|
@ -269,7 +288,7 @@ app.post('/event', deviceEvent)
|
||||||
app.post('/verify_user', verifyUser)
|
app.post('/verify_user', verifyUser)
|
||||||
app.post('/verify_transaction', verifyTx)
|
app.post('/verify_transaction', verifyTx)
|
||||||
|
|
||||||
app.post('/phone_code', phoneCode)
|
app.post('/phone_code', getCustomerWithPhoneCode)
|
||||||
app.post('/tx', postTx)
|
app.post('/tx', postTx)
|
||||||
app.get('/tx/:id', getTx)
|
app.get('/tx/:id', getTx)
|
||||||
app.get('/tx', getPhoneTx)
|
app.get('/tx', getPhoneTx)
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue