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:
@@ -7,11 +7,11 @@ import {
|
||||
NotificationEvents,
|
||||
NotificationStatus,
|
||||
} from "@medusajs/framework/utils"
|
||||
import { NotificationModuleService } from "@services"
|
||||
import {
|
||||
MockEventBusService,
|
||||
moduleIntegrationTestRunner,
|
||||
} from "@medusajs/test-utils"
|
||||
import { NotificationModuleService } from "@services"
|
||||
import { resolve } from "path"
|
||||
|
||||
let moduleOptions = {
|
||||
|
||||
@@ -0,0 +1,168 @@
|
||||
import { INotificationModuleService } from "@medusajs/framework/types"
|
||||
import { Modules, NotificationStatus } from "@medusajs/framework/utils"
|
||||
import { moduleIntegrationTestRunner } from "@medusajs/test-utils"
|
||||
import { resolve } from "path"
|
||||
|
||||
jest.setTimeout(30000)
|
||||
|
||||
const successMedusaCloudEmailResponse = {
|
||||
ok: true,
|
||||
status: 200,
|
||||
statusText: "OK",
|
||||
json: () => Promise.resolve({ id: "external_id_1" }),
|
||||
}
|
||||
|
||||
const testNotification = {
|
||||
to: "customer@test.com",
|
||||
template: "some-template",
|
||||
channel: "email",
|
||||
data: {
|
||||
link: "https://test.com",
|
||||
},
|
||||
}
|
||||
|
||||
moduleIntegrationTestRunner<INotificationModuleService>({
|
||||
moduleName: Modules.NOTIFICATION,
|
||||
moduleOptions: {
|
||||
cloud: {
|
||||
api_key: "test-api-key",
|
||||
endpoint: "https://medusacloud.com/emails",
|
||||
environment_handle: "test-environment",
|
||||
},
|
||||
},
|
||||
testSuite: ({ service }) =>
|
||||
describe("Medusa Cloud Email provider", () => {
|
||||
let fetchMock: jest.SpyInstance
|
||||
|
||||
beforeEach(() => {
|
||||
fetchMock = jest
|
||||
.spyOn(globalThis, "fetch")
|
||||
.mockImplementation(
|
||||
async () => successMedusaCloudEmailResponse as any
|
||||
)
|
||||
})
|
||||
|
||||
afterEach(() => {
|
||||
fetchMock.mockClear()
|
||||
})
|
||||
|
||||
it("should send email notification to Medusa Cloud", async () => {
|
||||
const result = await service.createNotifications(testNotification)
|
||||
expect(result).toEqual(
|
||||
expect.objectContaining({
|
||||
provider_id: "cloud",
|
||||
external_id: "external_id_1",
|
||||
status: NotificationStatus.SUCCESS,
|
||||
})
|
||||
)
|
||||
|
||||
const [url, request] = fetchMock.mock.calls[0]
|
||||
expect(url).toBe("https://medusacloud.com/emails/send")
|
||||
expect(request.method).toBe("POST")
|
||||
expect(request.headers["Content-Type"]).toBe("application/json")
|
||||
expect(request.headers["Authorization"]).toBe("Basic test-api-key")
|
||||
expect(request.headers["x-medusa-environment-handle"]).toBe(
|
||||
"test-environment"
|
||||
)
|
||||
expect(JSON.parse(request.body)).toEqual({
|
||||
to: "customer@test.com",
|
||||
template: "some-template",
|
||||
data: {
|
||||
link: "https://test.com",
|
||||
},
|
||||
})
|
||||
})
|
||||
|
||||
it("should return an error if the Medusa Cloud Email provider fails", async () => {
|
||||
fetchMock.mockImplementation(
|
||||
async () =>
|
||||
({
|
||||
ok: false,
|
||||
status: 500,
|
||||
statusText: "Internal Server Error",
|
||||
json: () => Promise.resolve({ message: "Internal Server Error" }),
|
||||
} as any)
|
||||
)
|
||||
|
||||
await expect(
|
||||
service.createNotifications(testNotification)
|
||||
).rejects.toThrow()
|
||||
})
|
||||
}),
|
||||
})
|
||||
|
||||
moduleIntegrationTestRunner<INotificationModuleService>({
|
||||
moduleName: Modules.NOTIFICATION,
|
||||
moduleOptions: {
|
||||
cloud: {
|
||||
api_key: "test-api-key",
|
||||
endpoint: "https://medusacloud.com/emails",
|
||||
environment_handle: "test-environment",
|
||||
},
|
||||
providers: [
|
||||
{
|
||||
resolve: resolve(
|
||||
process.cwd() +
|
||||
"/integration-tests/__fixtures__/providers/default-provider"
|
||||
),
|
||||
id: "test-provider",
|
||||
options: {
|
||||
name: "Test provider",
|
||||
channels: ["email"],
|
||||
},
|
||||
},
|
||||
],
|
||||
},
|
||||
testSuite: ({ service }) =>
|
||||
describe("Medusa Cloud Email provider - when another email provider is configured", () => {
|
||||
let fetchMock: jest.SpyInstance
|
||||
|
||||
beforeEach(() => {
|
||||
fetchMock = jest
|
||||
.spyOn(globalThis, "fetch")
|
||||
.mockImplementation(
|
||||
async () => successMedusaCloudEmailResponse as any
|
||||
)
|
||||
})
|
||||
|
||||
afterEach(() => {
|
||||
fetchMock.mockClear()
|
||||
})
|
||||
|
||||
it("should not enable Medusa Cloud Email provider", async () => {
|
||||
const result = await service.createNotifications(testNotification)
|
||||
expect(result).toMatchObject({ status: NotificationStatus.SUCCESS })
|
||||
|
||||
expect(fetchMock).not.toHaveBeenCalled()
|
||||
})
|
||||
}),
|
||||
})
|
||||
|
||||
moduleIntegrationTestRunner<INotificationModuleService>({
|
||||
moduleName: Modules.NOTIFICATION,
|
||||
moduleOptions: {},
|
||||
testSuite: ({ service }) =>
|
||||
describe("Medusa Cloud Email provider - when cloud options are not provided", () => {
|
||||
let fetchMock: jest.SpyInstance
|
||||
|
||||
beforeEach(() => {
|
||||
fetchMock = jest
|
||||
.spyOn(globalThis, "fetch")
|
||||
.mockImplementation(
|
||||
async () => successMedusaCloudEmailResponse as any
|
||||
)
|
||||
})
|
||||
|
||||
afterEach(() => {
|
||||
fetchMock.mockClear()
|
||||
})
|
||||
|
||||
it("should not enable Medusa Cloud Email provider", async () => {
|
||||
await expect(
|
||||
service.createNotifications(testNotification)
|
||||
).rejects.toThrow()
|
||||
|
||||
expect(fetchMock).not.toHaveBeenCalled()
|
||||
})
|
||||
}),
|
||||
})
|
||||
@@ -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
|
||||
}
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
import { asFunction, asValue, Lifetime } from "@medusajs/framework/awilix"
|
||||
import { moduleProviderLoader } from "@medusajs/framework/modules-sdk"
|
||||
import {
|
||||
CreatePaymentProviderDTO,
|
||||
@@ -5,7 +6,6 @@ import {
|
||||
ModuleProvider,
|
||||
ModulesSdkTypes,
|
||||
} from "@medusajs/framework/types"
|
||||
import { asFunction, asValue, Lifetime } from "@medusajs/framework/awilix"
|
||||
import { MedusaError } from "@medusajs/framework/utils"
|
||||
|
||||
import { PaymentProviderService } from "@services"
|
||||
|
||||
Reference in New Issue
Block a user