diff --git a/packages/backend/src/routes/songs.ts b/packages/backend/src/routes/songs.ts index 07d35e6..562d4ce 100644 --- a/packages/backend/src/routes/songs.ts +++ b/packages/backend/src/routes/songs.ts @@ -4,6 +4,101 @@ import { Playlist } from '../models/Playlist.js'; const router = express.Router(); +// Debug endpoint to check playlist data +router.get('/debug/playlist/:name', async (req: Request, res: Response) => { + try { + const playlistName = req.params.name; + console.log(`Debug: Checking playlist "${playlistName}"`); + + // Get all playlists + const allPlaylists = await Playlist.find({}); + console.log(`Debug: Found ${allPlaylists.length} playlist documents`); + + // Find the specific playlist + const findPlaylistRecursively = (nodes: any[], targetName: string): any => { + for (const node of nodes) { + if (node.name === targetName) { + return node; + } + if (node.children && node.children.length > 0) { + const found = findPlaylistRecursively(node.children, targetName); + if (found) return found; + } + } + return null; + }; + + let playlist = null; + for (const playlistDoc of allPlaylists) { + playlist = findPlaylistRecursively([playlistDoc], playlistName); + if (playlist) break; + } + + if (!playlist) { + // List all available playlists + const getAllPlaylistNames = (nodes: any[]): string[] => { + const names: string[] = []; + for (const node of nodes) { + names.push(node.name); + if (node.children && node.children.length > 0) { + names.push(...getAllPlaylistNames(node.children)); + } + } + return names; + }; + + const allNames: string[] = []; + for (const playlistDoc of allPlaylists) { + allNames.push(...getAllPlaylistNames([playlistDoc])); + } + + return res.json({ + error: `Playlist "${playlistName}" not found`, + availablePlaylists: allNames, + playlistDocuments: allPlaylists.map(p => ({ name: p.name, type: p.type })) + }); + } + + // Get track IDs from playlist + const getAllTrackIds = (node: any): string[] => { + if (node.type === 'playlist' && node.tracks) { + return node.tracks; + } + if (node.type === 'folder' && node.children) { + return node.children.flatMap((child: any) => getAllTrackIds(child)); + } + return []; + }; + + const trackIds = getAllTrackIds(playlist); + + // Check if songs exist for these track IDs + const songs = await Song.find({ id: { $in: trackIds } }).lean(); + const songIds = songs.map(s => s.id); + + res.json({ + playlist: { + name: playlist.name, + type: playlist.type, + trackIds: trackIds, + trackCount: trackIds.length + }, + songs: { + found: songIds, + foundCount: songs.length, + missing: trackIds.filter(id => !songIds.includes(id)) + }, + debug: { + playlistStructure: playlist, + allPlaylistDocuments: allPlaylists.length + } + }); + } catch (error) { + console.error('Debug error:', error); + res.status(500).json({ error: 'Debug endpoint error', details: error }); + } +}); + // Get songs with pagination and search router.get('/', async (req: Request, res: Response) => { try {