import SftpClient from "ssh2-sftp-client"; import { localStorageGet } from "./localStorage"; import { getUserSettings } from "./db"; export interface SftpConfig { host: string; port: number; username: string; password: string; remotePath: string; } /** * Test SFTP connection */ export async function testSftpConnection(config: SftpConfig): Promise { const sftp = new SftpClient(); try { await sftp.connect({ host: config.host, port: config.port, username: config.username, password: config.password, }); console.log("[SFTP] Connection successful"); return true; } catch (error) { console.error("[SFTP] Connection failed:", error); return false; } finally { await sftp.end(); } } /** * Export invoice files (PDF + JSON) to SFTP server */ export async function exportInvoiceToSftp( config: SftpConfig, pdfFileKey: string, jsonFileKey: string | null, invoiceDate: Date ): Promise { const sftp = new SftpClient(); try { // Connect to SFTP await sftp.connect({ host: config.host, port: config.port, username: config.username, password: config.password, }); console.log("[SFTP] Connected successfully"); // Create directory structure: remotePath/YYYY/MM/DD const year = invoiceDate.getFullYear(); const month = String(invoiceDate.getMonth() + 1).padStart(2, "0"); const day = String(invoiceDate.getDate()).padStart(2, "0"); const targetDir = `${config.remotePath}/${year}/${month}/${day}`.replace(/\/+/g, "/"); // Ensure directory exists await sftp.mkdir(targetDir, true); console.log(`[SFTP] Created directory: ${targetDir}`); // Upload PDF file const pdfBuffer = await localStorageGet(pdfFileKey); const pdfFileName = pdfFileKey.split("/").pop() || "invoice.pdf"; const pdfRemotePath = `${targetDir}/${pdfFileName}`; await sftp.put(pdfBuffer, pdfRemotePath); console.log(`[SFTP] Uploaded PDF: ${pdfRemotePath}`); // Upload JSON metadata file if exists if (jsonFileKey) { const jsonBuffer = await localStorageGet(jsonFileKey); const jsonFileName = jsonFileKey.split("/").pop() || "metadata.json"; const jsonRemotePath = `${targetDir}/${jsonFileName}`; await sftp.put(jsonBuffer, jsonRemotePath); console.log(`[SFTP] Uploaded JSON: ${jsonRemotePath}`); } console.log("[SFTP] Export completed successfully"); } catch (error) { console.error("[SFTP] Export failed:", error); throw error; } finally { await sftp.end(); } } /** * Get SFTP configuration for a user */ export async function getUserSftpConfig(userId: number): Promise { const settings = await getUserSettings(userId); if (!settings || !settings.sftpHost || !settings.sftpUsername || !settings.sftpPassword) { return null; } return { host: settings.sftpHost, port: settings.sftpPort || 22, username: settings.sftpUsername, password: settings.sftpPassword, remotePath: settings.sftpRemotePath || "/", }; }