refacto(services): only one getCurrentUser
This commit is contained in:
@@ -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 },
|
||||
|
||||
Reference in New Issue
Block a user