Skip to content

Commit

Permalink
feat(medusa): Emit "discount.created" event when discount is created (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
Arsenalist authored Dec 12, 2023
1 parent 079f0da commit 6f96ced
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 0 deletions.
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

0 comments on commit 6f96ced

Please sign in to comment.