PSA/server/migrations/20250814090000_update_system_email_processing_workflow.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

103 lines
4.4 KiB
JavaScript
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

// Updates the DB-stored System Email Processing workflow code to the latest
// implementation from services/workflow-worker/src/workflows/system-email-processing-workflow.ts
// by embedding the function body into a DB-executable wrapper.
const fs = require('fs');
const path = require('path');
// Helper: minimal TS→JS sanitization (strip casts and simple type annotations)
function sanitizeTsToJs(source) {
let s = source;
// Remove " as any" casts
s = s.replace(/\s+as\s+any\b/g, '');
// Normalize catch parameter annotations like "catch (e: any)"
s = s.replace(/catch\s*\(\s*([A-Za-z_$][\w$]*)\s*:\s*any\s*\)/g, 'catch ($1)');
// Also cover rare arrow/param annotations inside inline functions (very limited scope)
s = s.replace(/\(\s*([A-Za-z_$][\w$]*)\s*:\s*any\s*\)/g, '($1)');
return s;
}
// Helper: build DB-ready code from the shared workflow source
function buildDbWorkflowCode() {
// Prefer pre-generated JS file if present (compiled and sanitized)
const generatedPath = path.join(__dirname, '../../services/workflow-worker/src/workflows/system-email-processing-workflow.generated.js');
if (fs.existsSync(generatedPath)) {
const code = fs.readFileSync(generatedPath, 'utf8');
if (code && code.includes('function execute(')) {
return code;
}
console.warn('[workflow-migration] Generated file found but does not contain execute() — falling back to TS extraction');
}
const workflowPath = path.join(__dirname, '../../services/workflow-worker/src/workflows/system-email-processing-workflow.ts');
if (!fs.existsSync(workflowPath)) {
console.warn(`[workflow-migration] Shared workflow file not found at ${workflowPath}`);
return null;
}
const src = fs.readFileSync(workflowPath, 'utf8');
const fnSig = 'export async function systemEmailProcessingWorkflow(context) {';
const start = src.indexOf(fnSig);
if (start === -1) {
console.warn('[workflow-migration] Could not locate systemEmailProcessingWorkflow function signature');
return null;
}
const openBraceIx = src.indexOf('{', start);
const afterOpen = src.substring(openBraceIx + 1);
let body = afterOpen.substring(0, afterOpen.lastIndexOf('}')).trim();
body = sanitizeTsToJs(body);
// Wrap body in a function named `execute` expected by the runtime
return `async function execute(context) {\n${body}\n}`;
}
exports.up = async function up(knex) {
console.log('[workflow-migration] Updating System Email Processing workflow code...');
// Attempt to run the generator to ensure the latest code is available
try {
const { spawnSync } = require('child_process');
const scriptPath = path.join(__dirname, '../../scripts/generate-system-email-workflow.cjs');
// The generator script is not part of the repo; skip quietly instead of
// spawning a missing file on every fresh bootstrap (the fallback below
// handles it either way).
if (require('fs').existsSync(scriptPath)) {
const result = spawnSync(process.execPath, [scriptPath], { stdio: 'inherit' });
if (result.status !== 0) {
console.warn('[workflow-migration] Generator script exited with non-zero status; proceeding with fallback.');
}
}
} catch (e) {
console.warn('[workflow-migration] Failed to run generator script; proceeding with fallback.', e && e.message ? e.message : e);
}
const dbCode = buildDbWorkflowCode();
if (!dbCode) {
console.log('[workflow-migration] Skipping update; unable to construct DB workflow code');
return;
}
// Find the system email processing workflow registration by name
const registration = await knex('system_workflow_registrations')
.where({ name: 'System Email Processing' })
.first();
if (!registration) {
console.log('[workflow-migration] System Email Processing registration not found; skipping');
return;
}
// Update all versions for this registration to ensure consistency
const updated = await knex('system_workflow_registration_versions')
.where({ registration_id: registration.registration_id })
.update({ code: dbCode, updated_at: new Date().toISOString() });
console.log(`[workflow-migration] Updated ${updated} workflow version(s) for registration ${registration.registration_id}`);
};
exports.down = async function down(_knex) {
// No-op: we dont re-embed old code. Manual rollback would be needed if required.
console.log('[workflow-migration] Down migration is a no-op.');
};