Update Customer's data

This commit is contained in:
goga-m 2017-09-28 12:30:09 +03:00 committed by Josh Harvey
parent 9877fc8ef0
commit 94de659246
3 changed files with 534 additions and 104 deletions

View file

@ -29,6 +29,7 @@ const server = require('./server')
const transactions = require('./transactions')
const customers = require('../customers')
const funding = require('./funding')
const _ = require('lodash/fp')
const NEVER = new Date(Date.now() + 100 * T.years)
const REAUTHENTICATE_INTERVAL = T.minute
@ -184,9 +185,7 @@ app.get('/api/customer/:id', (req, res, next) => {
})
/**
* Endpoint for patching customer's authorizedOverride status
*
* Possible values: blocked, verified, automatic
* Endpoint for patching customer's data
*
* @param {string} '/api/customer/ Url to handle
* @param {object} req Request object
@ -194,11 +193,8 @@ app.get('/api/customer/:id', (req, res, next) => {
* @param {function} next Callback
*/
app.patch('/api/customer/:id', (req, res, next) => {
if (!req.query.authorizedOverride) return res.status(400).send({Error: 'Requires authorized'})
return customers.patch(req.params.id, {
authorizedOverride: req.query.authorizedOverride
})
if (!req.params.id) return res.status(400).send({Error: 'Requires id'})
return customers.update(req.params.id, req.query)
.then(r => res.send(r))
.catch(() => res.status(404).send({Error: 'Not found'}))
})

View file

@ -5,6 +5,7 @@ const BN = require('./bn')
const anonymous = require('../lib/constants').anonymousCustomer
const NUM_RESULTS = 20
const camelize = require('camelize')
const Pgp = require('pg-promise')()
function add (customer) {
const sql = 'insert into customers (id, phone, phone_at) values ($1, $2, now()) returning *'
@ -23,24 +24,27 @@ function get (phone) {
}
/**
* Patch Customer
* Note: Currently patching only authorized_verified field
* Update customer record
*
* @name update
* @function
*
* @param {string} id Customer's id
* @param {object} values Values to patch
* @returns {object} Updated customer
* @param {object} data Fields to update
* @returns {Promise} Newly updated Customer
*/
function patch (id, values) {
const sql = 'update customers set authorized_override=$2 where id=$1 returning *'
return db.oneOrNone(sql, [id, values.authorizedOverride])
function update (id, data) {
const updateData = _.omit(['id'], _.mapKeys(_.snakeCase, data))
const sql = Pgp.helpers.update(updateData, _.keys(updateData), 'customers') +
' where id=$1 returning *'
return db.oneOrNone(sql, [id])
.then(customer => customer ? format(customer) : null)
}
function getById (id) {
const sql = 'select * from customers where id=$1'
return db.oneOrNone(sql, [id])
.then(customer => {
return (customer) ? format(customer) : null
})
.then(customer => customer ? format(customer) : null)
}
function getDailyVolume (id) {
@ -104,4 +108,4 @@ function batch () {
.then(_.map(format))
}
module.exports = { add, get, batch, getById, patch}
module.exports = { add, get, batch, getById, update}

File diff suppressed because one or more lines are too long