Provide Daily Volume in customer's api

This commit is contained in:
goga-m 2017-10-12 15:28:44 +03:00 committed by Josh Harvey
parent 67dda25b20
commit 932be9e7db
2 changed files with 131 additions and 71 deletions

View file

@ -19,13 +19,8 @@ function add (customer) {
function get (phone) { function get (phone) {
const sql = 'select * from customers where phone=$1' const sql = 'select * from customers where phone=$1'
return db.oneOrNone(sql, [phone]) return db.oneOrNone(sql, [phone])
.then(customer => { .then(populateDailyVolume)
if (!customer) return .then(camelize)
return getDailyVolume(customer.id).then(dailyVolume => {
const formatted = camelize(customer)
return _.set('dailyVolume', dailyVolume, formatted)
})
})
} }
/** /**
@ -49,6 +44,7 @@ function update (id, data, userToken) {
.then(addComplianceOverrides(id, updateData, userToken)) .then(addComplianceOverrides(id, updateData, userToken))
.then(populateOverrideUsernames) .then(populateOverrideUsernames)
.then(computeStatus) .then(computeStatus)
.then(populateDailyVolume)
.then(camelize) .then(camelize)
} }
@ -57,9 +53,20 @@ function getById (id, userToken) {
return db.oneOrNone(sql, [id]) return db.oneOrNone(sql, [id])
.then(populateOverrideUsernames) .then(populateOverrideUsernames)
.then(computeStatus) .then(computeStatus)
.then(populateDailyVolume)
.then(camelize) .then(camelize)
} }
/**
* Get and calculate customer's daily volume
* for both cash_in & cash_out txs
*
* @name getDailyVolume
* @function
*
* @param {string} id Customer's id
* @returns {Bignumber} Customer's daily volume
*/
function getDailyVolume (id) { function getDailyVolume (id) {
return Promise.all([ return Promise.all([
db.one(`select coalesce(sum(fiat), 0) as total from cash_in_txs db.one(`select coalesce(sum(fiat), 0) as total from cash_in_txs
@ -73,6 +80,23 @@ function getDailyVolume (id) {
}) })
} }
/**
* Populate customer object
* with dailyVolume information
*
* @name populateDailyVolume
* @function
*
* @param {object} customer Customer object
* @returns {object} Customer object populated with dailyVolume
*/
function populateDailyVolume (customer) {
if (!customer) return
return getDailyVolume(customer.id).then(dailyVolume => {
return _.set('daily_volume', dailyVolume, customer)
})
}
/** /**
* Get all available complianceTypes * Get all available complianceTypes
* that can be overriden (excluding hard_limit) * that can be overriden (excluding hard_limit)
@ -236,6 +260,7 @@ function batch () {
.then(customers => Promise.all(_.map(customer => { .then(customers => Promise.all(_.map(customer => {
return populateOverrideUsernames(customer) return populateOverrideUsernames(customer)
.then(computeStatus) .then(computeStatus)
.then(populateDailyVolume)
.then(camelize) .then(camelize)
}, customers))) }, customers)))
} }

File diff suppressed because one or more lines are too long