feat(medusa): Emit "discount.created" event when discount is created (#5816)
This commit is contained in:
5
.changeset/twelve-parents-occur.md
Normal file
5
.changeset/twelve-parents-occur.md
Normal file
@@ -0,0 +1,5 @@
|
||||
---
|
||||
"@medusajs/medusa": patch
|
||||
---
|
||||
|
||||
Emit event when discount is created
|
||||
@@ -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 () => {
|
||||
|
||||
@@ -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
|
||||
})
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user