PSA/server/migrations/20250430143709_add_standard_template_fields.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

96 lines
3.7 KiB
JavaScript

const fs = require('fs/promises');
const crypto = require('crypto');
const path = require('path');
/**
* Helper function to read file content, calculate SHA256 hash, and update the template record.
* @param { import("knex").Knex } knex
* @param {string} templateCode The standard_invoice_template_code to update.
* @param {string} relativeFilePath The path to the AssemblyScript source file, relative to the migration file.
*/
const updateTemplateFromFile = async (knex, templateCode, relativeFilePath) => {
try {
// Construct the absolute path relative to the migration file's directory
const filePath = path.resolve(__dirname, '..', relativeFilePath); // Go up one level from /migrations
console.log(`Reading file for ${templateCode} from: ${filePath}`);
const fileContent = await fs.readFile(filePath, 'utf-8');
const hash = crypto.createHash('sha256');
hash.update(fileContent);
const calculatedSha = hash.digest('hex');
console.log(`Calculated SHA for ${templateCode}: ${calculatedSha}`);
const updateResult = await knex('standard_invoice_templates')
.where({ standard_invoice_template_code: templateCode })
.update({
assemblyScriptSource: fileContent,
sha: calculatedSha,
});
if (updateResult === 0) {
console.warn(`No standard template found with code '${templateCode}' to update.`);
} else {
console.log(`Successfully updated template '${templateCode}' with source and SHA.`);
}
} catch (error) {
console.error(`Error updating template ${templateCode} from file ${relativeFilePath}:`, error);
// Re-throw the error to ensure the migration fails if any file operation or update fails
throw error;
}
};
/**
* @param { import("knex").Knex } knex
* @returns { Promise<void> }
*/
exports.up = async function(knex) {
// Add the new columns
await knex.schema.alterTable('standard_invoice_templates', (table) => {
table.string('standard_invoice_template_code'); // Using string (VARCHAR) as TEXT might be overkill unless very long codes are expected
table.string('sha'); // SHA hashes are fixed length, string is appropriate
table.text('assemblyScriptSource'); // Source code can be long, TEXT is suitable
});
// Update the standard_invoice_template_code for existing standard templates
await knex('standard_invoice_templates')
.where('name', 'Detailed Template')
.update({ standard_invoice_template_code: 'standard-detailed' });
await knex('standard_invoice_templates')
.where('name', 'Standard Template')
.update({ standard_invoice_template_code: 'standard-default' });
// Now, populate the assemblyScriptSource and sha fields from files
await updateTemplateFromFile(
knex,
'standard-default',
'src/invoice-templates/assemblyscript/standard/standard-default.ts' // Corrected path relative to server/ directory
);
await updateTemplateFromFile(
knex,
'standard-detailed',
'src/invoice-templates/assemblyscript/standard/standard-detailed.ts' // Corrected path relative to server/ directory
);
};
/**
* @param { import("knex").Knex } knex
* @returns { Promise<void> }
*/
exports.down = async function(knex) {
// No need to explicitly nullify the codes before dropping the column,
// but it could be done here if needed for specific rollback scenarios.
// await knex('standard_invoice_templates')
// .whereIn('standard_invoice_template_code', ['standard-detailed', 'standard-default'])
// .update({ standard_invoice_template_code: null });
// Drop the columns
await knex.schema.alterTable('standard_invoice_templates', (table) => {
table.dropColumn('standard_invoice_template_code');
table.dropColumn('sha');
table.dropColumn('assemblyScriptSource');
});
};