|
| 1 | +import { Injectable, Logger, OnApplicationBootstrap } from '@nestjs/common' |
| 2 | +import fs from 'node:fs/promises' |
| 3 | +import path from 'node:path' |
| 4 | +import Handlebars from 'handlebars' |
| 5 | +import { existsSync, mkdirSync, readdirSync, readFileSync, writeFileSync } from 'node:fs' |
| 6 | +import { parse } from 'yaml' |
| 7 | +import { resolveConfigVariables } from '~/_common/functions/resolve-config-variables.function' |
| 8 | + |
| 9 | +@Injectable() |
| 10 | +export class MailTemplatesService implements OnApplicationBootstrap { |
| 11 | + private readonly logger = new Logger(MailTemplatesService.name) |
| 12 | + |
| 13 | + public onApplicationBootstrap(): void { |
| 14 | + this.ensureDefaultConfigPresent() |
| 15 | + } |
| 16 | + |
| 17 | + private ensureDefaultConfigPresent(): void { |
| 18 | + const configDir = path.join(process.cwd(), 'configs', 'mail') |
| 19 | + const defaultsDir = path.join(process.cwd(), 'defaults', 'mail') |
| 20 | + |
| 21 | + if (!existsSync(configDir)) { |
| 22 | + mkdirSync(configDir, { recursive: true }) |
| 23 | + } |
| 24 | + |
| 25 | + try { |
| 26 | + const files = readdirSync(configDir) |
| 27 | + const defaultFiles = readdirSync(defaultsDir) |
| 28 | + |
| 29 | + for (const file of defaultFiles) { |
| 30 | + if (!files.includes(file)) { |
| 31 | + const defaultFile = readFileSync(path.join(defaultsDir, file), 'utf-8') |
| 32 | + writeFileSync(path.join(configDir, file), defaultFile) |
| 33 | + this.logger.warn(`Copied default mail config file: ${file}`) |
| 34 | + } |
| 35 | + } |
| 36 | + } catch (e) { |
| 37 | + this.logger.error(`Error initializing mail configs: ${e?.message || e}`) |
| 38 | + } |
| 39 | + } |
| 40 | + |
| 41 | + private getTemplatesDir(): string { |
| 42 | + // Même chemin que la config MailerModule (voir app.module.ts) |
| 43 | + return path.join(process.cwd(), 'templates') |
| 44 | + } |
| 45 | + |
| 46 | + public async getMailTemplatesConfig(): Promise<{ |
| 47 | + variables: Array<{ |
| 48 | + key: string |
| 49 | + label?: string |
| 50 | + description?: string |
| 51 | + example?: string |
| 52 | + defaultValue?: unknown |
| 53 | + }> |
| 54 | + }> { |
| 55 | + const configPath = path.join(process.cwd(), 'configs', 'mail', 'mail_templates.yml') |
| 56 | + const raw = readFileSync(configPath, 'utf8') |
| 57 | + const parsed = parse(raw) as any |
| 58 | + const cfg = (parsed?.mailTemplates || {}) as any |
| 59 | + |
| 60 | + const resolved = await resolveConfigVariables(cfg) |
| 61 | + |
| 62 | + return { |
| 63 | + variables: Array.isArray((resolved as any)?.variables) ? (resolved as any).variables : [], |
| 64 | + } |
| 65 | + } |
| 66 | + |
| 67 | + public async listTemplates(): Promise<string[]> { |
| 68 | + const dir = this.getTemplatesDir() |
| 69 | + const entries = await fs.readdir(dir, { withFileTypes: true }) |
| 70 | + return entries |
| 71 | + .filter((e) => e.isFile()) |
| 72 | + .map((e) => e.name) |
| 73 | + .filter((name) => name.endsWith('.hbs')) |
| 74 | + .map((name) => name.replace(/\.hbs$/, '')) |
| 75 | + .filter((name) => name.startsWith('mail_')) |
| 76 | + .sort((a, b) => a.localeCompare(b)) |
| 77 | + } |
| 78 | + |
| 79 | + public async renderPreviewHtml(template: string, variables?: Record<string, unknown>): Promise<string> { |
| 80 | + const templateName = String(template || '').trim() |
| 81 | + const filePath = path.join(this.getTemplatesDir(), `${templateName}.hbs`) |
| 82 | + const source = await fs.readFile(filePath, 'utf8') |
| 83 | + |
| 84 | + const compiled = Handlebars.compile(source, { strict: true }) |
| 85 | + return compiled({ |
| 86 | + // Valeurs minimales pour éviter les crash sur templates existants |
| 87 | + displayName: 'Preview', |
| 88 | + uid: 'preview', |
| 89 | + url: 'https://example.invalid/initaccount/preview', |
| 90 | + mail: 'preview@example.invalid', |
| 91 | + ...(variables || {}), |
| 92 | + }) |
| 93 | + } |
| 94 | +} |
| 95 | + |
0 commit comments