summaryrefslogtreecommitdiff
path: root/lib/gtc-contract/validations.ts
blob: 0566c1cb8519a90d5d8250017c39adf3a87b19c9 (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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
import { gtcDocuments, type GtcDocument, type GtcDocumentWithRelations } from "@/db/schema/gtc"
import {
  createSearchParamsCache,
  parseAsArrayOf,
  parseAsInteger,
  parseAsString,
  parseAsStringEnum,
} from "nuqs/server"
import * as z from "zod"
import { getFiltersStateParser, getSortingStateParser } from "@/lib/parsers"

export const searchParamsCache = createSearchParamsCache({
  flags: parseAsArrayOf(z.enum(["advancedTable", "floatingBar"])).withDefault(
    []
  ),
  page: parseAsInteger.withDefault(1),
  perPage: parseAsInteger.withDefault(10),
  sort: getSortingStateParser<GtcDocumentWithRelations>().withDefault([
    { id: "updatedAt", desc: true },
  ]),
  // 검색 필터들
  type: parseAsStringEnum(["standard", "project"]).withDefault(""),
  projectId: parseAsInteger.withDefault(0),
  // advanced filter
  filters: getFiltersStateParser().withDefault([]),
  joinOperator: parseAsStringEnum(["and", "or"]).withDefault("and"),
  search: parseAsString.withDefault(""),
})

export const createGtcDocumentSchema = z.object({
  type: z.enum(["standard", "project"]),
  projectId: z.number().nullable().optional(),
  revision: z.number().min(0).default(0),
  editReason: z.string().optional(),
  title: z.string().optional(),
}).superRefine(async (data, ctx) => {
  // 프로젝트 타입인 경우 projectId 필수
  if (data.type === "project" && !data.projectId) {
    ctx.addIssue({
      code: z.ZodIssueCode.custom,
      message: "Project is required for project type GTC",
      path: ["projectId"],
    })
    return
  }
  
  // 표준 타입인 경우 projectId null이어야 함
  if (data.type === "standard" && data.projectId) {
    ctx.addIssue({
      code: z.ZodIssueCode.custom,
      message: "Project should not be set for standard type GTC",
      path: ["projectId"],
    })
    return
  }
  
})

export const updateGtcDocumentSchema = z.object({
  editReason: z.string().optional(),
  title: z.string().optional(),
  isActive: z.boolean().optional(),
})

// 리비전 업데이트용 스키마
export const createNewRevisionSchema = z.object({
  editReason: z.string().min(1, "Edit reason is required for new revision"),
})

export type GetGtcDocumentsSchema = Awaited<ReturnType<typeof searchParamsCache.parse>>
export type CreateGtcDocumentSchema = z.infer<typeof createGtcDocumentSchema>
export type UpdateGtcDocumentSchema = z.infer<typeof updateGtcDocumentSchema>
export type CreateNewRevisionSchema = z.infer<typeof createNewRevisionSchema>



// 복제용 schema
export const cloneGtcDocumentSchema = z.object({
  sourceDocumentId: z.number().min(1, "원본 문서 ID가 필요합니다."),
  type: z.enum(["standard", "project"]),
  projectId: z.number().nullable().optional(),
  title: z.string().optional(),
  editReason: z.string().optional(),
}).refine((data) => {
  // 프로젝트 타입인 경우 projectId가 필수
  if (data.type === "project") {
    return data.projectId !== null && data.projectId !== undefined
  }
  return true
}, {
  message: "프로젝트 타입인 경우 프로젝트를 선택해야 합니다.",
  path: ["projectId"]
}).refine((data) => {
  // 표준 타입인 경우 projectId는 null이어야 함
  if (data.type === "standard") {
    return data.projectId === null || data.projectId === undefined
  }
  return true
}, {
  message: "표준 타입인 경우 프로젝트를 선택할 수 없습니다.",
  path: ["projectId"]
})

export type CloneGtcDocumentSchema = z.infer<typeof cloneGtcDocumentSchema>