refacto(db): TTL conf in mongo
This commit is contained in:
47
src/app/api/komga/ttl-config/route.ts
Normal file
47
src/app/api/komga/ttl-config/route.ts
Normal file
@@ -0,0 +1,47 @@
|
||||
import { NextResponse } from "next/server";
|
||||
import { ConfigDBService } from "@/lib/services/config-db.service";
|
||||
|
||||
export async function GET() {
|
||||
try {
|
||||
const config = await ConfigDBService.getTTLConfig();
|
||||
return NextResponse.json(config);
|
||||
} catch (error) {
|
||||
console.error("Erreur lors de la récupération de la configuration TTL:", error);
|
||||
if (error instanceof Error) {
|
||||
if (error.message === "Utilisateur non authentifié") {
|
||||
return NextResponse.json({ error: "Non autorisé" }, { status: 401 });
|
||||
}
|
||||
}
|
||||
return NextResponse.json(
|
||||
{ error: "Erreur lors de la récupération de la configuration TTL" },
|
||||
{ status: 500 }
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
export async function POST(request: Request) {
|
||||
try {
|
||||
const data = await request.json();
|
||||
const config = await ConfigDBService.saveTTLConfig(data);
|
||||
return NextResponse.json({
|
||||
message: "Configuration TTL sauvegardée avec succès",
|
||||
config: {
|
||||
defaultTTL: config.defaultTTL,
|
||||
homeTTL: config.homeTTL,
|
||||
librariesTTL: config.librariesTTL,
|
||||
seriesTTL: config.seriesTTL,
|
||||
booksTTL: config.booksTTL,
|
||||
imagesTTL: config.imagesTTL,
|
||||
},
|
||||
});
|
||||
} catch (error) {
|
||||
console.error("Erreur lors de la sauvegarde de la configuration TTL:", error);
|
||||
if (error instanceof Error && error.message === "Utilisateur non authentifié") {
|
||||
return NextResponse.json({ error: "Non autorisé" }, { status: 401 });
|
||||
}
|
||||
return NextResponse.json(
|
||||
{ error: "Erreur lors de la sauvegarde de la configuration TTL" },
|
||||
{ status: 500 }
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -3,10 +3,11 @@ import { ClientSettings } from "@/components/settings/ClientSettings";
|
||||
|
||||
export default async function SettingsPage() {
|
||||
let config = null;
|
||||
let ttlConfig = null;
|
||||
|
||||
try {
|
||||
// Récupérer la configuration Komga
|
||||
const mongoConfig = await ConfigDBService.getConfig();
|
||||
// Convertir le document Mongoose en objet simple
|
||||
if (mongoConfig) {
|
||||
config = {
|
||||
url: mongoConfig.url,
|
||||
@@ -15,10 +16,13 @@ export default async function SettingsPage() {
|
||||
userId: mongoConfig.userId,
|
||||
};
|
||||
}
|
||||
|
||||
// Récupérer la configuration TTL
|
||||
ttlConfig = await ConfigDBService.getTTLConfig();
|
||||
} catch (error) {
|
||||
console.error("Erreur lors de la récupération de la configuration:", error);
|
||||
// On ne fait rien si la config n'existe pas, on laissera le composant client gérer l'état initial
|
||||
}
|
||||
|
||||
return <ClientSettings initialConfig={config} />;
|
||||
return <ClientSettings initialConfig={config} initialTTLConfig={ttlConfig} />;
|
||||
}
|
||||
|
||||
@@ -18,12 +18,21 @@ interface KomgaConfig {
|
||||
userId: string;
|
||||
}
|
||||
|
||||
interface ClientSettingsProps {
|
||||
initialConfig: KomgaConfig | null;
|
||||
interface TTLConfigData {
|
||||
defaultTTL: number;
|
||||
homeTTL: number;
|
||||
librariesTTL: number;
|
||||
seriesTTL: number;
|
||||
booksTTL: number;
|
||||
imagesTTL: number;
|
||||
}
|
||||
|
||||
export function ClientSettings({ initialConfig }: ClientSettingsProps) {
|
||||
console.log("initialConfig", initialConfig);
|
||||
interface ClientSettingsProps {
|
||||
initialConfig: KomgaConfig | null;
|
||||
initialTTLConfig: TTLConfigData | null;
|
||||
}
|
||||
|
||||
export function ClientSettings({ initialConfig, initialTTLConfig }: ClientSettingsProps) {
|
||||
const router = useRouter();
|
||||
const { toast } = useToast();
|
||||
const [isLoading, setIsLoading] = useState(false);
|
||||
@@ -36,22 +45,16 @@ export function ClientSettings({ initialConfig }: ClientSettingsProps) {
|
||||
username: initialConfig?.username || "",
|
||||
password: initialConfig?.password || "",
|
||||
});
|
||||
const [ttlConfig, setTTLConfig] = useState({
|
||||
const [ttlConfig, setTTLConfig] = useState<TTLConfigData>(
|
||||
initialTTLConfig || {
|
||||
defaultTTL: 5,
|
||||
homeTTL: 5,
|
||||
librariesTTL: 1440,
|
||||
seriesTTL: 5,
|
||||
booksTTL: 5,
|
||||
imagesTTL: 1440,
|
||||
});
|
||||
|
||||
useEffect(() => {
|
||||
// Charger la configuration des TTL
|
||||
const savedTTLConfig = storageService.getTTLConfig();
|
||||
if (savedTTLConfig) {
|
||||
setTTLConfig(savedTTLConfig);
|
||||
}
|
||||
}, []);
|
||||
);
|
||||
|
||||
const handleClearCache = async () => {
|
||||
setIsCacheClearing(true);
|
||||
@@ -206,15 +209,37 @@ export function ClientSettings({ initialConfig }: ClientSettingsProps) {
|
||||
}));
|
||||
};
|
||||
|
||||
const handleSaveTTL = (event: React.FormEvent<HTMLFormElement>) => {
|
||||
const handleSaveTTL = async (event: React.FormEvent<HTMLFormElement>) => {
|
||||
event.preventDefault();
|
||||
setSuccess(null);
|
||||
|
||||
storageService.setTTLConfig(ttlConfig);
|
||||
try {
|
||||
const response = await fetch("/api/komga/ttl-config", {
|
||||
method: "POST",
|
||||
headers: {
|
||||
"Content-Type": "application/json",
|
||||
},
|
||||
body: JSON.stringify(ttlConfig),
|
||||
});
|
||||
|
||||
if (!response.ok) {
|
||||
const data = await response.json();
|
||||
throw new Error(data.error || "Erreur lors de la sauvegarde de la configuration TTL");
|
||||
}
|
||||
|
||||
toast({
|
||||
title: "Configuration TTL sauvegardée",
|
||||
description: "La configuration des TTL a été sauvegardée avec succès",
|
||||
});
|
||||
} catch (error) {
|
||||
console.error("Erreur lors de la sauvegarde:", error);
|
||||
toast({
|
||||
variant: "destructive",
|
||||
title: "Erreur",
|
||||
description:
|
||||
error instanceof Error ? error.message : "Une erreur est survenue lors de la sauvegarde",
|
||||
});
|
||||
}
|
||||
};
|
||||
|
||||
return (
|
||||
|
||||
46
src/lib/models/ttl-config.model.ts
Normal file
46
src/lib/models/ttl-config.model.ts
Normal file
@@ -0,0 +1,46 @@
|
||||
import mongoose from "mongoose";
|
||||
|
||||
const ttlConfigSchema = new mongoose.Schema(
|
||||
{
|
||||
userId: {
|
||||
type: String,
|
||||
required: true,
|
||||
unique: true,
|
||||
},
|
||||
defaultTTL: {
|
||||
type: Number,
|
||||
default: 5,
|
||||
},
|
||||
homeTTL: {
|
||||
type: Number,
|
||||
default: 5,
|
||||
},
|
||||
librariesTTL: {
|
||||
type: Number,
|
||||
default: 1440,
|
||||
},
|
||||
seriesTTL: {
|
||||
type: Number,
|
||||
default: 5,
|
||||
},
|
||||
booksTTL: {
|
||||
type: Number,
|
||||
default: 5,
|
||||
},
|
||||
imagesTTL: {
|
||||
type: Number,
|
||||
default: 1440,
|
||||
},
|
||||
},
|
||||
{
|
||||
timestamps: true,
|
||||
}
|
||||
);
|
||||
|
||||
// Middleware pour mettre à jour le champ updatedAt avant la sauvegarde
|
||||
ttlConfigSchema.pre("save", function (next) {
|
||||
this.updatedAt = new Date();
|
||||
next();
|
||||
});
|
||||
|
||||
export const TTLConfig = mongoose.models.TTLConfig || mongoose.model("TTLConfig", ttlConfigSchema);
|
||||
@@ -1,6 +1,7 @@
|
||||
import { cookies } from "next/headers";
|
||||
import connectDB from "@/lib/mongodb";
|
||||
import { KomgaConfig } from "@/lib/models/config.model";
|
||||
import { TTLConfig } from "@/lib/models/ttl-config.model";
|
||||
|
||||
interface User {
|
||||
id: string;
|
||||
@@ -13,6 +14,15 @@ interface KomgaConfigData {
|
||||
password: string;
|
||||
}
|
||||
|
||||
interface TTLConfigData {
|
||||
defaultTTL: number;
|
||||
homeTTL: number;
|
||||
librariesTTL: number;
|
||||
seriesTTL: number;
|
||||
booksTTL: number;
|
||||
imagesTTL: number;
|
||||
}
|
||||
|
||||
export class ConfigDBService {
|
||||
private static async getCurrentUser(): Promise<User> {
|
||||
const userCookie = cookies().get("stripUser");
|
||||
@@ -59,4 +69,48 @@ export class ConfigDBService {
|
||||
|
||||
return config;
|
||||
}
|
||||
|
||||
static async saveTTLConfig(data: TTLConfigData) {
|
||||
const user = await this.getCurrentUser();
|
||||
await connectDB();
|
||||
|
||||
const config = await TTLConfig.findOneAndUpdate(
|
||||
{ userId: user.id },
|
||||
{
|
||||
userId: user.id,
|
||||
...data,
|
||||
},
|
||||
{ upsert: true, new: true }
|
||||
);
|
||||
|
||||
return config;
|
||||
}
|
||||
|
||||
static async getTTLConfig() {
|
||||
const user = await this.getCurrentUser();
|
||||
await connectDB();
|
||||
|
||||
const config = await TTLConfig.findOne({ userId: user.id });
|
||||
|
||||
if (!config) {
|
||||
// Retourner la configuration par défaut si aucune configuration n'existe
|
||||
return {
|
||||
defaultTTL: 5,
|
||||
homeTTL: 5,
|
||||
librariesTTL: 1440,
|
||||
seriesTTL: 5,
|
||||
booksTTL: 5,
|
||||
imagesTTL: 1440,
|
||||
};
|
||||
}
|
||||
|
||||
return {
|
||||
defaultTTL: config.defaultTTL,
|
||||
homeTTL: config.homeTTL,
|
||||
librariesTTL: config.librariesTTL,
|
||||
seriesTTL: config.seriesTTL,
|
||||
booksTTL: config.booksTTL,
|
||||
imagesTTL: config.imagesTTL,
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user