Skip to content

Commit

Permalink
refactor: Move handlebars template rendering to separate service
Browse files Browse the repository at this point in the history
Logic was embedded in EmailService.  Extracting allows for easier reuse.
  • Loading branch information
jrassa committed Aug 8, 2024
1 parent b241a12 commit 9854d44
Show file tree
Hide file tree
Showing 2 changed files with 87 additions and 42 deletions.
56 changes: 56 additions & 0 deletions src/app/common/template.service.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
import fs from 'fs/promises';

import config from 'config';
import handlebars from 'handlebars';
import _ from 'lodash';

handlebars.registerHelper('not', (value: boolean) => !value);
handlebars.registerHelper(
'isEqual',
(value1: unknown, value2: unknown) => value1 === value2
);
handlebars.registerHelper('lower', (str: string) => str?.toLowerCase());
handlebars.registerHelper('upper', (str: string) => str?.toUpperCase());
handlebars.registerHelper(
'head',
(str: string, count: number) => str?.substring(0, count)
);
handlebars.registerHelper(
'tail',
(str: string, count: number) => str?.substring(str.length - count)
);

class TemplateService {
async renderTemplate(
templatePath: string,
data: Record<string, unknown>,
options: RuntimeOptions = {}
) {
const template = await fs.readFile(templatePath, 'utf-8');
const templateData = _.merge(
{},
{
app: config.get('app')
},
data
);
return handlebars.compile(template)(templateData, options);
}

renderTemplateStr(
templateStr: string,
data: Record<string, unknown>,
options: RuntimeOptions = {}
) {
const templateData = _.merge(
{},
{
app: config.get('app')
},
data
);
return handlebars.compile(templateStr)(templateData, options);
}
}

export = new TemplateService();
73 changes: 31 additions & 42 deletions src/app/core/email/email.service.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,11 @@
import fs from 'fs';
import path from 'path';

import handlebars from 'handlebars';
import _ from 'lodash';

import { EmailProvider, MailOptions } from './providers/email.provider';
import { config } from '../../../dependencies';
import { logger } from '../../../lib/logger';

handlebars.registerHelper('toLowerCase', (str) => str.toLowerCase());
import templateService from '../../common/template.service';

class EmailService {
provider: EmailProvider;
Expand Down Expand Up @@ -64,44 +61,6 @@ class EmailService {
logger.debug(`Sent email to: ${mailOptions.to}`);
}

async buildEmailContent(templatePath, user, overrides = {}) {
const templateString = await new Promise((resolve, reject) => {
fs.readFile(templatePath, 'utf-8', (err, source) => {
if (err) {
reject(err);
} else {
resolve(source);
}
});
});

// Set email header/footer
const data = _.merge(
{},
config.get('coreEmails.default'),
{
app: config.get('app'),
user: user
},
overrides
);

return handlebars.compile(templateString)(data);
}

buildEmailSubject(template, user, overrides = {}): string {
const data = _.merge(
{},
config.get('coreEmails.default'),
{
app: config.get('app'),
user: user
},
overrides
);
return handlebars.compile(template)(data);
}

async generateMailOptions(
user,
req,
Expand Down Expand Up @@ -143,6 +102,36 @@ class EmailService {
);
}

buildEmailContent(
templatePath: string,
user,
overrides = {}
): Promise<string> {
// Set email header/footer
const data = _.merge(
{},
config.get('coreEmails.default'),
{
user: user
},
overrides
);

return templateService.renderTemplate(templatePath, data);
}

buildEmailSubject(template: string, user, overrides = {}): string {
const data = _.merge(
{},
config.get('coreEmails.default'),
{
user: user
},
overrides
);
return templateService.renderTemplateStr(template, data);
}

/**
* Initializes the provider only once. Use the getProvider() method
* to create and/or retrieve this singleton
Expand Down

0 comments on commit 9854d44

Please sign in to comment.