Skip to content

Commit

Permalink
fix(bling-callback): end process (clear proceed timer) when new tmp h…
Browse files Browse the repository at this point in the history
…as more orders only [#57]

start doc subscription after document set
  • Loading branch information
leomp12 committed Aug 28, 2021
1 parent 970e49e commit 22a2835
Showing 1 changed file with 26 additions and 21 deletions.
47 changes: 26 additions & 21 deletions functions/routes/bling/callback.js
Original file line number Diff line number Diff line change
Expand Up @@ -156,29 +156,34 @@ exports.post = ({ appSdk, admin }, req, res) => {
}
})
}
documentRef.set({ orderNumbers })

return new Promise((resolve, reject) => {
const unsubscribe = documentRef.onSnapshot(documentSnapshot => {
if (documentSnapshot && documentSnapshot.exists) {
clearTimeout(proceedTimer)
resolve(null)
}
}, err => {
console.log(`Snapshot watcher error: ${err}`)
})

const proceedTimer = setTimeout(() => {
unsubscribe()
documentRef.delete().catch(console.error)
console.log(`> #${storeId} order numbers: ${JSON.stringify(orderNumbers)}`)
updateAppData({ appSdk, storeId }, {
___importation: {
...appData.___importation,
order_numbers: orderNumbers
return documentRef.set({ orderNumbers }).then(() => {
return new Promise((resolve, reject) => {
const unsubscribe = documentRef.onSnapshot(newDocumentSnapshot => {
if (
newDocumentSnapshot &&
newDocumentSnapshot.exists &&
newDocumentSnapshot.get('orderNumbers').length > orderNumbers.length
) {
clearTimeout(proceedTimer)
resolve(null)
}
}).then(resolve).catch(reject)
}, 1500)
}, err => {
console.log(`Snapshot watcher error: ${err}`)
})

const proceedTimer = setTimeout(() => {
unsubscribe()
documentRef.delete().catch(console.error)
console.log(`> #${storeId} order numbers: ${JSON.stringify(orderNumbers)}`)
updateAppData({ appSdk, storeId }, {
___importation: {
...appData.___importation,
order_numbers: orderNumbers
}
}).then(resolve).catch(reject)
}, 1500)
})
})
})
}
Expand Down

0 comments on commit 22a2835

Please sign in to comment.