Fix(medusa): Swaps in tax inclusive regions (#2557)

**What**
- Ensure that swaps can be created for orders with discounts in tax inclusive regions

**How**
- Retrieve cart with discounts and region before creating adjustments for line items in cart. 
  - In `discountService.calculateDiscountForLineItem` we used the method `totalsService.getLineItemTotals` if a line-item is tax inclusive. This method uses some fields from the cart that aren't populated on cart creation (such a `items` which caused the original error).

**Testing**
- add integration test `swaps > tax inclusive > "Complete swap flow with discount"` that creates a swap in the following environment
  - tax inclusive region 
  - tax inclusive line item to be swapped
  - fixed type discount with allocation: total

Fixes CORE-748
This commit is contained in:
Philip Korsholm authored and GitHub committed 2022-11-07 10:47:26 +00:00
1 parent 434b11af46
commit 15d6f92964
4 files changed
+552 -302

No files matched your search

@@ -204,7 +204,7 @@ describe("SwapService", () => {
refund_amount: 11,
items: [{ item_id: IdMap.getId("line"), quantity: 1 }],
},
additional_items: [{ data: "lines", id: "test" }],
additional_items: [{ id: "test" }],
other: "data",
}
@@ -316,10 +316,11 @@ describe("SwapService", () => {
expect(
LineItemAdjustmentServiceMock.createAdjustmentForLineItem
).toHaveBeenCalledWith(
{ id: "cart" },
{ id: "cart", items: [{
id: "test-item",
}] },
{
id: "test",
data: "lines",
id: "test-item",
}
)
+26 -11
View File
@@ -1,5 +1,5 @@
import { MedusaError } from "medusa-core-utils"
import { EntityManager } from "typeorm"
import { EntityManager, In } from "typeorm"
import { buildQuery, isDefined, setMetadata, validateId } from "../utils"
import { TransactionBaseService } from "../interfaces"
@@ -600,7 +600,7 @@ class SwapService extends TransactionBaseService {
order?.discounts?.filter(({ rule }) => rule.type !== "free_shipping") ||
undefined
const cart = await this.cartService_.withTransaction(manager).create({
let cart = await this.cartService_.withTransaction(manager).create({
discounts,
email: order.email,
billing_address_id: order.billing_address_id,
@@ -627,16 +627,31 @@ class SwapService extends TransactionBaseService {
const lineItemServiceTx = this.lineItemService_.withTransaction(manager)
const lineItemAdjustmentServiceTx =
this.lineItemAdjustmentService_.withTransaction(manager)
for (const item of swap.additional_items) {
await lineItemServiceTx.update(item.id, {
cart_id: cart.id,
})
// we generate adjustments in case the cart has any discounts that should be applied to the additional items
await lineItemAdjustmentServiceTx.createAdjustmentForLineItem(
cart,
item
await Promise.all(
swap.additional_items.map(
async (item) =>
await lineItemServiceTx.update(item.id, {
cart_id: cart.id,
})
)
}
)
cart = await this.cartService_
.withTransaction(manager)
.retrieve(cart.id, {
relations: ["items", "region", "discounts", "discounts.rule"],
})
await Promise.all(
cart.items.map(async (item) => {
// we generate adjustments in case the cart has any discounts that should be applied to the additional items
await lineItemAdjustmentServiceTx.createAdjustmentForLineItem(
cart,
item
)
})
)
// If the swap has a return shipping method the price has to be added to
// the cart.