diff --git a/.changeset/famous-jeans-cheat.md b/.changeset/famous-jeans-cheat.md new file mode 100644 index 0000000000..aedc66d7b3 --- /dev/null +++ b/.changeset/famous-jeans-cheat.md @@ -0,0 +1,5 @@ +--- +"@medusajs/medusa": patch +--- + +feat(medusa): Trim discount code on insert and retrieve diff --git a/integration-tests/api/__tests__/admin/discount.js b/integration-tests/api/__tests__/admin/discount.js index 1b7832ffe4..d4821d081d 100644 --- a/integration-tests/api/__tests__/admin/discount.js +++ b/integration-tests/api/__tests__/admin/discount.js @@ -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", + }) + ) + }) }) }) diff --git a/packages/medusa/src/models/discount.ts b/packages/medusa/src/models/discount.ts index ae0c7d8fab..acdbc2084a 100644 --- a/packages/medusa/src/models/discount.ts +++ b/packages/medusa/src/models/discount.ts @@ -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 @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") } diff --git a/packages/medusa/src/services/__tests__/discount.js b/packages/medusa/src/services/__tests__/discount.js index c35328a81f..8a3f5fbca6 100644 --- a/packages/medusa/src/services/__tests__/discount.js +++ b/packages/medusa/src/services/__tests__/discount.js @@ -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", () => { diff --git a/packages/medusa/src/services/discount.ts b/packages/medusa/src/services/discount.ts index 3c0cfca7e7..8f713289d5 100644 --- a/packages/medusa/src/services/discount.ts +++ b/packages/medusa/src/services/discount.ts @@ -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 ) @@ -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)