Files
medusa-store/integration-tests/api/factories/simple-shipping-option-factory.ts
T
Oliver Windall Juhl 5ac7f08e4d 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
2022-08-24 14:07:44 +00:00

52 lines
1.4 KiB
TypeScript

import {
ShippingOption,
ShippingOptionPriceType,
ShippingProfile,
ShippingProfileType,
} from "@medusajs/medusa"
import faker from "faker"
import { Connection } from "typeorm"
export type ShippingOptionFactoryData = {
name?: string
region_id: string
is_return?: boolean
is_giftcard?: boolean
price?: number
price_type?: ShippingOptionPriceType
data?: object
}
export const simpleShippingOptionFactory = async (
connection: Connection,
data: ShippingOptionFactoryData,
seed?: number
): Promise<ShippingOption> => {
if (typeof seed !== "undefined") {
faker.seed(seed)
}
const manager = connection.manager
const defaultProfile = await manager.findOne(ShippingProfile, {
type: ShippingProfileType.DEFAULT,
})
const gcProfile = await manager.findOne(ShippingProfile, {
type: ShippingProfileType.GIFT_CARD,
})
const created = manager.create(ShippingOption, {
id: `simple-so-${Math.random() * 1000}`,
name: data.name || "Test Method",
is_return: data.is_return ?? false,
region_id: data.region_id,
provider_id: "test-ful",
profile_id: data.is_giftcard ? gcProfile.id : defaultProfile.id,
price_type: data.price_type ?? ShippingOptionPriceType.FLAT_RATE,
data: data.data ?? {},
amount: typeof data.price !== "undefined" ? data.price : 500,
})
const option = await manager.save(created)
return option
}