Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature: Emit "discount.created" event when discount is created #5816

Merged
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
47 changes: 47 additions & 0 deletions www/apps/docs/content/development/events/events-list.md
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Arsenalist let's remove the doc changes for now, as it will appear in the docs once the PR is merged, even if it's not released yet. I'll add it in after the release

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can alternatively add it in another PR that we merge once the release is out

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Arsenalist let's remove the doc changes for now, as it will appear in the docs once the PR is merged, even if it's not released yet. I'll add it in after the release

Hi, I have removed the doc changes. Will create another PR after it has been released.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would be great if you can create the PR now so that we can have it ready for release, and we'll merge it in time 😇

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would be great if you can create the PR now so that we can have it ready for release, and we'll merge it in time 😇

No problem, here it is: #5850

Original file line number Diff line number Diff line change
Expand Up @@ -705,6 +705,53 @@ Object of the following format:

---

## Discount Events

This section holds all events related to discounts.

<table class="reference-table">
<thead>
<tr>
<th>
Event Name
</th>
<th>
Description
</th>
<th>
Event Data Payload
</th>
</tr>
</thead>
<tbody>
<tr>
<td>

`discount.created`

</td>
<td>

Triggered when a discount is created.

</td>
<td>

Object of the following format:

```js noReport noCopy
{
id // string ID of discount
}
```

</td>
</tr>
</tbody>
</table>

---

## Draft Order Events

This section holds all events related to draft orders.
Expand Down