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
|
import {
createSearchParamsCache,
parseAsArrayOf,
parseAsInteger,
parseAsString,
parseAsStringEnum,
} from "nuqs/server"
import * as z from "zod"
import { getFiltersStateParser, getSortingStateParser } from "@/lib/parsers"
import { RoleView, users } from "@/db/schema/users";
export const searchParamsCache = createSearchParamsCache({
flags: parseAsArrayOf(z.enum(["advancedTable", "floatingBar"])).withDefault(
[]
),
page: parseAsInteger.withDefault(1),
perPage: parseAsInteger.withDefault(10),
sort: getSortingStateParser<RoleView>().withDefault([
{ id: "created_at", desc: true },
]),
name: parseAsString.withDefault(""),
// advanced filter
filters: getFiltersStateParser().withDefault([]),
joinOperator: parseAsStringEnum(["and", "or"]).withDefault("and"),
search: parseAsString.withDefault(""),
})
export const createRoleSchema = z.object({
name: z.string().min(1),
description: z.string().min(1),
companyId:z
.number()
.int()
.positive()
.nullish(), // number | nullish
domain: z.enum(users.domain.enumValues), // "evcp" | "partners"
});
export const createRoleAssignmentSchema = z.object({
evcpRoles:z.array(z.string()),
});
export const updateRoleSchema = z.object({
name: z.string().min(1),
description: z.string().min(1),
domain: z.enum(users.domain.enumValues), // "evcp" | "partners"
company_id: z
.number()
.int()
.positive()
.nullish(), // number | nullish
}).superRefine((data, ctx) => {
// domain이 partners 이면 companyId는 필수
if (data.domain === "partners" && !data.company_id) {
ctx.addIssue({
code: "custom",
path: ["company_id"],
message: "협력업체(domain=partners)일 경우 companyId는 필수입니다.",
})
}
// domain이 evcp 이면 companyId는 null이어야 한다면(정책상)
if (data.domain === "evcp" && data.company_id) {
ctx.addIssue({
code: "custom",
path: ["company_id"],
message: "domain=evcp이면 companyId를 입력할 수 없습니다.",
})
}
})
// TypeScript에서 사용할 타입
export type GetRolesSchema = Awaited<ReturnType<typeof searchParamsCache.parse>>
export type CreateRoleSchema = z.infer<typeof createRoleSchema>
export type UpdateRoleSchema = z.infer<typeof updateRoleSchema>
export type CreateRoleAssignmentSchema = z.infer<typeof createRoleAssignmentSchema>
|