Skip to content

Commit

Permalink
Add id-card compliance
Browse files Browse the repository at this point in the history
  • Loading branch information
joshmh committed Mar 5, 2018
1 parent c9ab708 commit 2df3da5
Show file tree
Hide file tree
Showing 6 changed files with 67 additions and 210 deletions.
30 changes: 24 additions & 6 deletions lib/customers.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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 ])
Expand Down
7 changes: 5 additions & 2 deletions lib/db.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
})
Expand Down
8 changes: 7 additions & 1 deletion lib/route-helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -110,4 +110,10 @@ function updateMachineDefaults (deviceId) {
})
}

module.exports = {stateChange, fetchPhoneTx, fetchStatusTx, updateDeviceConfigVersion, updateMachineDefaults}
module.exports = {
stateChange,
fetchPhoneTx,
fetchStatusTx,
updateDeviceConfigVersion,
updateMachineDefaults
}
14 changes: 14 additions & 0 deletions lib/routes.js
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down Expand Up @@ -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)
Expand Down
Loading

0 comments on commit 2df3da5

Please sign in to comment.