import { NextResponse } from 'next/server' import { auth } from '@/auth' import prisma from '@/lib/prisma' function getTodayTitle(): string { return new Date().toISOString().slice(0, 10) // YYYY-MM-DD } function getTodayContent(dateStr: string): string { const d = new Date(dateStr) const options: Intl.DateTimeFormatOptions = { weekday: 'long', year: 'numeric', month: 'long', day: 'numeric', } const formatted = d.toLocaleDateString('fr-FR', options) return JSON.stringify({ type: 'doc', content: [ { type: 'heading', attrs: { level: 1 }, content: [{ type: 'text', text: `📅 ${formatted}` }] }, { type: 'paragraph', content: [{ type: 'text', text: '' }] }, ], }) } /** * GET /api/notes/daily * Returns (or creates) today's daily note for the authenticated user. */ export async function GET() { const session = await auth() if (!session?.user?.id) { return NextResponse.json({ success: false, error: 'Unauthorized' }, { status: 401 }) } const today = getTodayTitle() const userId = session.user.id // Try to find existing daily note for today let note = await prisma.note.findFirst({ where: { userId, title: today, type: 'daily', trashedAt: null, }, }) if (!note) { note = await prisma.note.create({ data: { userId, title: today, content: getTodayContent(today), type: 'daily', color: '#FEF9C3', // yellow-100 — distinguishes daily notes visually labels: JSON.stringify(['daily']), }, }) } return NextResponse.json({ success: true, note }) }