📈 Open source, privacy-first web analytics — a cookie-less Google Analytics alternative with built-in error tracking and performance monitoring. Self-host it or use our cloud. Get started for free!
TypeScript chartsclickhousedockergdprgoogle-analyticsmetricsmonitoringmysqlnestjsprivacyredisself-hostedselfhostedstatisticstailwindcssweb-analyticswebanalyticswebsite
import fs from 'fs'
import path from 'path'
import { MailerService as NodeMailerService } from '@nestjs-modules/mailer'
import { Injectable } from '@nestjs/common'
import handlebars from 'handlebars'
import { LetterTemplate } from './letter'
import { AppLoggerService } from '../logger/logger.service'
import { SEND_WARNING_AT_PERC } from '../common/constants'
const TEMPLATES_PATH = path.join(__dirname, '..', 'common', 'templates')
const metaInfoJson = {
[LetterTemplate.SignUp]: {
subject: {
en: () => 'Welcome to Swetrix - your 14-day trial is ready',
},
},
[LetterTemplate.ConfirmPasswordChange]: {
subject: {
en: () => 'Please confirm the password change',
},
},
[LetterTemplate.MailAddressChangeConfirmation]: {
subject: {
en: () => 'Please confirm the new e-mail address',
},
},
[LetterTemplate.MailAddressHadChanged]: {
subject: {
en: () => 'A new e-mail-address has been saved',
},
},
[LetterTemplate.PasswordChanged]: {
subject: {
en: () => 'Your password was successfully changed',
},
},
[LetterTemplate.ProjectReport]: {
subject: {
en: (p: Params) =>
`${p.type === 'w' ? 'Weekly' : 'Monthly'} Report: ${p.date}`,
},
},
[LetterTemplate.TierWarning]: {
subject: {
en: () =>
`You have used more than ${SEND_WARNING_AT_PERC}% of the available events per your tier for this month`,
},
},
[LetterTemplate.SubscriptionCancelled]: {
subject: {
en: () => 'Your feedback on Swetrix',
},
},
[LetterTemplate.ProjectInvitation]: {
subject: {
en: () => 'You have been invited to join the project',
},
},
[LetterTemplate.OrganisationInvitation]: {
subject: {
en: () => 'You have been invited to join the organisation',
},
},
[LetterTemplate.ProjectInvitationUnregistered]: {
subject: {
en: () => 'You have been invited to join a project on Swetrix',
},
},
[LetterTemplate.OrganisationInvitationUnregistered]: {
subject: {
en: () => 'You have been invited to join an organisation on Swetrix',
},
},
[LetterTemplate.TwoFAOn]: {
subject: {
en: () => '2FA has been enabled on your Swetrix account',
},
},
[LetterTemplate.TwoFAOff]: {
subject: {
en: () => '2FA has been disabled on your Swetrix account',
},
},
[LetterTemplate.TrialWelcome]: {
subject: {
en: () => 'Your Swetrix trial is active',
},
},
[LetterTemplate.TrialValueCheckIn]: {
subject: {
en: () => 'Three checks for your Swetrix trial',
},
},
[LetterTemplate.TrialFeatureHighlight]: {
subject: {
en: () => 'Bring your analytics history into Swetrix',
},
},
[LetterTemplate.TrialEndsIn3Days]: {
subject: {
en: () => 'Your Swetrix trial ends in 3 days',
},
},
[LetterTemplate.TrialEndsIn3DaysCancelled]: {
subject: {
en: () => 'Your Swetrix trial access ends in 3 days',
},
},
[LetterTemplate.TrialExpired]: {
subject: {
en: () => 'Your free Swetrix trial has ended',
},
},
[LetterTemplate.TrialEndsTomorrow]: {
subject: {
en: () => 'Your Swetrix trial ends tomorrow',
},
},
[LetterTemplate.TrialEndingCancelled]: {
subject: {
en: () => 'Your Swetrix trial access ends tomorrow',
},
},
[LetterTemplate.SubscriptionPaymentFailed]: {
subject: {
en: () => 'Action required: your Swetrix payment failed',
},
},
[LetterTemplate.SubscriptionPaymentFinalWarning]: {
subject: {
en: () => 'Final reminder: update your Swetrix payment details',
},
},
[LetterTemplate.SubscriptionPaymentRecovered]: {
subject: {
en: () => 'Your Swetrix payment was recovered',
},
},
[LetterTemplate.ProjectSubscriberInvitation]: {
subject: {
en: () => 'You have been invited to join the project',
},
},
[LetterTemplate.ProjectTransfer]: {
subject: {
en: (p: Params) =>
`A Swetrix user offers to transfer ${p.name} project to your account`,
},
},
[LetterTemplate.UsageOverLimit]: {
subject: {
en: () =>
'Action required: you have exceeded your Swetrix subscription plan',
},
},
[LetterTemplate.DashboardLockedExceedingLimits]: {
subject: {
en: () => 'Action required: your Swetrix dashboard has been locked',
},
},
[LetterTemplate.DashboardLockedPaymentFailure]: {
subject: {
en: () =>
'Action required: your Swetrix dashboard has been locked due to a payment issue',
},
},
[LetterTemplate.SocialIdentityLinked]: {
subject: {
en: () => 'A social account has been linked to your Swetrix account',
},
},
[LetterTemplate.NoEventsAfterSignup]: {
subject: {
en: () => 'Need help setting up Swetrix?',
},
},
[LetterTemplate.SubscribeReminder]: {
subject: {
en: () => 'Your Swetrix trial is waiting for you',
},
},
}
interface Params {
[name: string]: any
}
handlebars.registerHelper('ifEquals', function ifEquals(arg1, arg2, options) {
return arg1 == arg2 ? options.fn(this) : options.inverse(this)
})
handlebars.registerHelper(
'ifNotEquals',
function ifNotEquals(arg1, arg2, options) {
return arg1 != arg2 ? options.fn(this) : options.inverse(this)
},
)
handlebars.registerHelper('greater', function greater(v1, v2, options) {
if (v1 > v2) {
return options.fn(this)
}
return options.inverse(this)
})
handlebars.registerHelper('less', function less(v1, v2, options) {
if (v1 < v2) {
return options.fn(this)
}
return options.inverse(this)
})
@Injectable()
export class MailerService {
constructor(
private readonly logger: AppLoggerService,
private readonly nodeMailerService: NodeMailerService,
) {}
async sendRawEmail(
email: string,
subject: string,
html: string,
): Promise<void> {
const message = {
from: `Swetrix <${process.env.FROM_EMAIL}>`,
to: email,
subject,
html,
}
if (process.env.SMTP_MOCK) {
this.logger.log({ ...message }, 'sendRawEmail', true)
} else {
await this.nodeMailerService.sendMail(message)
}
}
async sendEmail(
email: string,
templateName: LetterTemplate,
params: Params = null,
): Promise<void> {
try {
const templatePath = `${TEMPLATES_PATH}/en/${templateName}.html`
const letter = fs.readFileSync(templatePath, { encoding: 'utf-8' })
const subject = metaInfoJson[templateName].subject.en(params)
const template = handlebars.compile(letter)
const html = template(params)
const message = {
from: `Swetrix <${process.env.FROM_EMAIL}>`,
to: email,
subject,
html,
}
if (process.env.SMTP_MOCK) {
this.logger.log(
{
...message,
params,
},
'sendEmail',
true,
)
} else {
await this.nodeMailerService.sendMail(message)
}
} catch (reason) {
this.logger.error({ reason, email, templateName }, 'sendEmail')
throw reason
}
}
}