fix(medusa): Missing withTransaction on calculateDiscountForLineItem (#3247)

This commit is contained in:
Adrien de Peretti
2023-02-13 16:29:11 +01:00
committed by GitHub
parent 4d2dc3c3a1
commit a2cc084db8
3 changed files with 17 additions and 7 deletions

View File

@@ -0,0 +1,5 @@
---
"@medusajs/medusa": patch
---
fix(medusa): Generate adjustment missing transaction on calculateDiscountForLineItem

View File

@@ -53,16 +53,18 @@ export default async (req, res) => {
const cartService: CartService = req.scope.resolve("cartService")
await manager.transaction(async (m) => {
const cartServiceTx = cartService.withTransaction(m)
// Remove the line item
await cartService.withTransaction(m).removeLineItem(id, line_id)
await cartServiceTx.removeLineItem(id, line_id)
// If the cart has payment sessions update these
const updated = await cartService.withTransaction(m).retrieve(id, {
const updated = await cartServiceTx.retrieve(id, {
relations: ["payment_sessions"],
})
if (updated.payment_sessions?.length) {
await cartService.withTransaction(m).setPaymentSessions(id)
await cartServiceTx.setPaymentSessions(id)
}
})

View File

@@ -217,11 +217,14 @@ class LineItemAdjustmentService extends TransactionBaseService {
return []
}
const discountServiceTx = this.discountService.withTransaction(manager)
const lineItemProduct = context.variant.product_id
const isValid = await this.discountService
.withTransaction(manager)
.validateDiscountForProduct(discount.rule_id, lineItemProduct)
const isValid = await discountServiceTx.validateDiscountForProduct(
discount.rule_id,
lineItemProduct
)
// if discount is not valid for line item, then do nothing
if (!isValid) {
@@ -230,7 +233,7 @@ class LineItemAdjustmentService extends TransactionBaseService {
// In case of a generated line item the id is not available, it is mocked instead to be used for totals calculations
lineItem.id = lineItem.id ?? new Date().getTime()
const amount = await this.discountService.calculateDiscountForLineItem(
const amount = await discountServiceTx.calculateDiscountForLineItem(
discount.id,
lineItem,
calculationContextData