summaryrefslogtreecommitdiff
path: root/lib/admin-users/validations.ts
blob: e505067d124a57b06f0b9d11d38299f812f52f37 (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
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 searchParamsCache = 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(""),

})

export const createUserSchema = 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
  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 updateUserSchema = z.object({
  name: z.string().optional(),
  email: z.string().email().optional(),
  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 GetUsersSchema = Awaited<ReturnType<typeof searchParamsCache.parse>>
export type CreateUserSchema = z.infer<typeof createUserSchema>
export type UpdateUserSchema = z.infer<typeof updateUserSchema>