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) { function deleteEditedData (id, data) {
// NOT IMPLEMENTING THIS FEATURE FOR NOW // NOT IMPLEMENTING THIS FEATURE FOR THE CURRENT VERSION
// const defaults = [ // const defaults = [
// 'front_camera', // 'front_camera',
// 'id_card_data', // 'id_card_data',
@ -235,6 +235,53 @@ function deleteEditedData (id, data) {
return getCustomerById(id) 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) => { const invalidateCustomerNotifications = (id, data) => {
if (data.authorized_override !== 'verified') return Promise.resolve() if (data.authorized_override !== 'verified') return Promise.resolve()

View file

@ -3,7 +3,6 @@ const customers = require('../../../customers')
const filters = require('../../filters') const filters = require('../../filters')
const resolvers = { const resolvers = {
// Upload: GraphQLUpload,
Customer: { Customer: {
isAnonymous: parent => (parent.customerId === anonymous.uuid) isAnonymous: parent => (parent.customerId === anonymous.uuid)
@ -22,9 +21,15 @@ const resolvers = {
addCustomField: (...[, { customerId, label, value }]) => customers.addCustomField(customerId, label, value), addCustomField: (...[, { customerId, label, value }]) => customers.addCustomField(customerId, label, value),
saveCustomField: (...[, { customerId, fieldId, newValue }]) => customers.saveCustomField(customerId, fieldId, newValue), saveCustomField: (...[, { customerId, fieldId, newValue }]) => customers.saveCustomField(customerId, fieldId, newValue),
removeCustomField: (...[, [ { customerId, fieldId } ]]) => customers.removeCustomField(customerId, fieldId), removeCustomField: (...[, [ { customerId, fieldId } ]]) => customers.removeCustomField(customerId, fieldId),
editCustomer: (root, { customerId, customerEdit }, context) => { editCustomer: async (root, { customerId, customerEdit }, context) => {
const token = !!context.req.cookies.lid && context.req.session.user.id const token = !!context.req.cookies.lid && context.req.session.user.id
return customers.edit(customerId, customerEdit, token) const editedData = await customerEdit
return customers.edit(customerId, editedData, token)
},
replacePhoto: async (root, { customerId, photoType, newPhoto }, context) => {
const photo = await newPhoto
return customers.replacePhoto(customerId, photoType, photo)
.then(() => customers.getCustomerById(customerId))
}, },
deleteEditedData: (root, { customerId, customerEdit }) => { deleteEditedData: (root, { customerId, customerEdit }) => {
return customers.deleteEditedData(customerId, customerEdit) return customers.deleteEditedData(customerId, customerEdit)

View file

@ -12,7 +12,8 @@ const typeDef = gql`
authorizedOverride: String authorizedOverride: String
daysSuspended: Int daysSuspended: Int
isSuspended: Boolean isSuspended: Boolean
frontCamera: Upload newPhoto: Upload
photoType: String
frontCameraPath: String frontCameraPath: String
frontCameraAt: Date frontCameraAt: Date
frontCameraOverride: String frontCameraOverride: String
@ -68,7 +69,6 @@ const typeDef = gql`
} }
input CustomerEdit { input CustomerEdit {
frontCamera: Upload
idCardData: JSONObject idCardData: JSONObject
idCardPhoto: Upload idCardPhoto: Upload
usSsn: String usSsn: String
@ -87,6 +87,7 @@ const typeDef = gql`
removeCustomField(customerId: ID!, fieldId: ID!): CustomerCustomField @auth removeCustomField(customerId: ID!, fieldId: ID!): CustomerCustomField @auth
editCustomer(customerId: ID!, customerEdit: CustomerEdit): Customer @auth editCustomer(customerId: ID!, customerEdit: CustomerEdit): Customer @auth
deleteEditedData(customerId: ID!, customerEdit: CustomerEdit): Customer @auth deleteEditedData(customerId: ID!, customerEdit: CustomerEdit): Customer @auth
replacePhoto(customerId: ID!, photoType: String, newPhoto: Upload): Customer @auth
} }
` `

View file

@ -60,6 +60,7 @@ const Photo = ({ show, src }) => {
const CustomerData = ({ const CustomerData = ({
customer, customer,
updateCustomer, updateCustomer,
replacePhoto,
editCustomer, editCustomer,
deleteEditedData deleteEditedData
}) => { }) => {
@ -230,7 +231,11 @@ const CustomerData = ({
authorize: () => authorize: () =>
updateCustomer({ frontCameraOverride: OVERRIDE_AUTHORIZED }), updateCustomer({ frontCameraOverride: OVERRIDE_AUTHORIZED }),
reject: () => updateCustomer({ frontCameraOverride: OVERRIDE_REJECTED }), reject: () => updateCustomer({ frontCameraOverride: OVERRIDE_REJECTED }),
save: values => editCustomer({ frontCamera: values.frontCamera }), save: values =>
replacePhoto({
newPhoto: values.frontCamera,
photoType: 'frontCamera'
}),
deleteEditedData: () => deleteEditedData({ frontCamera: null }), deleteEditedData: () => deleteEditedData({ frontCamera: null }),
children: customer.frontCameraPath ? ( children: customer.frontCameraPath ? (
<Photo <Photo
@ -252,7 +257,11 @@ const CustomerData = ({
authorize: () => authorize: () =>
updateCustomer({ idCardPhotoOverride: OVERRIDE_AUTHORIZED }), updateCustomer({ idCardPhotoOverride: OVERRIDE_AUTHORIZED }),
reject: () => updateCustomer({ idCardPhotoOverride: OVERRIDE_REJECTED }), reject: () => updateCustomer({ idCardPhotoOverride: OVERRIDE_REJECTED }),
save: values => editCustomer({ idCardPhoto: values.idCardPhoto }), save: values =>
replacePhoto({
newPhoto: values.idCardPhoto,
photoType: 'idCardPhoto'
}),
deleteEditedData: () => deleteEditedData({ idCardPhoto: null }), deleteEditedData: () => deleteEditedData({ idCardPhoto: null }),
children: customer.idCardPhotoPath ? ( children: customer.idCardPhotoPath ? (
<Photo <Photo

View file

@ -120,14 +120,30 @@ const EDIT_CUSTOMER = gql`
mutation editCustomer($customerId: ID!, $customerEdit: CustomerEdit) { mutation editCustomer($customerId: ID!, $customerEdit: CustomerEdit) {
editCustomer(customerId: $customerId, customerEdit: $customerEdit) { editCustomer(customerId: $customerId, customerEdit: $customerEdit) {
id id
frontCamera
idCardData idCardData
idCardPhoto
usSsn usSsn
} }
} }
` `
const REPLACE_CUSTOMER_PHOTO = gql`
mutation replacePhoto(
$customerId: ID!
$photoType: String
$newPhoto: Upload
) {
replacePhoto(
customerId: $customerId
photoType: $photoType
newPhoto: $newPhoto
) {
id
newPhoto
photoType
}
}
`
const DELETE_EDITED_CUSTOMER = gql` const DELETE_EDITED_CUSTOMER = gql`
mutation deleteEditedData($customerId: ID!, $customerEdit: CustomerEdit) { mutation deleteEditedData($customerId: ID!, $customerEdit: CustomerEdit) {
deleteEditedData(customerId: $customerId, customerEdit: $customerEdit) { deleteEditedData(customerId: $customerId, customerEdit: $customerEdit) {
@ -156,6 +172,10 @@ const CustomerProfile = memo(() => {
} }
) )
const [replaceCustomerPhoto] = useMutation(REPLACE_CUSTOMER_PHOTO, {
onCompleted: () => getCustomer()
})
const [editCustomerData] = useMutation(EDIT_CUSTOMER, { const [editCustomerData] = useMutation(EDIT_CUSTOMER, {
onCompleted: () => getCustomer() onCompleted: () => getCustomer()
}) })
@ -176,6 +196,15 @@ const CustomerProfile = memo(() => {
} }
}) })
const replacePhoto = it =>
replaceCustomerPhoto({
variables: {
customerId,
newPhoto: it.newPhoto,
photoType: it.photoType
}
})
const editCustomer = it => const editCustomer = it =>
editCustomerData({ editCustomerData({
variables: { variables: {
@ -343,6 +372,7 @@ const CustomerProfile = memo(() => {
<CustomerData <CustomerData
customer={customerData} customer={customerData}
updateCustomer={updateCustomer} updateCustomer={updateCustomer}
replacePhoto={replacePhoto}
editCustomer={editCustomer} editCustomer={editCustomer}
deleteEditedData={deleteEditedData}></CustomerData> deleteEditedData={deleteEditedData}></CustomerData>
</div> </div>

View file

@ -243,6 +243,7 @@ const EditableCard = ({
<input <input
type="file" type="file"
alt="" alt=""
accept="image/*"
className={classes.input} className={classes.input}
ref={fileInput => setInput(fileInput)} ref={fileInput => setInput(fileInput)}
onChange={event => { onChange={event => {