-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfix_history.js
More file actions
40 lines (34 loc) · 1.31 KB
/
fix_history.js
File metadata and controls
40 lines (34 loc) · 1.31 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
const fs = require('fs');
const path = 'backend/routes/users.js';
let content = fs.readFileSync(path, 'utf8');
// Find the history route start
const start = content.indexOf("router.get('/history'");
// Find the NEXT router. after the history route to get the exact end
const afterStart = content.indexOf("router.", start + 10);
if (start === -1) { console.log('Not found!'); process.exit(1); }
const newRoute = `router.get('/history', authMiddleware, async(req, res) => {
try {
const result = await pool.query(
\`SELECT s.*, a.name AS artist_name, a.image_url AS artist_image
FROM songs s
LEFT JOIN artists a ON s.artist_id = a.id
WHERE s.id IN (
SELECT song_id FROM listening_history WHERE user_id = $1
)
ORDER BY (
SELECT MAX(listened_at) FROM listening_history
WHERE user_id = $1 AND song_id = s.id
) DESC
LIMIT 20\`, [req.userId]
);
res.json(result.rows);
} catch (err) {
res.status(500).json({ error: 'Failed to fetch history' });
}
});
// `;
const fixed = content.substring(0, start) + newRoute + content.substring(afterStart);
fs.writeFileSync(path, fixed);
console.log('Fixed! New history section:');
const i = fixed.indexOf("router.get('/history'");
console.log(fixed.substring(i, i + 500));