Files
medusa-store/packages/modules/notification/src/providers/medusa-cloud-email.ts
Pedro Guzman b81f958d41 Add provider data to notifications (#14104)
## Summary

**What** —
Add a providerData field to notifications

**Why** —
We need the ability to pass dynamic fields to specific providers. E.g. CC and BCC for emails

**How** —
Just adding the field to the model

**Testing** —
Added the field to existing tests

## 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
- [ ] I have linked the related issue(s) if applicable



---

> [!NOTE]
> Adds `provider_data` to notifications (types, workflow input, DB model/migration) and forwards it in the Medusa Cloud Email provider, with tests updated accordingly.
> 
> - **Data/Schema**:
>   - Add `provider_data` (`jsonb`) to `notification` model and DB via migration `Migration20251121150408` and snapshot update.
> - **Types/DTOs**:
>   - Add optional `provider_data` to `CreateNotificationDTO`, `NotificationDTO`, and `ProviderSendNotificationDTO`.
> - **Workflows**:
>   - Extend `send-notifications` step input with `provider_data`.
> - **Providers**:
>   - Medusa Cloud Email: include `provider_data` in outgoing request payload.
> - **Tests**:
>   - Update integration tests to pass and assert `provider_data` propagation.
> - **Changeset**:
>   - Patch bumps for `@medusajs/notification`, `@medusajs/core-flows`, `@medusajs/types`.
> 
> <sup>Written by [Cursor Bugbot](https://cursor.com/dashboard?tab=bugbot) for commit 6f114c75c974a145ef60213637d7c41bc605a0bf. This will update automatically on new commits. Configure [here](https://cursor.com/dashboard?tab=bugbot).</sup>
2025-11-24 09:18:47 +00:00

60 lines
1.8 KiB
TypeScript

import { Logger, NotificationTypes } from "@medusajs/framework/types"
import { AbstractNotificationProviderService } from "@medusajs/framework/utils"
import { MedusaCloudEmailOptions } from "@types"
export class MedusaCloudEmailNotificationProvider extends AbstractNotificationProviderService {
static identifier = "notification-medusa-cloud-email"
protected options_: MedusaCloudEmailOptions
protected logger_: Logger
constructor({}, options: MedusaCloudEmailOptions) {
super()
this.options_ = options
}
async send(
notification: NotificationTypes.ProviderSendNotificationDTO
): Promise<NotificationTypes.ProviderSendNotificationResultsDTO> {
const headers = {
"Content-Type": "application/json",
Authorization: `Basic ${this.options_.api_key}`,
}
if (this.options_.sandbox_handle) {
headers["x-medusa-sandbox-handle"] = this.options_.sandbox_handle
}
if (this.options_.environment_handle) {
headers["x-medusa-environment-handle"] = this.options_.environment_handle
}
try {
const response = await fetch(`${this.options_.endpoint}/send`, {
method: "POST",
headers,
body: JSON.stringify({
to: notification.to,
from: notification.from,
attachments: notification.attachments,
template: notification.template,
data: notification.data,
provider_data: notification.provider_data,
content: notification.content,
}),
})
const responseBody = await response.json()
if (!response.ok) {
throw new Error(
`Failed to send email: ${response.status} - ${response.statusText}: ${responseBody.message}`
)
}
return { id: responseBody.id }
} catch (error) {
throw new Error(`Failed to send email: ${error.message}`)
}
}
}