summaryrefslogtreecommitdiff
path: root/lib/vendor-users/validations.ts
blob: 904bb41ee4aac3ce41b84120e4b3333d3e2e7645 (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
import { userRoles, users, type UserView } from "@/db/schema/users";
import {
  createSearchParamsCache,
  parseAsArrayOf,
  parseAsInteger,
  parseAsString,
  parseAsStringEnum,
} from "nuqs/server"
import * as z from "zod"
import { getFiltersStateParser, getSortingStateParser } from "@/lib/parsers"
import { checkEmailExists } from "./service";



export const searchParamsVendorUserCache = createSearchParamsCache({
  flags: parseAsArrayOf(z.enum(["advancedTable", "floatingBar"])).withDefault(
    []
  ),
  page: parseAsInteger.withDefault(1),
  perPage: parseAsInteger.withDefault(10),
  sort: getSortingStateParser<UserView>().withDefault([
    { id: "created_at", desc: true },
  ]),
  email: parseAsString.withDefault(""),
  // advanced filter
  filters: getFiltersStateParser().withDefault([]),
  joinOperator: parseAsStringEnum(["and", "or"]).withDefault("and"),
  search: parseAsString.withDefault(""),

})

const phoneValidation = z
  .string()
  .optional()
  .refine(
    (phone) => {
      if (!phone || phone.trim() === "") return true; // Optional field
      // Remove spaces, hyphens, and parentheses for validation
      const cleanPhone = phone.replace(/[\s\-\(\)]/g, '');
      // Basic international phone number validation
      return /^\+\d{10,15}$/.test(cleanPhone);
    },
    {
      message: "올바른 국제 전화번호 형식이 아닙니다. +로 시작하는 10-15자리 번호를 입력해주세요. (예: +82 010-1234-5678)"
    }
  )

export const createVendorUserSchema = z.object({
  email: z
    .string()
    .email()
    .refine(
      async (email) => {
        // 1) DB 조회해서 이미 같은 email이 있으면 false 반환
        const isUsed = await checkEmailExists(email); 
        return !isUsed;
      },
      {
        message: "This email is already in use",
      }
    ),
  name: z.string().min(1),                           // 최소 길이 1
  phone: phoneValidation,
  domain: z.enum(users.domain.enumValues),           // "evcp" | "partners"
  companyId: z.number().nullable().optional(),       // number | null | undefined
  roles:z.array(z.string()).min(1, "At least one role must be selected"),
  language: z.enum(["ko", "en"]).optional(), 
  
});

export const updateVendorUserSchema = z.object({
  name: z.string().optional(),
  email: z.string().email().optional(),
  phone: phoneValidation,
  domain: z.enum(users.domain.enumValues).optional(),
  companyId: z.number().nullable().optional(),
  roles: z.array(z.string()).optional(),
  language: z.enum(["ko", "en"]).optional(), 

});
export type GetVendorUsersSchema = Awaited<ReturnType<typeof searchParamsVendorUserCache.parse>>
export type CreateVendorUserSchema = z.infer<typeof createVendorUserSchema>
export type UpdateVendorUserSchema = z.infer<typeof updateVendorUserSchema>