PSA/server/migrations/20251227100000_fix_email_template_html_escaping.cjs
Hermes 284313f908
Some checks are pending
Bidi Control Character Guard / bidi-control-guard (push) Waiting to run
Circular Dependency Check / Check for new circular dependencies (push) Waiting to run
Citus Migration Smoke / Combined migrations on single-node Citus (push) Waiting to run
E2E Fresh Install Tests / fresh-install-e2e (push) Waiting to run
ext-v2 guardrails / Run ext-v2 guard and ESLint (push) Waiting to run
Integration Tests / Check for relevant changes (push) Waiting to run
Integration Tests / ${{ (github.event_name == 'schedule' || github.event.inputs.suite == 'full') && 'Full integration suite' || 'Tier-1 integration subset' }} (push) Blocked by required conditions
Mobile checks / Mobile lint + typecheck (push) Waiting to run
Mobile checks / Mobile unit tests (push) Waiting to run
Mobile checks / Mobile dependency audit (report) (push) Waiting to run
Mobile checks / Mobile reproducibility checks (push) Waiting to run
Secrets guard (env backups) / Ensure no tracked env backup files (push) Waiting to run
Temporal Readiness / fast-readiness (push) Waiting to run
Temporal Readiness / docker-parity (push) Waiting to run
TypeScript Type Check / Nx affected typecheck (push) Waiting to run
Unit Tests / Skipped-test budget (push) Waiting to run
Unit Tests / Nx affected unit tests (push) Waiting to run
Unit Tests / Server unit coverage (informational) (push) Waiting to run
Validate Tenant Management Schema / Check for relevant changes (push) Waiting to run
Validate Tenant Management Schema / Validate Tenant Management Schema (push) Blocked by required conditions
EE Workflows Build Guard / ee-workflows-build-guard (push) Waiting to run
Initial import of AlgaPSA codebase from PSA server
Excluded: .git, node_modules, secrets/, compose.env, assemblyscript tgz

Source: /opt/alga-psa on psa.joliet.tech
2026-06-22 16:12:17 -05:00

80 lines
2.9 KiB
JavaScript

/**
* Fix HTML content escaping in email templates
*
* The email templates use Handlebars which escapes HTML by default with double braces {{...}}.
* Fields that contain HTML content (like comment.content, ticket.description, etc.) need to
* use triple braces {{{...}}} to render the HTML properly instead of showing raw HTML tags.
*
* This affects ticket notification emails where:
* - comment.content contains HTML from BlockNote editor
* - ticket.description may contain HTML
* - ticket.changes may contain HTML
* - ticket.resolution may contain HTML
*/
exports.up = async function(knex) {
console.log('Fixing HTML content escaping in email templates...');
// List of HTML content fields that need triple braces
// We only update html_content column since text is derived from rendered HTML
const htmlFields = [
{ double: '{{comment.content}}', triple: '{{{comment.content}}}' },
{ double: '{{ticket.description}}', triple: '{{{ticket.description}}}' },
{ double: '{{ticket.changes}}', triple: '{{{ticket.changes}}}' },
{ double: '{{ticket.resolution}}', triple: '{{{ticket.resolution}}}' },
];
// Update system_email_templates
for (const field of htmlFields) {
// Only update html_content where double braces exist (case-sensitive match)
await knex.raw(`
UPDATE system_email_templates
SET html_content = REPLACE(html_content, ?, ?)
WHERE html_content LIKE ?
`, [field.double, field.triple, `%${field.double}%`]);
}
// Also update tenant_email_templates if any exist
for (const field of htmlFields) {
await knex.raw(`
UPDATE tenant_email_templates
SET html_content = REPLACE(html_content, ?, ?)
WHERE html_content LIKE ?
`, [field.double, field.triple, `%${field.double}%`]);
}
console.log('✓ Email templates updated to properly render HTML content');
};
exports.down = async function(knex) {
console.log('Reverting HTML content escaping fix in email templates...');
// Revert back to double braces
const htmlFields = [
{ double: '{{comment.content}}', triple: '{{{comment.content}}}' },
{ double: '{{ticket.description}}', triple: '{{{ticket.description}}}' },
{ double: '{{ticket.changes}}', triple: '{{{ticket.changes}}}' },
{ double: '{{ticket.resolution}}', triple: '{{{ticket.resolution}}}' },
];
// Revert system_email_templates
for (const field of htmlFields) {
await knex.raw(`
UPDATE system_email_templates
SET html_content = REPLACE(html_content, ?, ?)
WHERE html_content LIKE ?
`, [field.triple, field.double, `%${field.triple}%`]);
}
// Also revert tenant_email_templates
for (const field of htmlFields) {
await knex.raw(`
UPDATE tenant_email_templates
SET html_content = REPLACE(html_content, ?, ?)
WHERE html_content LIKE ?
`, [field.triple, field.double, `%${field.triple}%`]);
}
console.log('Email templates reverted to escape HTML content');
};