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
@@ -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" })
|
||||
)
|
||||
})
|
||||
})
|
||||
})
|
||||
})
|
||||
})
|
||||
Reference in New Issue
Block a user