88 lines
3.0 KiB
TypeScript
88 lines
3.0 KiB
TypeScript
import { NextResponse } from 'next/server';
|
|
import { PrismaClient } from '@prisma/client';
|
|
|
|
const prisma = new PrismaClient();
|
|
|
|
export async function GET() {
|
|
try {
|
|
// Use timezone from environment variable (default: Europe/Berlin)
|
|
const timezone = process.env.TZ || 'Europe/Berlin';
|
|
const today = new Date().toLocaleDateString('en-CA', {
|
|
timeZone: timezone,
|
|
year: 'numeric',
|
|
month: '2-digit',
|
|
day: '2-digit'
|
|
}); // Format: "2025-11-21"
|
|
|
|
let dailyPuzzle = await prisma.dailyPuzzle.findUnique({
|
|
where: { date: today },
|
|
include: { song: true },
|
|
});
|
|
|
|
console.log(`[Daily Puzzle] Date: ${today}, Found existing: ${!!dailyPuzzle}`);
|
|
|
|
if (!dailyPuzzle) {
|
|
// Get all songs with their usage count
|
|
const allSongs = await prisma.song.findMany({
|
|
include: {
|
|
puzzles: true,
|
|
},
|
|
});
|
|
|
|
if (allSongs.length === 0) {
|
|
return NextResponse.json({ error: 'No songs available' }, { status: 404 });
|
|
}
|
|
|
|
// Calculate weights: songs never used get weight 1.0,
|
|
// songs used once get 0.5, twice 0.33, etc.
|
|
const weightedSongs = allSongs.map(song => ({
|
|
song,
|
|
weight: 1.0 / (song.puzzles.length + 1),
|
|
}));
|
|
|
|
// Calculate total weight
|
|
const totalWeight = weightedSongs.reduce((sum, item) => sum + item.weight, 0);
|
|
|
|
// Pick a random song based on weights
|
|
let random = Math.random() * totalWeight;
|
|
let selectedSong = weightedSongs[0].song;
|
|
|
|
for (const item of weightedSongs) {
|
|
random -= item.weight;
|
|
if (random <= 0) {
|
|
selectedSong = item.song;
|
|
break;
|
|
}
|
|
}
|
|
|
|
// Create the daily puzzle
|
|
dailyPuzzle = await prisma.dailyPuzzle.create({
|
|
data: {
|
|
date: today,
|
|
songId: selectedSong.id,
|
|
},
|
|
include: { song: true },
|
|
});
|
|
|
|
console.log(`[Daily Puzzle] Created new puzzle for ${today} with song: ${selectedSong.title} (ID: ${selectedSong.id})`);
|
|
}
|
|
|
|
if (!dailyPuzzle) {
|
|
return NextResponse.json({ error: 'Failed to create puzzle' }, { status: 500 });
|
|
}
|
|
|
|
return NextResponse.json({
|
|
id: dailyPuzzle.id,
|
|
audioUrl: `/uploads/${dailyPuzzle.song.filename}`,
|
|
songId: dailyPuzzle.songId,
|
|
title: dailyPuzzle.song.title,
|
|
artist: dailyPuzzle.song.artist,
|
|
coverImage: dailyPuzzle.song.coverImage ? `/uploads/covers/${dailyPuzzle.song.coverImage}` : null,
|
|
});
|
|
|
|
} catch (error) {
|
|
console.error('Error fetching daily puzzle:', error);
|
|
return NextResponse.json({ error: 'Internal Server Error' }, { status: 500 });
|
|
}
|
|
}
|