Files
Momento/memento-note/app/api/notes/daily/route.ts
Antigravity 42aa374be6
Some checks failed
CI / Lint, Unit Tests & Build (push) Failing after 1m25s
CI / Deploy production (on server) (push) Has been skipped
fix: note du jour — contenu HTML (pas JSON TipTap) + type richtext
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
2026-05-29 19:21:48 +00:00

60 lines
1.5 KiB
TypeScript

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)
// Store as HTML (same format as richtext notes via editor.getHTML())
return `<h1>📅 ${formatted}</h1><p></p>`
}
/**
* 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: { in: ['richtext', 'daily'] },
trashedAt: null,
},
})
if (!note) {
note = await prisma.note.create({
data: {
userId,
title: today,
content: getTodayContent(today),
type: 'richtext',
color: '#FEF9C3', // yellow-100 — distinguishes daily notes visually
labels: JSON.stringify(['daily']),
},
})
}
return NextResponse.json({ success: true, note })
}