Skip to content

Commit 59105cc

Browse files
committed
feat: add mail template preview functionality with default context
- Introduced MAIL_TEMPLATE_PREVIEW_DEFAULTS for consistent preview data in UI. - Added buildMailTemplatePreviewContext function to merge default values with provided variables. - Updated mail template compilation to use non-strict mode, ensuring previews do not fail on runtime variables.
1 parent 94803b0 commit 59105cc

2 files changed

Lines changed: 58 additions & 9 deletions

File tree

apps/api/src/management/mail/mail-templates.service.ts

Lines changed: 28 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,31 @@ export function isUserSendableMailTemplate(templateName: string): boolean {
1515
.startsWith(USER_SENDABLE_MAIL_TEMPLATE_PREFIX)
1616
}
1717

18+
/** Valeurs fictives pour l’aperçu UI (variables runtime non prédictibles, ex. code de reset). */
19+
export const MAIL_TEMPLATE_PREVIEW_DEFAULTS: Record<string, unknown> = {
20+
displayName: 'Jean Dupont (aperçu)',
21+
uid: 'preview.user',
22+
url: 'https://example.invalid/preview',
23+
mail: 'preview@example.invalid',
24+
code: '123456',
25+
token: 'preview-token-exemple',
26+
subject: 'Sujet (aperçu)',
27+
appName: 'Sesame',
28+
title: 'Titre (aperçu)',
29+
message: 'Message exemple pour l’aperçu du template.',
30+
ctaUrl: 'https://example.invalid/preview/action',
31+
ctaLabel: 'Ouvrir (aperçu)',
32+
hibpCount: 0,
33+
}
34+
35+
/** Contexte Handlebars pour l’aperçu : défauts + variables fournies. */
36+
export function buildMailTemplatePreviewContext(variables?: Record<string, unknown>): Record<string, unknown> {
37+
return {
38+
...MAIL_TEMPLATE_PREVIEW_DEFAULTS,
39+
...(variables && typeof variables === 'object' ? variables : {}),
40+
}
41+
}
42+
1843
@Injectable()
1944
export class MailTemplatesService implements OnApplicationBootstrap {
2045
private readonly logger = new Logger(MailTemplatesService.name)
@@ -96,15 +121,9 @@ export class MailTemplatesService implements OnApplicationBootstrap {
96121
const filePath = path.join(this.getTemplatesDir(), `${templateName}.hbs`)
97122
const source = await fs.readFile(filePath, 'utf8')
98123

99-
const compiled = Handlebars.compile(source, { strict: true })
100-
return compiled({
101-
// Valeurs minimales pour éviter les crash sur templates existants
102-
displayName: 'Preview',
103-
uid: 'preview',
104-
url: 'https://example.invalid/initaccount/preview',
105-
mail: 'preview@example.invalid',
106-
...(variables || {}),
107-
})
124+
// strict: false + défauts : l’aperçu ne doit pas échouer sur des variables runtime (ex. code reset).
125+
const compiled = Handlebars.compile(source, { strict: false })
126+
return compiled(buildMailTemplatePreviewContext(variables))
108127
}
109128
}
110129

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import Handlebars from 'handlebars'
2+
import {
3+
buildMailTemplatePreviewContext,
4+
MAIL_TEMPLATE_PREVIEW_DEFAULTS,
5+
} from '~/management/mail/mail-templates.service'
6+
7+
describe('buildMailTemplatePreviewContext', () => {
8+
it('should provide default code for reset templates', () => {
9+
const ctx = buildMailTemplatePreviewContext()
10+
expect(ctx.code).toBe(MAIL_TEMPLATE_PREVIEW_DEFAULTS.code)
11+
})
12+
13+
it('should allow overriding preview variables', () => {
14+
const ctx = buildMailTemplatePreviewContext({ code: '999999' })
15+
expect(ctx.code).toBe('999999')
16+
})
17+
18+
it('should not throw on unknown variables in non-strict preview mode', () => {
19+
const ctx = buildMailTemplatePreviewContext()
20+
const compiled = Handlebars.compile('Hello {{unknownVar}}', { strict: false })
21+
expect(() => compiled(ctx)).not.toThrow()
22+
expect(compiled(ctx)).toBe('Hello ')
23+
})
24+
25+
it('should render resetaccount-style code placeholder', () => {
26+
const ctx = buildMailTemplatePreviewContext()
27+
const compiled = Handlebars.compile('Votre code : {{ code }}', { strict: false })
28+
expect(compiled(ctx)).toContain('123456')
29+
})
30+
})

0 commit comments

Comments
 (0)