feat: add edit, delete and image input
This commit is contained in:
parent
421543f0c7
commit
3de7ae2fe9
12 changed files with 545 additions and 139 deletions
153
lib/customers.js
153
lib/customers.js
|
|
@ -138,6 +138,103 @@ async function updateCustomer (id, data, userToken) {
|
|||
return getCustomerById(id)
|
||||
}
|
||||
|
||||
/**
|
||||
* Update all customer record
|
||||
*
|
||||
* @name save
|
||||
* @function
|
||||
*
|
||||
* @param {string} id Customer's id
|
||||
* @param {object} data Fields to update
|
||||
*
|
||||
* @returns {Promise} Newly updated Customer
|
||||
*/
|
||||
|
||||
function edit (id, data, userToken) {
|
||||
const defaults = [
|
||||
'front_camera',
|
||||
'id_card_data',
|
||||
'id_card_photo',
|
||||
'us_ssn',
|
||||
'subcriber_info',
|
||||
'name'
|
||||
]
|
||||
const filteredData = _.pick(defaults, _.mapKeys(_.snakeCase, _.omitBy(_.isNil, data)))
|
||||
if (_.isEmpty(filteredData)) return getCustomerById(id)
|
||||
if (filteredData.front_camera_data || filteredData.id_card_photo_data) return getCustomerById(id)
|
||||
const formattedData = enhanceEditedFields(filteredData, userToken)
|
||||
const defaultDbData = {
|
||||
customer_id: id,
|
||||
created: new Date(),
|
||||
...formattedData
|
||||
}
|
||||
console.log(formattedData, 'FORMATED', defaultDbData, 'DEFAULT DB')
|
||||
const cs = new Pgp.helpers.ColumnSet(_.keys(defaultDbData),
|
||||
{ table: 'edited_customer_data' })
|
||||
const onConflict = ' ON CONFLICT (customer_id) DO UPDATE SET ' +
|
||||
cs.assignColumns({ from: 'EXCLUDED', skip: ['customer_id', 'created'] })
|
||||
const upsert = Pgp.helpers.insert(defaultDbData, cs) + onConflict
|
||||
db.none(upsert)
|
||||
.then(res => {
|
||||
console.log(res)
|
||||
return getCustomerById(id)
|
||||
})
|
||||
}
|
||||
|
||||
/**
|
||||
* Add *edited_by and *edited_at fields with acting user's token
|
||||
* and date of override respectively before saving to db.
|
||||
*
|
||||
* @name enhanceEditedFields
|
||||
* @function
|
||||
*
|
||||
* @param {object} fields Fields to be enhanced
|
||||
* @param {string} userToken Acting user's token
|
||||
* @returns {object} fields enhanced with *_by and *_at fields
|
||||
*/
|
||||
|
||||
function enhanceEditedFields (fields, userToken) {
|
||||
if (!userToken) return fields
|
||||
_.mapKeys((field) => {
|
||||
fields[field + '_edited_by'] = userToken
|
||||
fields[field + '_edited_at'] = 'now()^'
|
||||
}, fields)
|
||||
return fields
|
||||
}
|
||||
|
||||
/**
|
||||
* Remove the edited data from the db record
|
||||
*
|
||||
* @name enhanceOverrideFields
|
||||
* @function
|
||||
*
|
||||
* @param {string} id Customer's id
|
||||
* @param {object} data Fields to be deleted
|
||||
*
|
||||
* @returns {Promise} Newly updated Customer
|
||||
*
|
||||
*/
|
||||
|
||||
function deleteEditedData (id, data) {
|
||||
// NOT IMPLEMENTING THIS FEATURE FOR NOW
|
||||
// const defaults = [
|
||||
// 'front_camera',
|
||||
// 'id_card_data',
|
||||
// 'id_card_photo',
|
||||
// 'us_ssn',
|
||||
// 'subcriber_info',
|
||||
// 'name'
|
||||
// ]
|
||||
// const filteredData = _.pick(defaults, _.mapKeys(_.snakeCase, data))
|
||||
// if (_.isEmpty(filteredData)) return getCustomerById(id)
|
||||
|
||||
// const cs = new Pgp.helpers.ColumnSet(_.keys(filteredData),
|
||||
// { table: 'edited_customer_data' })
|
||||
// const update = Pgp.helpers.update(filteredData, cs)
|
||||
// db.none(update)
|
||||
return getCustomerById(id)
|
||||
}
|
||||
|
||||
const invalidateCustomerNotifications = (id, data) => {
|
||||
if (data.authorized_override !== 'verified') return Promise.resolve()
|
||||
|
||||
|
|
@ -510,7 +607,7 @@ function getCustomersList (phone = null, name = null, address = null, id = null)
|
|||
AND ($6 IS NULL OR id_card_data::json->>'address' = $6)
|
||||
AND ($7 IS NULL OR id_card_data::json->>'documentNumber' = $7)
|
||||
limit $3`
|
||||
return db.any(sql, [passableErrorCodes, anonymous.uuid, NUM_RESULTS, phone, name, address, id ])
|
||||
return db.any(sql, [ passableErrorCodes, anonymous.uuid, NUM_RESULTS, phone, name, address, id ])
|
||||
.then(customers => Promise.all(_.map(customer => {
|
||||
return populateOverrideUsernames(customer)
|
||||
.then(camelize)
|
||||
|
|
@ -518,11 +615,11 @@ function getCustomersList (phone = null, name = null, address = null, id = null)
|
|||
}
|
||||
|
||||
/**
|
||||
* Query all customers, ordered by last activity
|
||||
* Query a specific customer, ordered by last activity
|
||||
* and with aggregate columns based on their
|
||||
* transactions
|
||||
*
|
||||
* @returns {array} Array of customers with it's transactions aggregations
|
||||
* @returns {array} A single customer instance with non edited
|
||||
*/
|
||||
function getCustomerById (id) {
|
||||
const passableErrorCodes = _.map(Pgp.as.text, TX_PASSTHROUGH_ERROR_CODES).join(',')
|
||||
|
|
@ -560,6 +657,17 @@ function getCustomerById (id) {
|
|||
.then(camelize)
|
||||
}
|
||||
|
||||
/**
|
||||
* Query the specific customer manually edited data
|
||||
*
|
||||
* @param {String} id customer id
|
||||
*
|
||||
* @returns {array} A single customer instance with the most recent data
|
||||
*/
|
||||
function getManuallyEditedData (id) {
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* @param {String} id customer id
|
||||
* @param {Object} patch customer update record
|
||||
|
|
@ -768,35 +876,35 @@ function updateFrontCamera (id, patch) {
|
|||
})
|
||||
}
|
||||
|
||||
function addCustomField(customerId, label, value) {
|
||||
function addCustomField (customerId, label, value) {
|
||||
const sql = `SELECT * FROM custom_field_definitions WHERE label=$1 LIMIT 1`
|
||||
return db.oneOrNone(sql, [label])
|
||||
.then(res => db.tx(t => {
|
||||
if (_.isNil(res)) {
|
||||
const fieldId = uuid.v4()
|
||||
const q1 = t.none(`INSERT INTO custom_field_definitions (id, label) VALUES ($1, $2)`, [fieldId, label])
|
||||
const q2 = t.none(`INSERT INTO customer_custom_field_pairs (customer_id, custom_field_id, value) VALUES ($1, $2, $3)`, [customerId, fieldId, value])
|
||||
return t.batch([q1, q2])
|
||||
}
|
||||
if (_.isNil(res)) {
|
||||
const fieldId = uuid.v4()
|
||||
const q1 = t.none(`INSERT INTO custom_field_definitions (id, label) VALUES ($1, $2)`, [fieldId, label])
|
||||
const q2 = t.none(`INSERT INTO customer_custom_field_pairs (customer_id, custom_field_id, value) VALUES ($1, $2, $3)`, [customerId, fieldId, value])
|
||||
return t.batch([q1, q2])
|
||||
}
|
||||
|
||||
if (!_.isNil(res) && !res.active) {
|
||||
const q1 = t.none(`UPDATE custom_field_definitions SET active = true WHERE id=$1`, [res.id])
|
||||
const q2 = t.none(`INSERT INTO customer_custom_field_pairs (customer_id, custom_field_id, value) VALUES ($1, $2, $3)`, [customerId, res.id, value])
|
||||
return t.batch([q1, q2])
|
||||
} else if (!_.isNil(res) && res.active) {
|
||||
const q1 = t.none(`INSERT INTO customer_custom_field_pairs (customer_id, custom_field_id, value) VALUES ($1, $2, $3)`, [customerId, res.id, value])
|
||||
return t.batch([q1])
|
||||
}
|
||||
})
|
||||
if (!_.isNil(res) && !res.active) {
|
||||
const q1 = t.none(`UPDATE custom_field_definitions SET active = true WHERE id=$1`, [res.id])
|
||||
const q2 = t.none(`INSERT INTO customer_custom_field_pairs (customer_id, custom_field_id, value) VALUES ($1, $2, $3)`, [customerId, res.id, value])
|
||||
return t.batch([q1, q2])
|
||||
} else if (!_.isNil(res) && res.active) {
|
||||
const q1 = t.none(`INSERT INTO customer_custom_field_pairs (customer_id, custom_field_id, value) VALUES ($1, $2, $3)`, [customerId, res.id, value])
|
||||
return t.batch([q1])
|
||||
}
|
||||
})
|
||||
)
|
||||
}
|
||||
|
||||
function saveCustomField(customerId, fieldId, newValue) {
|
||||
function saveCustomField (customerId, fieldId, newValue) {
|
||||
const sql = `UPDATE customer_custom_field_pairs SET value=$1 WHERE customer_id=$2 AND custom_field_id=$3`
|
||||
return db.none(sql, [newValue, customerId, fieldId])
|
||||
}
|
||||
|
||||
function removeCustomField(customerId, fieldId) {
|
||||
function removeCustomField (customerId, fieldId) {
|
||||
const sql = `SELECT * FROM customer_custom_field_pairs WHERE custom_field_id=$1`
|
||||
return db.any(sql, [fieldId])
|
||||
.then(res => db.tx(t => {
|
||||
|
|
@ -827,5 +935,8 @@ module.exports = {
|
|||
addCustomField,
|
||||
saveCustomField,
|
||||
removeCustomField,
|
||||
edit,
|
||||
getManuallyEditedData,
|
||||
deleteEditedData,
|
||||
updateTxCustomerPhoto
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue