Files
recall/src/app/api/export-import/route.ts
2026-03-22 13:01:46 -03:00

122 lines
3.5 KiB
TypeScript

import { NextRequest, NextResponse } from 'next/server'
import { prisma } from '@/lib/prisma'
import { noteSchema } from '@/lib/validators'
export async function GET() {
const notes = await prisma.note.findMany({
include: { tags: { include: { tag: true } } },
})
const exportData = notes.map(note => ({
...note,
tags: note.tags.map(nt => nt.tag.name),
createdAt: note.createdAt.toISOString(),
updatedAt: note.updatedAt.toISOString(),
}))
return NextResponse.json(exportData, { status: 200 })
}
export async function POST(req: NextRequest) {
const body = await req.json()
if (!Array.isArray(body)) {
return NextResponse.json({ error: 'Invalid format: expected array' }, { status: 400 })
}
const importedNotes: Array<{ id?: string; title: string }> = []
const errors: string[] = []
for (let i = 0; i < body.length; i++) {
const result = noteSchema.safeParse(body[i])
if (!result.success) {
errors.push(`Item ${i}: ${result.error.issues.map(e => e.message).join(', ')}`)
continue
}
importedNotes.push(result.data)
}
if (errors.length > 0) {
return NextResponse.json({ error: 'Validation failed', details: errors }, { status: 400 })
}
const parseDate = (dateStr: string | undefined): Date => {
if (!dateStr) return new Date()
const parsed = new Date(dateStr)
return isNaN(parsed.getTime()) ? new Date() : parsed
}
let processed = 0
await prisma.$transaction(async (tx) => {
for (const item of importedNotes) {
const tags = item.tags || []
const { tags: _, ...noteData } = item
const createdAt = parseDate((item as { createdAt?: string }).createdAt)
const updatedAt = parseDate((item as { updatedAt?: string }).updatedAt)
if (item.id) {
const existing = await tx.note.findUnique({ where: { id: item.id } })
if (existing) {
await tx.note.update({
where: { id: item.id },
data: { ...noteData, createdAt, updatedAt },
})
await tx.noteTag.deleteMany({ where: { noteId: item.id } })
processed++
} else {
await tx.note.create({
data: {
...noteData,
id: item.id,
createdAt,
updatedAt,
},
})
processed++
}
} else {
const existingByTitle = await tx.note.findFirst({
where: { title: item.title },
})
if (existingByTitle) {
await tx.note.update({
where: { id: existingByTitle.id },
data: { ...noteData, updatedAt },
})
await tx.noteTag.deleteMany({ where: { noteId: existingByTitle.id } })
} else {
await tx.note.create({
data: {
...noteData,
createdAt,
updatedAt,
},
})
}
processed++
}
const noteId = item.id
? (await tx.note.findUnique({ where: { id: item.id } }))?.id
: (await tx.note.findFirst({ where: { title: item.title } }))?.id
if (noteId && tags.length > 0) {
for (const tagName of tags) {
const tag = await tx.tag.upsert({
where: { name: tagName },
create: { name: tagName },
update: {},
})
await tx.noteTag.create({
data: { noteId, tagId: tag.id },
})
}
}
}
})
return NextResponse.json({ success: true, count: processed }, { status: 201 })
}