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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
|
"use server"
import { revalidateTag, unstable_noStore } from "next/cache"
import { getErrorMessage } from "@/lib/handle-error"
import { unstable_cache } from "@/lib/unstable-cache"
import { filterColumns } from "@/lib/filter-columns"
import { asc, desc, ilike, and, or, eq } from "drizzle-orm"
import db from "@/db/db"
import { pageInformation, menuAssignments } from "@/db/schema"
import type {
CreateInformationSchema,
UpdateInformationSchema,
GetInformationSchema
} from "./validations"
import {
selectInformation,
countInformation,
getInformationByPageCode,
insertInformation,
updateInformation,
deleteInformationById,
deleteInformationByIds,
getInformationById,
selectInformationLists,
countInformationLists
} from "./repository"
import type { PageInformation } from "@/db/schema/information"
// 최신 패턴: 고급 필터링과 캐싱을 지원하는 인포메이션 목록 조회
export async function getInformationLists(input: GetInformationSchema) {
return unstable_cache(
async () => {
try {
const offset = (input.page - 1) * input.perPage
// 고급 필터링
const advancedWhere = filterColumns({
table: pageInformation,
filters: input.filters,
joinOperator: input.joinOperator,
})
// 전역 검색
let globalWhere
if (input.search) {
const s = `%${input.search}%`
globalWhere = or(
ilike(pageInformation.pageCode, s),
ilike(pageInformation.pageName, s),
ilike(pageInformation.title, s),
ilike(pageInformation.description, s)
)
}
// 기본 필터들
let basicWhere
const basicConditions = []
if (input.pageCode) {
basicConditions.push(ilike(pageInformation.pageCode, `%${input.pageCode}%`))
}
if (input.pageName) {
basicConditions.push(ilike(pageInformation.pageName, `%${input.pageName}%`))
}
if (input.title) {
basicConditions.push(ilike(pageInformation.title, `%${input.title}%`))
}
if (input.isActive !== undefined && input.isActive !== null) {
basicConditions.push(eq(pageInformation.isActive, input.isActive))
}
if (basicConditions.length > 0) {
basicWhere = and(...basicConditions)
}
// 최종 where 조건
const finalWhere = and(
advancedWhere,
globalWhere,
basicWhere
)
// 정렬 처리
const orderBy = input.sort.length > 0
? input.sort.map((item) => {
if (item.id === "createdAt") {
return item.desc ? desc(pageInformation.createdAt) : asc(pageInformation.createdAt)
} else if (item.id === "updatedAt") {
return item.desc ? desc(pageInformation.updatedAt) : asc(pageInformation.updatedAt)
} else if (item.id === "pageCode") {
return item.desc ? desc(pageInformation.pageCode) : asc(pageInformation.pageCode)
} else if (item.id === "pageName") {
return item.desc ? desc(pageInformation.pageName) : asc(pageInformation.pageName)
} else if (item.id === "title") {
return item.desc ? desc(pageInformation.title) : asc(pageInformation.title)
} else if (item.id === "isActive") {
return item.desc ? desc(pageInformation.isActive) : asc(pageInformation.isActive)
} else {
return desc(pageInformation.createdAt) // 기본값
}
})
: [desc(pageInformation.createdAt)]
// 트랜잭션 내부에서 Repository 호출
const { data, total } = await db.transaction(async (tx) => {
const data = await selectInformationLists(tx, {
where: finalWhere,
orderBy,
offset,
limit: input.perPage,
})
const total = await countInformationLists(tx, finalWhere)
return { data, total }
})
const pageCount = Math.ceil(total / input.perPage)
return { data, pageCount, total }
} catch (err) {
console.error("Failed to get information lists:", err)
// 에러 발생 시 기본값 반환
return { data: [], pageCount: 0, total: 0 }
}
},
[JSON.stringify(input)], // 캐싱 키
{
revalidate: 3600,
tags: ["information-lists"],
}
)()
}
// 기존 패턴 (하위 호환성을 위해 유지)
export async function getInformationList(input: Partial<GetInformationSchema> & { page: number; per_page: number }) {
unstable_noStore()
try {
const [data, total] = await Promise.all([
selectInformation(input as Parameters<typeof selectInformation>[0]),
countInformation(input as Parameters<typeof countInformation>[0])
])
const pageCount = Math.ceil(total / input.per_page)
return {
data,
pageCount,
total
}
} catch (error) {
console.error("Failed to get information list:", error)
throw new Error(getErrorMessage(error))
}
}
// 페이지별 인포메이션 조회 (일반 사용자용)
export async function getPageInformation(pageCode: string): Promise<PageInformation | null> {
try {
return await getInformationByPageCode(pageCode)
} catch (error) {
console.error(`Failed to get information for page ${pageCode}:`, error)
return null
}
}
// 캐시된 페이지별 인포메이션 조회
export const getCachedPageInformation = unstable_cache(
async (pageCode: string) => getPageInformation(pageCode),
["page-information"],
{
tags: ["page-information"],
revalidate: 3600, // 1시간 캐시
}
)
// 인포메이션 생성
export async function createInformation(input: CreateInformationSchema) {
try {
const result = await insertInformation(input)
revalidateTag("page-information")
revalidateTag("information-lists")
revalidateTag("information-edit-permission")
return {
success: true,
data: result,
message: "인포메이션이 성공적으로 생성되었습니다."
}
} catch (error) {
console.error("Failed to create information:", error)
return {
success: false,
message: getErrorMessage(error)
}
}
}
// 인포메이션 수정
export async function updateInformationData(input: UpdateInformationSchema) {
try {
const { id, ...updateData } = input
const result = await updateInformation(id, updateData)
if (!result) {
return {
success: false,
message: "인포메이션을 찾을 수 없거나 수정에 실패했습니다."
}
}
revalidateTag("page-information")
revalidateTag("information-lists")
revalidateTag("information-edit-permission") // 편집 권한 캐시 무효화
return {
success: true,
message: "인포메이션이 성공적으로 수정되었습니다."
}
} catch (error) {
console.error("Failed to update information:", error)
return {
success: false,
message: getErrorMessage(error)
}
}
}
// 인포메이션 삭제
export async function deleteInformation(id: number) {
try {
const success = await deleteInformationById(id)
if (!success) {
return {
success: false,
message: "인포메이션을 찾을 수 없거나 삭제에 실패했습니다."
}
}
revalidateTag("page-information")
revalidateTag("information-lists")
return {
success: true,
message: "인포메이션이 성공적으로 삭제되었습니다."
}
} catch (error) {
console.error("Failed to delete information:", error)
return {
success: false,
message: getErrorMessage(error)
}
}
}
// 인포메이션 다중 삭제
export async function deleteMultipleInformation(ids: number[]) {
try {
const deletedCount = await deleteInformationByIds(ids)
revalidateTag("page-information")
revalidateTag("information-lists")
return {
success: true,
deletedCount,
message: `${deletedCount}개의 인포메이션이 성공적으로 삭제되었습니다.`
}
} catch (error) {
console.error("Failed to delete multiple information:", error)
return {
success: false,
message: getErrorMessage(error)
}
}
}
// ID로 인포메이션 조회
export async function getInformationDetail(id: number): Promise<PageInformation | null> {
try {
return await getInformationById(id)
} catch (error) {
console.error(`Failed to get information detail for id ${id}:`, error)
return null
}
}
// 인포메이션 편집 권한 확인
export async function checkInformationEditPermission(pageCode: string, userId: string): Promise<boolean> {
try {
// pageCode를 menuPath로 변환 (pageCode가 menuPath의 마지막 부분이라고 가정)
// 예: pageCode "vendor-list" -> menuPath "/evcp/vendor-list" 또는 "/partners/vendor-list"
const menuPathQueries = [
`/evcp/${pageCode}`,
`/partners/${pageCode}`,
`/${pageCode}`, // 루트 경로
pageCode // 정확한 매칭
]
// menu_assignments에서 해당 pageCode와 매칭되는 메뉴 찾기
const menuAssignment = await db
.select()
.from(menuAssignments)
.where(
or(
...menuPathQueries.map(path => eq(menuAssignments.menuPath, path))
)
)
.limit(1)
if (menuAssignment.length === 0) {
// 매칭되는 메뉴가 없으면 권한 없음
return false
}
const assignment = menuAssignment[0]
const userIdNumber = parseInt(userId)
// 현재 사용자가 manager1 또는 manager2인지 확인
return assignment.manager1Id === userIdNumber || assignment.manager2Id === userIdNumber
} catch (error) {
console.error("Failed to check information edit permission:", error)
return false
}
}
// 캐시된 권한 확인
export const getCachedEditPermission = unstable_cache(
async (pageCode: string, userId: string) => checkInformationEditPermission(pageCode, userId),
["information-edit-permission"],
{
tags: ["information-edit-permission"],
revalidate: 300, // 5분 캐시
}
)
|