feat: handle T&C photos and UI photos roll

This commit is contained in:
José Oliveira 2021-08-01 02:46:13 +01:00
parent ff474ee507
commit e729de1410
12 changed files with 428 additions and 73 deletions

View file

@ -528,7 +528,7 @@ function getCustomerById (id) {
select c.id, c.authorized_override,
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_override,
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.sanctions_at, c.sanctions_override, c.subscriber_info, t.tx_class, t.fiat, t.fiat_code, t.created,
@ -654,6 +654,59 @@ function updateIdCardData (patch, id) {
})
}
/**
* @param {String} customerId customer id
* @param {String} txId customer id
* @param {Object} patch customer t&c photo data
* @returns {Promise<Object>} new patch to be applied
*/
function updateTxCustomerPhoto (imageData) {
return Promise.resolve(imageData)
.then(imageData => {
const newPatch = {}
const directory = `${operatorDataDir}/customersphotos`
if (_.isEmpty(imageData)) {
return
}
// decode the base64 string to binary data
const decodedImageData = Buffer.from(imageData, 'base64')
// workout the image hash
// i.e. 240e85ff2e4bb931f235985dd0134e459239496d2b5af6c5665168d38ef89b50
const hash = crypto
.createHash('sha256')
.update(imageData)
.digest('hex')
// 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>/<operator-dir>/customersphotos/24/0e/85
const dirname = path.join(directory, rpath)
// create the directory tree if needed
_.attempt(() => makeDir.sync(dirname))
// i.e. ../<lamassu-server-home>/<operator-dir>/customersphotos/24/0e/85/240e85ff2e4bb931f235985dd01....jpg
const filename = path.join(dirname, hash + '.jpg')
// update db record patch
// i.e. {
// "idCustomerTxPhoto": "24/0e/85/240e85ff2e4bb931f235985dd01....jpg",
// "idCustomerTxPhotoAt": "now()"
// }
newPatch.txCustomerPhotoPath = path.join(rpath, hash + '.jpg')
newPatch.txCustomerPhotoAt = 'now()'
// write image file
return writeFile(filename, decodedImageData)
.then(() => newPatch)
})
}
function updateFrontCamera (id, patch) {
return Promise.resolve(patch)
.then(patch => {
@ -704,4 +757,4 @@ function updateFrontCamera (id, patch) {
})
}
module.exports = { add, get, batch, getCustomersList, getCustomerById, getById, update, updateCustomer, updatePhotoCard, updateFrontCamera, updateIdCardData }
module.exports = { add, get, batch, getCustomersList, getCustomerById, getById, update, updateCustomer, updatePhotoCard, updateFrontCamera, updateIdCardData, updateTxCustomerPhoto }