import { createSearchParamsCache, parseAsArrayOf, parseAsInteger, parseAsString, parseAsStringEnum, } from "nuqs/server" import * as z from "zod" import { getFiltersStateParser, getSortingStateParser } from "@/lib/parsers" import { vendorInvestigationsView } from "@/db/schema" export const searchParamsInvestigationCache = createSearchParamsCache({ // Common flags flags: parseAsArrayOf(z.enum(["advancedTable", "floatingBar"])).withDefault([]), // Paging page: parseAsInteger.withDefault(1), perPage: parseAsInteger.withDefault(10), // Sorting - adjusting for vendorInvestigationsView sort: getSortingStateParser().withDefault([ { id: "createdAt", desc: true }, ]), // Advanced filter filters: getFiltersStateParser().withDefault([]), joinOperator: parseAsStringEnum(["and", "or"]).withDefault("and"), // Global search search: parseAsString.withDefault(""), // ----------------------------------------------------------------- // Fields specific to vendor investigations // ----------------------------------------------------------------- // investigationStatus: PLANNED, IN_PROGRESS, COMPLETED, CANCELED investigationStatus: parseAsStringEnum(["PLANNED", "IN_PROGRESS", "COMPLETED", "CANCELED"]), // In case you also want to filter by vendorName, vendorCode, etc. vendorName: parseAsString.withDefault(""), vendorCode: parseAsString.withDefault(""), // If you need to filter by vendor status (e.g., PQ_SUBMITTED, ACTIVE, etc.), // you can include it here too. Example: // vendorStatus: parseAsStringEnum([ // "PENDING_REVIEW", // "IN_REVIEW", // "REJECTED", // "IN_PQ", // "PQ_SUBMITTED", // "PQ_FAILED", // "PQ_APPROVED", // "APPROVED", // "ACTIVE", // "INACTIVE", // "BLACKLISTED", // ]).optional(), }) // Finally, export the type you can use in your server action: export type GetVendorsInvestigationSchema = Awaited> export const updateVendorInvestigationSchema = z.object({ investigationId: z.number({ required_error: "Investigation ID is required", }), investigationStatus: z.enum(["PLANNED", "IN_PROGRESS", "COMPLETED", "CANCELED"], { required_error: "실사 상태를 선택해주세요.", }), evaluationType: z.enum(["SITE_AUDIT", "QM_SELF_AUDIT"]).optional(), investigationAddress: z.string().optional(), investigationMethod: z.string().max(100, "실사 방법은 100자 이내로 입력해주세요.").optional(), // 날짜 필드들을 string에서 Date로 변환하도록 수정 forecastedAt: z.union([ z.date(), z.string().transform((str) => str ? new Date(str) : undefined) ]).optional(), requestedAt: z.union([ z.date(), z.string().transform((str) => str ? new Date(str) : undefined) ]).optional(), confirmedAt: z.union([ z.date(), z.string().transform((str) => str ? new Date(str) : undefined) ]).optional(), completedAt: z.union([ z.date(), z.string().transform((str) => str ? new Date(str) : undefined) ]).optional(), evaluationScore: z.number() .int("평가 점수는 정수여야 합니다.") .min(0, "평가 점수는 0점 이상이어야 합니다.") .max(100, "평가 점수는 100점 이하여야 합니다.") .optional(), evaluationResult: z.enum(["APPROVED", "SUPPLEMENT", "REJECTED"]).optional(), investigationNotes: z.string().max(1000, "QM 의견은 1000자 이내로 입력해주세요.").optional(), attachments: z.any().optional(), // File 업로드를 위한 필드 }) export type UpdateVendorInvestigationSchema = z.infer