summaryrefslogtreecommitdiff
path: root/lib/gtc-contract/validations.ts
diff options
context:
space:
mode:
Diffstat (limited to 'lib/gtc-contract/validations.ts')
-rw-r--r--lib/gtc-contract/validations.ts89
1 files changed, 89 insertions, 0 deletions
diff --git a/lib/gtc-contract/validations.ts b/lib/gtc-contract/validations.ts
new file mode 100644
index 00000000..b79a8b08
--- /dev/null
+++ b/lib/gtc-contract/validations.ts
@@ -0,0 +1,89 @@
+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"
+import { checkProjectExists } from "./service"
+
+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()
+ .refine(
+ async (projectId, ctx) => {
+ // 프로젝트 타입인 경우 projectId 필수
+ if (ctx.parent.type === "project" && !projectId) {
+ ctx.addIssue({
+ code: z.ZodIssueCode.custom,
+ message: "Project is required for project type GTC",
+ })
+ return false
+ }
+
+ // 표준 타입인 경우 projectId null이어야 함
+ if (ctx.parent.type === "standard" && projectId) {
+ ctx.addIssue({
+ code: z.ZodIssueCode.custom,
+ message: "Project should not be set for standard type GTC",
+ })
+ return false
+ }
+
+ // 프로젝트 ID가 유효한지 검사
+ if (projectId) {
+ const exists = await checkProjectExists(projectId)
+ if (!exists) {
+ ctx.addIssue({
+ code: z.ZodIssueCode.custom,
+ message: "Invalid project ID",
+ })
+ return false
+ }
+ }
+
+ return true
+ }
+ ),
+ revision: z.number().min(0).default(0),
+ editReason: z.string().optional(),
+})
+
+export const updateGtcDocumentSchema = z.object({
+ editReason: 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> \ No newline at end of file