import { z } from "zod"; import { createTRPCRouter, publicProcedure } from "~/server/api/trpc"; import { getDatabase } from "~/server/database"; export const installedScriptsRouter = createTRPCRouter({ // Get all installed scripts getAllInstalledScripts: publicProcedure .query(async () => { try { const db = getDatabase(); const scripts = db.getAllInstalledScripts(); return { success: true, scripts }; } catch (error) { console.error('Error in getAllInstalledScripts:', error); return { success: false, error: error instanceof Error ? error.message : 'Failed to fetch installed scripts', scripts: [] }; } }), // Get installed scripts by server getInstalledScriptsByServer: publicProcedure .input(z.object({ serverId: z.number() })) .query(async ({ input }) => { try { const db = getDatabase(); const scripts = db.getInstalledScriptsByServer(input.serverId); return { success: true, scripts }; } catch (error) { console.error('Error in getInstalledScriptsByServer:', error); return { success: false, error: error instanceof Error ? error.message : 'Failed to fetch installed scripts by server', scripts: [] }; } }), // Get installed script by ID getInstalledScriptById: publicProcedure .input(z.object({ id: z.number() })) .query(async ({ input }) => { try { const db = getDatabase(); const script = db.getInstalledScriptById(input.id); if (!script) { return { success: false, error: 'Installed script not found', script: null }; } return { success: true, script }; } catch (error) { console.error('Error in getInstalledScriptById:', error); return { success: false, error: error instanceof Error ? error.message : 'Failed to fetch installed script', script: null }; } }), // Create new installed script record createInstalledScript: publicProcedure .input(z.object({ script_name: z.string(), script_path: z.string(), container_id: z.string().optional(), server_id: z.number().optional(), execution_mode: z.enum(['local', 'ssh']), status: z.enum(['in_progress', 'success', 'failed']), output_log: z.string().optional() })) .mutation(async ({ input }) => { try { const db = getDatabase(); const result = db.createInstalledScript(input); return { success: true, id: result.lastInsertRowid, message: 'Installed script record created successfully' }; } catch (error) { console.error('Error in createInstalledScript:', error); return { success: false, error: error instanceof Error ? error.message : 'Failed to create installed script record' }; } }), // Update installed script updateInstalledScript: publicProcedure .input(z.object({ id: z.number(), script_name: z.string().optional(), container_id: z.string().optional(), status: z.enum(['in_progress', 'success', 'failed']).optional(), output_log: z.string().optional() })) .mutation(async ({ input }) => { try { const { id, ...updateData } = input; const db = getDatabase(); const result = db.updateInstalledScript(id, updateData); if (result.changes === 0) { return { success: false, error: 'No changes made or script not found' }; } return { success: true, message: 'Installed script updated successfully' }; } catch (error) { console.error('Error in updateInstalledScript:', error); return { success: false, error: error instanceof Error ? error.message : 'Failed to update installed script' }; } }), // Delete installed script deleteInstalledScript: publicProcedure .input(z.object({ id: z.number() })) .mutation(async ({ input }) => { try { const db = getDatabase(); const result = db.deleteInstalledScript(input.id); if (result.changes === 0) { return { success: false, error: 'Script not found or already deleted' }; } return { success: true, message: 'Installed script deleted successfully' }; } catch (error) { console.error('Error in deleteInstalledScript:', error); return { success: false, error: error instanceof Error ? error.message : 'Failed to delete installed script' }; } }), // Get installation statistics getInstallationStats: publicProcedure .query(async () => { try { const db = getDatabase(); const allScripts = db.getAllInstalledScripts(); const stats = { total: allScripts.length, byStatus: { success: allScripts.filter((s: any) => s.status === 'success').length, failed: allScripts.filter((s: any) => s.status === 'failed').length, in_progress: allScripts.filter((s: any) => s.status === 'in_progress').length }, byMode: { local: allScripts.filter((s: any) => s.execution_mode === 'local').length, ssh: allScripts.filter((s: any) => s.execution_mode === 'ssh').length }, byServer: {} as Record }; // Count by server allScripts.forEach((script: any) => { const serverKey = script.server_name ?? 'Local'; stats.byServer[serverKey] = (stats.byServer[serverKey] ?? 0) + 1; }); return { success: true, stats }; } catch (error) { console.error('Error in getInstallationStats:', error); return { success: false, error: error instanceof Error ? error.message : 'Failed to fetch installation statistics', stats: null }; } }) });