'use strict'; const { db } = require('../db'); const DEFAULTS = { request_to_admin: { subject: 'Action needed: access request from {{username}} awaiting approval', body_html: '

Access request pending approval

' + '

{{username}} ({{email}}) has requested access to ' + 'the {{target_type}} {{target_name}}.

' + '

Requested at {{created_at}}. Request reference #{{request_id}}.

' + '

{{pending_count}} request(s) are currently waiting for a decision.

' + '

' + 'Review pending requests

' + '

If the button does not work, open: {{approvals_url}}

', }, approval_to_user: { subject: 'Your access request was approved', body_html: '

Request approved

' + '

Hello {{username}},

' + '

Your request to access the {{target_type}} {{target_name}} ' + 'has been approved. You may now open it from the View Data page.

' + '

{{validity}} — access expires on {{expires_at}}.

' + '

A live countdown is shown next to the folder on the View Data page.

', }, denial_to_user: { subject: 'Your access request was declined', body_html: '

Request declined

' + '

Hello {{username}},

' + '

Unfortunately your request to access the {{target_type}} ' + '{{target_name}} was not approved at this time.

', }, }; function seedDefaults() { const exists = db.prepare('SELECT key FROM email_templates WHERE key = ?'); const insert = db.prepare( 'INSERT INTO email_templates (key, subject, body_html) VALUES (?, ?, ?)' ); for (const [key, t] of Object.entries(DEFAULTS)) { if (!exists.get(key)) insert.run(key, t.subject, t.body_html); } } function getTemplate(key) { return ( db.prepare('SELECT * FROM email_templates WHERE key = ?').get(key) || DEFAULTS[key] ); } function render(str, vars) { return String(str).replace(/\{\{\s*([\w.]+)\s*\}\}/g, (_, k) => vars[k] !== undefined && vars[k] !== null ? String(vars[k]) : '' ); } /** Returns { subject, html } with placeholders filled and footer appended. */ function buildEmail(key, vars) { const t = getTemplate(key); const footer = '
' + '

' + '© 2026 Martinhal IT - Joao Vaz - Version 2.3

'; return { subject: render(t.subject, vars), html: render(t.body_html, vars) + footer, }; } const PLACEHOLDERS = { request_to_admin: ['username', 'email', 'target_type', 'target_name', 'created_at', 'request_id', 'pending_count', 'approvals_url', 'portal_url'], approval_to_user: ['username', 'email', 'target_type', 'target_name', 'portal_url', 'validity', 'expires_at'], denial_to_user: ['username', 'email', 'target_type', 'target_name', 'portal_url'], }; module.exports = { seedDefaults, getTemplate, render, buildEmail, DEFAULTS, PLACEHOLDERS, KEYS: Object.keys(DEFAULTS), };