feat(medusa): Emit "discount.created" event when discount is created (#5816)

This commit is contained in:
Zarar Siddiqi
2023-12-12 04:52:57 -05:00
committed by GitHub
parent 079f0da83f
commit 6f96ced40f
3 changed files with 22 additions and 0 deletions

View File

@@ -0,0 +1,5 @@
---
"@medusajs/medusa": patch
---
Emit event when discount is created

View File

@@ -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({})
@@ -29,6 +36,7 @@ describe("DiscountService", () => {
discountRuleRepository,
regionService,
featureFlagRouter,
eventBusService
})
beforeEach(() => {
@@ -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 () => {

View File

@@ -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
@@ -231,6 +234,10 @@ class DiscountService extends TransactionBaseService {
)
}
await this.eventBus_
.withTransaction(manager)
.emit(DiscountService.Events.CREATED, { id: result.id })
return result
})
}