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

@ -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}