fix(medusa): Applying Discounts (with Conditions) on DraftOrders and Carts (#3197)

This commit is contained in:
Adrien de Peretti authored and GitHub committed 2023-02-08 19:01:23 +01:00
1 parent 4d3210bfbb
commit bfa33f444c
14 files changed
+636 -365

No files matched your search

@@ -3,6 +3,7 @@ import { FlagRouter } from "../../utils/flag-router"
import DiscountService from "../discount"
import { TotalsServiceMock } from "../__mocks__/totals"
import { newTotalsServiceMock } from "../__mocks__/new-totals"
import { In } from "typeorm"
const featureFlagRouter = new FlagRouter({})
@@ -54,14 +55,16 @@ describe("DiscountService", () => {
})
it("fails to create a discount without regions", async () => {
const err = await discountService.create({
code: "test",
rule: {
type: "fixed",
allocation: "total",
value: 20,
},
}).catch(e => e)
const err = await discountService
.create({
code: "test",
rule: {
type: "fixed",
allocation: "total",
value: 20,
},
})
.catch((e) => e)
expect(err.type).toEqual("invalid_data")
expect(err.message).toEqual("Discount must have atleast 1 region")
@@ -237,7 +240,6 @@ describe("DiscountService", () => {
expect(discountRepository.findOne).toHaveBeenCalledWith({
where: {
code: "10%OFF",
is_dynamic: false,
},
})
})
@@ -248,7 +250,54 @@ describe("DiscountService", () => {
expect(discountRepository.findOne).toHaveBeenCalledWith({
where: {
code: "10%OFF",
is_dynamic: false,
},
})
})
})
describe("listByCodes", () => {
const discountRepository = MockRepository({
find: (query) => {
if (query.where.code.value.includes("10%OFF")) {
return Promise.resolve([
{ id: IdMap.getId("total10"), code: "10%OFF" },
])
}
if (query.where.code.value.includes("DYNAMIC")) {
return Promise.resolve([
{ id: IdMap.getId("total10"), code: "10%OFF" },
])
}
return Promise.resolve([])
},
})
const discountService = new DiscountService({
manager: MockManager,
discountRepository,
featureFlagRouter,
})
beforeEach(() => {
jest.clearAllMocks()
})
it("successfully finds discount by code", async () => {
await discountService.listByCodes(["10%OFF"])
expect(discountRepository.find).toHaveBeenCalledTimes(1)
expect(discountRepository.find).toHaveBeenCalledWith({
where: {
code: In(["10%OFF"]),
},
})
})
it("successfully trims, uppdercases, and finds discount by code", async () => {
await discountService.listByCodes([" 10%Off "])
expect(discountRepository.find).toHaveBeenCalledTimes(1)
expect(discountRepository.find).toHaveBeenCalledWith({
where: {
code: In(["10%OFF"]),
},
})
})
@@ -746,6 +795,7 @@ describe("DiscountService", () => {
})
describe("validateDiscountForCartOrThrow", () => {
const discount = { code: "TEST" }
const getCart = (id) => {
if (id === "with-d") {
return {
@@ -878,50 +928,46 @@ describe("DiscountService", () => {
})
it("throws when hasReachedLimit returns true", async () => {
const discount = {}
const cart = getCart("with-d-and-customer")
discountService.hasReachedLimit = jest.fn().mockImplementation(() => true)
expect(
discountService.validateDiscountForCartOrThrow(cart, discount)
).rejects.toThrow({
message: "Discount has been used maximum allowed times",
message: `Discount ${discount.code} has been used maximum allowed times`,
})
})
it("throws when hasNotStarted returns true", async () => {
const discount = {}
const cart = getCart("with-d-and-customer")
discountService.hasNotStarted = jest.fn().mockImplementation(() => true)
expect(
discountService.validateDiscountForCartOrThrow(cart, discount)
).rejects.toThrow({
message: "Discount is not valid yet",
message: `Discount ${discount.code} is not valid yet`,
})
})
it("throws when hasExpired returns true", async () => {
const discount = {}
const cart = getCart("with-d-and-customer")
discountService.hasExpired = jest.fn().mockImplementation(() => true)
expect(
discountService.validateDiscountForCartOrThrow(cart, discount)
).rejects.toThrow({
message: "Discount is expired",
message: `Discount ${discount.code} is expired`,
})
})
it("throws when isDisabled returns true", async () => {
const discount = {}
const cart = getCart("with-d-and-customer")
discountService.isDisabled = jest.fn().mockImplementation(() => true)
expect(
discountService.validateDiscountForCartOrThrow(cart, discount)
).rejects.toThrow({
message: "The discount code is disabled",
message: `The discount code ${discount.code} is disabled`,
})
})
@@ -940,16 +986,16 @@ describe("DiscountService", () => {
})
it("throws when canApplyForCustomer returns false", async () => {
const discount = { rule: { id: "" } }
const discount_ = { code: discount.code, rule: { id: "" } }
const cart = getCart("with-d-and-customer")
discountService.canApplyForCustomer = jest
.fn()
.mockImplementation(() => Promise.resolve(false))
expect(
discountService.validateDiscountForCartOrThrow(cart, discount)
discountService.validateDiscountForCartOrThrow(cart, discount_)
).rejects.toThrow({
message: "Discount is not valid for customer",
message: `Discount ${discount.code} is not valid for customer`,
})
})
})