Commit 6a958d2
authored
feat: add support for custom arguments in Twilio Sendgrid email notifications (#14879)
## Summary
**What** — What changes are introduced in this PR?
*Added support for passing SendGrid personalizations directly via provider_data in the notification payload.*
**Why** — Why are these changes relevant or necessary?
*This allows callers to pass any SendGrid-native personalization data (e.g. customArgs for analytics/event tracking, per-recipient overrides, dynamic template data per recipient) without the provider needing to know about each specific field.*
**How** — How have these changes been implemented?
*Modified the SendgridNotificationService.send() method to check notification.provider_data.personalizations. When present and non-empty, it is forwarded directly to the SendGrid send() call and the top-level to field is omitted (since personalizations handles recipient routing). When absent or empty, the existing behavior using the top-level to field is preserved unchanged.*
**Testing** — How have these changes been tested, or how can the reviewer test the feature?
*Unit tests added in integration-tests/__tests__/services.spec.ts using a mocked @sendgrid/mail. Three cases are covered: personalizations present and forwarded, absent (falls back to top-level to), and empty array (also falls back to top-level to).*
resolves #15115
---
## Examples
Provide examples or code snippets that demonstrate how this feature works, or how it can be used in practice.
This helps with documentation and ensures maintainers can quickly understand and verify the change.
```ts
import type { SubscriberArgs, SubscriberConfig } from "@medusajs/framework"
import { Modules } from "@medusajs/framework/utils"
import { INotificationModuleService } from "@medusajs/framework/types"
export default async function productCreateHandler({
event: { data },
container,
}: SubscriberArgs<{ id: string }>) {
const notificationModuleService: INotificationModuleService =
container.resolve(Modules.NOTIFICATION)
const emailData = {
name: "Sample Product",
message: `New product created with id ${data.id}`,
}
const email = await notificationModuleService.createNotifications({
to: "xxxxx@xxxx.com",
channel: "email",
template: "xxxxxxxxxx",
data: emailData,
provider_data: {
personalizations: [
{
to: [{ email: "xxxxx@xxxx.com" }],
customArgs: {
customer_id: "cust_abc",
ga4_id: "GA1.2.xxxxx",
notification_type: "order_confirmation",
},
},
],
},
})
}
export const config: SubscriberConfig = {
event: "product.created",
}
```
---
## Checklist
Please ensure the following before requesting a review:
- [x] I have added a **changeset** for this PR
- Every non-breaking change should be marked as a **patch**
- To add a changeset, run `yarn changeset` and follow the prompts
- [x] The changes are covered by relevant **tests**
- [x] I have verified the code works as intended locally
- [x] I have linked the related issue(s) if applicable
---
## Additional Context
Add any additional context, related issues, or references that might help the reviewer understand this PR.
---
> [!NOTE]
> **Low Risk**
> Low risk: small, additive change to the SendGrid payload; main risk is passing unexpected `provider_data.custom_args` values, mitigated by coercing all values to strings.
>
> **Overview**
> Adds support for SendGrid `customArgs` by reading `notification.provider_data.custom_args`, coercing values to strings, and including the result in the outgoing `sendgrid.send()` message payload.
>
> Includes a changeset bumping `@medusajs/notification-sendgrid` as a minor release to document the new capability.
>
> <sup>Written by [Cursor Bugbot](https://cursor.com/dashboard?tab=bugbot) for commit 69b1ef0. This will update automatically on new commits. Configure [here](https://cursor.com/dashboard?tab=bugbot).</sup>1 parent c18750f commit 6a958d2
3 files changed
Lines changed: 104 additions & 1 deletion
File tree
- .changeset
- packages/modules/providers/notification-sendgrid
- integration-tests/__tests__
- src/services
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
Lines changed: 94 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
1 | 2 | | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
2 | 29 | | |
3 | 30 | | |
| 31 | + | |
| 32 | + | |
| 33 | + | |
| 34 | + | |
| 35 | + | |
| 36 | + | |
| 37 | + | |
| 38 | + | |
| 39 | + | |
| 40 | + | |
| 41 | + | |
| 42 | + | |
| 43 | + | |
| 44 | + | |
| 45 | + | |
| 46 | + | |
| 47 | + | |
| 48 | + | |
| 49 | + | |
| 50 | + | |
| 51 | + | |
| 52 | + | |
| 53 | + | |
| 54 | + | |
| 55 | + | |
| 56 | + | |
| 57 | + | |
| 58 | + | |
| 59 | + | |
| 60 | + | |
| 61 | + | |
| 62 | + | |
| 63 | + | |
| 64 | + | |
| 65 | + | |
| 66 | + | |
| 67 | + | |
| 68 | + | |
| 69 | + | |
| 70 | + | |
| 71 | + | |
| 72 | + | |
| 73 | + | |
| 74 | + | |
| 75 | + | |
| 76 | + | |
| 77 | + | |
| 78 | + | |
| 79 | + | |
| 80 | + | |
| 81 | + | |
| 82 | + | |
| 83 | + | |
| 84 | + | |
| 85 | + | |
| 86 | + | |
| 87 | + | |
| 88 | + | |
| 89 | + | |
| 90 | + | |
| 91 | + | |
| 92 | + | |
| 93 | + | |
| 94 | + | |
| 95 | + | |
| 96 | + | |
| 97 | + | |
4 | 98 | | |
5 | 99 | | |
6 | 100 | | |
| |||
Lines changed: 5 additions & 1 deletion
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
79 | 79 | | |
80 | 80 | | |
81 | 81 | | |
| 82 | + | |
| 83 | + | |
| 84 | + | |
| 85 | + | |
82 | 86 | | |
83 | | - | |
84 | 87 | | |
85 | 88 | | |
86 | 89 | | |
87 | 90 | | |
88 | 91 | | |
| 92 | + | |
89 | 93 | | |
90 | 94 | | |
91 | 95 | | |
| |||
0 commit comments