feat(medusa): Trim discount code on insert and retrieve (#2369)
This commit is contained in:
committed by
GitHub
parent
1c688ec499
commit
d2b272fab6
@@ -6,7 +6,7 @@ import {
|
||||
JoinColumn,
|
||||
JoinTable,
|
||||
ManyToMany,
|
||||
ManyToOne
|
||||
ManyToOne,
|
||||
} from "typeorm"
|
||||
import { DbAwareColumn, resolveDbType } from "../utils/db-aware-column"
|
||||
|
||||
@@ -78,9 +78,11 @@ export class Discount extends SoftDeletableEntity {
|
||||
metadata: Record<string, unknown>
|
||||
|
||||
@BeforeInsert()
|
||||
private upperCaseCode(): void {
|
||||
this.code = this.code.toUpperCase()
|
||||
if (this.id) return
|
||||
private upperCaseCodeAndTrim(): void {
|
||||
this.code = this.code.toUpperCase().trim()
|
||||
if (this.id) {
|
||||
return
|
||||
}
|
||||
|
||||
this.id = generateEntityId(this.id, "disc")
|
||||
}
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
import { IdMap, MockManager, MockRepository } from "medusa-test-utils"
|
||||
import DiscountService from "../discount"
|
||||
import { FlagRouter } from "../../utils/flag-router"
|
||||
import DiscountService from "../discount"
|
||||
|
||||
const featureFlagRouter = new FlagRouter({})
|
||||
|
||||
@@ -74,7 +74,7 @@ describe("DiscountService", () => {
|
||||
|
||||
expect(discountRepository.create).toHaveBeenCalledTimes(1)
|
||||
expect(discountRepository.create).toHaveBeenCalledWith({
|
||||
code: "TEST",
|
||||
code: "test",
|
||||
rule: expect.anything(),
|
||||
regions: [{ id: IdMap.getId("france") }],
|
||||
})
|
||||
@@ -106,7 +106,7 @@ describe("DiscountService", () => {
|
||||
|
||||
expect(discountRepository.create).toHaveBeenCalledTimes(1)
|
||||
expect(discountRepository.create).toHaveBeenCalledWith({
|
||||
code: "TEST",
|
||||
code: "test",
|
||||
rule: expect.anything(),
|
||||
regions: [{ id: IdMap.getId("france") }],
|
||||
starts_at: new Date("03/14/2021"),
|
||||
@@ -140,7 +140,7 @@ describe("DiscountService", () => {
|
||||
|
||||
expect(discountRepository.create).toHaveBeenCalledTimes(1)
|
||||
expect(discountRepository.create).toHaveBeenCalledWith({
|
||||
code: "TEST",
|
||||
code: "test",
|
||||
rule: expect.anything(),
|
||||
regions: [{ id: IdMap.getId("france") }],
|
||||
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", () => {
|
||||
|
||||
@@ -212,8 +212,6 @@ class DiscountService extends TransactionBaseService {
|
||||
const discountRule = ruleRepo.create(validatedRule)
|
||||
const createdDiscountRule = await ruleRepo.save(discountRule)
|
||||
|
||||
discount.code = discount.code!.toUpperCase()
|
||||
|
||||
const created: Discount = discountRepo.create(
|
||||
discount as DeepPartial<Discount>
|
||||
)
|
||||
@@ -277,7 +275,7 @@ class DiscountService extends TransactionBaseService {
|
||||
const manager = this.manager_
|
||||
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 discount = await discountRepo.findOne(query)
|
||||
|
||||
Reference in New Issue
Block a user