tractatus/scripts/add-event-delegation.js
TheFlow 2af47035ac refactor: remove website code and fix critical startup crashes (Phase 8)
CRITICAL FIX: Server would CRASH ON STARTUP (multiple import errors)

REMOVED (2 scripts):
1. scripts/framework-watchdog.js
   - Monitored .claude/session-state.json (OUR Claude Code setup)
   - Monitored .claude/token-checkpoints.json (OUR file structure)
   - Implementers won't have our .claude/ directory

2. scripts/init-db.js
   - Created website collections: blog_posts, media_inquiries, case_submissions
   - Created website collections: resources, moderation_queue, users, citations
   - Created website collections: translations, koha_donations
   - Next steps referenced deleted scripts (npm run seed:admin)

REWRITTEN (2 files):

src/models/index.js (29 lines → 27 lines)
- REMOVED imports: Document, BlogPost, MediaInquiry, CaseSubmission, Resource
- REMOVED imports: ModerationQueue, User (all deleted in Phase 2)
- KEPT imports: AuditLog, DeliberationSession, GovernanceLog, GovernanceRule
- KEPT imports: Precedent, Project, SessionState, VariableValue, VerificationLog
- Result: Only framework models exported

src/server.js (284 lines → 163 lines, 43% reduction)
- REMOVED: Imports to deleted middleware (csrf-protection, response-sanitization)
- REMOVED: Stripe webhook handling (/api/koha/webhook)
- REMOVED: Static file caching (for deleted public/ directory)
- REMOVED: Static file serving (public/ deleted in Phase 6)
- REMOVED: CSRF token endpoint
- REMOVED: Website homepage with "auth, documents, blog, admin" references
- REMOVED: Instruction sync (scripts/sync-instructions-to-db.js reference)
- REMOVED: Hardcoded log path (${process.env.HOME}/var/log/tractatus/...)
- REMOVED: Website-specific security middleware
- KEPT: Security headers, rate limiting, CORS, body parsers
- KEPT: API routes, governance services, MongoDB connections
- RESULT: Clean framework-only server

RESULT: Repository can now start without crashes, all imports resolve

🤖 Generated with Claude Code
Co-Authored-By: Claude <noreply@anthropic.com>
2025-10-21 22:17:02 +13:00

121 lines
3.8 KiB
JavaScript

#!/usr/bin/env node
/**
* Add event delegation to remaining admin files
*/
const fs = require('fs');
const path = require('path');
// project-editor.js
const projectEditorFile = path.join(__dirname, '../public/js/admin/project-editor.js');
let projectEditorContent = fs.readFileSync(projectEditorFile, 'utf8');
const projectEditorDelegation = `
// Event delegation for data-action buttons (CSP compliance)
document.addEventListener('click', (e) => {
const button = e.target.closest('[data-action]');
if (!button) return;
const action = button.dataset.action;
const arg0 = button.dataset.arg0;
if (action === 'editVariable') {
window.projectEditor.editVariable(arg0);
} else if (action === 'deleteVariable') {
window.projectEditor.deleteVariable(arg0);
}
});
`;
if (!projectEditorContent.includes('Event delegation for data-action')) {
// Add before the end
projectEditorContent = projectEditorContent.trim() + '\n' + projectEditorDelegation;
fs.writeFileSync(projectEditorFile, projectEditorContent);
console.log('✓ Added event delegation to project-editor.js');
}
// rule-editor.js
const ruleEditorFile = path.join(__dirname, '../public/js/admin/rule-editor.js');
let ruleEditorContent = fs.readFileSync(ruleEditorFile, 'utf8');
const ruleEditorDelegation = `
// Event delegation for data-action buttons (CSP compliance)
document.addEventListener('click', (e) => {
const button = e.target.closest('[data-action]');
if (!button) return;
const action = button.dataset.action;
const arg0 = button.dataset.arg0;
switch (action) {
case 'editRule':
editRule(arg0);
break;
case 'remove-parent':
button.parentElement.remove();
break;
}
});
`;
if (!ruleEditorContent.includes('Event delegation for data-action')) {
ruleEditorContent = ruleEditorContent.trim() + '\n' + ruleEditorDelegation;
fs.writeFileSync(ruleEditorFile, ruleEditorContent);
console.log('✓ Added event delegation to rule-editor.js');
}
// audit-analytics.js
const auditFile = path.join(__dirname, '../public/js/admin/audit-analytics.js');
let auditContent = fs.readFileSync(auditFile, 'utf8');
const auditDelegation = `
// Event delegation for data-action buttons (CSP compliance)
document.addEventListener('click', (e) => {
const button = e.target.closest('[data-action]');
if (!button) return;
const action = button.dataset.action;
const arg0 = button.dataset.arg0;
if (action === 'showDecisionDetails') {
showDecisionDetails(arg0);
}
});
`;
if (!auditContent.includes('Event delegation for data-action')) {
auditContent = auditContent.trim() + '\n' + auditDelegation;
fs.writeFileSync(auditFile, auditContent);
console.log('✓ Added event delegation to audit-analytics.js');
}
// claude-md-migrator.js
const migratorFile = path.join(__dirname, '../public/js/admin/claude-md-migrator.js');
let migratorContent = fs.readFileSync(migratorFile, 'utf8');
const migratorDelegation = `
// Event delegation for data-change-action checkboxes (CSP compliance)
document.addEventListener('change', (e) => {
const checkbox = e.target.closest('[data-change-action]');
if (!checkbox) return;
const action = checkbox.dataset.changeAction;
const index = parseInt(checkbox.dataset.index);
if (action === 'toggleCandidate') {
// Need to get the candidate from the analysis based on index
if (window.currentAnalysis && window.currentAnalysis.candidates[index]) {
toggleCandidate(window.currentAnalysis.candidates[index], checkbox.checked);
}
}
});
`;
if (!migratorContent.includes('Event delegation for data-change-action')) {
migratorContent = migratorContent.trim() + '\n' + migratorDelegation;
fs.writeFileSync(migratorFile, migratorContent);
console.log('✓ Added event delegation to claude-md-migrator.js');
}
console.log('\n✅ Event delegation added to all remaining admin files\n');