Add *override_by fields in customer

This commit is contained in:
goga-m 2017-09-28 16:03:49 +03:00 committed by Josh Harvey
parent e351d1b495
commit 89eb617a4a
3 changed files with 39 additions and 3 deletions

View file

@ -31,10 +31,13 @@ function get (phone) {
*
* @param {string} id Customer's id
* @param {object} data Fields to update
* @param {string} Acting user's token
*
* @returns {Promise} Newly updated Customer
*/
function update (id, data) {
const updateData = _.omit(['id'], _.mapKeys(_.snakeCase, data))
function update (id, data, userToken) {
const formattedData = _.omit(['id'], _.mapKeys(_.snakeCase, data))
const updateData = addOverrideUser(formattedData, userToken)
const sql = Pgp.helpers.update(updateData, _.keys(updateData), 'customers') +
' where id=$1 returning *'
return db.oneOrNone(sql, [id])
@ -60,6 +63,32 @@ function getDailyVolume (id) {
})
}
/**
* Add *override_by fields with acting user's token
*
* @name addOverrideUser
* @function
*
* @param {object} customer Customer's object to add the fields
* @param {string} userToken Acting user's token
* @returns {object} Customer populated with *_by fields
*/
function addOverrideUser (customer, userToken) {
if (!userToken) return customer
// Overrode fields
const overrideFields = [
'sms_override',
'id_card_data_override',
'id_card_photo_override',
'front_facing_cam_override',
'sanctions_check_override',
'authorized_override' ]
overrideFields.forEach(field => {
if (customer[field]) customer[field + '_by'] = userToken
})
return customer
}
/**
* Format and populate fields
* for customer record