Skip to content

Commit

Permalink
chore(import-images): update handle import images
Browse files Browse the repository at this point in the history
  • Loading branch information
wisley7l committed Sep 11, 2024
1 parent 0d86deb commit 6a9f249
Show file tree
Hide file tree
Showing 2 changed files with 80 additions and 22 deletions.
5 changes: 3 additions & 2 deletions functions/lib/integration/handle-images/import-images.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// const getAppData = require('../store-api/get-app-data')
const { logger } = require('../../../context')
const { Timestamp } = require('firebase-admin/firestore')
const { getAppSdk } = require('./utils')
const { getAppSdk, saveImagesProduct } = require('./utils')

const getProductById = (appSdk, storeId, auth, productId) => appSdk
.apiRequest(storeId, `/products/${productId}.json`, 'GET', null, auth)
Expand Down Expand Up @@ -43,7 +43,8 @@ module.exports = async (change, context) => {
])
// const appData = promises[0]
const product = promises[0]
logger.info(`${JSON.stringify(product)} anexos: ${JSON.stringify(anexos)}`)
// logger.info(`${JSON.stringify(product)} anexos: ${JSON.stringify(anexos)}`)
saveImagesProduct({ appSdk, storeId, auth }, product, anexos)
}

// logger.info(`${storeId} ${productId} ${JSON.stringify(anexos)}`)
Expand Down
97 changes: 77 additions & 20 deletions functions/lib/integration/handle-images/utils.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
const ecomUtils = require('@ecomplus/utils')
const axios = require('axios')
// const { logger } = require('../../../context')
const { logger } = require('../../../context')
const admin = require('firebase-admin')
const { setup } = require('@ecomplus/application-sdk')

Expand All @@ -11,7 +11,7 @@ const getAppSdk = () => {
})
}

const tryImageUpload = (storeId, auth, originImgUrl, product, index) => new Promise(resolve => {
const tryImageUpload = (storeId, auth, originImgUrl, product, isRetry) => new Promise(resolve => {
axios.get(originImgUrl, {
responseType: 'arraybuffer'
})
Expand Down Expand Up @@ -56,27 +56,84 @@ const tryImageUpload = (storeId, auth, originImgUrl, product, index) => new Prom
})

.catch(err => {
console.error(err)
resolve({
_id: ecomUtils.randomObjectId(),
normal: {
url: originImgUrl,
alt: product.name
}
})
if (err.name !== 'Unexpected Storage API response' && !isRetry) {
setTimeout(tryImageUpload(storeId, auth, originImgUrl, product, true), 700)
} else {
console.error(err)
resolve({
_id: ecomUtils.randomObjectId(),
normal: {
url: originImgUrl,
alt: product.name
}
})
}
})
}).then(picture => {
if (product && product.pictures) {
if (index === 0 || index) {
product.pictures[index] = picture
} else {
product.pictures.push(picture)
}
}
return picture
})

// .then(picture => {
// if (product && product.pictures) {
// if (index === 0 || index) {
// product.pictures[index] = picture
// } else {
// product.pictures.push(picture)
// }
// }
// return picture
// })

const saveImagesProduct = async ({ appSdk, storeId, auth }, product, anexos) => {
if (!product.pictures) {
product.pictures = []
}
// const promises = []
anexos.forEach((anexo, i) => {
let url
if (anexo && anexo.anexo) {
url = anexo.anexo
} else if (anexo.url) {
url = anexo.url
}

if (typeof url === 'string' && url.startsWith('http')) {
let isImgExists = false
let index = i
if (product.pictures.length) {
const pathImg = url.split('/')
const nameImg = pathImg[pathImg.length - 1]
const oldPictureIndex = product.pictures.findIndex(({ normal }) => normal.url.includes(nameImg))
if (oldPictureIndex > -1) {
index = oldPictureIndex
const oldPicture = product.pictures[oldPictureIndex]
isImgExists = oldPicture.normal.url !== url
}
}

logger.info(`${product._id} ${JSON.stringify(product.picture)} exists: ${isImgExists} index: ${index} (${i}) ${url}`)
// const pictureExis
// if (!isImgExists) {
// }
// promises.push(tryImageUpload(storeId, auth, url, product, i))
}
})
// return Promise.all(promises).then((images) => {
// if (Array.isArray(product.variations) && product.variations.length) {
// product.variations.forEach(variation => {
// if (variation.picture_id || variation.picture_id === 0) {
// const variationImage = images[variation.picture_id]
// if (variationImage._id) {
// variation.picture_id = variationImage._id
// } else {
// delete variation.picture_id
// }
// }
// })
// }
// return resolve(product)
// })
}

module.exports = {
tryImageUpload,
saveImagesProduct,
getAppSdk
}

0 comments on commit 6a9f249

Please sign in to comment.