Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(medusa): Trim discount code on insert and retrieve #2369

Merged
merged 4 commits into from
Oct 7, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/famous-jeans-cheat.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@medusajs/medusa": patch
---

feat(medusa): Trim discount code on insert and retrieve
66 changes: 66 additions & 0 deletions integration-tests/api/__tests__/admin/discount.js
Original file line number Diff line number Diff line change
Expand Up @@ -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",
})
)
})
})
})
10 changes: 6 additions & 4 deletions packages/medusa/src/models/discount.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import {
JoinColumn,
JoinTable,
ManyToMany,
ManyToOne
ManyToOne,
} from "typeorm"
import { DbAwareColumn, resolveDbType } from "../utils/db-aware-column"

Expand Down Expand Up @@ -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")
}
Expand Down
19 changes: 15 additions & 4 deletions packages/medusa/src/services/__tests__/discount.js
Original file line number Diff line number Diff line change
@@ -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({})

Expand Down Expand Up @@ -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") }],
})
Expand Down Expand Up @@ -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"),
Expand Down Expand Up @@ -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"),
Expand Down Expand Up @@ -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", () => {
Expand Down
4 changes: 1 addition & 3 deletions packages/medusa/src/services/discount.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

comment: Removed from create as we are using beforeInsert in the model

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

question: why is this removed?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry didnt see your comment

discount as DeepPartial<Discount>
)
Expand Down Expand Up @@ -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()
Copy link
Contributor Author

@olivermrbl olivermrbl Oct 6, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

comment: This is in retrieveByCode


let query = buildQuery({ code: normalizedCode, is_dynamic: false }, config)
let discount = await discountRepo.findOne(query)
Expand Down