feat(medusa): Trim discount code on insert and retrieve (#2369)
This commit is contained in:
committed by
GitHub
parent
1c688ec499
commit
d2b272fab6
@@ -0,0 +1,5 @@
|
|||||||
|
---
|
||||||
|
"@medusajs/medusa": patch
|
||||||
|
---
|
||||||
|
|
||||||
|
feat(medusa): Trim discount code on insert and retrieve
|
||||||
@@ -2480,5 +2480,71 @@ describe("/admin/discounts", () => {
|
|||||||
)
|
)
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
|
it("should trim and uppercase code on insert", async () => {
|
||||||
|
const api = useApi()
|
||||||
|
|
||||||
|
const response = await api
|
||||||
|
.post(
|
||||||
|
"/admin/discounts",
|
||||||
|
{
|
||||||
|
code: " Testing ",
|
||||||
|
rule: {
|
||||||
|
description: "test",
|
||||||
|
type: "percentage",
|
||||||
|
value: 10,
|
||||||
|
allocation: "total",
|
||||||
|
},
|
||||||
|
usage_limit: 10,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
headers: {
|
||||||
|
Authorization: "Bearer test_token",
|
||||||
|
},
|
||||||
|
}
|
||||||
|
)
|
||||||
|
.catch((err) => {
|
||||||
|
console.log(err)
|
||||||
|
})
|
||||||
|
|
||||||
|
const disc = response.data.discount
|
||||||
|
expect(response.status).toEqual(200)
|
||||||
|
expect(disc).toEqual(
|
||||||
|
expect.objectContaining({
|
||||||
|
code: "TESTING",
|
||||||
|
})
|
||||||
|
)
|
||||||
|
})
|
||||||
|
|
||||||
|
it("should trim and uppercase code on retrieve", async () => {
|
||||||
|
const api = useApi()
|
||||||
|
|
||||||
|
await simpleDiscountFactory(dbConnection, {
|
||||||
|
code: "Testing",
|
||||||
|
rule: {
|
||||||
|
type: "percentage",
|
||||||
|
value: "10",
|
||||||
|
allocation: "total",
|
||||||
|
},
|
||||||
|
})
|
||||||
|
|
||||||
|
const response = await api
|
||||||
|
.get("/admin/discounts/code/ testing", {
|
||||||
|
headers: {
|
||||||
|
Authorization: "Bearer test_token",
|
||||||
|
},
|
||||||
|
})
|
||||||
|
.catch((err) => {
|
||||||
|
console.log(err)
|
||||||
|
})
|
||||||
|
|
||||||
|
const disc = response.data.discount
|
||||||
|
expect(response.status).toEqual(200)
|
||||||
|
expect(disc).toEqual(
|
||||||
|
expect.objectContaining({
|
||||||
|
code: "TESTING",
|
||||||
|
})
|
||||||
|
)
|
||||||
|
})
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|||||||
@@ -6,7 +6,7 @@ import {
|
|||||||
JoinColumn,
|
JoinColumn,
|
||||||
JoinTable,
|
JoinTable,
|
||||||
ManyToMany,
|
ManyToMany,
|
||||||
ManyToOne
|
ManyToOne,
|
||||||
} from "typeorm"
|
} from "typeorm"
|
||||||
import { DbAwareColumn, resolveDbType } from "../utils/db-aware-column"
|
import { DbAwareColumn, resolveDbType } from "../utils/db-aware-column"
|
||||||
|
|
||||||
@@ -78,9 +78,11 @@ export class Discount extends SoftDeletableEntity {
|
|||||||
metadata: Record<string, unknown>
|
metadata: Record<string, unknown>
|
||||||
|
|
||||||
@BeforeInsert()
|
@BeforeInsert()
|
||||||
private upperCaseCode(): void {
|
private upperCaseCodeAndTrim(): void {
|
||||||
this.code = this.code.toUpperCase()
|
this.code = this.code.toUpperCase().trim()
|
||||||
if (this.id) return
|
if (this.id) {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
this.id = generateEntityId(this.id, "disc")
|
this.id = generateEntityId(this.id, "disc")
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
import { IdMap, MockManager, MockRepository } from "medusa-test-utils"
|
import { IdMap, MockManager, MockRepository } from "medusa-test-utils"
|
||||||
import DiscountService from "../discount"
|
|
||||||
import { FlagRouter } from "../../utils/flag-router"
|
import { FlagRouter } from "../../utils/flag-router"
|
||||||
|
import DiscountService from "../discount"
|
||||||
|
|
||||||
const featureFlagRouter = new FlagRouter({})
|
const featureFlagRouter = new FlagRouter({})
|
||||||
|
|
||||||
@@ -74,7 +74,7 @@ describe("DiscountService", () => {
|
|||||||
|
|
||||||
expect(discountRepository.create).toHaveBeenCalledTimes(1)
|
expect(discountRepository.create).toHaveBeenCalledTimes(1)
|
||||||
expect(discountRepository.create).toHaveBeenCalledWith({
|
expect(discountRepository.create).toHaveBeenCalledWith({
|
||||||
code: "TEST",
|
code: "test",
|
||||||
rule: expect.anything(),
|
rule: expect.anything(),
|
||||||
regions: [{ id: IdMap.getId("france") }],
|
regions: [{ id: IdMap.getId("france") }],
|
||||||
})
|
})
|
||||||
@@ -106,7 +106,7 @@ describe("DiscountService", () => {
|
|||||||
|
|
||||||
expect(discountRepository.create).toHaveBeenCalledTimes(1)
|
expect(discountRepository.create).toHaveBeenCalledTimes(1)
|
||||||
expect(discountRepository.create).toHaveBeenCalledWith({
|
expect(discountRepository.create).toHaveBeenCalledWith({
|
||||||
code: "TEST",
|
code: "test",
|
||||||
rule: expect.anything(),
|
rule: expect.anything(),
|
||||||
regions: [{ id: IdMap.getId("france") }],
|
regions: [{ id: IdMap.getId("france") }],
|
||||||
starts_at: new Date("03/14/2021"),
|
starts_at: new Date("03/14/2021"),
|
||||||
@@ -140,7 +140,7 @@ describe("DiscountService", () => {
|
|||||||
|
|
||||||
expect(discountRepository.create).toHaveBeenCalledTimes(1)
|
expect(discountRepository.create).toHaveBeenCalledTimes(1)
|
||||||
expect(discountRepository.create).toHaveBeenCalledWith({
|
expect(discountRepository.create).toHaveBeenCalledWith({
|
||||||
code: "TEST",
|
code: "test",
|
||||||
rule: expect.anything(),
|
rule: expect.anything(),
|
||||||
regions: [{ id: IdMap.getId("france") }],
|
regions: [{ id: IdMap.getId("france") }],
|
||||||
starts_at: new Date("03/14/2021"),
|
starts_at: new Date("03/14/2021"),
|
||||||
@@ -225,6 +225,17 @@ describe("DiscountService", () => {
|
|||||||
},
|
},
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
|
it("successfully trims, uppdercases, and finds discount by code", async () => {
|
||||||
|
await discountService.retrieveByCode(" 10%Off ")
|
||||||
|
expect(discountRepository.findOne).toHaveBeenCalledTimes(1)
|
||||||
|
expect(discountRepository.findOne).toHaveBeenCalledWith({
|
||||||
|
where: {
|
||||||
|
code: "10%OFF",
|
||||||
|
is_dynamic: false,
|
||||||
|
},
|
||||||
|
})
|
||||||
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
describe("update", () => {
|
describe("update", () => {
|
||||||
|
|||||||
@@ -212,8 +212,6 @@ class DiscountService extends TransactionBaseService {
|
|||||||
const discountRule = ruleRepo.create(validatedRule)
|
const discountRule = ruleRepo.create(validatedRule)
|
||||||
const createdDiscountRule = await ruleRepo.save(discountRule)
|
const createdDiscountRule = await ruleRepo.save(discountRule)
|
||||||
|
|
||||||
discount.code = discount.code!.toUpperCase()
|
|
||||||
|
|
||||||
const created: Discount = discountRepo.create(
|
const created: Discount = discountRepo.create(
|
||||||
discount as DeepPartial<Discount>
|
discount as DeepPartial<Discount>
|
||||||
)
|
)
|
||||||
@@ -277,7 +275,7 @@ class DiscountService extends TransactionBaseService {
|
|||||||
const manager = this.manager_
|
const manager = this.manager_
|
||||||
const discountRepo = manager.getCustomRepository(this.discountRepository_)
|
const discountRepo = manager.getCustomRepository(this.discountRepository_)
|
||||||
|
|
||||||
const normalizedCode = discountCode.toUpperCase()
|
const normalizedCode = discountCode.toUpperCase().trim()
|
||||||
|
|
||||||
let query = buildQuery({ code: normalizedCode, is_dynamic: false }, config)
|
let query = buildQuery({ code: normalizedCode, is_dynamic: false }, config)
|
||||||
let discount = await discountRepo.findOne(query)
|
let discount = await discountRepo.findOne(query)
|
||||||
|
|||||||
Reference in New Issue
Block a user