diff --git a/.changeset/twelve-parents-occur.md b/.changeset/twelve-parents-occur.md new file mode 100644 index 0000000000000..a95fc0e368dcf --- /dev/null +++ b/.changeset/twelve-parents-occur.md @@ -0,0 +1,5 @@ +--- +"@medusajs/medusa": patch +--- + +Emit event when discount is created diff --git a/packages/medusa/src/services/__tests__/discount.js b/packages/medusa/src/services/__tests__/discount.js index 124de4463f350..58ade91a5e7e5 100644 --- a/packages/medusa/src/services/__tests__/discount.js +++ b/packages/medusa/src/services/__tests__/discount.js @@ -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 () => { diff --git a/packages/medusa/src/services/discount.ts b/packages/medusa/src/services/discount.ts index 0f7106b4156e5..5bc90eb67a481 100644 --- a/packages/medusa/src/services/discount.ts +++ b/packages/medusa/src/services/discount.ts @@ -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 }) }