mirror of
https://github.com/R0m1k3/CollectFlow.git
synced 2026-10-19 05:06:56 +02:00
Add a "Qlik Sense — Données réseau" section in Paramètres (host/user/password + Save & Test buttons), stored in data/.db-config.json. qlik-client reads this file first (env fallback), so no env vars needed for the sync. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
129 lines
4.5 KiB
TypeScript
129 lines
4.5 KiB
TypeScript
"use server";
|
|
|
|
import { Pool } from "pg";
|
|
import fs from "fs/promises";
|
|
import path from "path";
|
|
|
|
const DATA_DIR = path.join(process.cwd(), "data");
|
|
const CONFIG_FILE = path.join(DATA_DIR, ".db-config.json");
|
|
|
|
export interface DbConfig {
|
|
url: string;
|
|
openRouterKey?: string;
|
|
openRouterModel?: string;
|
|
aiProvider?: "openrouter" | "google";
|
|
googleAiKey?: string;
|
|
googleAiModel?: string;
|
|
/** Qlik Sense — données réseau */
|
|
qlikHost?: string;
|
|
qlikUser?: string;
|
|
qlikPassword?: string;
|
|
}
|
|
|
|
/** Lit la config existante (ou {} si absente). */
|
|
async function readConfig(): Promise<Partial<DbConfig>> {
|
|
try {
|
|
const data = await fs.readFile(CONFIG_FILE, "utf-8");
|
|
return JSON.parse(data);
|
|
} catch {
|
|
return {};
|
|
}
|
|
}
|
|
|
|
export async function testDatabaseConnection(url: string) {
|
|
console.log("Testing connection to:", url.replace(/:([^@]+)@/, ":****@"));
|
|
const pool = new Pool({
|
|
connectionString: url,
|
|
connectionTimeoutMillis: 5000,
|
|
});
|
|
|
|
try {
|
|
const client = await pool.connect();
|
|
await client.query("SELECT 1");
|
|
client.release();
|
|
return { success: true };
|
|
} catch (error: unknown) {
|
|
const errorMessage = error instanceof Error ? error.message : String(error);
|
|
console.error("Database connection test failed:", errorMessage);
|
|
return { success: false, error: errorMessage };
|
|
} finally {
|
|
await pool.end();
|
|
}
|
|
}
|
|
|
|
export async function saveDatabaseSettings(
|
|
url: string,
|
|
openRouterKey?: string,
|
|
openRouterModel?: string,
|
|
aiProvider?: "openrouter" | "google",
|
|
googleAiKey?: string,
|
|
googleAiModel?: string,
|
|
) {
|
|
try {
|
|
// Merge avec l'existant pour préserver les autres réglages (ex: Qlik)
|
|
const existing = await readConfig();
|
|
const config: DbConfig = { ...existing, url, openRouterKey, openRouterModel, aiProvider, googleAiKey, googleAiModel };
|
|
|
|
// S'assurer que le dossier data existe
|
|
await fs.mkdir(DATA_DIR, { recursive: true });
|
|
|
|
console.log(`[Settings] Saving config to ${CONFIG_FILE}`);
|
|
await fs.writeFile(CONFIG_FILE, JSON.stringify(config, null, 2));
|
|
console.log("[Settings] Database configuration saved successfully.");
|
|
|
|
// Refresh the shared DB instance immediately
|
|
const { refreshDb } = await import("@/db");
|
|
refreshDb();
|
|
|
|
return { success: true };
|
|
} catch (error: unknown) {
|
|
const errorMessage = error instanceof Error ? error.message : String(error);
|
|
console.error("Failed to save database configuration:", errorMessage);
|
|
return { success: false, error: errorMessage };
|
|
}
|
|
}
|
|
|
|
export async function saveQlikSettings(qlikHost: string, qlikUser: string, qlikPassword: string) {
|
|
try {
|
|
const existing = await readConfig();
|
|
const config = { ...existing, qlikHost, qlikUser, qlikPassword } as DbConfig;
|
|
await fs.mkdir(DATA_DIR, { recursive: true });
|
|
await fs.writeFile(CONFIG_FILE, JSON.stringify(config, null, 2));
|
|
console.log("[Settings] Qlik configuration saved.");
|
|
return { success: true };
|
|
} catch (error: unknown) {
|
|
const msg = error instanceof Error ? error.message : String(error);
|
|
console.error("[Settings] Failed to save Qlik config:", msg);
|
|
return { success: false, error: msg };
|
|
}
|
|
}
|
|
|
|
export async function testQlikConnection(qlikHost: string, qlikUser: string, qlikPassword: string) {
|
|
try {
|
|
const { getQlikConfig, qlikNtlmSession } = await import("@/lib/qlik-client");
|
|
const cfg = { ...getQlikConfig(), user: qlikUser, password: qlikPassword };
|
|
if (qlikHost) cfg.host = qlikHost;
|
|
await qlikNtlmSession(cfg);
|
|
return { success: true };
|
|
} catch (error: unknown) {
|
|
const msg = error instanceof Error ? error.message : String(error);
|
|
return { success: false, error: msg };
|
|
}
|
|
}
|
|
|
|
export async function getSavedDatabaseConfig(): Promise<DbConfig | null> {
|
|
try {
|
|
if (!(await fs.stat(CONFIG_FILE).catch(() => null))) {
|
|
console.log(`[Settings] Config file not found at ${CONFIG_FILE}`);
|
|
return null;
|
|
}
|
|
const data = await fs.readFile(CONFIG_FILE, "utf-8");
|
|
const config = JSON.parse(data);
|
|
console.log(`[Settings] Config read from ${CONFIG_FILE}. Key present: ${!!config.openRouterKey}, Model: ${config.openRouterModel || "default"}`);
|
|
return config;
|
|
} catch (error) {
|
|
console.error(`[Settings] Error reading config from ${CONFIG_FILE}:`, error);
|
|
return null;
|
|
}
|
|
}
|