fix: apollo server image upload

This commit is contained in:
José Oliveira 2021-11-03 20:56:26 +00:00
parent 9baa16c824
commit 3e4f4a4962
6 changed files with 103 additions and 10 deletions

View file

@ -216,7 +216,7 @@ function enhanceEditedFields (fields, userToken) {
*/
function deleteEditedData (id, data) {
// NOT IMPLEMENTING THIS FEATURE FOR NOW
// NOT IMPLEMENTING THIS FEATURE FOR THE CURRENT VERSION
// const defaults = [
// 'front_camera',
// 'id_card_data',
@ -235,6 +235,53 @@ function deleteEditedData (id, data) {
return getCustomerById(id)
}
/**
* Replace customer's compliance photos
*
* @name save
* @function
*
* @param {string} id Customer's id
* @param {File} photo New photo data
* @param {string} photoType Photo's compliance type
*
* @returns {object} path New photo path
*
*/
function replacePhoto (id, photo, photoType) {
const baseDir = photoType === 'frontCamera' ? frontCameraBaseDir : idPhotoCardBasedir
const { createReadStream } = photo
const stream = createReadStream()
// 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>/idphotocard/24/0e/85
const dirname = path.join(idPhotoCardBasedir, rpath)
// create the directory tree if needed
_.attempt(() => makeDir.sync(dirname))
// i.e. ../<lamassu-server-home>/idphotocard/24/0e/85/240e85ff2e4bb931f235985dd01....jpg
const filename = path.join(dirname, hash + '.jpg')
// update db record patch
// i.e. {
// "idCardPhotoPath": "24/0e/85/240e85ff2e4bb931f235985dd01....jpg",
// "idCardPhotoAt": "now()"
// }
newPatch.idCardPhotoPath = path.join(rpath, hash + '.jpg')
newPatch.idCardPhotoAt = 'now()'
}
const invalidateCustomerNotifications = (id, data) => {
if (data.authorized_override !== 'verified') return Promise.resolve()