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

78 lines
2.6 KiB
JavaScript

/**
* Upsert utility for internal_notification_templates.
*
* Used by both migrations and seeds to insert or update internal notification
* templates from the source-of-truth template files.
*/
/**
* Upsert one internal notification template (all language variants).
*
* @param {import('knex').Knex} knex
* @param {Object} templateDef
* @param {string} templateDef.templateName - e.g., 'ticket-assigned'
* @param {string} templateDef.subtypeName - internal_notification_subtypes.name for FK lookup
* @param {Object<string, {title: string, message: string}>} templateDef.translations
* Keyed by language code, e.g., { en: { title: '...', message: '...' }, fr: { ... } }
* @param {Object} [options]
* @param {boolean} [options.skipMissingSubtype=false]
*/
async function upsertInternalTemplate(knex, templateDef, options = {}) {
const { templateName, subtypeName, translations } = templateDef;
const subtype = await knex('internal_notification_subtypes')
.where({ name: subtypeName })
.first();
if (!subtype) {
if (options.skipMissingSubtype) {
console.warn(`[upsertInternalTemplate] Subtype '${subtypeName}' not found, skipping '${templateName}'`);
return;
}
throw new Error(`Internal notification subtype '${subtypeName}' not found for template '${templateName}'`);
}
const rows = Object.entries(translations).map(([lang, { title, message }]) => ({
name: templateName,
language_code: lang,
title,
message,
subtype_id: subtype.internal_notification_subtype_id,
}));
await knex('internal_notification_templates')
.insert(rows)
.onConflict(['name', 'language_code'])
.merge({
title: knex.raw('excluded.title'),
message: knex.raw('excluded.message'),
subtype_id: knex.raw('excluded.subtype_id'),
});
}
/**
* Upsert multiple internal notification templates at once.
*
* @param {import('knex').Knex} knex
* @param {Array<Object>} templateDefs - Array of template definitions
* @param {Object} [options]
* @param {boolean} [options.skipMissingSubtype=false]
*/
async function upsertInternalTemplates(knex, templateDefs, options = {}) {
for (const def of templateDefs) {
await upsertInternalTemplate(knex, def, options);
}
}
/**
* Delete all language variants of an internal notification template.
*
* @param {import('knex').Knex} knex
* @param {string} templateName
*/
async function deleteInternalTemplate(knex, templateName) {
await knex('internal_notification_templates').where({ name: templateName }).del();
}
module.exports = { upsertInternalTemplate, upsertInternalTemplates, deleteInternalTemplate };