Add id-card compliance

This commit is contained in:
Josh Harvey 2018-02-03 14:32:05 +02:00
parent fbcb8b5ff2
commit 86dfca60c1
6 changed files with 67 additions and 210 deletions

View file

@ -57,9 +57,12 @@ function get (phone) {
*/
function update (id, data, userToken) {
const formattedData = _.omit(['id'], _.mapKeys(_.snakeCase, data))
const updateData = enhanceOverrideFields(formattedData, userToken)
const updateData = enhanceAtFields(enhanceOverrideFields(formattedData, userToken))
const sql = Pgp.helpers.update(updateData, _.keys(updateData), 'customers') +
' where id=$1 returning *'
return db.one(sql, [id])
.then(addComplianceOverrides(id, updateData, userToken))
.then(populateOverrideUsernames)
@ -100,11 +103,11 @@ function getById (id, userToken) {
*/
function getDailyVolume (id) {
return Promise.all([
db.one(`select coalesce(sum(fiat), 0) as total from cash_in_txs
where customer_id=$1
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
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)
@ -161,6 +164,21 @@ function getComplianceTypes () {
'authorized' ]
}
function enhanceAtFields (fields) {
const updateableFields = [
'id_card_data',
'id_card_photo',
'front_camera',
'sanctions',
'authorized'
]
const updatedFields = _.intersection(updateableFields, _.keys(fields))
const atFields = _.fromPairs(_.map(f => [`${f}_at`, 'now()^'], updatedFields))
return _.merge(fields, atFields)
}
/**
* Add *override_by and *override_at fields with acting user's token
* and date of override respectively before saving to db.
@ -298,7 +316,7 @@ function populateOverrideUsernames (customer) {
* @returns {array} Array of customers populated with status field
*/
function batch () {
const sql = `select * from customers
const sql = `select * from customers
where id != $1
order by created desc limit $2`
return db.any(sql, [ anonymous.uuid, NUM_RESULTS ])

View file

@ -5,8 +5,11 @@ const logger = require('./logger')
const pgp = Pgp({
pgNative: true,
error: (err, e) => {
if (e.cn) return logger.error('Database not reachable.')
if (e.query) return
if (e.cn) logger.error('Database not reachable.')
if (e.query) {
logger.error(e.query)
logger.error(e.params)
}
logger.error(err)
}
})

View file

@ -110,4 +110,10 @@ function updateMachineDefaults (deviceId) {
})
}
module.exports = {stateChange, fetchPhoneTx, fetchStatusTx, updateDeviceConfigVersion, updateMachineDefaults}
module.exports = {
stateChange,
fetchPhoneTx,
fetchStatusTx,
updateDeviceConfigVersion,
updateMachineDefaults
}

View file

@ -177,6 +177,18 @@ function getCustomerWithPhoneCode (req, res, next) {
.catch(next)
}
function updateCustomer (req, res, next) {
const id = req.params.id
const patch = req.body
customers.getById(id)
.then(customer => {
if (!customer) { throw httpError('Not Found', 404)}
return customers.update(id, patch)
})
.then(customer => respond(req, res, {customer}))
.catch(next)
}
function getLastSeen (req, res, next) {
return logs.getLastSeen(req.deviceId)
.then(r => res.json(r))
@ -304,6 +316,8 @@ app.post('/verify_user', verifyUser)
app.post('/verify_transaction', verifyTx)
app.post('/phone_code', getCustomerWithPhoneCode)
app.patch('/customer/:id', updateCustomer)
app.post('/tx', postTx)
app.get('/tx/:id', getTx)
app.get('/tx', getPhoneTx)