Skip to content

Commit

Permalink
fix(cron-events): check hour to update price
Browse files Browse the repository at this point in the history
  • Loading branch information
wisley7l committed Aug 13, 2024
1 parent ad83307 commit 92d4f6f
Showing 1 changed file with 61 additions and 53 deletions.
114 changes: 61 additions & 53 deletions functions/lib/cron-events-horus.js
Original file line number Diff line number Diff line change
Expand Up @@ -188,78 +188,86 @@ const productsPriceEvents = async (horus, storeId, opts) => {

// console.log('>> ', resource, ' => ', field)
const docSnapshot = await docRef.get()
let isExec = true
if (docSnapshot.exists) {
const data = docSnapshot.data()
const dateInitDoc = data?.dateInit
const dateEndtDoc = data?.dateEnd
const offsetDoc = data?.offset
const hasRepeatDoc = data?.hasRepeat
const updatedAtDoc = data?.updated_at && new Date(data?.updated_at)
const now = new Date()

isExec = Boolean(hasRepeatDoc || (now.getTime() >= (updatedAtDoc.getTime() + (5 * 60 * 1000))))
if (hasRepeatDoc) {
dateInit = dateInitDoc ? new Date(dateInitDoc) : dateInit
dateEnd = dateEndtDoc ? new Date(dateEndtDoc) : dateEnd
offset = offsetDoc || 0
} else {
dateInit = dateEndtDoc ? new Date(dateEndtDoc) : dateEnd
}
}
const codCaract = opts?.appData?.code_characteristic || 5
const codTpoCaract = opts?.appData?.code_type_characteristic || 3

console.log(`>> Check PRICE ${parseDate(dateInit, true)} at ${parseDate(dateEnd, true)}`)
const query = `?DATA_INI=${parseDate(dateInit, true)}&DATA_FIM=${parseDate(dateEnd, true)}` +
`&COD_TPO_CARACT=${codTpoCaract}&COD_CARACT=${codCaract}`

let hasRepeat = true
console.log(`isExec: ${isExec} upDoc: ${updatedAtDoc.toISOString()} now ${now.toISOString()}`)
}

let total = 0
// const init = Date.now()
const promisesSendTopics = []
// create Object Horus to request api Horus
const endpoint = `/Busca_preco_item${query}&offset=${offset}&limit=${limit}`
const products = await requestHorus(horus, endpoint, 'get', true)
.catch((_err) => {
// if (_err.response) {
// console.warn(JSON.stringify(_err.response))
// } else {
// console.error(_err)
// }
return null
})
if (isExec) {
const codCaract = opts?.appData?.code_characteristic || 5
const codTpoCaract = opts?.appData?.code_type_characteristic || 3

console.log(`>> Check PRICE ${parseDate(dateInit, true)} at ${parseDate(dateEnd, true)}`)
const query = `?DATA_INI=${parseDate(dateInit, true)}&DATA_FIM=${parseDate(dateEnd, true)}` +
`&COD_TPO_CARACT=${codTpoCaract}&COD_CARACT=${codCaract}`

let hasRepeat = true

let total = 0
const promisesSendTopics = []
// create Object Horus to request api Horus
const endpoint = `/Busca_preco_item${query}&offset=${offset}&limit=${limit}`
const products = await requestHorus(horus, endpoint, 'get', true)
.catch((_err) => {
// if (_err.response) {
// console.warn(JSON.stringify(_err.response))
// } else {
// console.error(_err)
// }
return null
})

hasRepeat = products?.length === limit
hasRepeat = products?.length === limit

if (products?.length) {
total += products.length
products.forEach((productHorus, index) => {
promisesSendTopics.push(
sendMessageTopic(
topicResourceToEcom,
{
storeId,
resource: resourcePrefix,
objectHorus: productHorus,
opts
})
)
})
}

if (products?.length) {
total += products.length
products.forEach((productHorus, index) => {
promisesSendTopics.push(
sendMessageTopic(
topicResourceToEcom,
{
storeId,
resource: resourcePrefix,
objectHorus: productHorus,
opts
})
)
})
offset = hasRepeat ? offset + limit : 0
console.log(`>>Cron PRICE #${storeId} Updates: ${total} Repeat ${hasRepeat}`)

return Promise.all(promisesSendTopics)
.then(async () => {
await docRef.set({
dateInit: dateInit.toISOString(),
dateEnd: dateEnd.toISOString(),
offset,
hasRepeat,
updated_at: new Date().toISOString()
}, { merge: true })
.catch(console.error)
console.log(`Finish Exec PRICE in #${storeId}`)
})
}

offset = hasRepeat ? offset + limit : 0
console.log(`>>Cron PRICE #${storeId} Updates: ${total} Repeat ${hasRepeat}`)

return Promise.all(promisesSendTopics)
.then(async () => {
await docRef.set({
dateInit: dateInit.toISOString(),
dateEnd: dateEnd.toISOString(),
offset,
hasRepeat,
updated_at: new Date().toISOString()
}, { merge: true })
.catch(console.error)
console.log(`Finish Exec PRICE in #${storeId}`)
})
}

module.exports = async (appSdk) => {
Expand Down

0 comments on commit 92d4f6f

Please sign in to comment.