add Medusa Cloud Email provider (#13781)
* add Medusa Cloud Email provider * move cloud config to project level * add tests * Create breezy-flowers-fly.md * rename medusa_cloud_config to cloud --------- Co-authored-by: Oli Juhl <59018053+olivermrbl@users.noreply.github.com>
This commit is contained in:
@@ -0,0 +1,49 @@
|
||||
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> {
|
||||
try {
|
||||
const response = await fetch(`${this.options_.endpoint}/send`, {
|
||||
method: "POST",
|
||||
headers: {
|
||||
"Content-Type": "application/json",
|
||||
Authorization: `Basic ${this.options_.api_key}`,
|
||||
"x-medusa-environment-handle": this.options_.environment_handle,
|
||||
},
|
||||
body: JSON.stringify({
|
||||
to: notification.to,
|
||||
from: notification.from,
|
||||
attachments: notification.attachments,
|
||||
template: notification.template,
|
||||
data: notification.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}`)
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user