Provide customer's daily volume
Provide customer's info on sms phone code
This commit is contained in:
parent
1fcd0ec2e8
commit
81b0fadec1
3 changed files with 56 additions and 2 deletions
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 }
|
||||
Loading…
Add table
Add a link
Reference in a new issue