fix(medusa): Incorrect swap difference due (#2086)

### What
Creating a swap on an order with a discount leads to an incorrect difference due. 

**Scenario**
- Create a store with minimum 2 products (Prod A, Prod B)
- Create a discount that only works for Prod A
- Create an order for Prod A with the discount applied
- Create a swap between Prod A and Prod B

**Expected outcome**
We would expect the difference_due amount to come out to the sum of:
- -1 * (price of prod a - discount applied to prod a) 
- price of prod b

**Actual outcome**
Instead the discount is applied across both products when calculating difference due. This results in a total that is instead the sum of:
- -1 * (price of prod a - discount applied to prod a)
- price of prod b - discount on prod b ignoring the condition

### How
Adds `line_item.adjustments` to relations in cart retrieval prior to setting the difference_due to car total

Fixes CORE-361
This commit is contained in:
Oliver Windall Juhl
2022-08-24 14:07:44 +00:00
committed by GitHub
parent b7b0a7d3a4
commit 5ac7f08e4d
6 changed files with 289 additions and 69 deletions
+29 -15
View File
@@ -184,8 +184,11 @@ class SwapService extends BaseService {
const validatedId = this.validateId_(id)
const { cartSelects, cartRelations, ...newConfig } =
this.transformQueryForCart_(config)
const {
cartSelects,
cartRelations,
...newConfig
} = this.transformQueryForCart_(config)
const query = this.buildQuery_({ id: validatedId }, newConfig)
@@ -600,8 +603,9 @@ class SwapService extends BaseService {
},
})
const customShippingOptionServiceTx =
this.customShippingOptionService_.withTransaction(manager)
const customShippingOptionServiceTx = this.customShippingOptionService_.withTransaction(
manager
)
for (const customShippingOption of customShippingOptions) {
await customShippingOptionServiceTx.create({
cart_id: cart.id,
@@ -611,8 +615,9 @@ class SwapService extends BaseService {
}
const lineItemServiceTx = this.lineItemService_.withTransaction(manager)
const lineItemAdjustmentServiceTx =
this.lineItemAdjustmentService_.withTransaction(manager)
const lineItemAdjustmentServiceTx = this.lineItemAdjustmentService_.withTransaction(
manager
)
for (const item of swap.additional_items) {
await lineItemServiceTx.update(item.id, {
cart_id: cart.id,
@@ -691,7 +696,12 @@ class SwapService extends BaseService {
const cart = await this.cartService_.retrieve(swap.cart_id, {
select: ["total"],
relations: ["payment", "shipping_methods", "items"],
relations: [
"payment",
"shipping_methods",
"items",
"items.adjustments",
],
})
const { payment } = cart
@@ -699,10 +709,12 @@ class SwapService extends BaseService {
const items = cart.items
if (!swap.allow_backorder) {
const inventoryServiceTx =
this.inventoryService_.withTransaction(manager)
const paymentProviderServiceTx =
this.paymentProviderService_.withTransaction(manager)
const inventoryServiceTx = this.inventoryService_.withTransaction(
manager
)
const paymentProviderServiceTx = this.paymentProviderService_.withTransaction(
manager
)
const cartServiceTx = this.cartService_.withTransaction(manager)
for (const item of items) {
@@ -750,8 +762,9 @@ class SwapService extends BaseService {
order_id: swap.order_id,
})
const inventoryServiceTx =
this.inventoryService_.withTransaction(manager)
const inventoryServiceTx = this.inventoryService_.withTransaction(
manager
)
for (const item of items) {
await inventoryServiceTx.adjustInventory(
@@ -771,8 +784,9 @@ class SwapService extends BaseService {
const swapRepo = manager.getCustomRepository(this.swapRepository_)
const result = await swapRepo.save(swap)
const shippingOptionServiceTx =
this.shippingOptionService_.withTransaction(manager)
const shippingOptionServiceTx = this.shippingOptionService_.withTransaction(
manager
)
for (const method of cart.shipping_methods) {
await shippingOptionServiceTx.updateShippingMethod(method.id, {