fix(medusa): quantity prices for line item updates (#5137)
* initial code push * update metadata and only merge if the existing line item allows merging * update should_merge check * undo changes to taxrate service * update results with unit pricing corresponding to the db values after update * add should_merge property to line_item creation * add should_merge property to line_item creation * fix unit tests * undo adding "should_merge" to create-line-item * undo change to "addOrUpdateLineItem" * :wqh_merge from generate method * undo changes to unit tests * revert to adding pricing in updateLineItem method * update cart service test * Create funny-radios-juggle.md --------- Co-authored-by: Oli Juhl <59018053+olivermrbl@users.noreply.github.com>
This commit is contained in:
@@ -22,6 +22,7 @@ const {
|
||||
simpleShippingOptionFactory,
|
||||
simpleLineItemFactory,
|
||||
simpleSalesChannelFactory,
|
||||
simplePriceListFactory,
|
||||
} = require("../../../../factories")
|
||||
const {
|
||||
simpleDiscountFactory,
|
||||
@@ -650,7 +651,7 @@ describe("/store/carts", () => {
|
||||
{
|
||||
id: "line-item-2",
|
||||
cart_id: discountCart.id,
|
||||
variant_id: "test-variant-quantity",
|
||||
variant_id: "test-variant-quantity-1",
|
||||
product_id: "test-product",
|
||||
unit_price: 950,
|
||||
quantity: 1,
|
||||
@@ -790,7 +791,7 @@ describe("/store/carts", () => {
|
||||
expect.arrayContaining([
|
||||
expect.objectContaining({
|
||||
cart_id: "test-cart-3",
|
||||
unit_price: 8000,
|
||||
unit_price: 500,
|
||||
variant_id: "test-variant-sale-cg",
|
||||
quantity: 3,
|
||||
adjustments: [],
|
||||
@@ -854,7 +855,7 @@ describe("/store/carts", () => {
|
||||
allow_discounts: true,
|
||||
title: "Line Item Disc",
|
||||
thumbnail: "https://test.js/1234",
|
||||
unit_price: 1000,
|
||||
unit_price: 800,
|
||||
quantity: 1,
|
||||
variant_id: "test-variant-quantity",
|
||||
product_id: "test-product",
|
||||
@@ -876,12 +877,12 @@ describe("/store/carts", () => {
|
||||
expect.arrayContaining([
|
||||
expect.objectContaining({
|
||||
cart_id: "test-cart-w-total-percentage-discount",
|
||||
unit_price: 1000,
|
||||
unit_price: 800,
|
||||
variant_id: "test-variant-quantity",
|
||||
quantity: 10,
|
||||
adjustments: [
|
||||
expect.objectContaining({
|
||||
amount: 1000,
|
||||
amount: 800,
|
||||
discount_id: "10Percent",
|
||||
description: "discount",
|
||||
}),
|
||||
@@ -978,6 +979,111 @@ describe("/store/carts", () => {
|
||||
])
|
||||
)
|
||||
})
|
||||
|
||||
it("updates line item quantity with unit price reflected", async () => {
|
||||
const api = useApi()
|
||||
|
||||
await simplePriceListFactory(dbConnection, {
|
||||
id: "pl_current",
|
||||
prices: [
|
||||
{
|
||||
variant_id: "test-variant-sale-cg",
|
||||
amount: 10,
|
||||
min_quantity: 5,
|
||||
currency_code: "usd",
|
||||
},
|
||||
],
|
||||
})
|
||||
|
||||
const response = await api
|
||||
.post(
|
||||
"/store/carts/test-cart-3/line-items/test-item3/",
|
||||
{
|
||||
quantity: 5,
|
||||
},
|
||||
{ withCredentials: true }
|
||||
)
|
||||
.catch((err) => console.log(err))
|
||||
|
||||
expect(response.data.cart.items).toEqual(
|
||||
expect.arrayContaining([
|
||||
expect.objectContaining({
|
||||
cart_id: "test-cart-3",
|
||||
unit_price: 10,
|
||||
variant_id: "test-variant-sale-cg",
|
||||
quantity: 5,
|
||||
}),
|
||||
])
|
||||
)
|
||||
})
|
||||
|
||||
it("creates and updates line item quantity with unit price reflected", async () => {
|
||||
const api = useApi()
|
||||
|
||||
await simplePriceListFactory(dbConnection, {
|
||||
id: "pl_current",
|
||||
prices: [
|
||||
{
|
||||
variant_id: "test-variant-sale-cg",
|
||||
amount: 10,
|
||||
min_quantity: 5,
|
||||
currency_code: "usd",
|
||||
},
|
||||
],
|
||||
})
|
||||
|
||||
const createResponse = await api
|
||||
.post(
|
||||
"/store/carts/test-cart/line-items/",
|
||||
{
|
||||
quantity: 1,
|
||||
variant_id: "test-variant-sale-cg",
|
||||
},
|
||||
{ withCredentials: true }
|
||||
)
|
||||
.catch((err) => console.log(err))
|
||||
|
||||
expect(createResponse.data.cart.items).toEqual(
|
||||
expect.arrayContaining([
|
||||
expect.objectContaining({
|
||||
cart_id: "test-cart",
|
||||
unit_price: 500,
|
||||
variant_id: "test-variant-sale-cg",
|
||||
quantity: 1,
|
||||
}),
|
||||
])
|
||||
)
|
||||
|
||||
const lineItemId = createResponse.data.cart.items.find(
|
||||
(i) => i.variant_id === "test-variant-sale-cg"
|
||||
).id
|
||||
|
||||
const response = await api
|
||||
.post(
|
||||
`/store/carts/test-cart/line-items/${lineItemId}`,
|
||||
{
|
||||
quantity: 5,
|
||||
},
|
||||
{ withCredentials: true }
|
||||
)
|
||||
.catch((err) => console.log(err))
|
||||
|
||||
const lineItemIdCount = response.data.cart.items.filter(
|
||||
(i) => i.variant_id === "test-variant-sale-cg"
|
||||
)
|
||||
|
||||
expect(lineItemIdCount.length).toEqual(1)
|
||||
expect(response.data.cart.items).toEqual(
|
||||
expect.arrayContaining([
|
||||
expect.objectContaining({
|
||||
cart_id: "test-cart",
|
||||
unit_price: 10,
|
||||
variant_id: "test-variant-sale-cg",
|
||||
quantity: 5,
|
||||
}),
|
||||
])
|
||||
)
|
||||
})
|
||||
})
|
||||
|
||||
describe("POST /store/carts/:id", () => {
|
||||
|
||||
@@ -16,6 +16,8 @@ type ProductListPrice = {
|
||||
currency_code: string
|
||||
region_id: string
|
||||
amount: number
|
||||
min_quantity?: number
|
||||
max_quantity?: number
|
||||
}
|
||||
|
||||
export type PriceListFactoryData = {
|
||||
|
||||
@@ -531,6 +531,33 @@ module.exports = async (dataSource, data = {}) => {
|
||||
variant_id: "test-variant-quantity",
|
||||
})
|
||||
|
||||
const quantityVariant1 = manager.create(ProductVariant, {
|
||||
id: "test-variant-quantity-1",
|
||||
title: "test variant quantity 1",
|
||||
product_id: "test-product",
|
||||
inventory_quantity: 1000,
|
||||
options: [
|
||||
{
|
||||
option_id: "test-option",
|
||||
value: "Fit",
|
||||
},
|
||||
],
|
||||
})
|
||||
|
||||
await manager.save(quantityVariant1)
|
||||
|
||||
await manager.insert(MoneyAmount, {
|
||||
id: "test-price_quantity-1.5",
|
||||
currency_code: "usd",
|
||||
amount: 950,
|
||||
})
|
||||
|
||||
await manager.insert(ProductVariantMoneyAmount, {
|
||||
id: "pvma-quantity-1.5",
|
||||
money_amount_id: "test-price_quantity-1.5",
|
||||
variant_id: "test-variant-quantity-1",
|
||||
})
|
||||
|
||||
await manager.insert(MoneyAmount, {
|
||||
id: "test-price_quantity-2",
|
||||
currency_code: "usd",
|
||||
|
||||
Reference in New Issue
Block a user