refacto(db): TTL conf in mongo
This commit is contained in:
@@ -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