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:
@@ -1,9 +1,6 @@
|
||||
import { Lifetime, asFunction, asValue } from "@medusajs/framework/awilix"
|
||||
import { moduleProviderLoader } from "@medusajs/framework/modules-sdk"
|
||||
import {
|
||||
LoaderOptions,
|
||||
ModuleProvider,
|
||||
ModulesSdkTypes,
|
||||
} from "@medusajs/framework/types"
|
||||
import { LoaderOptions, ModulesSdkTypes } from "@medusajs/framework/types"
|
||||
import {
|
||||
ContainerRegistrationKeys,
|
||||
lowerCaseFirst,
|
||||
@@ -13,9 +10,10 @@ import { NotificationProvider } from "@models"
|
||||
import { NotificationProviderService } from "@services"
|
||||
import {
|
||||
NotificationIdentifiersRegistrationName,
|
||||
NotificationModuleOptions,
|
||||
NotificationProviderRegistrationPrefix,
|
||||
} from "@types"
|
||||
import { Lifetime, asFunction, asValue } from "@medusajs/framework/awilix"
|
||||
import { MedusaCloudEmailNotificationProvider } from "../providers/medusa-cloud-email"
|
||||
|
||||
const registrationFn = async (klass, container, pluginOptions) => {
|
||||
container.register({
|
||||
@@ -40,8 +38,34 @@ export default async ({
|
||||
(
|
||||
| ModulesSdkTypes.ModuleServiceInitializeOptions
|
||||
| ModulesSdkTypes.ModuleServiceInitializeCustomDataLayerOptions
|
||||
) & { providers: ModuleProvider[] }
|
||||
) &
|
||||
NotificationModuleOptions
|
||||
>): Promise<void> => {
|
||||
let providers = options?.providers || []
|
||||
|
||||
// We add the Medusa Cloud Email provider if there is no other email provider configured
|
||||
const hasEmailProvider = options?.providers?.some((provider) =>
|
||||
provider.options?.channels?.some((channel) => channel === "email")
|
||||
)
|
||||
if (!hasEmailProvider) {
|
||||
const { api_key, endpoint, environment_handle } = options?.cloud ?? {}
|
||||
if (api_key && endpoint && environment_handle) {
|
||||
await registrationFn(MedusaCloudEmailNotificationProvider, container, {
|
||||
options: options?.cloud,
|
||||
id: "cloud",
|
||||
})
|
||||
const provider = {
|
||||
id: "cloud",
|
||||
resolve: "",
|
||||
options: {
|
||||
...options?.cloud,
|
||||
channels: ["email"],
|
||||
},
|
||||
}
|
||||
providers = [...providers, provider]
|
||||
}
|
||||
}
|
||||
|
||||
await moduleProviderLoader({
|
||||
container,
|
||||
providers: options?.providers || [],
|
||||
@@ -50,7 +74,7 @@ export default async ({
|
||||
|
||||
await syncDatabaseProviders({
|
||||
container,
|
||||
providers: options?.providers || [],
|
||||
providers: providers,
|
||||
})
|
||||
}
|
||||
|
||||
@@ -59,7 +83,7 @@ async function syncDatabaseProviders({
|
||||
providers,
|
||||
}: {
|
||||
container: any
|
||||
providers: ModuleProvider[]
|
||||
providers: Exclude<NotificationModuleOptions["providers"], undefined>
|
||||
}) {
|
||||
const providerServiceRegistrationKey = lowerCaseFirst(
|
||||
NotificationProviderService.name
|
||||
@@ -76,13 +100,12 @@ async function syncDatabaseProviders({
|
||||
)
|
||||
}
|
||||
|
||||
const config = provider.options as { channels: string[] }
|
||||
return {
|
||||
id: provider.id,
|
||||
handle: provider.id,
|
||||
name: provider.id,
|
||||
is_enabled: true,
|
||||
channels: config?.channels ?? [],
|
||||
channels: provider.options?.channels ?? [],
|
||||
}
|
||||
})
|
||||
|
||||
|
||||
@@ -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}`)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -28,8 +28,19 @@ export type NotificationModuleOptions =
|
||||
*/
|
||||
id: string
|
||||
/**
|
||||
* key value pair of the configuration to be passed to the provider constructor
|
||||
* key value pair of the configuration to be passed to the provider constructor, plus the channels supported by the provider
|
||||
*/
|
||||
options?: Record<string, unknown>
|
||||
options?: Record<string, unknown> & { channels: string[] }
|
||||
}[]
|
||||
/**
|
||||
* Options for the default Medusa Cloud Email provider
|
||||
* @private
|
||||
*/
|
||||
cloud?: MedusaCloudEmailOptions
|
||||
}
|
||||
|
||||
export type MedusaCloudEmailOptions = {
|
||||
api_key: string
|
||||
endpoint: string
|
||||
environment_handle: string
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user