summaryrefslogtreecommitdiff
path: root/lib/pq/validations.ts
blob: 93daf460499e8c2dc22d4b377a4713aeadaee33d (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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
import {
  createSearchParamsCache,
  parseAsArrayOf,
  parseAsInteger,
  parseAsString,
  parseAsStringEnum,
} from "nuqs/server"
import * as z from "zod"

import { getFiltersStateParser, getSortingStateParser } from "@/lib/parsers"
import { PqCriterias, vendorPQSubmissions } from "@/db/schema/pq"

export const searchParamsCache = createSearchParamsCache({
  flags: parseAsArrayOf(z.enum(["advancedTable", "floatingBar"])).withDefault(
    []
  ),
  page: parseAsInteger.withDefault(1),
  perPage: parseAsInteger.withDefault(10),
  sort: getSortingStateParser<PqCriterias>().withDefault([
    { id: "createdAt", desc: true },
  ]),
  code: parseAsString.withDefault(""),
  checkPoint: parseAsString.withDefault(""),
  description: parseAsString.withDefault(""),
  remarks: parseAsString.withDefault(""),
  groupName: parseAsString.withDefault(""),
  subGroupName: parseAsString.withDefault(""),
  // advanced filter
  filters: getFiltersStateParser().withDefault([]),
  joinOperator: parseAsStringEnum(["and", "or"]).withDefault("and"),
  search: parseAsString.withDefault(""),

})


export type GetPQSchema = Awaited<ReturnType<typeof searchParamsCache.parse>>


export const searchParamsPQReviewCache = createSearchParamsCache({
  // 공통 플래그
  flags: parseAsArrayOf(z.enum(["advancedTable", "floatingBar"])).withDefault([]),

  // 페이징
  page: parseAsInteger.withDefault(1),
  perPage: parseAsInteger.withDefault(10),

  // 정렬
  sort: getSortingStateParser<typeof vendorPQSubmissions.$inferSelect>().withDefault([
    { id: "updatedAt", desc: true },
  ]),

  // 고급 필터
  filters: getFiltersStateParser().withDefault([]),
  joinOperator: parseAsStringEnum(["and", "or"]).withDefault("and"),

  // 기본 필터 (새로 추가)
  pqBasicFilters: getFiltersStateParser().withDefault([]),
  pqBasicJoinOperator: parseAsStringEnum(["and", "or"]).withDefault("and"),
  basicFilters: getFiltersStateParser().withDefault([]),
  basicJoinOperator: parseAsStringEnum(["and", "or"]).withDefault("and"),
  // 검색 키워드
  search: parseAsString.withDefault(""),

  // PQ 특화 필터 (기존 유지)
  vendorName: parseAsString.withDefault(""),
  vendorCode: parseAsString.withDefault(""),
  projectName: parseAsString.withDefault(""),
  type: parseAsStringEnum(["GENERAL", "PROJECT", "NON_INSPECTION"]),
  status: parseAsStringEnum(["REQUESTED", "IN_PROGRESS", "SUBMITTED", "APPROVED", "REJECTED"]),
  submittedDateFrom: parseAsString.withDefault(""),
  submittedDateTo: parseAsString.withDefault(""),
});
export const getPqListsSchema = z.object({
  page: z.number().min(1).default(1),
  perPage: z.number().min(1).max(100).default(20),
  sort: z
    .array(
      z.object({
        id: z.string(),
        desc: z.boolean().optional(),
      })
    )
    .default([]),
  filters: z.any().optional(),
  joinOperator: z.enum(["and", "or"]).default("and"),
  search: z.string().optional(),
});
// CREATE PQ LIST
export const createPqListSchema = z.object({
  name: z.string().min(1, "PQ 목록 명을 입력해주세요"),
  type: z.enum(["GENERAL", "PROJECT", "NON_INSPECTION"], {
    required_error: "PQ 유형을 선택해주세요"
  }),
  projectId: z.number().optional().nullable(),
  validTo: z.date().optional().nullable(),
}).refine((data) => {
  if (data.type === "PROJECT" && !data.projectId) {
    return false;
  }
  return true;
}, {
  message: "프로젝트 PQ인 경우 프로젝트를 선택해야 합니다",
  path: ["projectId"]
});
// PQ 목록 복사 (불러오기 기능)
export const copyPqListSchema = z.object({
  sourcePqListId: z.number({
    required_error: "복사할 PQ 목록을 선택해주세요"
  }),
  targetProjectId: z.number({
    required_error: "대상 프로젝트를 선택해주세요"
  }),
  newName: z.string().min(1, "새 PQ 목록 명을 입력해주세요").optional(),
  validTo: z.date().optional().nullable(),
});
export type CreatePqListInput = z.infer<typeof createPqListSchema>;

// // UPDATE PQ LIST
// export const updatePqListSchema = createPqListSchema.extend({
//   id: z.number().min(1, "PQ 목록 ID가 필요합니다"),
// });

// export type UpdatePqListInput = z.infer<typeof updatePqListSchema>;

export type CopyPqListInput = z.infer<typeof copyPqListSchema>;
export type GetPqListsSchema = z.infer<typeof getPqListsSchema>;
export type GetPQSubmissionsSchema = Awaited<ReturnType<typeof searchParamsPQReviewCache.parse>>