diff --git a/functions/lib/integration/handle-images/import-images.js b/functions/lib/integration/handle-images/import-images.js index 6ce2d8c..50063ce 100644 --- a/functions/lib/integration/handle-images/import-images.js +++ b/functions/lib/integration/handle-images/import-images.js @@ -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() @@ -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 diff --git a/functions/lib/integration/handle-images/utils.js b/functions/lib/integration/handle-images/utils.js index 690c01a..81107dc 100644 --- a/functions/lib/integration/handle-images/utils.js +++ b/functions/lib/integration/handle-images/utils.js @@ -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 => { @@ -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 + } + }) }) })