Feat(medusa): remove item from order (#2273)
* wait for update to order edit model * delete line item tests * create remove method for lineitem with tax lines * add remove item tests * split delete allocation tests into two: more and less than total * remove unused import * cleanup * add medusa-js and react endpoints * pr feedback fixes * linting * remove unused relation from query * remove removed-event and unused imports * add await
This commit is contained in:
@@ -1419,7 +1419,8 @@ describe("[MEDUSA_FF_ORDER_EDITING] /admin/order-edits", () => {
|
||||
})
|
||||
|
||||
describe("POST /admin/order-edits/:id/confirm", () => {
|
||||
let product, product2
|
||||
let product
|
||||
let product2
|
||||
const prodId1 = IdMap.getId("product-1")
|
||||
const prodId2 = IdMap.getId("product-2")
|
||||
const lineItemId1 = IdMap.getId("line-item-1")
|
||||
@@ -1623,7 +1624,8 @@ describe("[MEDUSA_FF_ORDER_EDITING] /admin/order-edits", () => {
|
||||
})
|
||||
|
||||
describe("POST /admin/order-edits/:id/items/:item_id", () => {
|
||||
let product, product2
|
||||
let product
|
||||
let product2
|
||||
const orderId = IdMap.getId("order-1")
|
||||
const prodId1 = IdMap.getId("product-1")
|
||||
const prodId2 = IdMap.getId("product-2")
|
||||
@@ -2332,4 +2334,511 @@ describe("[MEDUSA_FF_ORDER_EDITING] /admin/order-edits", () => {
|
||||
)
|
||||
})
|
||||
})
|
||||
|
||||
describe("DELETE /admin/order-edits/:id/items/:item_id", () => {
|
||||
let product
|
||||
let product2
|
||||
let discountCode
|
||||
let discountCodeLarge
|
||||
let cart
|
||||
const orderId = IdMap.getId("order-1")
|
||||
const discountOrderId = IdMap.getId("order-2")
|
||||
const prodId1 = IdMap.getId("product-1")
|
||||
const prodId2 = IdMap.getId("product-2")
|
||||
const lineItemId1 = IdMap.getId("line-item-1")
|
||||
const lineItemId2 = IdMap.getId("line-item-2")
|
||||
const lineItemId1Discount = IdMap.getId("line-item-1-discount")
|
||||
const lineItemId2Discount = IdMap.getId("line-item-2-discount")
|
||||
|
||||
beforeEach(async () => {
|
||||
const api = useApi()
|
||||
await adminSeeder(dbConnection)
|
||||
|
||||
product = await simpleProductFactory(dbConnection, {
|
||||
id: prodId1,
|
||||
})
|
||||
|
||||
product2 = await simpleProductFactory(dbConnection, {
|
||||
id: prodId2,
|
||||
})
|
||||
|
||||
const reagion = await simpleRegionFactory(dbConnection, {
|
||||
id: "test-region",
|
||||
name: "Test region",
|
||||
tax_rate: 12.5,
|
||||
})
|
||||
|
||||
await simpleOrderFactory(dbConnection, {
|
||||
id: orderId,
|
||||
email: "test-2@testson.com",
|
||||
tax_rate: null,
|
||||
fulfillment_status: "fulfilled",
|
||||
payment_status: "captured",
|
||||
region_id: "test-region",
|
||||
line_items: [
|
||||
{
|
||||
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",
|
||||
},
|
||||
],
|
||||
},
|
||||
{
|
||||
id: lineItemId2,
|
||||
variant_id: product2.variants[0].id,
|
||||
quantity: 1,
|
||||
fulfilled_quantity: 1,
|
||||
shipped_quantity: 1,
|
||||
unit_price: 1000,
|
||||
tax_lines: [
|
||||
{
|
||||
item_id: lineItemId2,
|
||||
rate: 12.5,
|
||||
code: "default",
|
||||
name: "default",
|
||||
},
|
||||
],
|
||||
},
|
||||
],
|
||||
})
|
||||
|
||||
discountCode = "FIX_DISCOUNT_SMALL"
|
||||
const discount = await simpleDiscountFactory(dbConnection, {
|
||||
code: discountCode,
|
||||
rule: {
|
||||
type: "fixed",
|
||||
allocation: "total",
|
||||
value: 500,
|
||||
},
|
||||
regions: ["test-region"],
|
||||
})
|
||||
|
||||
discountCodeLarge = "FIX_DISCOUNT_LARGE"
|
||||
const discountLarge = await simpleDiscountFactory(dbConnection, {
|
||||
code: discountCodeLarge,
|
||||
rule: {
|
||||
type: "fixed",
|
||||
allocation: "total",
|
||||
value: 1200,
|
||||
},
|
||||
regions: ["test-region"],
|
||||
})
|
||||
|
||||
cart = await simpleCartFactory(dbConnection, {
|
||||
email: "adrien@test.com",
|
||||
region: "test-region",
|
||||
line_items: [
|
||||
{
|
||||
id: lineItemId1Discount,
|
||||
variant_id: product.variants[0].id,
|
||||
quantity: 1,
|
||||
unit_price: 1000,
|
||||
},
|
||||
{
|
||||
id: lineItemId2Discount,
|
||||
variant_id: product2.variants[0].id,
|
||||
quantity: 1,
|
||||
unit_price: 1000,
|
||||
},
|
||||
],
|
||||
})
|
||||
})
|
||||
|
||||
afterEach(async () => {
|
||||
const db = useDb()
|
||||
return await db.teardown()
|
||||
})
|
||||
|
||||
it("creates an order edit item change of type delete on line item delete", async () => {
|
||||
const api = useApi()
|
||||
|
||||
const {
|
||||
data: { order_edit },
|
||||
} = await api
|
||||
.post(
|
||||
`/admin/order-edits/`,
|
||||
{
|
||||
order_id: orderId,
|
||||
},
|
||||
adminHeaders
|
||||
)
|
||||
.catch(console.log)
|
||||
|
||||
const orderEditId = order_edit.id
|
||||
const editLineItemId = order_edit.items.find(
|
||||
(it) => it.original_item_id === lineItemId1
|
||||
).id
|
||||
|
||||
const response = await api
|
||||
.delete(
|
||||
`/admin/order-edits/${orderEditId}/items/${editLineItemId}`,
|
||||
adminHeaders
|
||||
)
|
||||
.catch(console.log)
|
||||
|
||||
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_remove",
|
||||
order_edit_id: orderEditId,
|
||||
original_line_item_id: lineItemId1,
|
||||
line_item_id: null,
|
||||
line_item: null,
|
||||
original_line_item: expect.objectContaining({
|
||||
id: lineItemId1,
|
||||
created_at: expect.any(String),
|
||||
updated_at: expect.any(String),
|
||||
cart_id: null,
|
||||
order_id: orderId,
|
||||
swap_id: null,
|
||||
claim_order_id: null,
|
||||
title: expect.any(String),
|
||||
description: "",
|
||||
thumbnail: "",
|
||||
is_return: false,
|
||||
is_giftcard: false,
|
||||
should_merge: true,
|
||||
allow_discounts: true,
|
||||
has_shipping: null,
|
||||
unit_price: 1000,
|
||||
variant_id: expect.any(String),
|
||||
quantity: 1,
|
||||
fulfilled_quantity: 1,
|
||||
returned_quantity: null,
|
||||
shipped_quantity: 1,
|
||||
metadata: null,
|
||||
variant: expect.any(Object),
|
||||
}),
|
||||
}),
|
||||
]),
|
||||
status: "created",
|
||||
order_id: orderId,
|
||||
created_by: "admin_user",
|
||||
items: [
|
||||
expect.objectContaining({
|
||||
id: expect.any(String),
|
||||
original_item_id: lineItemId2,
|
||||
order_edit_id: orderEditId,
|
||||
cart_id: null,
|
||||
order_id: null,
|
||||
swap_id: null,
|
||||
claim_order_id: null,
|
||||
title: expect.any(String),
|
||||
is_return: false,
|
||||
is_giftcard: false,
|
||||
should_merge: true,
|
||||
allow_discounts: true,
|
||||
has_shipping: null,
|
||||
unit_price: 1000,
|
||||
variant_id: expect.any(String),
|
||||
quantity: 1,
|
||||
fulfilled_quantity: 1,
|
||||
returned_quantity: null,
|
||||
shipped_quantity: 1,
|
||||
metadata: null,
|
||||
tax_lines: expect.arrayContaining([
|
||||
expect.objectContaining({
|
||||
rate: 12.5,
|
||||
name: "default",
|
||||
code: "default",
|
||||
}),
|
||||
]),
|
||||
}),
|
||||
],
|
||||
discount_total: 0,
|
||||
gift_card_total: 0,
|
||||
gift_card_tax_total: 0,
|
||||
shipping_total: 0,
|
||||
subtotal: 1000,
|
||||
tax_total: 125,
|
||||
total: 1125,
|
||||
})
|
||||
)
|
||||
})
|
||||
|
||||
it("creates an order edit item change of type delete on line item delete and adjusts discount allocation from one to two items", async () => {
|
||||
const api = useApi()
|
||||
|
||||
await api.post(`/store/carts/${cart.id}`, {
|
||||
discounts: [{ code: discountCode }],
|
||||
})
|
||||
|
||||
await api.post(`/store/carts/${cart.id}/payment-sessions`)
|
||||
|
||||
const completeRes = await api.post(`/store/carts/${cart.id}/complete`)
|
||||
|
||||
const discountOrder = completeRes.data.data
|
||||
|
||||
const {
|
||||
data: { order_edit },
|
||||
} = await api.post(
|
||||
`/admin/order-edits/`,
|
||||
{
|
||||
order_id: discountOrder.id,
|
||||
},
|
||||
adminHeaders
|
||||
)
|
||||
|
||||
const editLineItemId = order_edit.items.find(
|
||||
(it) => it.original_item_id === lineItemId1Discount
|
||||
).id
|
||||
|
||||
const orderEditId = order_edit.id
|
||||
await api.delete(
|
||||
`/admin/order-edits/${orderEditId}/items/${editLineItemId}`,
|
||||
adminHeaders
|
||||
)
|
||||
|
||||
const response = await api.get(
|
||||
`/admin/order-edits/${orderEditId}`,
|
||||
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.objectContaining({
|
||||
id: expect.any(String),
|
||||
created_at: expect.any(String),
|
||||
updated_at: expect.any(String),
|
||||
deleted_at: null,
|
||||
type: "item_remove",
|
||||
order_edit_id: orderEditId,
|
||||
original_line_item_id: lineItemId1Discount,
|
||||
line_item_id: null,
|
||||
original_line_item: expect.objectContaining({
|
||||
id: lineItemId1Discount,
|
||||
created_at: expect.any(String),
|
||||
updated_at: expect.any(String),
|
||||
cart_id: expect.any(String),
|
||||
order_id: discountOrder.id,
|
||||
swap_id: null,
|
||||
claim_order_id: null,
|
||||
title: expect.any(String),
|
||||
description: "",
|
||||
thumbnail: "",
|
||||
is_return: false,
|
||||
is_giftcard: false,
|
||||
should_merge: true,
|
||||
allow_discounts: true,
|
||||
has_shipping: null,
|
||||
unit_price: 1000,
|
||||
variant_id: expect.any(String),
|
||||
quantity: 1,
|
||||
returned_quantity: null,
|
||||
shipped_quantity: null,
|
||||
metadata: null,
|
||||
variant: expect.any(Object),
|
||||
}),
|
||||
}),
|
||||
],
|
||||
status: "created",
|
||||
order_id: discountOrder.id,
|
||||
created_by: "admin_user",
|
||||
items: [
|
||||
expect.objectContaining({
|
||||
id: expect.any(String),
|
||||
original_item_id: lineItemId2Discount,
|
||||
order_edit_id: orderEditId,
|
||||
cart_id: null,
|
||||
order_id: null,
|
||||
swap_id: null,
|
||||
claim_order_id: null,
|
||||
title: expect.any(String),
|
||||
description: "",
|
||||
thumbnail: "",
|
||||
is_return: false,
|
||||
is_giftcard: false,
|
||||
should_merge: true,
|
||||
allow_discounts: true,
|
||||
has_shipping: null,
|
||||
unit_price: 1000,
|
||||
variant_id: expect.any(String),
|
||||
quantity: 1,
|
||||
returned_quantity: null,
|
||||
metadata: null,
|
||||
adjustments: [
|
||||
expect.objectContaining({
|
||||
id: expect.any(String),
|
||||
item_id: expect.any(String),
|
||||
description: "discount",
|
||||
discount_id: expect.any(String),
|
||||
amount: 500,
|
||||
metadata: null,
|
||||
}),
|
||||
],
|
||||
tax_lines: expect.arrayContaining([
|
||||
expect.objectContaining({
|
||||
rate: 12.5,
|
||||
name: "default",
|
||||
code: "default",
|
||||
}),
|
||||
]),
|
||||
}),
|
||||
],
|
||||
discount_total: 500,
|
||||
gift_card_total: 0,
|
||||
gift_card_tax_total: 0,
|
||||
shipping_total: 0,
|
||||
subtotal: 1000,
|
||||
tax_total: 63,
|
||||
total: 563,
|
||||
})
|
||||
)
|
||||
})
|
||||
|
||||
it("creates an order edit item change of type delete on line item delete and adjusts discount allocation from one to two items with discount amount being larger than the unit price of the remaining line item", async () => {
|
||||
const api = useApi()
|
||||
|
||||
await api.post(`/store/carts/${cart.id}`, {
|
||||
discounts: [{ code: discountCodeLarge }],
|
||||
})
|
||||
|
||||
await api.post(`/store/carts/${cart.id}/payment-sessions`)
|
||||
|
||||
const completeRes = await api.post(`/store/carts/${cart.id}/complete`)
|
||||
|
||||
const discountOrder = completeRes.data.data
|
||||
|
||||
const {
|
||||
data: { order_edit },
|
||||
} = await api.post(
|
||||
`/admin/order-edits/`,
|
||||
{
|
||||
order_id: discountOrder.id,
|
||||
},
|
||||
adminHeaders
|
||||
)
|
||||
|
||||
const editLineItemId = order_edit.items.find(
|
||||
(it) => it.original_item_id === lineItemId1Discount
|
||||
).id
|
||||
|
||||
const orderEditId = order_edit.id
|
||||
await api.delete(
|
||||
`/admin/order-edits/${orderEditId}/items/${editLineItemId}`,
|
||||
adminHeaders
|
||||
)
|
||||
|
||||
const response = await api.get(
|
||||
`/admin/order-edits/${orderEditId}`,
|
||||
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.objectContaining({
|
||||
id: expect.any(String),
|
||||
created_at: expect.any(String),
|
||||
updated_at: expect.any(String),
|
||||
deleted_at: null,
|
||||
type: "item_remove",
|
||||
order_edit_id: orderEditId,
|
||||
original_line_item_id: lineItemId1Discount,
|
||||
line_item_id: null,
|
||||
original_line_item: expect.objectContaining({
|
||||
id: lineItemId1Discount,
|
||||
created_at: expect.any(String),
|
||||
updated_at: expect.any(String),
|
||||
cart_id: expect.any(String),
|
||||
order_id: discountOrder.id,
|
||||
swap_id: null,
|
||||
claim_order_id: null,
|
||||
title: expect.any(String),
|
||||
description: "",
|
||||
thumbnail: "",
|
||||
is_return: false,
|
||||
is_giftcard: false,
|
||||
should_merge: true,
|
||||
allow_discounts: true,
|
||||
has_shipping: null,
|
||||
unit_price: 1000,
|
||||
variant_id: expect.any(String),
|
||||
quantity: 1,
|
||||
returned_quantity: null,
|
||||
shipped_quantity: null,
|
||||
metadata: null,
|
||||
variant: expect.any(Object),
|
||||
}),
|
||||
}),
|
||||
],
|
||||
status: "created",
|
||||
order_id: discountOrder.id,
|
||||
created_by: "admin_user",
|
||||
items: [
|
||||
expect.objectContaining({
|
||||
id: expect.any(String),
|
||||
original_item_id: lineItemId2Discount,
|
||||
order_edit_id: orderEditId,
|
||||
cart_id: null,
|
||||
order_id: null,
|
||||
swap_id: null,
|
||||
claim_order_id: null,
|
||||
title: expect.any(String),
|
||||
description: "",
|
||||
thumbnail: "",
|
||||
is_return: false,
|
||||
is_giftcard: false,
|
||||
should_merge: true,
|
||||
allow_discounts: true,
|
||||
has_shipping: null,
|
||||
unit_price: 1000,
|
||||
variant_id: expect.any(String),
|
||||
quantity: 1,
|
||||
returned_quantity: null,
|
||||
metadata: null,
|
||||
adjustments: [
|
||||
expect.objectContaining({
|
||||
id: expect.any(String),
|
||||
item_id: expect.any(String),
|
||||
description: "discount",
|
||||
discount_id: expect.any(String),
|
||||
amount: 1000,
|
||||
metadata: null,
|
||||
}),
|
||||
],
|
||||
tax_lines: expect.arrayContaining([
|
||||
expect.objectContaining({
|
||||
rate: 12.5,
|
||||
name: "default",
|
||||
code: "default",
|
||||
}),
|
||||
]),
|
||||
}),
|
||||
],
|
||||
discount_total: 1000,
|
||||
gift_card_total: 0,
|
||||
gift_card_tax_total: 0,
|
||||
shipping_total: 0,
|
||||
subtotal: 1000,
|
||||
tax_total: 0,
|
||||
total: 0,
|
||||
})
|
||||
)
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
Reference in New Issue
Block a user