Files
back_end_oudelaa/scripts/preview-emails.js

59 lines
4.3 KiB
JavaScript

'use strict';
// Renders only the pure template module. Never loads .env, AuthService,
// EmailService, Nest, or Nodemailer; never creates or sends a real OTP.
const { copyFileSync, existsSync, mkdirSync, writeFileSync } = require('node:fs');
const { join } = require('node:path');
const { renderOtpEmail } = require('../src/modules/email/templates/otp-email.template');
const root = join(__dirname, '..');
const output = join(root, 'preview', 'emails');
const logoSource = join(root, 'oudelaa_dashboard', 'public', 'logo.png');
const hasLocalLogo = existsSync(logoSource);
const logoPlaceholder = 'https://email-preview.invalid/oudelaa-logo.png';
const samples = [
{ purpose: 'signup', label: 'تسجيل حساب', expiresMinutes: 20 },
{ purpose: 'email-verification', label: 'تفعيل البريد', expiresMinutes: 7 },
{ purpose: 'password-reset', label: 'استعادة كلمة المرور', expiresMinutes: 13 },
];
mkdirSync(output, { recursive: true });
if (hasLocalLogo) {
mkdirSync(join(output, 'assets'), { recursive: true });
copyFileSync(logoSource, join(output, 'assets', 'logo.png'));
}
for (const sample of samples) {
const input = { purpose: sample.purpose, code: '000000', expiresMinutes: sample.expiresMinutes };
const message = renderOtpEmail(input);
writeFileSync(join(output, `${sample.purpose}.html`), message.html, 'utf8');
writeFileSync(join(output, `${sample.purpose}.txt`), message.text, 'utf8');
if (hasLocalLogo) {
const withLogo = renderOtpEmail({ ...input, logoUrl: logoPlaceholder });
// Preview-only substitution of a trusted constant. The production renderer
// continues to accept HTTPS images only; no remote image is fetched here.
writeFileSync(join(output, `${sample.purpose}.with-logo.html`),
withLogo.html.replace(`src="${logoPlaceholder}"`, 'src="assets/logo.png"'), 'utf8');
}
}
// This stylesheet belongs to the preview gallery only, never to sent emails.
const sections = samples.map((sample) => {
const file = `${sample.purpose}${hasLocalLogo ? '.with-logo' : ''}.html`;
return `<section><h2>${sample.label}</h2>
<p><a href="${file}">فتح الرسالة</a> · <a href="${sample.purpose}.txt">Plain text</a> · <a href="${sample.purpose}.html">البديل دون صورة</a></p>
<div class="screens"><div><h3>سطح المكتب · 600px</h3><iframe title="${sample.label} — سطح المكتب" src="${file}" width="600" height="900" sandbox=""></iframe></div>
<div><h3>هاتف · 360px</h3><iframe title="${sample.label} — هاتف" src="${file}" width="360" height="900" sandbox=""></iframe></div></div></section>`;
}).join('\n');
writeFileSync(join(output, 'index.html'), `<!DOCTYPE html>
<html lang="ar" dir="rtl"><head><meta charset="utf-8"><meta name="viewport" content="width=device-width,initial-scale=1"><title>OUDELAA — معاينة البريد</title>
<style>body{margin:0;background:#F5F1EB;color:#352315;font-family:Tahoma,Arial,sans-serif;line-height:1.9}main{max-width:1050px;margin:40px auto;padding:0 20px}h1{font-size:30px;margin:0}h2{margin-top:36px}h3{font-size:14px;font-weight:normal}a{color:#352315;text-underline-offset:4px}.note{padding:16px 20px;border:1px solid #C5A16A;border-radius:12px;background:white}.screens{display:flex;gap:24px;align-items:flex-start;overflow:auto}iframe{display:block;border:1px solid #E6DDD0;border-radius:12px;background:#F5F1EB;max-width:none}section{border-top:1px solid #D9CDBD;margin-top:32px;padding-bottom:12px}</style></head>
<body><main><p dir="ltr">OUDELAA / EMAIL STUDIO</p><h1>رسائل تحمل هويتنا.</h1>
<p>تسجيل حساب · تفعيل البريد الإلكتروني · استعادة كلمة المرور</p>
<div class="note">معاينة محلية ببيانات وهمية فقط، دون إرسال بريد أو تحميل إعدادات الإنتاج. المدد هنا أمثلة متنوعة؛ الرسائل الفعلية تعرض المدة التي يمررها تدفق OTP الحالي.${hasLocalLogo ? '<br>الشعار المعروض نسخة محلية من شعار المشروع. للإرسال يلزم رابط HTTPS عام في EMAIL_LOGO_URL. الاسم النصي يبقى ظاهرًا عند حجب الصور.' : ''}</div>
${sections}</main></body></html>`, 'utf8');
console.log(`Email previews created: ${join(output, 'index.html')}`);
console.log('Local dummy content only. No email sent.');