summaryrefslogtreecommitdiff
path: root/lib/vendor-investigation/validations.ts
blob: 18a5002258edc4ca029227ae6af444740cfccde3 (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
import { vendorInvestigationsView } from "@/db/schema/vendors"
import {
    createSearchParamsCache,
    parseAsArrayOf,
    parseAsInteger,
    parseAsString,
    parseAsStringEnum,
} from "nuqs/server"
import * as z from "zod"
import { getFiltersStateParser, getSortingStateParser } from "@/lib/parsers"

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<typeof vendorInvestigationsView.$inferSelect>().withDefault([
        { id: "investigationCreatedAt", 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<ReturnType<typeof searchParamsInvestigationCache.parse>>


export const updateVendorInvestigationSchema = z.object({
    investigationId: z.number(),
    investigationStatus: z.enum(["PLANNED", "IN_PROGRESS", "COMPLETED", "CANCELED"]),
  
    // If the user might send empty strings, we'll allow it by unioning with z.literal('')
    // Then transform empty string to undefined
    scheduledStartAt: z.preprocess(
        // null이나 빈 문자열을 undefined로 변환
        (val) => (val === null || val === '') ? undefined : val,
        z.date().optional()
      ),
  
    scheduledEndAt:z.preprocess(
        // null이나 빈 문자열을 undefined로 변환
        (val) => (val === null || val === '') ? undefined : val,
        z.date().optional()
      ),
  
    completedAt: z.preprocess(
        // null이나 빈 문자열을 undefined로 변환
        (val) => (val === null || val === '') ? undefined : val,
        z.date().optional()
      ),
    investigationNotes: z.string().optional(),
    attachments: z.any().optional(),
  })

export type UpdateVendorInvestigationSchema = z.infer<
    typeof updateVendorInvestigationSchema
>