refacto(services): only one getCurrentUser

This commit is contained in:
Julien Froidefond
2025-02-23 15:19:32 +01:00
parent 1cffe6913a
commit 442f318be8
4 changed files with 39 additions and 46 deletions

View File

@@ -1,7 +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";
import { AuthServerService } from "./auth-server.service";
interface User {
id: string;
@@ -24,23 +24,16 @@ interface TTLConfigData {
}
export class ConfigDBService {
private static async getCurrentUser(): Promise<User> {
const userCookie = cookies().get("stripUser");
if (!userCookie) {
throw new Error("Utilisateur non authentifié");
}
try {
return JSON.parse(atob(userCookie.value));
} catch (error) {
console.error("Erreur lors de la récupération de l'utilisateur depuis le cookie:", error);
private static getCurrentUser(): User {
const user = AuthServerService.getCurrentUser();
if (!user) {
throw new Error("Utilisateur non authentifié");
}
return user;
}
static async saveConfig(data: KomgaConfigData) {
const user = await this.getCurrentUser();
const user = this.getCurrentUser();
await connectDB();
const config = await KomgaConfig.findOneAndUpdate(
@@ -58,7 +51,7 @@ export class ConfigDBService {
}
static async getConfig() {
const user = await this.getCurrentUser();
const user = this.getCurrentUser();
await connectDB();
const config = await KomgaConfig.findOne({ userId: user.id });
@@ -71,7 +64,7 @@ export class ConfigDBService {
}
static async saveTTLConfig(data: TTLConfigData) {
const user = await this.getCurrentUser();
const user = this.getCurrentUser();
await connectDB();
const config = await TTLConfig.findOneAndUpdate(
@@ -87,7 +80,7 @@ export class ConfigDBService {
}
static async getTTLConfig() {
const user = await this.getCurrentUser();
const user = this.getCurrentUser();
await connectDB();
const config = await TTLConfig.findOne({ userId: user.id });

View File

@@ -1,6 +1,6 @@
import { cookies } from "next/headers";
import connectDB from "@/lib/mongodb";
import { FavoriteModel } from "@/lib/models/favorite.model";
import { AuthServerService } from "./auth-server.service";
interface User {
id: string;
@@ -17,19 +17,12 @@ export class FavoriteService {
}
}
private static async getCurrentUser(): Promise<User> {
const userCookie = cookies().get("stripUser");
if (!userCookie) {
throw new Error("Utilisateur non authentifié");
}
try {
return JSON.parse(atob(userCookie.value));
} catch (error) {
console.error("Erreur lors de la récupération de l'utilisateur depuis le cookie:", error);
private static getCurrentUser(): User {
const user = AuthServerService.getCurrentUser();
if (!user) {
throw new Error("Utilisateur non authentifié");
}
return user;
}
/**
@@ -37,7 +30,7 @@ export class FavoriteService {
*/
static async isFavorite(seriesId: string): Promise<boolean> {
try {
const user = await this.getCurrentUser();
const user = this.getCurrentUser();
await connectDB();
const favorite = await FavoriteModel.findOne({
@@ -57,7 +50,7 @@ export class FavoriteService {
*/
static async addToFavorites(seriesId: string): Promise<void> {
try {
const user = await this.getCurrentUser();
const user = this.getCurrentUser();
await connectDB();
await FavoriteModel.findOneAndUpdate(
@@ -78,7 +71,7 @@ export class FavoriteService {
*/
static async removeFromFavorites(seriesId: string): Promise<void> {
try {
const user = await this.getCurrentUser();
const user = this.getCurrentUser();
await connectDB();
await FavoriteModel.findOneAndDelete({
@@ -98,7 +91,7 @@ export class FavoriteService {
*/
static async getAllFavoriteIds(): Promise<string[]> {
try {
const user = await this.getCurrentUser();
const user = this.getCurrentUser();
await connectDB();
const favorites = await FavoriteModel.find({ userId: user.id });

View File

@@ -1,5 +1,5 @@
import { cookies } from "next/headers";
import { PreferencesModel } from "@/lib/models/preferences.model";
import { AuthServerService } from "./auth-server.service";
interface User {
id: string;
@@ -21,24 +21,17 @@ const defaultPreferences: UserPreferences = {
};
export class PreferencesService {
static async getCurrentUser(): Promise<User> {
const userCookie = cookies().get("stripUser");
if (!userCookie) {
throw new Error("Utilisateur non authentifié");
}
try {
return JSON.parse(atob(userCookie.value));
} catch (error) {
console.error("Erreur lors de la récupération de l'utilisateur depuis le cookie:", error);
static getCurrentUser(): User {
const user = AuthServerService.getCurrentUser();
if (!user) {
throw new Error("Utilisateur non authentifié");
}
return user;
}
static async getPreferences(): Promise<UserPreferences> {
try {
const user = await this.getCurrentUser();
const user = this.getCurrentUser();
const preferences = await PreferencesModel.findOne({ userId: user.id });
if (!preferences) {
return defaultPreferences;
@@ -55,7 +48,7 @@ export class PreferencesService {
static async updatePreferences(preferences: Partial<UserPreferences>): Promise<UserPreferences> {
try {
const user = await this.getCurrentUser();
const user = this.getCurrentUser();
const updatedPreferences = await PreferencesModel.findOneAndUpdate(
{ userId: user.id },
{ $set: preferences },