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 16:07:44 +02:00
committed by GitHub
parent b7b0a7d3a4
commit 5ac7f08e4d
6 changed files with 289 additions and 69 deletions

View File

@@ -8,6 +8,17 @@ const orderSeeder = require("../../helpers/order-seeder")
const swapSeeder = require("../../helpers/swap-seeder")
const adminSeeder = require("../../helpers/admin-seeder")
const {
simpleProductFactory,
simpleCartFactory,
simpleDiscountFactory,
simpleRegionFactory,
simpleShippingOptionFactory,
} = require("../../factories")
const {
simpleCustomerFactory,
} = require("../../factories/simple-customer-factory")
jest.setTimeout(30000)
describe("/admin/swaps", () => {
@@ -144,4 +155,208 @@ describe("/admin/swaps", () => {
)
})
})
describe("Complete swap flow", () => {
beforeEach(async () => {
try {
await adminSeeder(dbConnection)
} catch (err) {
console.log(err)
throw err
}
})
afterEach(async () => {
const db = useDb()
await db.teardown()
})
it("completes swap and ensures difference due", async () => {
// ********* FACTORIES *********
const prodA = await simpleProductFactory(dbConnection, {
id: "prod-a",
variants: [
{ id: "prod-a-var", prices: [{ amount: 1000, currency: "dkk" }] },
],
})
await simpleProductFactory(dbConnection, {
id: "prod-b",
variants: [
{ id: "prod-b-var", prices: [{ amount: 1000, currency: "dkk" }] },
],
})
await simpleRegionFactory(dbConnection, {
id: "test-region",
currency_code: "dkk",
})
await simpleDiscountFactory(dbConnection, {
id: "test-discount",
regions: ["test-region"],
code: "TEST",
rule: {
type: "percentage",
value: "10",
allocation: "total",
conditions: [
{
type: "products",
operator: "in",
products: [prodA.id],
},
],
},
})
await simpleCustomerFactory(dbConnection, {
id: "test-customer",
email: "test@customer.com",
})
const so = await simpleShippingOptionFactory(dbConnection, {
region_id: "test-region",
})
await simpleCartFactory(dbConnection, {
customer: "test-customer",
id: "cart-test",
line_items: [
{
id: "line-item",
variant_id: "prod-a-var",
cart_id: "cart-test",
unit_price: 1000,
quantity: 1,
},
],
region: "test-region",
shipping_address: {
address_1: "test",
country_code: "us",
first_name: "chris",
last_name: "rock",
postal_code: "101",
},
})
const api = useApi()
// ********* PREPARE CART *********
try {
await api.post("/store/carts/cart-test", {
discounts: [{ code: "TEST" }],
})
} catch (error) {
console.log(error)
}
await api.post("/store/carts/cart-test/shipping-methods", {
option_id: so.id,
data: {},
})
await api.post("/store/carts/cart-test/payment-sessions")
const TEST = await api.post("/store/carts/cart-test/payment-session", {
provider_id: "test-pay",
})
console.log("Testing, ", TEST.data.cart.items[0])
// ********* COMPLETE CART *********
const completedOrder = await api.post("/store/carts/cart-test/complete")
// ********* PREPARE ORDER *********
const orderId = completedOrder.data.data.id
const fulfilledOrder = await api.post(
`/admin/orders/${orderId}/fulfillment`,
{
items: [{ item_id: "line-item", quantity: 1 }],
},
{
headers: {
Authorization: "Bearer test_token",
},
}
)
const fulfillmentId = fulfilledOrder.data.order.fulfillments[0].id
await api.post(
`/admin/orders/${orderId}/shipment`,
{
fulfillment_id: fulfillmentId,
},
{
headers: {
Authorization: "Bearer test_token",
},
}
)
await api.post(
`/admin/orders/${orderId}/capture`,
{},
{
headers: {
Authorization: "Bearer test_token",
},
}
)
// ********* CREATE SWAP *********
const createSwap = await api.post(
`/admin/orders/${completedOrder.data.data.id}/swaps`,
{
return_items: [
{
item_id: "line-item",
quantity: 1,
},
],
additional_items: [{ variant_id: "prod-b-var", quantity: 1 }],
},
{
headers: {
authorization: "Bearer test_token",
},
}
)
let swap = createSwap.data.order.swaps[0]
// ********* PREPARE SWAP CART *********
await api.post(`/store/carts/${swap.cart_id}/shipping-methods`, {
option_id: so.id,
data: {},
})
await api.post(`/store/carts/${swap.cart_id}/payment-sessions`)
await api.post(`/store/carts/${swap.cart_id}/payment-session`, {
provider_id: "test-pay",
})
// ********* COMPLETE SWAP CART *********
await api.post(`/store/carts/${swap.cart_id}/complete`)
swap = await api
.get(`/admin/swaps/${swap.id}`, {
headers: {
Authorization: "Bearer test_token",
},
})
.catch((err) => {
console.log(err)
})
const swapCart = await api.get(
`/store/carts/${swap.data.swap.cart_id}`,
{}
)
// ********* VALIDATE *********
expect(swap.data.swap.difference_due).toBe(swapCart.data.cart.total)
})
})
})