refacto: error and error codes in services

This commit is contained in:
Julien Froidefond
2025-02-25 06:39:19 +01:00
parent d4871d1afb
commit 4b710cbac2
16 changed files with 389 additions and 125 deletions

View File

@@ -3,6 +3,8 @@ import { KomgaConfig } from "@/lib/models/config.model";
import { TTLConfig } from "@/lib/models/ttl-config.model";
import { DebugService } from "./debug.service";
import { AuthServerService } from "./auth-server.service";
import { ERROR_CODES } from "../../constants/errorCodes";
import { AppError } from "../../utils/errors";
interface User {
id: string;
@@ -29,66 +31,94 @@ export class ConfigDBService {
private static getCurrentUser(): User {
const user = AuthServerService.getCurrentUser();
if (!user) {
throw new Error("Utilisateur non authentifié");
throw new AppError(ERROR_CODES.AUTH.UNAUTHENTICATED);
}
return user;
}
static async saveConfig(data: KomgaConfigData) {
const user = this.getCurrentUser();
await connectDB();
try {
const user = this.getCurrentUser();
await connectDB();
const authHeader = Buffer.from(`${data.username}:${data.password}`).toString("base64");
const authHeader = Buffer.from(`${data.username}:${data.password}`).toString("base64");
const config = await KomgaConfig.findOneAndUpdate(
{ userId: user.id },
{
userId: user.id,
url: data.url,
username: data.username,
// password: data.password,
authHeader,
},
{ upsert: true, new: true }
);
return config;
}
static async getConfig() {
const user = this.getCurrentUser();
await connectDB();
return DebugService.measureMongoOperation("getConfig", async () => {
const config = await KomgaConfig.findOne({ userId: user.id });
return config;
});
}
static async getTTLConfig() {
const user = this.getCurrentUser();
await connectDB();
return DebugService.measureMongoOperation("getTTLConfig", async () => {
const config = await TTLConfig.findOne({ userId: user.id });
return config;
});
}
static async saveTTLConfig(data: TTLConfigData) {
const user = this.getCurrentUser();
await connectDB();
return DebugService.measureMongoOperation("saveTTLConfig", async () => {
const config = await TTLConfig.findOneAndUpdate(
const config = await KomgaConfig.findOneAndUpdate(
{ userId: user.id },
{
userId: user.id,
...data,
url: data.url,
username: data.username,
// password: data.password,
authHeader,
},
{ upsert: true, new: true }
);
return config;
});
} catch (error) {
if (error instanceof AppError) {
throw error;
}
throw new AppError(ERROR_CODES.CONFIG.SAVE_ERROR, {}, error);
}
}
static async getConfig() {
try {
const user = this.getCurrentUser();
await connectDB();
return DebugService.measureMongoOperation("getConfig", async () => {
const config = await KomgaConfig.findOne({ userId: user.id });
return config;
});
} catch (error) {
if (error instanceof AppError) {
throw error;
}
throw new AppError(ERROR_CODES.CONFIG.FETCH_ERROR, {}, error);
}
}
static async getTTLConfig() {
try {
const user = this.getCurrentUser();
await connectDB();
return DebugService.measureMongoOperation("getTTLConfig", async () => {
const config = await TTLConfig.findOne({ userId: user.id });
return config;
});
} catch (error) {
if (error instanceof AppError) {
throw error;
}
throw new AppError(ERROR_CODES.CONFIG.TTL_FETCH_ERROR, {}, error);
}
}
static async saveTTLConfig(data: TTLConfigData) {
try {
const user = this.getCurrentUser();
await connectDB();
return DebugService.measureMongoOperation("saveTTLConfig", async () => {
const config = await TTLConfig.findOneAndUpdate(
{ userId: user.id },
{
userId: user.id,
...data,
},
{ upsert: true, new: true }
);
return config;
});
} catch (error) {
if (error instanceof AppError) {
throw error;
}
throw new AppError(ERROR_CODES.CONFIG.TTL_SAVE_ERROR, {}, error);
}
}
}