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

@@ -0,0 +1,14 @@
"use client";
import { usePreferences } from "@/contexts/PreferencesContext";
import { DebugInfo } from "./DebugInfo";
export function DebugWrapper() {
const { preferences } = usePreferences();
if (!preferences.debug) {
return null;
}
return <DebugInfo />;
}

View File

@@ -1,7 +1,7 @@
import { cookies } from "next/headers";
import connectDB from "@/lib/mongodb"; import connectDB from "@/lib/mongodb";
import { KomgaConfig } from "@/lib/models/config.model"; import { KomgaConfig } from "@/lib/models/config.model";
import { TTLConfig } from "@/lib/models/ttl-config.model"; import { TTLConfig } from "@/lib/models/ttl-config.model";
import { AuthServerService } from "./auth-server.service";
interface User { interface User {
id: string; id: string;
@@ -24,23 +24,16 @@ interface TTLConfigData {
} }
export class ConfigDBService { export class ConfigDBService {
private static async getCurrentUser(): Promise<User> { private static getCurrentUser(): User {
const userCookie = cookies().get("stripUser"); const user = AuthServerService.getCurrentUser();
if (!user) {
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);
throw new Error("Utilisateur non authentifié"); throw new Error("Utilisateur non authentifié");
} }
return user;
} }
static async saveConfig(data: KomgaConfigData) { static async saveConfig(data: KomgaConfigData) {
const user = await this.getCurrentUser(); const user = this.getCurrentUser();
await connectDB(); await connectDB();
const config = await KomgaConfig.findOneAndUpdate( const config = await KomgaConfig.findOneAndUpdate(
@@ -58,7 +51,7 @@ export class ConfigDBService {
} }
static async getConfig() { static async getConfig() {
const user = await this.getCurrentUser(); const user = this.getCurrentUser();
await connectDB(); await connectDB();
const config = await KomgaConfig.findOne({ userId: user.id }); const config = await KomgaConfig.findOne({ userId: user.id });
@@ -71,7 +64,7 @@ export class ConfigDBService {
} }
static async saveTTLConfig(data: TTLConfigData) { static async saveTTLConfig(data: TTLConfigData) {
const user = await this.getCurrentUser(); const user = this.getCurrentUser();
await connectDB(); await connectDB();
const config = await TTLConfig.findOneAndUpdate( const config = await TTLConfig.findOneAndUpdate(
@@ -87,7 +80,7 @@ export class ConfigDBService {
} }
static async getTTLConfig() { static async getTTLConfig() {
const user = await this.getCurrentUser(); const user = this.getCurrentUser();
await connectDB(); await connectDB();
const config = await TTLConfig.findOne({ userId: user.id }); 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 connectDB from "@/lib/mongodb";
import { FavoriteModel } from "@/lib/models/favorite.model"; import { FavoriteModel } from "@/lib/models/favorite.model";
import { AuthServerService } from "./auth-server.service";
interface User { interface User {
id: string; id: string;
@@ -17,19 +17,12 @@ export class FavoriteService {
} }
} }
private static async getCurrentUser(): Promise<User> { private static getCurrentUser(): User {
const userCookie = cookies().get("stripUser"); const user = AuthServerService.getCurrentUser();
if (!user) {
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);
throw new Error("Utilisateur non authentifié"); throw new Error("Utilisateur non authentifié");
} }
return user;
} }
/** /**
@@ -37,7 +30,7 @@ export class FavoriteService {
*/ */
static async isFavorite(seriesId: string): Promise<boolean> { static async isFavorite(seriesId: string): Promise<boolean> {
try { try {
const user = await this.getCurrentUser(); const user = this.getCurrentUser();
await connectDB(); await connectDB();
const favorite = await FavoriteModel.findOne({ const favorite = await FavoriteModel.findOne({
@@ -57,7 +50,7 @@ export class FavoriteService {
*/ */
static async addToFavorites(seriesId: string): Promise<void> { static async addToFavorites(seriesId: string): Promise<void> {
try { try {
const user = await this.getCurrentUser(); const user = this.getCurrentUser();
await connectDB(); await connectDB();
await FavoriteModel.findOneAndUpdate( await FavoriteModel.findOneAndUpdate(
@@ -78,7 +71,7 @@ export class FavoriteService {
*/ */
static async removeFromFavorites(seriesId: string): Promise<void> { static async removeFromFavorites(seriesId: string): Promise<void> {
try { try {
const user = await this.getCurrentUser(); const user = this.getCurrentUser();
await connectDB(); await connectDB();
await FavoriteModel.findOneAndDelete({ await FavoriteModel.findOneAndDelete({
@@ -98,7 +91,7 @@ export class FavoriteService {
*/ */
static async getAllFavoriteIds(): Promise<string[]> { static async getAllFavoriteIds(): Promise<string[]> {
try { try {
const user = await this.getCurrentUser(); const user = this.getCurrentUser();
await connectDB(); await connectDB();
const favorites = await FavoriteModel.find({ userId: user.id }); 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 { PreferencesModel } from "@/lib/models/preferences.model";
import { AuthServerService } from "./auth-server.service";
interface User { interface User {
id: string; id: string;
@@ -21,24 +21,17 @@ const defaultPreferences: UserPreferences = {
}; };
export class PreferencesService { export class PreferencesService {
static async getCurrentUser(): Promise<User> { static getCurrentUser(): User {
const userCookie = cookies().get("stripUser"); const user = AuthServerService.getCurrentUser();
if (!user) {
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);
throw new Error("Utilisateur non authentifié"); throw new Error("Utilisateur non authentifié");
} }
return user;
} }
static async getPreferences(): Promise<UserPreferences> { static async getPreferences(): Promise<UserPreferences> {
try { try {
const user = await this.getCurrentUser(); const user = this.getCurrentUser();
const preferences = await PreferencesModel.findOne({ userId: user.id }); const preferences = await PreferencesModel.findOne({ userId: user.id });
if (!preferences) { if (!preferences) {
return defaultPreferences; return defaultPreferences;
@@ -55,7 +48,7 @@ export class PreferencesService {
static async updatePreferences(preferences: Partial<UserPreferences>): Promise<UserPreferences> { static async updatePreferences(preferences: Partial<UserPreferences>): Promise<UserPreferences> {
try { try {
const user = await this.getCurrentUser(); const user = this.getCurrentUser();
const updatedPreferences = await PreferencesModel.findOneAndUpdate( const updatedPreferences = await PreferencesModel.findOneAndUpdate(
{ userId: user.id }, { userId: user.id },
{ $set: preferences }, { $set: preferences },