forked from novuhq/novu
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Add Mailchain Email Provider novuhq#4336
- Loading branch information
Showing
20 changed files
with
255 additions
and
0 deletions.
There are no files selected for viewing
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
18 changes: 18 additions & 0 deletions
18
packages/application-generic/src/factories/mail/handlers/mailchain.handler.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
import { ChannelTypeEnum, ICredentialsDto, ICredentials } from '@novu/shared'; | ||
import { MailchainEmailProvider } from '@novu/mailchain'; | ||
import { BaseHandler } from './base.handler'; | ||
|
||
export class MailchainHandler extends BaseHandler { | ||
constructor() { | ||
super('mailchain', ChannelTypeEnum.EMAIL); | ||
} | ||
|
||
buildProvider(credentials: ICredentials, from?: string) { | ||
const config: { secretRecoveryPhrase: string; from: string } = { | ||
secretRecoveryPhrase: credentials.secretPhrase, | ||
from: credentials.from, | ||
}; | ||
|
||
this.provider = new MailchainEmailProvider(config); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
{ | ||
"path": "cz-conventional-changelog" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
{ | ||
"extends": "../../.eslintrc.js" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
# Novu Mailchain Provider | ||
|
||
A Mailchain email provider library for [@novu/node](https://github.com/novuhq/novu) | ||
|
||
## Usage | ||
|
||
```javascript | ||
FILL IN THE INITIALIZATION USAGE | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
/** @type {import('ts-jest/dist/types').InitialOptionsTsJest} */ | ||
module.exports = { | ||
preset: 'ts-jest', | ||
testEnvironment: 'node', | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
{ | ||
"name": "@novu/mailchain", | ||
"version": "0.16.3", | ||
"description": "A mailchain wrapper for novu", | ||
"main": "build/main/index.js", | ||
"typings": "build/main/index.d.ts", | ||
"module": "build/module/index.js", | ||
"private": false, | ||
"repository": "https://github.com/novuhq/novu", | ||
"license": "MIT", | ||
"keywords": [], | ||
"scripts": { | ||
"prebuild": "rimraf build", | ||
"build": "run-p build:*", | ||
"build:main": "tsc -p tsconfig.json", | ||
"build:module": "tsc -p tsconfig.module.json", | ||
"fix": "run-s fix:*", | ||
"fix:prettier": "prettier \"src/**/*.ts\" --write", | ||
"fix:lint": "eslint src --ext .ts --fix", | ||
"test": "run-s test:*", | ||
"lint": "eslint src --ext .ts", | ||
"test:unit": "jest src", | ||
"watch:build": "tsc -p tsconfig.json -w", | ||
"watch:test": "jest src --watch", | ||
"reset-hard": "git clean -dfx && git reset --hard && yarn", | ||
"prepare-release": "run-s reset-hard test" | ||
}, | ||
"publishConfig": { | ||
"access": "public" | ||
}, | ||
"engines": { | ||
"node": ">=13.0.0 <17.0.0", | ||
"pnpm": "^7.26.0" | ||
}, | ||
"dependencies": { | ||
"@mailchain/sdk": "^0.25.0", | ||
"@novu/stateless": "0.16.3" | ||
}, | ||
"devDependencies": { | ||
"@istanbuljs/nyc-config-typescript": "~1.0.1", | ||
"@types/jest": "~27.5.2", | ||
"cspell": "~6.19.2", | ||
"jest": "~27.5.1", | ||
"npm-run-all": "^4.1.5", | ||
"nyc": "~15.1.0", | ||
"prettier": "~2.8.0", | ||
"rimraf": "~3.0.2", | ||
"ts-jest": "~27.1.5", | ||
"ts-node": "~10.9.1", | ||
"typescript": "4.9.5" | ||
}, | ||
"files": [ | ||
"build/main", | ||
"build/module", | ||
"!**/*.spec.*", | ||
"!**/*.json", | ||
"CHANGELOG.md", | ||
"LICENSE", | ||
"README.md" | ||
], | ||
"ava": { | ||
"failFast": true, | ||
"timeout": "60s", | ||
"typescript": { | ||
"rewritePaths": { | ||
"src/": "build/main/" | ||
} | ||
}, | ||
"files": [ | ||
"!build/module/**" | ||
] | ||
}, | ||
"prettier": { | ||
"singleQuote": true | ||
}, | ||
"nyc": { | ||
"extends": "@istanbuljs/nyc-config-typescript", | ||
"exclude": [ | ||
"**/*.spec.js" | ||
] | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export * from './lib/mailchain.provider'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
import { MailchainEmailProvider } from './mailchain.provider'; | ||
|
||
test('should trigger mailchain library correctly', async () => {}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
import { | ||
ChannelTypeEnum, | ||
ISendMessageSuccessResponse, | ||
IEmailOptions, | ||
IEmailProvider, | ||
ICheckIntegrationResponse, | ||
IEmailEventBody, | ||
CheckIntegrationResponseEnum, | ||
} from '@novu/stateless'; | ||
import { Mailchain, SendMailParams } from '@mailchain/sdk'; | ||
|
||
export class MailchainEmailProvider implements IEmailProvider { | ||
id = 'mailchain'; | ||
channelType = ChannelTypeEnum.EMAIL as ChannelTypeEnum.EMAIL; | ||
private mailchainClient: Mailchain; | ||
|
||
constructor( | ||
private config: { | ||
secretRecoveryPhrase: string; | ||
from: string; | ||
} | ||
) { | ||
this.mailchainClient = Mailchain.fromSecretRecoveryPhrase( | ||
config.secretRecoveryPhrase | ||
); | ||
} | ||
|
||
getMessageId?: (body: any) => string[]; | ||
parseEventBody?: (body: any, identifier: string) => IEmailEventBody; | ||
|
||
async checkIntegration( | ||
options: IEmailOptions | ||
): Promise<ICheckIntegrationResponse> { | ||
try { | ||
const user = await this.mailchainClient.user(); | ||
await this.mailchainClient.sendMail({ | ||
from: user.address || this.config.from, | ||
to: options.to, | ||
subject: options.subject, | ||
content: { | ||
text: options.text, | ||
html: options.html, | ||
}, | ||
}); | ||
|
||
return { | ||
success: true, | ||
message: 'Integrated successfully!', | ||
code: CheckIntegrationResponseEnum.SUCCESS, | ||
}; | ||
} catch (error) { | ||
return { | ||
success: false, | ||
message: error?.message, | ||
code: CheckIntegrationResponseEnum.FAILED, | ||
}; | ||
} | ||
} | ||
|
||
async sendMessage( | ||
options: IEmailOptions | ||
): Promise<ISendMessageSuccessResponse> { | ||
const user = await this.mailchainClient.user(); | ||
console.log(`username: ${user.username}, address: ${user.address}`); | ||
|
||
const result = await this.mailchainClient.sendMail({ | ||
from: user.address || this.config.from, | ||
to: options.to, | ||
subject: options.subject, | ||
content: { | ||
text: options.text, | ||
html: options.html, | ||
}, | ||
}); | ||
|
||
console.log(result.data); | ||
|
||
const savedMessageId = '12345'; | ||
|
||
return { | ||
id: savedMessageId, | ||
date: new Date().toISOString(), | ||
}; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
{ | ||
"extends": "../../tsconfig.json", | ||
"compilerOptions": { | ||
"outDir": "build/main", | ||
"rootDir": "src", | ||
"types": ["node", "jest"] | ||
}, | ||
"include": ["src/**/*.ts"], | ||
"exclude": ["node_modules/**"] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
{ | ||
"extends": "./tsconfig", | ||
"compilerOptions": { | ||
"target": "esnext", | ||
"outDir": "build/module", | ||
"module": "esnext" | ||
}, | ||
"exclude": ["node_modules/**"] | ||
} |