/** * 去掉 nav_menu_layout 中的「履约作业」分组,并将验收照片复核放入「业务流程」。 * Usage (from server dir): node ../scripts/fix-nav-remove-fulfillment-ops.cjs [--execute] */ const path = require('path'); const fs = require('fs'); function loadDotenv() { const candidates = [ path.join(process.cwd(), '.env'), path.join(process.cwd(), 'server', '.env'), path.join(__dirname, '../server/.env'), ]; for (const file of candidates) { if (!fs.existsSync(file)) continue; try { require('dotenv').config({ path: file }); return; } catch (_) { /* try next */ } } } loadDotenv(); const { Pool } = require('pg'); const execute = process.argv.includes('--execute'); function patchLayout(raw) { const layout = typeof raw === 'string' ? JSON.parse(raw) : JSON.parse(JSON.stringify(raw)); if (!layout || !Array.isArray(layout.groups)) return { layout, changed: false }; let changed = false; let flow = layout.groups.find((g) => String(g.label || '').trim() === '业务流程') || layout.groups.find((g) => String(g.label || '').trim() === '验收管理') || layout.groups.find((g) => g.id === 'grp-inspection' || g.id === 'grp-business'); if (!flow) { flow = { id: 'grp-business', label: '业务流程', icon: 'CheckCircle', visible: true, items: [], }; layout.groups.push(flow); if ( Array.isArray(layout.order) && !layout.order.some((e) => e.kind === 'group' && e.groupId === flow.id) ) { const financeIdx = layout.order.findIndex( (e) => e.kind === 'group' && e.groupId === 'grp-finance' ); if (financeIdx >= 0) layout.order.splice(financeIdx, 0, { kind: 'group', groupId: flow.id }); else layout.order.push({ kind: 'group', groupId: flow.id }); } changed = true; } const assigned = new Set(); for (const g of layout.groups) { for (const item of g.items || []) assigned.add(item.menuKey); } const legacyIds = []; layout.groups = layout.groups.filter((g) => { if (String(g.label || '').trim() !== '履约作业') return true; legacyIds.push(g.id); for (const item of g.items || []) { if (assigned.has(item.menuKey)) continue; flow.items.push({ ...item }); assigned.add(item.menuKey); changed = true; } changed = true; return false; }); if (legacyIds.length && Array.isArray(layout.order)) { layout.order = layout.order.filter( (e) => !(e.kind === 'group' && legacyIds.includes(e.groupId)) ); } if (!flow.items.some((i) => i.menuKey === 'inspection-review')) { flow.items.push({ menuKey: 'inspection-review', visible: true }); changed = true; } return { layout, changed }; } (async () => { if (!process.env.DATABASE_URL) { console.error('DATABASE_URL missing'); process.exit(1); } const pool = new Pool({ connectionString: process.env.DATABASE_URL }); const rows = await pool.query( `SELECT id, branch_code, config_value FROM system_configs WHERE config_key = 'nav_menu_layout'` ); for (const row of rows.rows) { const { layout, changed } = patchLayout(row.config_value); const groups = (layout.groups || []).map((g) => ({ id: g.id, label: g.label, items: (g.items || []).map((i) => i.menuKey), })); console.log(`branch=${row.branch_code ?? 'platform'} changed=${changed}`); console.log(JSON.stringify(groups, null, 2)); if (changed && execute) { await pool.query(`UPDATE system_configs SET config_value = $1::jsonb WHERE id = $2`, [ JSON.stringify(layout), row.id, ]); console.log(' -> updated'); } } if (!execute) console.log('\nDry run. Re-run with --execute to write.'); await pool.end(); })().catch((e) => { console.error(e); process.exit(1); });