feat(medusa): Preserve custom adjustments when refreshing adjustments (#3085)

This commit is contained in:
Frane Polić
2023-02-06 19:22:05 +01:00
committed by GitHub
parent d0adaf57ed
commit 5b63533c77
10 changed files with 299 additions and 41 deletions
@@ -2699,6 +2699,154 @@ describe("/admin/order-edits", () => {
})
})
describe("Preserve custom adjustments on items change", () => {
let product
let product2
const orderId = IdMap.getId("order-1")
const prodId1 = IdMap.getId("product-1")
const prodId2 = IdMap.getId("product-2")
const lineItemId1 = IdMap.getId("line-item-1")
beforeEach(async () => {
await adminSeeder(dbConnection)
product = await simpleProductFactory(dbConnection, {
id: prodId1,
})
product2 = await simpleProductFactory(dbConnection, {
id: prodId2,
})
await simpleOrderFactory(dbConnection, {
id: orderId,
email: "test@testson.com",
region: {
id: "test-region",
name: "Test region",
tax_rate: 12.5,
},
tax_rate: null,
line_items: [
{
adjustments: [
{
item_id: lineItemId1,
amount: 200,
description: "custom adjustment that should be persisted",
},
],
id: lineItemId1,
variant_id: product.variants[0].id,
quantity: 1,
fulfilled_quantity: 1,
shipped_quantity: 1,
unit_price: 1000,
tax_lines: [
{
item_id: lineItemId1,
rate: 12.5,
code: "default",
name: "default",
},
],
},
],
})
})
afterEach(async () => {
const db = useDb()
return await db.teardown()
})
it("preserve custom line item on update item change", async () => {
const api = useApi()
const {
data: { order_edit },
} = await api.post(
`/admin/order-edits/`,
{
order_id: orderId,
internal_note: "This is an internal note",
},
adminHeaders
)
const orderEditId = order_edit.id
const updateItemId = order_edit.items.find(
(item) => item.original_item_id === lineItemId1
).id
const response = await api.post(
`/admin/order-edits/${orderEditId}/items/${updateItemId}`,
{ quantity: 2 },
adminHeaders
)
expect(response.status).toEqual(200)
expect(response.data.order_edit.changes).toHaveLength(1)
expect(response.data.order_edit).toEqual(
expect.objectContaining({
id: orderEditId,
changes: expect.arrayContaining([
expect.objectContaining({
id: expect.any(String),
created_at: expect.any(String),
updated_at: expect.any(String),
deleted_at: null,
type: "item_update",
order_edit_id: orderEditId,
original_line_item_id: lineItemId1,
line_item_id: expect.any(String),
line_item: expect.objectContaining({
id: expect.any(String),
}),
original_line_item: expect.objectContaining({
id: lineItemId1,
}),
}),
]),
status: "created",
order_id: orderId,
internal_note: "This is an internal note",
created_by: "admin_user",
items: expect.arrayContaining([
expect.objectContaining({
id: expect.any(String),
original_item_id: lineItemId1,
order_edit_id: orderEditId,
quantity: 2,
adjustments: [
expect.objectContaining({
amount: 200,
description: "custom adjustment that should be persisted",
discount_id: null,
}),
],
tax_lines: expect.arrayContaining([
expect.objectContaining({
rate: 12.5,
name: "default",
code: "default",
}),
]),
}),
]),
// 2 items with unit price 1000 and a custom line item adjustment of 200
gift_card_total: 0,
gift_card_tax_total: 0,
shipping_total: 0,
discount_total: 200,
subtotal: 2 * 1000,
tax_total: (2000 - 200) * 0.125,
total: 1800 * 0.125 + 1800,
})
)
})
})
describe("DELETE /admin/order-edits/:id/items/:item_id", () => {
let product
let product2
@@ -207,4 +207,81 @@ describe("Line Item Adjustments", () => {
})
})
})
describe("When refreshing adjustments make sure that only adjustments associated with a Medusa Discount are deleted", () => {
let cart
let discount
const lineItemId = "line-test"
beforeEach(async () => {
await cartSeeder(dbConnection)
discount = await simpleDiscountFactory(dbConnection, {
code: "MEDUSATEST",
id: "discount-test",
rule: {
value: 100,
type: "fixed",
allocation: "total",
},
regions: ["test-region"],
})
cart = await simpleCartFactory(
dbConnection,
{
customer: "test-customer",
id: "cart-test",
line_items: [
{
id: lineItemId,
variant_id: "test-variant",
cart_id: "cart-test",
unit_price: 1000,
quantity: 1,
adjustments: [
{
amount: 10,
discount_id: discount.id,
description: "discount",
item_id: lineItemId,
},
{
// this one shouldn't be deleted because it's
// not associated with a Medusa Discount, i.e.
// it's created by a third party system, for example,
// a custom promotions engine
amount: 20,
description: "custom adjustment without discount",
item_id: lineItemId,
},
],
},
],
region: "test-region",
},
100
)
})
afterEach(async () => {
await doAfterEach()
})
it("Delete only adjustments of the removed discount and keep 'custom' adjustments", async () => {
const api = useApi()
const response = await api.delete(
`/store/carts/${cart.id}/discounts/${discount.code}`
)
expect(response.status).toEqual(200)
expect(response.data.cart.items[0].adjustments.length).toEqual(1)
expect(response.data.cart.items[0].adjustments[0]).toMatchObject({
amount: 20,
description: "custom adjustment without discount",
item_id: lineItemId,
})
})
})
})
@@ -165,7 +165,9 @@ describe("/admin/orders", () => {
* shipping method will have 12.5 rate 1000 * 1.125 = 1125
*/
expect(response.data.order.returns[0].refund_amount).toEqual(75)
expect(response.data.order.returns[0].shipping_method.tax_lines).toHaveLength(1)
expect(
response.data.order.returns[0].shipping_method.tax_lines
).toHaveLength(1)
expect(response.data.order.returns[0].shipping_method.tax_lines).toEqual(
expect.arrayContaining([
expect.objectContaining({
@@ -358,14 +360,16 @@ const createReturnableOrder = async (dbConnection, options) => {
fulfilled_quantity: options.shipped ? 2 : undefined,
shipped_quantity: options.shipped ? 2 : undefined,
unit_price: 1000,
adjustments: [
{
amount: 200,
discount_code: "TESTCODE",
description: "discount",
item_id: "test-item",
},
],
adjustments: options.discount
? [
{
amount: 200,
discount_code: "TESTCODE",
description: "discount",
item_id: "test-item",
},
]
: [],
tax_lines: [
{
name: "default",