Skip to content

Commit

Permalink
fix(import-images): fix import images
Browse files Browse the repository at this point in the history
  • Loading branch information
wisley7l committed Sep 11, 2024
1 parent 3ce1a94 commit 8a08a5f
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 13 deletions.
15 changes: 14 additions & 1 deletion functions/lib/integration/handle-images/import-images.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ module.exports = async (change, context) => {
processingAt,
isNew
} = data
let attempts = data.attempts || 0

if (storeId > 100) {
logger.info(`Event: StoreId ${storeId} productId: ${productId} ${isNew}`)
const now = Timestamp.now()
Expand All @@ -41,7 +43,18 @@ module.exports = async (change, context) => {
return saveImagesProduct({ appSdk, storeId, auth }, product, anexos)
.then(async () => doc.ref.delete())
.then(() => logger.info(`>Finish[${docId}]`))
.catch(logger.error)
.catch(err => {
logger.error(err)
attempts += 1
return Promise((resolve, reject) => {
// eslint-disable-next-line promise/no-nesting
const updateDoc = () => doc.ref.set({ attempts }, { merge: true })
.then(() => resolve(true))
.catch(reject)

setTimeout(updateDoc(), 60 * 1000)
})
})
}
}
return null
Expand Down
21 changes: 9 additions & 12 deletions functions/lib/integration/handle-images/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ const axios = require('axios')
const { logger } = require('../../../context')
const admin = require('firebase-admin')
const { setup } = require('@ecomplus/application-sdk')
const FormData = require('form-data')

const getAppSdk = () => {
return new Promise(resolve => {
Expand Down Expand Up @@ -64,18 +65,14 @@ const tryImageUpload = (storeId, auth, originImgUrl, product, index, isRetry) =>
})

.catch(err => {
if (err.name !== 'Unexpected Storage API response' && !isRetry) {
setTimeout(tryImageUpload(storeId, auth, originImgUrl, product, index, true), 700)
} else {
logger.error(err)
resolve({
_id: ecomUtils.randomObjectId(),
normal: {
url: originImgUrl,
alt: product.name
}
})
}
logger.error(err)
resolve({
_id: ecomUtils.randomObjectId(),
normal: {
url: originImgUrl,
alt: product.name
}
})
})
})

Expand Down

0 comments on commit 8a08a5f

Please sign in to comment.