diff options
Diffstat (limited to 'app/api/attachment-delete')
| -rw-r--r-- | app/api/attachment-delete/route.ts | 48 |
1 files changed, 35 insertions, 13 deletions
diff --git a/app/api/attachment-delete/route.ts b/app/api/attachment-delete/route.ts index 254c579f..cfaba61c 100644 --- a/app/api/attachment-delete/route.ts +++ b/app/api/attachment-delete/route.ts @@ -1,14 +1,23 @@ // /api/attachment-delete/route.ts import { NextRequest, NextResponse } from 'next/server' -import db from '@/db/db' -import { documentAttachments } from '@/db/schema' // 실제 스키마에 맞게 수정 +import db from '@/db/db' +import { documentAttachments, changeLogs } from '@/db/schema' import { eq, and } from 'drizzle-orm' -import fs from 'fs/promises' -import path from 'path' +import { getServerSession } from 'next-auth'; +import { authOptions } from '@/app/api/auth/[...nextauth]/route'; export async function DELETE(request: NextRequest) { try { + + const session = await getServerSession(authOptions); + if (!session?.user?.id) { + return NextResponse.json( + { error: '인증이 필요합니다' }, + { status: 401 } + ); + } + const { attachmentId, revisionId } = await request.json() if (!attachmentId || !revisionId) { @@ -47,19 +56,32 @@ export async function DELETE(request: NextRequest) { ) } - // 4. 데이터베이스에서 첨부파일 레코드 삭제 - await db - .delete(documentAttachments) - .where( - and( - eq(documentAttachments.id, attachmentId), - eq(documentAttachments.revisionId, revisionId) + // 3. 트랜잭션으로 첨부파일과 changeLogs 함께 삭제 + await db.transaction(async (tx) => { + // 3-1. changeLogs에서 해당 attachment 관련 로그 삭제 + await tx + .delete(changeLogs) + .where( + and( + eq(changeLogs.entityType, 'attachment'), + eq(changeLogs.entityId, attachmentId) + ) ) - ) + + // 3-2. 첨부파일 레코드 삭제 + await tx + .delete(documentAttachments) + .where( + and( + eq(documentAttachments.id, attachmentId), + eq(documentAttachments.revisionId, revisionId) + ) + ) + }) return NextResponse.json({ success: true, - message: 'Attachment deleted successfully', + message: 'Attachment and related logs deleted successfully', deletedAttachmentId: attachmentId }) |
