Skip to content
This repository has been archived by the owner on Jul 11, 2024. It is now read-only.

Commit

Permalink
Merge pull request #334 from Uconnect-Technologies/dev-ilyas/mailer-s…
Browse files Browse the repository at this point in the history
…hould-throw-an-event-when-an-email-is-sent-and-should-also-throw-when-an-email-is-not-sent-successfully

Allow using events with node mailer
  • Loading branch information
ilyaskarim authored Feb 9, 2022
2 parents 24ab905 + 638c4c7 commit d3b24d4
Show file tree
Hide file tree
Showing 6 changed files with 115 additions and 19 deletions.
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "wertik-js",
"version": "3.1.9",
"version": "3.2.0",
"main": "lib/main.js",
"types": "lib/types.d.ts",
"repository": "https://github.com/Uconnect-Technologies/wertik-js.git",
Expand Down Expand Up @@ -79,7 +79,7 @@
"@types/http-codes": "^1.0.1",
"@types/knex": "^0.14.19",
"@types/morgan": "^1.7.35",
"@types/node": "^9.6.55",
"@types/node": "^9.6.7",
"@types/nodemailer": "^4.3.4",
"@types/winston": "^2.4.4",
"concurrently": "^3.6.1",
Expand Down
2 changes: 1 addition & 1 deletion src/next/crud/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ export const paginate = async (arg, tableInstance) => {
previousPage: page == 1 ? 1 : page - 1,
pages: totalPages,
hasMore: page < totalPages,
limit: limit
limit: limit,
},
}
}
Expand Down
23 changes: 23 additions & 0 deletions src/next/devServers/dev-ilyas.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,23 @@
import { useMysqlDatabase } from "../database"
import { useGraphql } from "../graphql"
import wertik, { useModule } from "../index"
import { useMailer } from "../mailer"

const devIlyas = async () => {
wertik({
port: 1200,
graphql: useGraphql(),
mailer: {
instances: {
default: useMailer({
name: "default",
}),
},
events: {
onEmailSent: (props) => {},
onEmailSentFailed: () => {},
},
},
database: {
default: useMysqlDatabase({
port: 3306,
Expand Down Expand Up @@ -47,6 +59,17 @@ const devIlyas = async () => {
},
}),
},
}).then(async (a) => {
let r = await a.sendEmail({
mailer: "default",
options: {
from: "[email protected]",
template: "hello",
to: "[email protected]",
subject: "hello how are you?",
},
})
console.log(r)
})
}

Expand Down
15 changes: 11 additions & 4 deletions src/next/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,12 @@ const Wertik: (configuration: WertikConfiguration) => Promise<WertikApp> = (
wertikApp.express = expressApp
wertikApp.port = configuration.port

for (const mailName of Object.keys(configuration.mailer || {})) {
wertikApp.mailer[mailName] = await configuration.mailer[mailName]()
for (const mailName of Object.keys(
configuration.mailer.instances || {}
)) {
wertikApp.mailer[mailName] = await configuration.mailer.instances[
mailName
]()
}

if (configuration.storage) {
Expand Down Expand Up @@ -131,7 +135,7 @@ const Wertik: (configuration: WertikConfiguration) => Promise<WertikApp> = (
}

applyRelationshipsFromStoreToDatabase(store, wertikApp)
applyRelationshipsFromStoreToGraphql(store, wertikApp)
applyRelationshipsFromStoreToGraphql(store, wertikApp)

expressApp.get("/w/info", function (req, res) {
res.json({
Expand All @@ -140,7 +144,10 @@ const Wertik: (configuration: WertikConfiguration) => Promise<WertikApp> = (
})
})

wertikApp.sendEmail = emailSender(wertikApp)
wertikApp.sendEmail = emailSender({
wertikApp,
configuration,
})

if (configuration.graphql) {
wertikApp.graphql = configuration.graphql({
Expand Down
52 changes: 41 additions & 11 deletions src/next/mailer/index.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,17 @@
import nodemailer from "nodemailer"
import handlebars from "handlebars"
import { iObject, WertikApp } from "../types"
import { useMailerProps, WertikApp, WertikConfiguration } from "../types"
import { emailSendProps } from "../types/mailer"
import { get } from "lodash"

export const useMailer = (props?: iObject) => {
export const useMailer = (props: useMailerProps) => {
return async () => {
let testAccount = props ? null : await nodemailer.createTestAccount()
let testAccount = props.options
? null
: await nodemailer.createTestAccount()

const wertiknodemailerDefaultConfiguration = props
? props
const emailConfiguration = props.options
? props.options
: {
host: "smtp.ethereal.email",
port: 587,
Expand All @@ -19,23 +22,31 @@ export const useMailer = (props?: iObject) => {
},
}

return nodemailer.createTransport(wertiknodemailerDefaultConfiguration)
console.log(`[Mailer]`, `Initialized mailer "${props.name}"`)

return nodemailer.createTransport(emailConfiguration)
}
}

export const emailSender = (app: WertikApp) => {
const fn = (props: { mailer: string; options: emailSendProps }) => {
return async () => {
let transporter = app.mailer[props.mailer]
export const emailSender = ({
configuration,
wertikApp,
}: {
configuration: WertikConfiguration
wertikApp: WertikApp
}) => {
const fn = async (props: { mailer: string; options: emailSendProps }) => {
let transporter = wertikApp.mailer[props.mailer]

try {
if (!transporter) {
throw new Error(
`Email integration ${props.mailer} not found. Please check the typo.`
)
}

let compiled = handlebars.compile(props.options.template)
let resultTemplate = compiled(props.options.variables)
let resultTemplate = compiled(props.options.variables ?? {})
let emailInstance = await transporter.sendMail({
from: props.options.from,
to: props.options.to,
Expand All @@ -51,7 +62,26 @@ export const emailSender = (app: WertikApp) => {
nodemailer.getTestMessageUrl(emailInstance)
)
}

get(configuration, "mailer.events.onEmailSent", () => {})({
mailer: props.mailer,
wertikApp,
configuration,
emailInstance,
previewURL:
nodemailer && nodemailer.getTestMessageUrl
? nodemailer.getTestMessageUrl(emailInstance)
: "",
})

return emailInstance
} catch (e) {
get(configuration, "mailer.events.onEmailSentFailed", () => {})({
mailer: props.mailer,
wertikApp,
configuration,
error: e,
})
}
}
return fn
Expand Down
38 changes: 37 additions & 1 deletion src/next/types/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,30 @@ export interface WertikConfiguration {
* Mailer
*/
mailer?: {
[key: string]: () => Promise<unknown>
instances: {
[key: string]: () => Promise<unknown>
}
events?: {
/**
* Runs when email sents successfully.
*/
onEmailSent?: ({
wertikApp: WertikApp,
configuration: WertikConfiguration,
emailInstance: any,
previewURL: string,
mailer: String,
}) => void | any | null | undefined
/**
* Runs when email fails to send.
*/
onEmailSentFailed?: ({
mailer: String,
wertikApp: WertikApp,
configuration: WertikConfiguration,
error: any,
}) => void | any | null | undefined
}
}
/**
* Sockets
Expand Down Expand Up @@ -150,3 +173,16 @@ export interface useRedisProps {
[key: string]: any
name: string
}

export interface useMailerProps {
/**
* Provide name for your mailer.
*/
name: string
/**
* Provide options that you provide procide to nodemailer.createTransport function.
*/
options?: {
[key: string]: any
}
}

0 comments on commit d3b24d4

Please sign in to comment.