feat: debugmode full request

This commit is contained in:
Julien Froidefond
2025-02-23 15:12:59 +01:00
parent 1cffe6913a
commit 6b19f5b54b
18 changed files with 721 additions and 133 deletions

View File

@@ -1,6 +1,8 @@
import { cookies } from "next/headers";
import connectDB from "@/lib/mongodb";
import { FavoriteModel } from "@/lib/models/favorite.model";
import { DebugService } from "./debug.service";
import { AuthServerService } from "./auth-server.service";
interface User {
id: string;
@@ -17,19 +19,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,15 +32,16 @@ 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({
userId: user.id,
seriesId: seriesId,
return DebugService.measureMongoOperation("isFavorite", async () => {
const favorite = await FavoriteModel.findOne({
userId: user.id,
seriesId: seriesId,
});
return !!favorite;
});
return !!favorite;
} catch (error) {
console.error("Erreur lors de la vérification du favori:", error);
return false;
@@ -57,14 +53,16 @@ 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(
{ userId: user.id, seriesId },
{ userId: user.id, seriesId },
{ upsert: true }
);
await DebugService.measureMongoOperation("addToFavorites", async () => {
await FavoriteModel.findOneAndUpdate(
{ userId: user.id, seriesId },
{ userId: user.id, seriesId },
{ upsert: true }
);
});
this.dispatchFavoritesChanged();
} catch (error) {
@@ -78,12 +76,14 @@ 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({
userId: user.id,
seriesId,
await DebugService.measureMongoOperation("removeFromFavorites", async () => {
await FavoriteModel.findOneAndDelete({
userId: user.id,
seriesId,
});
});
this.dispatchFavoritesChanged();
@@ -97,15 +97,36 @@ export class FavoriteService {
* Récupère tous les IDs des séries favorites
*/
static async getAllFavoriteIds(): Promise<string[]> {
try {
const user = await this.getCurrentUser();
await connectDB();
const user = this.getCurrentUser();
await connectDB();
return DebugService.measureMongoOperation("getAllFavoriteIds", async () => {
const favorites = await FavoriteModel.find({ userId: user.id });
return favorites.map((favorite) => favorite.seriesId);
} catch (error) {
console.error("Erreur lors de la récupération des favoris:", error);
return [];
}
});
}
static async addFavorite(seriesId: string) {
const user = this.getCurrentUser();
await connectDB();
return DebugService.measureMongoOperation("addFavorite", async () => {
const favorite = await FavoriteModel.findOneAndUpdate(
{ userId: user.id, seriesId },
{ userId: user.id, seriesId },
{ upsert: true, new: true }
);
return favorite;
});
}
static async removeFavorite(seriesId: string): Promise<boolean> {
const user = this.getCurrentUser();
await connectDB();
return DebugService.measureMongoOperation("removeFavorite", async () => {
const result = await FavoriteModel.deleteOne({ userId: user.id, seriesId });
return result.deletedCount > 0;
});
}
}