authenzify
Version:
server to manage authentication authorization of users and more
214 lines (203 loc) • 6.39 kB
JavaScript
import httpStatus from 'http-status'
import { initNodemailer } from '../email-providers/nodemailer.js'
import { sleep } from '../../../../util/util.js'
export class NodemailerEmailNotifications {
#retry
#retryEvery
#innerSendMail
#constantParams
#templatesRender
#iNodemailerEmailSettings
constructor(templates, iNodemailerEmailSettings, constantParams, config) {
this.#templatesRender = templates
this.#constantParams = constantParams
this.#iNodemailerEmailSettings = iNodemailerEmailSettings
this.#innerSendMail = initNodemailer(iNodemailerEmailSettings).sendMail
const { retry = 3, retryEvery = 500 } = config || {}
this.#retryEvery = retryEvery
this.#retry = retry
}
async sendMail(emailInfo, retry, retryEvery) {
try {
const emailResponse = await this.#innerSendMail(emailInfo)
return emailResponse
} catch (error) {
if (retry > 0 && error.responseCode === httpStatus.MISDIRECTED_REQUEST) {
await sleep(500)
return this.sendMail(emailInfo, retry--, retryEvery)
}
throw error
}
}
async #prepareAndSendEmail(template, to) {
try {
const emailInfo = {
...template,
to,
}
const emailResponse = this.sendMail(
emailInfo,
this.#retry,
this.#retryEvery,
)
return emailResponse
} catch (error) {
throw error
}
}
async sendActivationMail(user, verification) {
try {
const templates = this.#templatesRender.renderActivation({
from: {
from: this.#iNodemailerEmailSettings.from,
applicationName: this.#constantParams.applicationName,
},
subject: {
applicationName: this.#constantParams.applicationName,
email: user.email,
firstName: user.firstName || '',
lastName: user.lastName || '',
},
html: {
activationLink:
this.#constantParams.activationVerificationRoute.replace(
new RegExp(':id', 'g'),
verification.id,
),
},
})
return this.#prepareAndSendEmail(templates, user.email)
} catch (error) {
throw error
}
}
sendVerificationMail(user, verification) {
try {
const templates = this.#templatesRender.renderVerification({
from: this.#iNodemailerEmailSettings.from,
subject: {
applicationName: this.#constantParams.applicationName,
email: user.email,
firstName: user.firstName || '',
lastName: user.lastName || '',
},
html: {
activationLink: `${this.#constantParams.domain}/verify/${
verification.id
}`,
},
})
return this.#prepareAndSendEmail(templates, user.email)
} catch (error) {
throw error
}
}
sendForgotPasswordMail(user, verification) {
try {
const templates = this.#templatesRender.renderForgotPassword({
from: this.#iNodemailerEmailSettings.from,
subject: {
applicationName: this.#constantParams.applicationName,
email: user.email,
firstName: user.firstName || '',
lastName: user.lastName || '',
},
html: {
loginUrl: `/${verification.id}`,
},
})
return this.#prepareAndSendEmail(templates, user.email)
} catch (error) {
throw error
}
}
sendPermissionsRequestMailToAdmin({
groupsNames,
verification,
user,
adminEmail,
}) {
const templates = this.#templatesRender.renderPermissionsRequest({
from: {
from: this.#iNodemailerEmailSettings.from,
applicationName: this.#constantParams.applicationName,
},
subject: {
applicationName: this.#constantParams.applicationName,
email: user.email,
firstName: user.firstName || '',
lastName: user.lastName || '',
},
html: {
email: user.email,
firstName: user.firstName || '',
lastName: user.lastName || '',
permissions: groupsNames.map((name) => {
return {
name,
link: this.#constantParams.permissionsVerificationRoute
.replace(new RegExp(':id', 'g'), verification.id)
.replace(new RegExp(':role', 'g'), name),
}
}),
},
})
return this.#prepareAndSendEmail(templates, adminEmail)
}
sendPermissionsApprovedMailToUser({ user, adminUser }) {
const templates = this.#templatesRender.renderPermissionsApprovedToUser({
from: {
from: this.#iNodemailerEmailSettings.from,
applicationName: this.#constantParams.applicationName,
},
subject: {
applicationName: this.#constantParams.applicationName,
email: adminUser.email,
firstName: adminUser.firstName || '',
lastName: adminUser.lastName || '',
},
html: {
email: adminUser.email,
firstName: adminUser.firstName || '',
lastName: adminUser.lastName || '',
applicationName: this.#constantParams.applicationName,
signInRoute: this.#constantParams.signInRoute,
},
})
return this.#prepareAndSendEmail(templates, user.email)
}
sendResetPasswordMailToUser({
user,
verification,
notRequestedVerification,
}) {
const templates = this.#templatesRender.renderResetPasswordRequested({
from: {
from: this.#iNodemailerEmailSettings.from,
applicationName: this.#constantParams.applicationName,
},
subject: {
applicationName: this.#constantParams.applicationName,
email: user.email,
firstName: user.firstName || '',
lastName: user.lastName || '',
},
html: {
email: user.email,
firstName: user.firstName || '',
lastName: user.lastName || '',
applicationName: this.#constantParams.applicationName,
resetPasswordRoute: this.#constantParams.resetPasswordRoute.replace(
new RegExp(':id', 'g'),
verification.id,
),
didNotAskedToResetPasswordRoute:
this.#constantParams.didNotAskedToResetPasswordRoute.replace(
new RegExp(':id', 'g'),
notRequestedVerification.id,
),
},
})
return this.#prepareAndSendEmail(templates, user.email)
}
}