PSA/server/migrations/utils/templates/_shared/upsertEmailTemplates.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

76 lines
2.6 KiB
JavaScript

/**
* Upsert utility for system_email_templates.
*
* Used by both migrations and seeds to insert or update email templates
* from the source-of-truth template files.
*/
/**
* Upsert one email template (all language variants) into system_email_templates.
*
* @param {import('knex').Knex} knex
* @param {Object} templateDef - Template definition from a source file's getTemplate()
* @param {string} templateDef.templateName - e.g., 'ticket-created'
* @param {string} templateDef.subtypeName - notification_subtypes.name for FK lookup
* @param {Array<{language: string, subject: string, htmlContent: string, textContent: string}>} templateDef.translations
* @param {Object} [options]
* @param {boolean} [options.skipMissingSubtype=false] - Warn and skip instead of throwing if subtype not found
*/
async function upsertEmailTemplate(knex, templateDef, options = {}) {
const { templateName, subtypeName, translations } = templateDef;
const subtype = await knex('notification_subtypes')
.where({ name: subtypeName })
.first();
if (!subtype) {
if (options.skipMissingSubtype) {
console.warn(`[upsertEmailTemplate] Subtype '${subtypeName}' not found, skipping '${templateName}'`);
return;
}
throw new Error(`Notification subtype '${subtypeName}' not found for template '${templateName}'`);
}
const now = new Date();
const rows = translations.map(t => ({
name: templateName,
language_code: t.language,
subject: t.subject,
html_content: t.htmlContent,
text_content: t.textContent,
notification_subtype_id: subtype.id,
updated_at: now,
}));
await knex('system_email_templates')
.insert(rows)
.onConflict(['name', 'language_code'])
.merge(['subject', 'html_content', 'text_content', 'notification_subtype_id', 'updated_at']);
}
/**
* Upsert multiple email templates at once.
*
* @param {import('knex').Knex} knex
* @param {Array<Object>} templateDefs - Array of template definitions from getTemplate()
* @param {Object} [options]
* @param {boolean} [options.skipMissingSubtype=false]
*/
async function upsertEmailTemplates(knex, templateDefs, options = {}) {
for (const def of templateDefs) {
await upsertEmailTemplate(knex, def, options);
}
}
/**
* Delete all language variants of an email template by name.
*
* @param {import('knex').Knex} knex
* @param {string} templateName
*/
async function deleteEmailTemplate(knex, templateName) {
await knex('system_email_templates').where({ name: templateName }).del();
}
module.exports = { upsertEmailTemplate, upsertEmailTemplates, deleteEmailTemplate };