summaryrefslogtreecommitdiff
path: root/lib/vendor-candidates/validations.ts
blob: 1e53e5a5ea1fc387621774a5059d030f23b80939 (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
import { vendorCandidates, vendorCandidatesWithVendorInfo } 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 searchParamsCandidateCache = createSearchParamsCache({
    // Common flags
    flags: parseAsArrayOf(z.enum(["advancedTable", "floatingBar"])).withDefault([]),
    from: parseAsString.withDefault(""),
    to: parseAsString.withDefault(""),
    // Paging
    page: parseAsInteger.withDefault(1),
    perPage: parseAsInteger.withDefault(10),

    // Sorting - adjusting for vendorInvestigationsView
    sort: getSortingStateParser<typeof vendorCandidatesWithVendorInfo.$inferSelect>().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
    status: parseAsStringEnum(["COLLECTED", "INVITED", "DISCARDED"]),

    // In case you also want to filter by vendorName, vendorCode, etc.
    companyName: parseAsString.withDefault(""),
    contactEmail: parseAsString.withDefault(""),
    contactPhone: parseAsString.withDefault(""),
    country: parseAsString.withDefault(""),
    source: parseAsString.withDefault(""),

  
})

// Finally, export the type you can use in your server action:
export type GetVendorsCandidateSchema = Awaited<ReturnType<typeof searchParamsCandidateCache.parse>>


// Updated version of the updateVendorCandidateSchema
export const updateVendorCandidateSchema = z.object({
  id: z.number(),
  // 필수 필드
  companyName: z.string().min(1, "회사명은 필수입니다").max(255),
  // null을 명시적으로 처리
  contactEmail: z.union([
    z.string().email("유효한 이메일 형식이 아닙니다").max(255),
    z.literal(''),
    z.null()
  ]).optional().transform(val => val === null ? '' : val),
  contactPhone: z.union([z.string().max(50), z.literal(''), z.null()]).optional()
    .transform(val => val === null ? '' : val),
  country: z.union([z.string().max(100), z.literal(''), z.null()]).optional()
    .transform(val => val === null ? '' : val),
  // 필수 필드
  source: z.string().min(1, "출처는 필수입니다").max(100),
  address: z.union([z.string(), z.literal(''), z.null()]).optional()
    .transform(val => val === null ? '' : val),
  taxId: z.union([z.string(), z.literal(''), z.null()]).optional()
    .transform(val => val === null ? '' : val),
  // 필수 필드
  items: z.string().min(1, "항목은 필수입니다"),
  remark: z.union([z.string(), z.literal(''), z.null()]).optional()
    .transform(val => val === null ? '' : val),
  status: z.enum(["COLLECTED", "INVITED", "DISCARDED"]),
  updatedAt: z.date().optional().default(() => new Date()),
});;

// Create schema for vendor candidates
export const createVendorCandidateSchema = z.object({
  companyName: z.string().min(1, "회사명은 필수입니다").max(255),
  // contactEmail을 필수값으로 변경
  contactEmail: z.string().min(1, "이메일은 필수입니다").email("유효한 이메일 형식이 아닙니다").max(255),
  contactPhone: z.string().max(50).optional(),
  country: z.string().max(100).optional(),
  source: z.string().min(1, "출처는 필수입니다").max(100),
  address: z.string().optional(),
  taxId: z.string().optional(),
  items: z.string().min(1, "항목은 필수입니다"),
  remark: z.string().optional(),
  vendorId: z.number().optional(),
  status: z.enum(["COLLECTED", "INVITED", "DISCARDED"]).default("COLLECTED"),
});

// Export types for both schemas
export type UpdateVendorCandidateSchema = z.infer<typeof updateVendorCandidateSchema>;
export type CreateVendorCandidateSchema = z.infer<typeof createVendorCandidateSchema>;


export const removeCandidatesSchema = z.object({
  ids: z.array(z.number()).min(1, "At least one candidate ID must be provided"),
});

export type RemoveCandidatesInput = z.infer<typeof removeCandidatesSchema>;