diff --git a/bun.lockb b/bun.lockb index 7dcebf1..913134b 100644 Binary files a/bun.lockb and b/bun.lockb differ diff --git a/package.json b/package.json index 58cf923..5696002 100644 --- a/package.json +++ b/package.json @@ -12,6 +12,7 @@ "@hono/zod-openapi": "0.18.0", "@hono/zod-validator": "0.4.1", "bullmq": "5.28.1", + "handlebars": "^4.7.8", "hono": "4.6.11", "ioredis": "5.4.1", "nodemailer": "^6.9.16", diff --git a/src/templates/index.ts b/src/templates/index.ts index cc259b8..b58d15d 100644 --- a/src/templates/index.ts +++ b/src/templates/index.ts @@ -1,16 +1,41 @@ import { Language } from '../types' -import { welcomeTemplate } from './welcome' -import { passwordResetTemplate } from './password-reset' +import { readFileSync } from 'fs' +import { join } from 'path' +import { compile } from 'handlebars' -const templates = { - welcome: welcomeTemplate, - passwordReset: passwordResetTemplate +interface TemplateCache { + [key: string]: { + html: HandlebarsTemplateDelegate + text: HandlebarsTemplateDelegate + } +} + +const templateCache: TemplateCache = {} + +function loadTemplate(name: string, language: Language) { + const cacheKey = `${name}:${language}` + + if (!templateCache[cacheKey]) { + const htmlPath = join(process.cwd(), 'src', 'templates', name, `${language}.html`) + const textPath = join(process.cwd(), 'src', 'templates', name, `${language}.txt`) + + const htmlTemplate = readFileSync(htmlPath, 'utf-8') + const textTemplate = readFileSync(textPath, 'utf-8') + + templateCache[cacheKey] = { + html: compile(htmlTemplate), + text: compile(textTemplate) + } + } + + return templateCache[cacheKey] } export function getEmailTemplate(name: string, language: Language) { - const template = templates[name as keyof typeof templates] - if (!template) { - throw new Error(`Template ${name} not found`) + const template = loadTemplate(name, language) + + return { + html: (data: any) => template.html(data), + text: (data: any) => template.text(data) } - return template[language] } \ No newline at end of file diff --git a/src/templates/password-reset/de.html b/src/templates/password-reset/de.html new file mode 100644 index 0000000..dac8602 --- /dev/null +++ b/src/templates/password-reset/de.html @@ -0,0 +1,7 @@ +
Hallo {{name}},
+Wir haben eine Anfrage zum Zurücksetzen Ihres Passworts erhalten. Klicken Sie auf den untenstehenden Link, um ein neues Passwort zu erstellen:
+Passwort zurücksetzen +Dieser Link läuft in {{expiresIn}} ab.
+Falls Sie diese Anfrage nicht gestellt haben, ignorieren Sie bitte diese E-Mail oder kontaktieren Sie den Support, wenn Sie Bedenken haben.
+Mit freundlichen Grüßen,
Ihr Support-Team
Hello {{name}},
+We received a request to reset your password. Click the link below to create a new password:
+Reset Password +This link will expire in {{expiresIn}}.
+If you didn't request this password reset, please ignore this email or contact support if you have concerns.
+Best regards,
Your Support Team
Danke, dass Sie sich bei uns registriert haben. Bitte klicken Sie auf den Link unten, um Ihr Konto zu aktivieren:
+Konto aktivieren \ No newline at end of file diff --git a/src/templates/welcome/de.txt b/src/templates/welcome/de.txt new file mode 100644 index 0000000..4a7356c --- /dev/null +++ b/src/templates/welcome/de.txt @@ -0,0 +1,4 @@ +Willkommen {{name}}! + +Danke, dass Sie sich bei uns registriert haben. Bitte verwenden Sie den folgenden Link, um Ihr Konto zu aktivieren: +{{activationLink}} \ No newline at end of file diff --git a/src/templates/welcome/en.html b/src/templates/welcome/en.html new file mode 100644 index 0000000..8292560 --- /dev/null +++ b/src/templates/welcome/en.html @@ -0,0 +1,3 @@ +Thank you for joining us. Please click the link below to activate your account:
+Activate Account \ No newline at end of file diff --git a/src/templates/welcome/en.txt b/src/templates/welcome/en.txt new file mode 100644 index 0000000..b1ac5a2 --- /dev/null +++ b/src/templates/welcome/en.txt @@ -0,0 +1,4 @@ +Welcome {{name}}! + +Thank you for joining us. Please use the following link to activate your account: +{{activationLink}} \ No newline at end of file