summaryrefslogtreecommitdiff
path: root/app/api/vendor-responses/update-comment/route.ts
blob: f1e4c48753a51027e20024350e0bc3ca1679bc19 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
// app/api/vendor-responses/update-comment/route.ts
import { NextRequest, NextResponse } from "next/server";
import db from "@/db/db";
import { vendorAttachmentResponses } from "@/db/schema";

import { getServerSession } from "next-auth/next"
import { authOptions } from "@/app/api/auth/[...nextauth]/route"
import { eq } from "drizzle-orm";

export async function POST(request: NextRequest) {
  try {
    // 인증 확인
    const session = await getServerSession(authOptions);
    if (!session?.user?.id) {
      return NextResponse.json(
        { message: "인증이 필요합니다." },
        { status: 401 }
      );
    }

    const body = await request.json();
    const { responseId, responseComment, vendorComment } = body;

    if (!responseId) {
      return NextResponse.json(
        { message: "응답 ID가 필요합니다." },
        { status: 400 }
      );
    }

    // 코멘트만 업데이트
    const [updatedResponse] = await db
      .update(vendorAttachmentResponses)
      .set({
        responseComment,
        vendorComment,
        updatedAt: new Date(),
        updatedBy:Number(session?.user.id)
      })
      .where(eq(vendorAttachmentResponses.id, parseInt(responseId)))
      .returning();

    if (!updatedResponse) {
      return NextResponse.json(
        { message: "응답을 찾을 수 없습니다." },
        { status: 404 }
      );
    }

    return NextResponse.json({
      message: "코멘트가 성공적으로 업데이트되었습니다.",
      response: updatedResponse,
    });

  } catch (error) {
    console.error("Comment update error:", error);
    return NextResponse.json(
      { message: "코멘트 업데이트 중 오류가 발생했습니다." },
      { status: 500 }
    );
  }
}