Skip to content

Commit

Permalink
Fix race condition between order payment and PDF creation
Browse files Browse the repository at this point in the history
  • Loading branch information
bkimminich committed Dec 21, 2020
1 parent 76ed097 commit d5f67b6
Showing 1 changed file with 8 additions and 7 deletions.
15 changes: 8 additions & 7 deletions routes/order.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,12 @@ module.exports = function placeOrder () {
const date = new Date().toJSON().slice(0, 10)
const fileWriter = doc.pipe(fs.createWriteStream(path.join(__dirname, '../ftp/', pdfFile)))

fileWriter.on('finish', () => {
basket.update({ coupon: null })
models.BasketItem.destroy({ where: { BasketId: id } })
res.json({ orderConfirmation: orderId })
})

doc.font('Times-Roman', 40).text(config.get('application.name'), { align: 'center' })
doc.moveTo(70, 115).lineTo(540, 115).stroke()
doc.moveTo(70, 120).lineTo(540, 120).stroke()
Expand Down Expand Up @@ -104,7 +110,6 @@ module.exports = function placeOrder () {
doc.moveDown()
doc.moveDown()
doc.font('Times-Roman', 15).text(req.__('Thank you for your order!'))
doc.end()

utils.solveIf(challenges.negativeOrderChallenge, () => { return totalPrice < 0 })

Expand Down Expand Up @@ -136,12 +141,8 @@ module.exports = function placeOrder () {
bonus: totalPoints,
deliveryPrice: deliveryAmount,
eta: deliveryMethod.eta.toString()
})

fileWriter.on('finish', () => {
basket.update({ coupon: null })
models.BasketItem.destroy({ where: { BasketId: id } })
res.json({ orderConfirmation: orderId })
}).then(() => {
doc.end()
})
} else {
next(new Error(`Basket with id=${id} does not exist.`))
Expand Down

0 comments on commit d5f67b6

Please sign in to comment.