feat: line item adjustments (#1319)
* add: crud services + model + totals * fix: enforce unique constraint on line item adjustment model and update service (#1241) * add: unique constraint on model + fix service * fix: unique constraint * fix: add cascade on delete + fix discount relation * fix: remove optional unique prop * add: tests for ensuring line item adjustment db constraints (#1279) * add: tests for ensuring db constraints * fix: use given when then * feat: adjust cart to include line item adjustments (#1242) * fix: cart service + cart tests * fix: remaining tests * fix: swap tests * fix: add relationship + fix oas * refactor: applyDiscount * fix: refactor applyDiscount and fix + add unit tests * fix: plugins tests * feat: line item adjustments draft orders (#1243) * fix: draft order tests * fix: constraint * fix: wrong variable name * fix: unique constraint * progress: add tests * fix: add cascade on delete + fix discount relation * fix: remove optional unique prop * fix: cart removeLineItem + tests * fix: cart unit tests * fix: update snapshot * remove: verbose option * rename arg Co-authored-by: Sebastian Rindom <skrindom@gmail.com> * add: create adjustments for swap additional_items * add: create adjustments for return lines * fix: unit test for creating adjustment for additional_items * fix: create adjustments only for non return items + no deletion when item is a return item * add: integration tests * refactor: use refreshAdjustments method * refactor test Co-authored-by: Sebastian Rindom <skrindom@gmail.com> Co-authored-by: Sebastian Rindom <skrindom@gmail.com> Co-authored-by: Sebastian Rindom <skrindom@gmail.com>
This commit is contained in:
co-authored by
Sebastian Rindom
parent
607a382b4e
commit
1cfeb5dbd8
@@ -297,6 +297,62 @@ describe("/admin/draft-orders", () => {
|
||||
)
|
||||
})
|
||||
|
||||
it("creates a draft order with discount and line item", async () => {
|
||||
const api = useApi()
|
||||
|
||||
const payload = {
|
||||
email: "oli@test.dk",
|
||||
shipping_address: "oli-shipping",
|
||||
discounts: [{ code: "TEST" }],
|
||||
items: [
|
||||
{
|
||||
variant_id: "test-variant",
|
||||
quantity: 2,
|
||||
metadata: {},
|
||||
},
|
||||
],
|
||||
region_id: "test-region",
|
||||
customer_id: "oli-test",
|
||||
shipping_methods: [
|
||||
{
|
||||
option_id: "test-option",
|
||||
},
|
||||
],
|
||||
}
|
||||
|
||||
const response = await api
|
||||
.post("/admin/draft-orders", payload, {
|
||||
headers: {
|
||||
Authorization: "Bearer test_token",
|
||||
},
|
||||
})
|
||||
.catch((err) => {
|
||||
console.log(err)
|
||||
})
|
||||
|
||||
const draftOrder = response.data.draft_order
|
||||
const lineItemId = draftOrder.cart.items[0].id
|
||||
|
||||
expect(response.status).toEqual(200)
|
||||
expect(draftOrder.cart.items).toEqual(
|
||||
expect.arrayContaining([
|
||||
expect.objectContaining({
|
||||
variant_id: "test-variant",
|
||||
unit_price: 8000,
|
||||
quantity: 2,
|
||||
adjustments: expect.arrayContaining([
|
||||
expect.objectContaining({
|
||||
item_id: lineItemId,
|
||||
amount: 1600,
|
||||
description: "discount",
|
||||
discount_id: "test-discount",
|
||||
}),
|
||||
]),
|
||||
}),
|
||||
])
|
||||
)
|
||||
})
|
||||
|
||||
it("creates a draft order with created shipping address", async () => {
|
||||
const api = useApi()
|
||||
|
||||
|
||||
@@ -1555,6 +1555,116 @@ describe("/admin/orders", () => {
|
||||
expect(response.status).toEqual(200)
|
||||
})
|
||||
|
||||
describe("Given an existing discount order", () => {
|
||||
describe("When a store operator attemps to create a swap form the discount order", () => {
|
||||
it("Then should successfully create the swap", async () => {
|
||||
const api = useApi()
|
||||
|
||||
const response = await api.post(
|
||||
"/admin/orders/test-order/swaps",
|
||||
{
|
||||
return_items: [
|
||||
{
|
||||
item_id: "test-item",
|
||||
quantity: 1,
|
||||
},
|
||||
],
|
||||
additional_items: [{ variant_id: "test-variant-2", quantity: 1 }],
|
||||
},
|
||||
{
|
||||
headers: {
|
||||
authorization: "Bearer test_token",
|
||||
},
|
||||
}
|
||||
)
|
||||
|
||||
const swapCartId = response.data.order.swaps[0].cart_id
|
||||
|
||||
const swapCartRes = await api.get(`/store/carts/${swapCartId}`, {
|
||||
headers: {
|
||||
authorization: "Bearer test_token",
|
||||
},
|
||||
})
|
||||
const cart = swapCartRes.data.cart
|
||||
|
||||
expect(response.status).toEqual(200)
|
||||
expect(cart.items.length).toEqual(2)
|
||||
expect(cart.items).toEqual(
|
||||
expect.arrayContaining([
|
||||
expect.objectContaining({
|
||||
unit_price: -8000,
|
||||
adjustments: [
|
||||
expect.objectContaining({
|
||||
amount: -800,
|
||||
}),
|
||||
],
|
||||
}),
|
||||
expect.objectContaining({
|
||||
unit_price: 8000,
|
||||
adjustments: [
|
||||
expect.objectContaining({
|
||||
amount: 800,
|
||||
}),
|
||||
],
|
||||
}),
|
||||
])
|
||||
)
|
||||
expect(cart.total).toEqual(0)
|
||||
})
|
||||
})
|
||||
|
||||
describe("And given a swap cart", () => {
|
||||
describe("When a line item is added to the swap cart", () => {
|
||||
it("Then should not delete existing return line item adjustments", async () => {
|
||||
const api = useApi()
|
||||
|
||||
const createSwapRes = await api.post(
|
||||
"/admin/orders/test-order/swaps",
|
||||
{
|
||||
return_items: [
|
||||
{
|
||||
item_id: "test-item",
|
||||
quantity: 1,
|
||||
},
|
||||
],
|
||||
additional_items: [{ variant_id: "test-variant", quantity: 1 }],
|
||||
},
|
||||
{
|
||||
headers: {
|
||||
authorization: "Bearer test_token",
|
||||
},
|
||||
}
|
||||
)
|
||||
|
||||
const swapCartId = createSwapRes.data.order.swaps[0].cart_id
|
||||
|
||||
const response = await api.post(
|
||||
`/store/carts/${swapCartId}/line-items`,
|
||||
{
|
||||
variant_id: "test-variant-2",
|
||||
quantity: 1,
|
||||
},
|
||||
{
|
||||
headers: {
|
||||
authorization: "Bearer test_token",
|
||||
},
|
||||
}
|
||||
)
|
||||
|
||||
const cart = response.data.cart
|
||||
const items = cart.items
|
||||
const [returnItem] = items.filter((i) => i.is_return)
|
||||
expect(returnItem.adjustments).toEqual([
|
||||
expect.objectContaining({
|
||||
amount: -800,
|
||||
}),
|
||||
])
|
||||
expect(cart.total).toBe(7200)
|
||||
})
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
it("creates a swap with custom shipping options", async () => {
|
||||
const api = useApi()
|
||||
|
||||
|
||||
@@ -35,6 +35,7 @@ exports[`Claims creates a replace claim 1`] = `
|
||||
Object {
|
||||
"additional_items": Array [
|
||||
Object {
|
||||
"adjustments": Array [],
|
||||
"allow_discounts": true,
|
||||
"cart_id": null,
|
||||
"claim_order_id": StringMatching /\\^claim_\\*/,
|
||||
|
||||
@@ -0,0 +1,222 @@
|
||||
const path = require("path")
|
||||
const { LineItemAdjustment } = require("@medusajs/medusa")
|
||||
const setupServer = require("../../../helpers/setup-server")
|
||||
const { useApi } = require("../../../helpers/use-api")
|
||||
const { initDb, useDb } = require("../../../helpers/use-db")
|
||||
const cartSeeder = require("../../helpers/cart-seeder")
|
||||
const { simpleCartFactory, simpleLineItemFactory } = require("../../factories")
|
||||
const {
|
||||
simpleDiscountFactory,
|
||||
} = require("../../factories/simple-discount-factory")
|
||||
|
||||
jest.setTimeout(30000)
|
||||
|
||||
describe("Line Item Adjustments", () => {
|
||||
let dbConnection
|
||||
let medusaProcess
|
||||
|
||||
const doAfterEach = async () => {
|
||||
const db = useDb()
|
||||
return await db.teardown()
|
||||
}
|
||||
|
||||
beforeAll(async () => {
|
||||
const cwd = path.resolve(path.join(__dirname, "..", ".."))
|
||||
try {
|
||||
dbConnection = await initDb({ cwd })
|
||||
medusaProcess = await setupServer({ cwd })
|
||||
} catch (error) {
|
||||
console.log(error)
|
||||
}
|
||||
})
|
||||
|
||||
afterAll(async () => {
|
||||
const db = useDb()
|
||||
await db.shutdown()
|
||||
medusaProcess.kill()
|
||||
})
|
||||
|
||||
describe("Tests database constraints", () => {
|
||||
let cart,
|
||||
discount,
|
||||
lineItemId = "line-test"
|
||||
beforeEach(async () => {
|
||||
try {
|
||||
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,
|
||||
},
|
||||
],
|
||||
},
|
||||
],
|
||||
region: "test-region",
|
||||
shipping_address: {
|
||||
address_1: "test",
|
||||
country_code: "us",
|
||||
first_name: "chris",
|
||||
last_name: "rock",
|
||||
postal_code: "101",
|
||||
},
|
||||
shipping_methods: [
|
||||
{
|
||||
shipping_option: "test-option",
|
||||
},
|
||||
],
|
||||
},
|
||||
100
|
||||
)
|
||||
} catch (err) {
|
||||
console.log(err)
|
||||
throw err
|
||||
}
|
||||
})
|
||||
|
||||
afterEach(async () => {
|
||||
await doAfterEach()
|
||||
})
|
||||
|
||||
afterEach(async () => {
|
||||
await doAfterEach()
|
||||
})
|
||||
|
||||
describe("Given an existing line item, a discount, and a line item adjustment for both", () => {
|
||||
describe("When creating an adjustment for another line item w. same discount", () => {
|
||||
test("Then should create an adjustment", async () => {
|
||||
const createLineItemWithAdjustment = async () => {
|
||||
return await simpleLineItemFactory(dbConnection, {
|
||||
id: "line-test-2",
|
||||
variant_id: "test-variant-quantity",
|
||||
cart_id: "test-cart",
|
||||
unit_price: 1000,
|
||||
quantity: 1,
|
||||
adjustments: [
|
||||
{
|
||||
amount: 10,
|
||||
discount_id: discount.id,
|
||||
description: "discount",
|
||||
item_id: "line-test-2",
|
||||
},
|
||||
],
|
||||
})
|
||||
}
|
||||
|
||||
expect(createLineItemWithAdjustment()).resolves.toEqual(
|
||||
expect.anything()
|
||||
)
|
||||
})
|
||||
})
|
||||
|
||||
describe("When creating an adjustment for another line item w. null discount", () => {
|
||||
test("Then should create an adjustment", async () => {
|
||||
const createAdjustmentNullDiscount = async () => {
|
||||
return await dbConnection.manager.insert(LineItemAdjustment, {
|
||||
id: "lia-1",
|
||||
item_id: lineItemId,
|
||||
amount: 35,
|
||||
description: "custom discount",
|
||||
discount_id: null,
|
||||
})
|
||||
}
|
||||
|
||||
expect(createAdjustmentNullDiscount()).resolves.toEqual(
|
||||
expect.anything()
|
||||
)
|
||||
})
|
||||
})
|
||||
|
||||
describe("When creating multiple adjustments w. a null discount_id", () => {
|
||||
test("Then should create multiple adjustments", async () => {
|
||||
const createAdjustmentsNullDiscount = async () => {
|
||||
await dbConnection.manager.insert(LineItemAdjustment, {
|
||||
id: "lia-1",
|
||||
item_id: lineItemId,
|
||||
amount: 35,
|
||||
description: "custom discount",
|
||||
discount_id: null,
|
||||
})
|
||||
return await dbConnection.manager.insert(LineItemAdjustment, {
|
||||
id: "lia-2",
|
||||
item_id: lineItemId,
|
||||
amount: 100,
|
||||
description: "custom discount",
|
||||
discount_id: null,
|
||||
})
|
||||
}
|
||||
expect(createAdjustmentsNullDiscount()).resolves.toEqual(
|
||||
expect.anything()
|
||||
)
|
||||
})
|
||||
})
|
||||
|
||||
describe("When creating an adjustment w. for same line item and different discount", () => {
|
||||
test("Then should create an adjustment", async () => {
|
||||
const createAdjustment = async () => {
|
||||
await simpleDiscountFactory(dbConnection, {
|
||||
code: "ANOTHER",
|
||||
id: "discount-2",
|
||||
rule: {
|
||||
value: 10,
|
||||
type: "percentage",
|
||||
allocation: "item",
|
||||
},
|
||||
regions: ["test-region"],
|
||||
})
|
||||
|
||||
return await dbConnection.manager.insert(LineItemAdjustment, {
|
||||
id: "lia-1",
|
||||
item_id: lineItemId,
|
||||
amount: 10,
|
||||
description: "discount",
|
||||
discount_id: "discount-2",
|
||||
})
|
||||
}
|
||||
|
||||
expect(createAdjustment()).resolves.toEqual(expect.anything())
|
||||
})
|
||||
})
|
||||
|
||||
describe("When creating an adjustment w. existing line item and discount pair", () => {
|
||||
test("Then should throw a duplicate error", async () => {
|
||||
const createDuplicateAdjustment = async () =>
|
||||
await dbConnection.manager.insert(LineItemAdjustment, {
|
||||
id: "lia-1",
|
||||
item_id: lineItemId,
|
||||
amount: 20,
|
||||
description: "discount",
|
||||
discount_id: discount.id,
|
||||
})
|
||||
|
||||
expect(createDuplicateAdjustment()).rejects.toEqual(
|
||||
expect.objectContaining({ code: "23505" })
|
||||
)
|
||||
})
|
||||
})
|
||||
})
|
||||
})
|
||||
})
|
||||
@@ -259,6 +259,14 @@ const createReturnableOrder = async (dbConnection, options) => {
|
||||
variant_id: "test-variant",
|
||||
quantity: 2,
|
||||
unit_price: 1000,
|
||||
adjustments: [
|
||||
{
|
||||
amount: 200,
|
||||
discount_code: "TESTCODE",
|
||||
description: "discount",
|
||||
item_id: "test-item",
|
||||
},
|
||||
],
|
||||
tax_lines: [
|
||||
{
|
||||
name: "default",
|
||||
|
||||
@@ -16,7 +16,7 @@ const { initDb, useDb } = require("../../../helpers/use-db")
|
||||
const cartSeeder = require("../../helpers/cart-seeder")
|
||||
const productSeeder = require("../../helpers/product-seeder")
|
||||
const swapSeeder = require("../../helpers/swap-seeder")
|
||||
const { simpleCartFactory } = require("../../factories")
|
||||
const { simpleCartFactory, simpleLineItemFactory } = require("../../factories")
|
||||
const {
|
||||
simpleDiscountFactory,
|
||||
} = require("../../factories/simple-discount-factory")
|
||||
@@ -235,6 +235,131 @@ describe("/store/carts", () => {
|
||||
unit_price: 1000,
|
||||
variant_id: "test-variant-quantity",
|
||||
quantity: 1,
|
||||
adjustments: [],
|
||||
}),
|
||||
])
|
||||
})
|
||||
|
||||
it("adds line item to cart containing a total fixed discount", async () => {
|
||||
const api = useApi()
|
||||
|
||||
const response = await api
|
||||
.post(
|
||||
"/store/carts/test-cart-w-total-fixed-discount/line-items",
|
||||
{
|
||||
variant_id: "test-variant-quantity",
|
||||
quantity: 2,
|
||||
},
|
||||
{ withCredentials: true }
|
||||
)
|
||||
.catch((err) => console.log(err))
|
||||
|
||||
expect(response.data.cart.items).toEqual([
|
||||
expect.objectContaining({
|
||||
cart_id: "test-cart-w-total-fixed-discount",
|
||||
unit_price: 1000,
|
||||
variant_id: "test-variant-quantity",
|
||||
quantity: 2,
|
||||
adjustments: [
|
||||
expect.objectContaining({
|
||||
amount: 100,
|
||||
discount_id: "total-fixed-100",
|
||||
description: "discount",
|
||||
}),
|
||||
],
|
||||
}),
|
||||
])
|
||||
})
|
||||
|
||||
it("adds line item to cart containing a total percentage discount", async () => {
|
||||
const api = useApi()
|
||||
|
||||
const response = await api
|
||||
.post(
|
||||
"/store/carts/test-cart-w-total-percentage-discount/line-items",
|
||||
{
|
||||
variant_id: "test-variant-quantity",
|
||||
quantity: 2,
|
||||
},
|
||||
{ withCredentials: true }
|
||||
)
|
||||
.catch((err) => console.log(err))
|
||||
|
||||
expect(response.data.cart.items).toEqual([
|
||||
expect.objectContaining({
|
||||
cart_id: "test-cart-w-total-percentage-discount",
|
||||
unit_price: 1000,
|
||||
variant_id: "test-variant-quantity",
|
||||
quantity: 2,
|
||||
adjustments: [
|
||||
expect.objectContaining({
|
||||
amount: 200,
|
||||
discount_id: "10Percent",
|
||||
description: "discount",
|
||||
}),
|
||||
],
|
||||
}),
|
||||
])
|
||||
})
|
||||
|
||||
it("adds line item to cart containing an item fixed discount", async () => {
|
||||
const api = useApi()
|
||||
|
||||
const response = await api
|
||||
.post(
|
||||
"/store/carts/test-cart-w-item-fixed-discount/line-items",
|
||||
{
|
||||
variant_id: "test-variant-quantity",
|
||||
quantity: 2,
|
||||
},
|
||||
{ withCredentials: true }
|
||||
)
|
||||
.catch((err) => console.log(err))
|
||||
|
||||
expect(response.data.cart.items).toEqual([
|
||||
expect.objectContaining({
|
||||
cart_id: "test-cart-w-item-fixed-discount",
|
||||
unit_price: 1000,
|
||||
variant_id: "test-variant-quantity",
|
||||
quantity: 2,
|
||||
adjustments: [
|
||||
expect.objectContaining({
|
||||
amount: 400,
|
||||
discount_id: "item-fixed-200",
|
||||
description: "discount",
|
||||
}),
|
||||
],
|
||||
}),
|
||||
])
|
||||
})
|
||||
|
||||
it("adds line item to cart containing an item percentage discount", async () => {
|
||||
const api = useApi()
|
||||
|
||||
const response = await api
|
||||
.post(
|
||||
"/store/carts/test-cart-w-item-percentage-discount/line-items",
|
||||
{
|
||||
variant_id: "test-variant-quantity",
|
||||
quantity: 2,
|
||||
},
|
||||
{ withCredentials: true }
|
||||
)
|
||||
.catch((err) => console.log(err))
|
||||
|
||||
expect(response.data.cart.items).toEqual([
|
||||
expect.objectContaining({
|
||||
cart_id: "test-cart-w-item-percentage-discount",
|
||||
unit_price: 1000,
|
||||
variant_id: "test-variant-quantity",
|
||||
quantity: 2,
|
||||
adjustments: [
|
||||
expect.objectContaining({
|
||||
amount: 300,
|
||||
discount_id: "item-percentage-15",
|
||||
description: "discount",
|
||||
}),
|
||||
],
|
||||
}),
|
||||
])
|
||||
})
|
||||
@@ -242,7 +367,6 @@ describe("/store/carts", () => {
|
||||
it("adds line item to cart time limited sale", async () => {
|
||||
const api = useApi()
|
||||
|
||||
// Add standard line item to cart
|
||||
const response = await api
|
||||
.post(
|
||||
"/store/carts/test-cart/line-items",
|
||||
@@ -305,7 +429,6 @@ describe("/store/carts", () => {
|
||||
it("adds line item with quantity to cart with quantity discount", async () => {
|
||||
const api = useApi()
|
||||
|
||||
// Add standard line item to cart
|
||||
const response = await api
|
||||
.post(
|
||||
"/store/carts/test-cart/line-items",
|
||||
@@ -330,7 +453,6 @@ describe("/store/carts", () => {
|
||||
it("adds line item with quantity to cart with quantity discount no ceiling", async () => {
|
||||
const api = useApi()
|
||||
|
||||
// Add standard line item to cart
|
||||
const response = await api
|
||||
.post(
|
||||
"/store/carts/test-cart/line-items",
|
||||
@@ -351,6 +473,421 @@ describe("/store/carts", () => {
|
||||
}),
|
||||
])
|
||||
})
|
||||
|
||||
describe("ensures correct line item adjustment generation", () => {
|
||||
const discountData = {
|
||||
code: "MEDUSA185DKK",
|
||||
id: "medusa-185",
|
||||
rule: {
|
||||
allocation: "total",
|
||||
type: "fixed",
|
||||
value: 185,
|
||||
},
|
||||
regions: ["test-region"],
|
||||
}
|
||||
|
||||
let discountCart, discount
|
||||
beforeEach(async () => {
|
||||
try {
|
||||
discount = await simpleDiscountFactory(
|
||||
dbConnection,
|
||||
discountData,
|
||||
100
|
||||
)
|
||||
discountCart = await simpleCartFactory(
|
||||
dbConnection,
|
||||
{
|
||||
id: "discount-cart",
|
||||
customer: "test-customer",
|
||||
region: "test-region",
|
||||
shipping_address: {
|
||||
address_1: "next door",
|
||||
first_name: "lebron",
|
||||
last_name: "james",
|
||||
country_code: "dk",
|
||||
postal_code: "100",
|
||||
},
|
||||
line_items: [
|
||||
{
|
||||
id: "test-li",
|
||||
variant_id: "test-variant",
|
||||
quantity: 1,
|
||||
unit_price: 100,
|
||||
adjustments: [
|
||||
{
|
||||
amount: 185,
|
||||
description: "discount",
|
||||
discount_id: "medusa-185",
|
||||
},
|
||||
],
|
||||
},
|
||||
],
|
||||
shipping_methods: [
|
||||
{
|
||||
shipping_option: "test-option",
|
||||
price: 1000,
|
||||
},
|
||||
],
|
||||
},
|
||||
100
|
||||
)
|
||||
await dbConnection.manager
|
||||
.createQueryBuilder()
|
||||
.relation(Cart, "discounts")
|
||||
.of(discountCart)
|
||||
.add(discount)
|
||||
} catch (err) {
|
||||
console.log(err)
|
||||
}
|
||||
})
|
||||
|
||||
afterEach(async () => {
|
||||
await doAfterEach()
|
||||
})
|
||||
|
||||
it("updates an old line item adjustment when a new line item is added to a discount cart", async () => {
|
||||
const api = useApi()
|
||||
|
||||
const response = await api
|
||||
.post(
|
||||
"/store/carts/discount-cart/line-items",
|
||||
{
|
||||
quantity: 1,
|
||||
variant_id: "test-variant-quantity",
|
||||
},
|
||||
{
|
||||
withCredentials: true,
|
||||
}
|
||||
)
|
||||
.catch((err) => console.log(err))
|
||||
|
||||
expect(response.data.cart.items.length).toEqual(2)
|
||||
expect(response.data.cart.items).toEqual(
|
||||
expect.arrayContaining([
|
||||
expect.objectContaining({
|
||||
adjustments: [
|
||||
expect.objectContaining({
|
||||
item_id: "test-li",
|
||||
amount: 17,
|
||||
discount_id: "medusa-185",
|
||||
}),
|
||||
],
|
||||
}),
|
||||
expect.objectContaining({
|
||||
adjustments: [
|
||||
expect.objectContaining({
|
||||
amount: 168,
|
||||
discount_id: "medusa-185",
|
||||
}),
|
||||
],
|
||||
}),
|
||||
])
|
||||
)
|
||||
})
|
||||
|
||||
it("updates an existing item adjustment when a line item is updated", async () => {
|
||||
const api = useApi()
|
||||
|
||||
await simpleLineItemFactory(
|
||||
dbConnection,
|
||||
{
|
||||
id: "line-item-2",
|
||||
cart_id: discountCart.id,
|
||||
variant_id: "test-variant-quantity",
|
||||
unit_price: 950,
|
||||
quantity: 1,
|
||||
adjustments: [
|
||||
{
|
||||
id: "lia-2",
|
||||
amount: 92,
|
||||
description: "discount",
|
||||
discount_id: "medusa-185",
|
||||
},
|
||||
],
|
||||
},
|
||||
100
|
||||
)
|
||||
|
||||
const response = await api
|
||||
.post(
|
||||
"/store/carts/discount-cart/line-items/line-item-2",
|
||||
{
|
||||
quantity: 2,
|
||||
},
|
||||
{
|
||||
withCredentials: true,
|
||||
}
|
||||
)
|
||||
.catch((err) => console.log(err))
|
||||
|
||||
expect(response.data.cart.items.length).toEqual(2)
|
||||
expect(response.data.cart.items).toEqual(
|
||||
expect.arrayContaining([
|
||||
expect.objectContaining({
|
||||
adjustments: [
|
||||
expect.objectContaining({
|
||||
item_id: "test-li",
|
||||
discount_id: "medusa-185",
|
||||
amount: 9,
|
||||
}),
|
||||
],
|
||||
}),
|
||||
expect.objectContaining({
|
||||
adjustments: [
|
||||
expect.objectContaining({
|
||||
item_id: "line-item-2",
|
||||
amount: 176,
|
||||
discount_id: "medusa-185",
|
||||
}),
|
||||
],
|
||||
}),
|
||||
])
|
||||
)
|
||||
})
|
||||
|
||||
it("updates an existing item adjustment when a line item is deleted from a discount cart", async () => {
|
||||
const api = useApi()
|
||||
|
||||
await simpleLineItemFactory(
|
||||
dbConnection,
|
||||
{
|
||||
id: "line-item-2",
|
||||
cart_id: discountCart.id,
|
||||
variant_id: "test-variant-quantity",
|
||||
unit_price: 1000,
|
||||
quantity: 1,
|
||||
adjustments: [
|
||||
{
|
||||
id: "lia-2",
|
||||
amount: 93,
|
||||
description: "discount",
|
||||
discount_id: "medusa-185",
|
||||
},
|
||||
],
|
||||
},
|
||||
100
|
||||
)
|
||||
|
||||
const response = await api
|
||||
.delete("/store/carts/discount-cart/line-items/test-li", {
|
||||
withCredentials: true,
|
||||
})
|
||||
.catch((err) => console.log(err))
|
||||
|
||||
expect(response.data.cart.items.length).toEqual(1)
|
||||
expect(response.data.cart.items).toEqual([
|
||||
expect.objectContaining({
|
||||
adjustments: [
|
||||
expect.objectContaining({
|
||||
item_id: "line-item-2",
|
||||
amount: 185,
|
||||
discount_id: "medusa-185",
|
||||
}),
|
||||
],
|
||||
}),
|
||||
])
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
describe("POST /store/carts/:id/line-items/:line_id", () => {
|
||||
beforeEach(async () => {
|
||||
try {
|
||||
await cartSeeder(dbConnection)
|
||||
await swapSeeder(dbConnection)
|
||||
} catch (err) {
|
||||
console.log(err)
|
||||
throw err
|
||||
}
|
||||
})
|
||||
|
||||
afterEach(async () => {
|
||||
await doAfterEach()
|
||||
})
|
||||
|
||||
it("updates line item of cart", async () => {
|
||||
const api = useApi()
|
||||
|
||||
const response = await api
|
||||
.post(
|
||||
"/store/carts/test-cart-3/line-items/test-item3/",
|
||||
{
|
||||
quantity: 3,
|
||||
},
|
||||
{ withCredentials: true }
|
||||
)
|
||||
.catch((err) => console.log(err))
|
||||
|
||||
expect(response.data.cart.items).toEqual([
|
||||
expect.objectContaining({
|
||||
cart_id: "test-cart-3",
|
||||
unit_price: 8000,
|
||||
variant_id: "test-variant-sale-cg",
|
||||
quantity: 3,
|
||||
adjustments: [],
|
||||
}),
|
||||
])
|
||||
})
|
||||
|
||||
it("updates line item of a cart containing a total fixed discount", async () => {
|
||||
const api = useApi()
|
||||
await simpleLineItemFactory(dbConnection, {
|
||||
id: "test-li-disc",
|
||||
allow_discounts: true,
|
||||
title: "Line Item Disc",
|
||||
thumbnail: "https://test.js/1234",
|
||||
unit_price: 1000,
|
||||
quantity: 1,
|
||||
variant_id: "test-variant-quantity",
|
||||
cart_id: "test-cart-w-total-fixed-discount",
|
||||
})
|
||||
|
||||
const response = await api
|
||||
.post(
|
||||
`store/carts/test-cart-w-total-fixed-discount/line-items/test-li-disc`,
|
||||
{
|
||||
quantity: 3,
|
||||
},
|
||||
{ withCredentials: true }
|
||||
)
|
||||
.catch((err) => console.log(err))
|
||||
|
||||
expect(response.data.cart.items).toEqual([
|
||||
expect.objectContaining({
|
||||
cart_id: "test-cart-w-total-fixed-discount",
|
||||
unit_price: 1000,
|
||||
variant_id: "test-variant-quantity",
|
||||
quantity: 3,
|
||||
adjustments: [
|
||||
expect.objectContaining({
|
||||
amount: 100,
|
||||
discount_id: "total-fixed-100",
|
||||
description: "discount",
|
||||
}),
|
||||
],
|
||||
}),
|
||||
])
|
||||
})
|
||||
|
||||
it("updates line item of a cart containing a total percentage discount", async () => {
|
||||
const api = useApi()
|
||||
await simpleLineItemFactory(dbConnection, {
|
||||
id: "test-li-disc",
|
||||
allow_discounts: true,
|
||||
title: "Line Item Disc",
|
||||
thumbnail: "https://test.js/1234",
|
||||
unit_price: 1000,
|
||||
quantity: 1,
|
||||
variant_id: "test-variant-quantity",
|
||||
cart_id: "test-cart-w-total-percentage-discount",
|
||||
})
|
||||
|
||||
const response = await api
|
||||
.post(
|
||||
"/store/carts/test-cart-w-total-percentage-discount/line-items/test-li-disc",
|
||||
{
|
||||
quantity: 10,
|
||||
},
|
||||
{ withCredentials: true }
|
||||
)
|
||||
.catch((err) => console.log(err))
|
||||
|
||||
expect(response.data.cart.items).toEqual([
|
||||
expect.objectContaining({
|
||||
cart_id: "test-cart-w-total-percentage-discount",
|
||||
unit_price: 1000,
|
||||
variant_id: "test-variant-quantity",
|
||||
quantity: 10,
|
||||
adjustments: [
|
||||
expect.objectContaining({
|
||||
amount: 1000,
|
||||
discount_id: "10Percent",
|
||||
description: "discount",
|
||||
}),
|
||||
],
|
||||
}),
|
||||
])
|
||||
})
|
||||
|
||||
it("updates line item of a cart containing an item fixed discount", async () => {
|
||||
const api = useApi()
|
||||
await simpleLineItemFactory(dbConnection, {
|
||||
id: "test-li-disc",
|
||||
allow_discounts: true,
|
||||
title: "Line Item Disc",
|
||||
thumbnail: "https://test.js/1234",
|
||||
unit_price: 1000,
|
||||
quantity: 1,
|
||||
variant_id: "test-variant-quantity",
|
||||
cart_id: "test-cart-w-item-fixed-discount",
|
||||
})
|
||||
|
||||
const response = await api
|
||||
.post(
|
||||
"/store/carts/test-cart-w-item-fixed-discount/line-items/test-li-disc",
|
||||
{
|
||||
quantity: 4,
|
||||
},
|
||||
{ withCredentials: true }
|
||||
)
|
||||
.catch((err) => console.log(err))
|
||||
|
||||
expect(response.data.cart.items).toEqual([
|
||||
expect.objectContaining({
|
||||
cart_id: "test-cart-w-item-fixed-discount",
|
||||
unit_price: 1000,
|
||||
variant_id: "test-variant-quantity",
|
||||
quantity: 4,
|
||||
adjustments: [
|
||||
expect.objectContaining({
|
||||
amount: 800,
|
||||
discount_id: "item-fixed-200",
|
||||
description: "discount",
|
||||
}),
|
||||
],
|
||||
}),
|
||||
])
|
||||
})
|
||||
|
||||
it("updates line item of a cart containing an item percentage discount", async () => {
|
||||
const api = useApi()
|
||||
await simpleLineItemFactory(dbConnection, {
|
||||
id: "test-li-disc",
|
||||
allow_discounts: true,
|
||||
title: "Line Item Disc",
|
||||
thumbnail: "https://test.js/1234",
|
||||
unit_price: 1000,
|
||||
quantity: 1,
|
||||
variant_id: "test-variant-quantity",
|
||||
cart_id: "test-cart-w-item-percentage-discount",
|
||||
})
|
||||
|
||||
const response = await api
|
||||
.post(
|
||||
"/store/carts/test-cart-w-item-percentage-discount/line-items/test-li-disc",
|
||||
{
|
||||
quantity: 3,
|
||||
},
|
||||
{ withCredentials: true }
|
||||
)
|
||||
.catch((err) => console.log(err))
|
||||
|
||||
expect(response.data.cart.items).toEqual([
|
||||
expect.objectContaining({
|
||||
cart_id: "test-cart-w-item-percentage-discount",
|
||||
unit_price: 1000,
|
||||
variant_id: "test-variant-quantity",
|
||||
quantity: 3,
|
||||
adjustments: [
|
||||
expect.objectContaining({
|
||||
amount: 450,
|
||||
discount_id: "item-percentage-15",
|
||||
description: "discount",
|
||||
}),
|
||||
],
|
||||
}),
|
||||
])
|
||||
})
|
||||
})
|
||||
|
||||
describe("POST /store/carts/:id", () => {
|
||||
@@ -1202,6 +1739,22 @@ describe("/store/carts", () => {
|
||||
.catch((err) => console.log(err))
|
||||
|
||||
// Ensure that the discount is only applied to the standard item
|
||||
const itemId = cartWithGiftcard.data.cart.items[0].id
|
||||
expect(cartWithGiftcard.data.cart.items).toEqual(
|
||||
expect.arrayContaining([
|
||||
expect.objectContaining({
|
||||
variant_id: "test-variant",
|
||||
quantity: 1,
|
||||
adjustments: [
|
||||
expect.objectContaining({
|
||||
discount_id: "10Percent",
|
||||
amount: 100,
|
||||
item_id: itemId,
|
||||
}),
|
||||
],
|
||||
}),
|
||||
])
|
||||
)
|
||||
expect(cartWithGiftcard.data.cart.total).toBe(1900) // 1000 (giftcard) + 900 (standard item with 10% discount)
|
||||
expect(cartWithGiftcard.data.cart.discount_total).toBe(100)
|
||||
expect(cartWithGiftcard.status).toEqual(200)
|
||||
|
||||
Reference in New Issue
Block a user