60 lines
1.5 KiB
TypeScript
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 })
|
|
}
|