feat(medusa): Emit "discount.created" event when discount is created (#5816)
This commit is contained in:
@@ -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 featureFlagRouter = new FlagRouter({})
|
||||||
|
|
||||||
|
const eventBusService = {
|
||||||
|
emit: jest.fn(),
|
||||||
|
withTransaction: function () {
|
||||||
|
return this
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
describe("DiscountService", () => {
|
describe("DiscountService", () => {
|
||||||
describe("create", () => {
|
describe("create", () => {
|
||||||
const discountRepository = MockRepository({})
|
const discountRepository = MockRepository({})
|
||||||
@@ -29,6 +36,7 @@ describe("DiscountService", () => {
|
|||||||
discountRuleRepository,
|
discountRuleRepository,
|
||||||
regionService,
|
regionService,
|
||||||
featureFlagRouter,
|
featureFlagRouter,
|
||||||
|
eventBusService
|
||||||
})
|
})
|
||||||
|
|
||||||
beforeEach(() => {
|
beforeEach(() => {
|
||||||
@@ -99,6 +107,8 @@ describe("DiscountService", () => {
|
|||||||
})
|
})
|
||||||
|
|
||||||
expect(discountRepository.save).toHaveBeenCalledTimes(1)
|
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 () => {
|
it("successfully creates discount with start and end dates", async () => {
|
||||||
|
|||||||
@@ -54,6 +54,9 @@ import EventBusService from "./event-bus"
|
|||||||
* @implements {BaseService}
|
* @implements {BaseService}
|
||||||
*/
|
*/
|
||||||
class DiscountService extends TransactionBaseService {
|
class DiscountService extends TransactionBaseService {
|
||||||
|
static readonly Events = {
|
||||||
|
CREATED: "discount.created",
|
||||||
|
}
|
||||||
protected readonly discountRepository_: typeof DiscountRepository
|
protected readonly discountRepository_: typeof DiscountRepository
|
||||||
protected readonly customerService_: CustomerService
|
protected readonly customerService_: CustomerService
|
||||||
protected readonly discountRuleRepository_: typeof DiscountRuleRepository
|
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
|
return result
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user