- Replace connect/disconnect pattern with persistent connection pool - Add explicit database initialization on server startup - Remove all dbDisconnect() calls from API endpoints to prevent race conditions - Fix MongoNotConnectedError when scheduler runs concurrently with API requests - Add connection pooling with proper MongoDB driver options - Add safety check for recipes array in favorites utility 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude <noreply@anthropic.com>
54 lines
1.5 KiB
TypeScript
54 lines
1.5 KiB
TypeScript
/**
|
|
* Utility functions for handling user favorites on the server side
|
|
*/
|
|
|
|
export async function getUserFavorites(fetch: any, locals: any): Promise<string[]> {
|
|
const session = await locals.auth();
|
|
|
|
if (!session?.user?.nickname) {
|
|
return [];
|
|
}
|
|
|
|
try {
|
|
const favRes = await fetch('/api/rezepte/favorites');
|
|
if (favRes.ok) {
|
|
const favData = await favRes.json();
|
|
return favData.favorites || [];
|
|
}
|
|
} catch (e) {
|
|
// Silently fail if favorites can't be loaded
|
|
console.error('Error loading user favorites:', e);
|
|
}
|
|
|
|
return [];
|
|
}
|
|
|
|
export function addFavoriteStatusToRecipes(recipes: any[], userFavorites: string[]): any[] {
|
|
// Safety check: ensure recipes is an array
|
|
if (!Array.isArray(recipes)) {
|
|
console.error('addFavoriteStatusToRecipes: recipes is not an array:', recipes);
|
|
return [];
|
|
}
|
|
|
|
return recipes.map(recipe => ({
|
|
...recipe,
|
|
isFavorite: userFavorites.some(favId => favId.toString() === recipe._id.toString())
|
|
}));
|
|
}
|
|
|
|
export async function loadRecipesWithFavorites(
|
|
fetch: any,
|
|
locals: any,
|
|
recipeLoader: () => Promise<any>
|
|
): Promise<{ recipes: any[], session: any }> {
|
|
const [recipes, userFavorites, session] = await Promise.all([
|
|
recipeLoader(),
|
|
getUserFavorites(fetch, locals),
|
|
locals.auth()
|
|
]);
|
|
|
|
return {
|
|
recipes: addFavoriteStatusToRecipes(recipes, userFavorites),
|
|
session
|
|
};
|
|
} |