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

Feature: Emit "discount.created" event when discount is created #5816

Merged
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/twelve-parents-occur.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@medusajs/medusa": patch
---

Emit event when discount is created
10 changes: 10 additions & 0 deletions packages/medusa/src/services/__tests__/discount.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,13 @@ import { In } from "typeorm"

const featureFlagRouter = new FlagRouter({})

const eventBusService = {
emit: jest.fn(),
withTransaction: function () {
return this
},
}

describe("DiscountService", () => {
describe("create", () => {
const discountRepository = MockRepository({})
Expand All @@ -29,6 +36,7 @@ describe("DiscountService", () => {
discountRuleRepository,
regionService,
featureFlagRouter,
eventBusService
})

beforeEach(() => {
Expand Down Expand Up @@ -99,6 +107,8 @@ describe("DiscountService", () => {
})

expect(discountRepository.save).toHaveBeenCalledTimes(1)
expect(eventBusService.emit).toHaveBeenCalledTimes(1)
expect(eventBusService.emit).toHaveBeenCalledWith(DiscountService.Events.CREATED, {id: undefined})
})

it("successfully creates discount with start and end dates", async () => {
Expand Down
7 changes: 7 additions & 0 deletions packages/medusa/src/services/discount.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,9 @@ import EventBusService from "./event-bus"
* @implements {BaseService}
*/
class DiscountService extends TransactionBaseService {
static readonly Events = {
CREATED: "discount.created",
}
protected readonly discountRepository_: typeof DiscountRepository
protected readonly customerService_: CustomerService
protected readonly discountRuleRepository_: typeof DiscountRuleRepository
Expand Down Expand Up @@ -231,6 +234,10 @@ class DiscountService extends TransactionBaseService {
)
}

await this.eventBus_
.withTransaction(manager)
.emit(DiscountService.Events.CREATED, { id: result.id })

return result
})
}
Expand Down