summaryrefslogtreecommitdiff
path: root/app/api/basicContract/create-revision/route.ts
blob: 19d0ceb1330870c3bc097c7948a56e207efafd8f (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
63
64
65
66
67
// app/api/basicContract/create-revision/route.ts
import { NextRequest, NextResponse } from "next/server";
import { unstable_noStore } from "next/cache";
import { z } from "zod";
import { getErrorMessage } from "@/lib/handle-error";
import { createBasicContractTemplateRevision } from "@/lib/basic-contract/service";

// 리비전 생성 스키마
const createRevisionSchema = z.object({
  baseTemplateId: z.coerce.number().int().positive(),
  templateName: z.string().min(1),
  revision: z.number().int().min(1),
  legalReviewRequired: z.boolean(),
  fileName: z.string().min(1),
  filePath: z.string().min(1),
});

export async function POST(request: NextRequest) {
  unstable_noStore();
  
  try {
    // 요청 본문 파싱
    const body = await request.json();
    const validatedData = createRevisionSchema.parse(body);

    // 같은 템플릿 이름에 대해 리비전이 이미 존재하는지 확인하는 로직은 
    // 서비스 함수에서 처리됨

    // 새 리비전 생성
    const { data: newRevision, error } = await createBasicContractTemplateRevision(validatedData);

    if (error) {
      return NextResponse.json(
        { success: false, error },
        { status: 400 }
      );
    }

    return NextResponse.json({
      success: true,
      data: newRevision,
      message: `${validatedData.templateName} v${validatedData.revision} 리비전이 성공적으로 생성되었습니다.`
    });

  } catch (error) {
    console.error("Create revision API error:", error);
    
    if (error instanceof z.ZodError) {
      return NextResponse.json(
        { 
          success: false, 
          error: "입력 데이터가 올바르지 않습니다.",
          details: error.errors 
        },
        { status: 400 }
      );
    }

    return NextResponse.json(
      { 
        success: false, 
        error: getErrorMessage(error) 
      },
      { status: 500 }
    );
  }
}