fix: use gql upload lib and remove logs

This commit is contained in:
José Oliveira 2021-11-09 13:55:12 +00:00
parent 3e4f4a4962
commit 68c635ce38
10 changed files with 112 additions and 70 deletions

View file

@ -161,24 +161,20 @@ function edit (id, data, userToken) {
]
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 formattedData = enhanceEditedPhotos(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)
})
return db.none(upsert)
.then(getCustomerById(id))
}
/**
@ -202,6 +198,25 @@ function enhanceEditedFields (fields, userToken) {
return fields
}
/**
* Add *_path to edited photos fields
*
* @name enhanceEditedFields
* @function
*
* @param {object} fields Fields to be enhanced
* @returns {object} fields enhanced with *_path
*/
function enhanceEditedPhotos (fields) {
return _.mapKeys((field) => {
if (_.includes(field, ['front_camera', 'id_card_photo'])) {
return field + '_path'
}
return field
}, fields)
}
/**
* Remove the edited data from the db record
*
@ -248,38 +263,27 @@ function deleteEditedData (id, data) {
* @returns {object} path New photo path
*
*/
function replacePhoto (id, photo, photoType) {
async function updateEditedPhoto (id, photo, photoType) {
const newPatch = {}
const baseDir = photoType === 'frontCamera' ? frontCameraBaseDir : idPhotoCardBasedir
const { createReadStream } = photo
const { createReadStream, filename } = photo
const stream = createReadStream()
// workout the image hash
// i.e. 240e85ff2e4bb931f235985dd0134e459239496d2b5af6c5665168d38ef89b50
const hash = crypto
.createHash('sha256')
.update(imageData)
.digest('hex')
const randomString = uuid.v4().toString() + '/'
// workout the image folder
// i.e. 24/0e/85
const rpath = _.join(path.sep, _.map(_.wrap(_.join, ''), _.take(3, _.chunk(2, _.split('', hash)))))
// i.e. ../<lamassu-server-home>/idphotocard/24/0e/85
const dirname = path.join(idPhotoCardBasedir, rpath)
// i.e. ..62ed29c5-f37e-4fb7-95bb-c52d4a3738f7/filename.jpg
const rpath = path.join(randomString, filename)
// create the directory tree if needed
_.attempt(() => makeDir.sync(dirname))
_.attempt(() => makeDir.sync(path.join(baseDir, randomString)))
// i.e. ../<lamassu-server-home>/idphotocard/24/0e/85/240e85ff2e4bb931f235985dd01....jpg
const filename = path.join(dirname, hash + '.jpg')
// i.e. ../<lamassu-server-home>/idphotocard/62ed29c5-f37e-4fb7-95bb-c52d4a3738f7/filename.jpg
const pathName = path.join(baseDir, rpath)
// update db record patch
// i.e. {
// "idCardPhotoPath": "24/0e/85/240e85ff2e4bb931f235985dd01....jpg",
// "idCardPhotoAt": "now()"
// }
newPatch.idCardPhotoPath = path.join(rpath, hash + '.jpg')
newPatch.idCardPhotoAt = 'now()'
await stream.pipe(fs.createWriteStream(pathName))
newPatch[photoType] = rpath
return newPatch
}
const invalidateCustomerNotifications = (id, data) => {
@ -670,9 +674,9 @@ function getCustomersList (phone = null, name = null, address = null, id = null)
*/
function getCustomerById (id) {
const passableErrorCodes = _.map(Pgp.as.text, TX_PASSTHROUGH_ERROR_CODES).join(',')
const sql = `select id, authorized_override, days_suspended, is_suspended, front_camera_path, front_camera_at, front_camera_override,
phone, sms_override, id_card_data, id_card_data_override, id_card_data_expiration,
id_card_photo_path, id_card_photo_override, us_ssn, us_ssn_override, sanctions, sanctions_at,
const sql = `select id, authorized_override, days_suspended, is_suspended, front_camera_at, front_camera_path, front_camera_at, front_camera_override,
phone, sms_override, id_card_data_at, id_card_data, id_card_data_override, id_card_data_expiration,
id_card_photo_at, id_card_photo_path, id_card_photo_override, us_ssn_at, us_ssn, us_ssn_override, sanctions, sanctions_at,
sanctions_override, total_txs, total_spent, created as last_active, fiat as last_tx_fiat,
fiat_code as last_tx_fiat_code, tx_class as last_tx_class, subscriber_info, custom_fields
from (
@ -680,8 +684,8 @@ function getCustomerById (id) {
greatest(0, date_part('day', c.suspended_until - now())) as days_suspended,
c.suspended_until > now() as is_suspended,
c.front_camera_path, c.front_camera_at, c.front_camera_override,
c.phone, c.sms_override, c.id_card_data, c.id_card_data_override, c.id_card_data_expiration,
c.id_card_photo_path, c.id_card_photo_override, c.us_ssn, c.us_ssn_override, c.sanctions,
c.phone, c.sms_override, c.id_card_data, c.id_card_data_at, c.id_card_data_override, c.id_card_data_expiration,
c.id_card_photo_path, c.id_card_photo_at, c.id_card_photo_override, c.us_ssn, c.us_ssn_at, c.us_ssn_override, c.sanctions,
c.sanctions_at, c.sanctions_override, c.subscriber_info, t.tx_class, t.fiat, t.fiat_code, t.created,
row_number() over (partition by c.id order by t.created desc) as rn,
sum(case when t.id is not null then 1 else 0 end) over (partition by c.id) as total_txs,
@ -700,6 +704,10 @@ function getCustomerById (id) {
where c.id = $2
) as cl where rn = 1`
return db.oneOrNone(sql, [passableErrorCodes, id])
.then(customerData => {
return getEditedData(id)
.then(customerEditedData => selectLatestData(customerData, customerEditedData))
})
.then(populateOverrideUsernames)
.then(camelize)
}
@ -709,10 +717,16 @@ function getCustomerById (id) {
*
* @param {String} id customer id
*
* @returns {array} A single customer instance with the most recent data
* @returns {array} A single customer instance with the most recent edited data
*/
function getManuallyEditedData (id) {
function getEditedData (id) {
const sql = `SELECT * FROM edited_customer_data WHERE customer_id = $1`
return db.oneOrNone(sql, [id])
.then(_.omitBy(_.isNil))
}
function selectLatestData (customerData, customerEditedData) {
return customerData
}
/**
@ -983,7 +997,7 @@ module.exports = {
saveCustomField,
removeCustomField,
edit,
getManuallyEditedData,
deleteEditedData,
updateEditedPhoto,
updateTxCustomerPhoto
}