feat: Add support for sendgrid and logger notification providers (#7290)

* feat: Add support for sendgrid and logger notification providers

* fix: changes based on PR review
This commit is contained in:
Stevche Radevski authored and GitHub committed 2024-05-11 17:40:03 +02:00
1 parent 1a68f4602c
commit 79758c46b6
24 files changed
+631 -1

No files matched your search

Whitespace-only changes.
Whitespace-only changes.
@@ -0,0 +1,36 @@
import { LocalNotificationService } from "../../src/services/local"
jest.setTimeout(100000)
describe("Local notification provider", () => {
let localService: LocalNotificationService
beforeAll(() => {
localService = new LocalNotificationService(
{
logger: console as any,
},
{}
)
})
afterEach(() => {
jest.restoreAllMocks()
})
it("sends logs to the console output with the notification details", async () => {
const logSpy = jest.spyOn(console, "info")
await localService.send({
to: "test@medusajs.com",
channel: "email",
template: "some-template",
data: {
username: "john-doe",
},
})
expect(logSpy).toHaveBeenCalled()
expect(logSpy).toHaveBeenCalledWith(
'Attempting to send a notification to: test@medusajs.com on the channel: email with template: some-template and data: {"username":"john-doe"}'
)
})
})
@@ -0,0 +1,7 @@
module.exports = {
transform: {
"^.+\\.[jt]s?$": "@swc/jest",
},
testEnvironment: `node`,
moduleFileExtensions: [`js`, `jsx`, `ts`, `tsx`, `json`],
}
@@ -0,0 +1,39 @@
{
"name": "@medusajs/notification-local",
"version": "0.0.1",
"description": "Local (logging) notification provider for Medusa, useful for testing purposes and log audits",
"main": "dist/index.js",
"repository": {
"type": "git",
"url": "https://github.com/medusajs/medusa",
"directory": "packages/modules/providers/notification-local"
},
"files": [
"dist"
],
"engines": {
"node": ">=16"
},
"author": "Medusa",
"license": "MIT",
"scripts": {
"prepublishOnly": "cross-env NODE_ENV=production tsc --build",
"test": "jest --passWithNoTests src",
"test:integration": "jest --forceExit -- integration-tests/**/__tests__/**/*.spec.ts",
"build": "rimraf dist && tsc -p ./tsconfig.json",
"watch": "tsc --watch"
},
"devDependencies": {
"cross-env": "^5.2.1",
"jest": "^25.5.4",
"rimraf": "^5.0.1",
"typescript": "^4.9.5"
},
"dependencies": {
"@medusajs/utils": "^1.11.7"
},
"keywords": [
"medusa-provider",
"medusa-provider-local"
]
}
@@ -0,0 +1,10 @@
import { ModuleProviderExports } from "@medusajs/types"
import { LocalNotificationService } from "./services/local"
const services = [LocalNotificationService]
const providerExport: ModuleProviderExports = {
services,
}
export default providerExport
@@ -0,0 +1,48 @@
import {
Logger,
NotificationTypes,
LocalNotificationServiceOptions,
} from "@medusajs/types"
import {
AbstractNotificationProviderService,
MedusaError,
} from "@medusajs/utils"
type InjectedDependencies = {
logger: Logger
}
interface LocalServiceConfig {}
export class LocalNotificationService extends AbstractNotificationProviderService {
protected config_: LocalServiceConfig
protected logger_: Logger
constructor(
{ logger }: InjectedDependencies,
options: LocalNotificationServiceOptions
) {
super()
this.config_ = options
this.logger_ = logger
}
async send(
notification: NotificationTypes.ProviderSendNotificationDTO
): Promise<NotificationTypes.ProviderSendNotificationResultsDTO> {
if (!notification) {
throw new MedusaError(
MedusaError.Types.INVALID_DATA,
`No notification information provided`
)
}
const message =
`Attempting to send a notification to: ${notification.to}` +
` on the channel: ${notification.channel} with template: ${notification.template}` +
` and data: ${JSON.stringify(notification.data)}`
this.logger_.info(message)
return {}
}
}
@@ -0,0 +1,32 @@
{
"compilerOptions": {
"lib": ["es2020"],
"target": "es2020",
"jsx": "react-jsx" /* Specify JSX code generation: 'preserve', 'react-native', or 'react'. */,
"outDir": "./dist",
"esModuleInterop": true,
"declaration": true,
"module": "commonjs",
"moduleResolution": "node",
"emitDecoratorMetadata": true,
"experimentalDecorators": true,
"noImplicitReturns": true,
"strictNullChecks": true,
"strictFunctionTypes": true,
"noImplicitThis": true,
"allowJs": true,
"skipLibCheck": true,
"downlevelIteration": true, // to use ES5 specific tooling
"inlineSourceMap": true /* Emit a single file with source maps instead of having a separate file. */
},
"include": ["src"],
"exclude": [
"dist",
"build",
"src/**/__tests__",
"src/**/__mocks__",
"src/**/__fixtures__",
"node_modules",
".eslintrc.js"
]
}