diff options
| author | 0-Zz-ang <s1998319@gmail.com> | 2025-07-29 09:08:52 +0900 |
|---|---|---|
| committer | 0-Zz-ang <s1998319@gmail.com> | 2025-07-29 09:11:22 +0900 |
| commit | 8d92c88ab341156d82156bae49c62a8101280e75 (patch) | |
| tree | 065ed1838de4164da23e3777b5367143e4f13982 /lib/docu-list-rule/code-groups/service.ts | |
| parent | 75249e6fa46864f49d4eb91bd755171b6b65eaae (diff) | |
(박서영) 설계 Document Numbering Rule 수정
Diffstat (limited to 'lib/docu-list-rule/code-groups/service.ts')
| -rw-r--r-- | lib/docu-list-rule/code-groups/service.ts | 89 |
1 files changed, 48 insertions, 41 deletions
diff --git a/lib/docu-list-rule/code-groups/service.ts b/lib/docu-list-rule/code-groups/service.ts index 34ec5610..2c30cedb 100644 --- a/lib/docu-list-rule/code-groups/service.ts +++ b/lib/docu-list-rule/code-groups/service.ts @@ -6,28 +6,18 @@ import { codeGroups, comboBoxSettings, documentClasses } from "@/db/schema/docu- import { eq, sql, count } from "drizzle-orm" import { unstable_noStore } from "next/cache" -// Code Groups 목록 조회 -export async function getCodeGroups(input: { - page: number - perPage: number - search?: string - sort?: Array<{ id: string; desc: boolean }> - filters?: Array<{ id: string; value: string }> - joinOperator?: "and" | "or" - flags?: string[] - groupId?: string - description?: string - controlType?: string - isActive?: string -} | any) { +// Code Groups 목록 조회 +export async function getCodeGroups(input: any) { unstable_noStore() try { - const { page, perPage, sort, search } = input + const { page, perPage, search, filters, joinOperator } = input const offset = (page - 1) * perPage // 검색 조건 (Document Class 제외) let whereConditions = sql`${codeGroups.groupId} != 'DOC_CLASS'` + + // 검색어 필터링 if (search) { const searchTerm = `%${search}%` whereConditions = sql`${codeGroups.groupId} != 'DOC_CLASS' AND ( @@ -38,33 +28,50 @@ export async function getCodeGroups(input: { )` } - // 정렬 + // 고급 필터링 (단순화) + if (filters && filters.length > 0) { + const filterConditions = filters.map(filter => { + const { id, value } = filter + if (!value || Array.isArray(value)) return null + + switch (id) { + case "groupId": + return sql`${codeGroups.groupId} ILIKE ${`%${value}%`}` + case "description": + return sql`${codeGroups.description} ILIKE ${`%${value}%`}` + case "codeFormat": + return sql`${codeGroups.codeFormat} ILIKE ${`%${value}%`}` + case "controlType": + return sql`${codeGroups.controlType} = ${value}` + case "isActive": + return sql`${codeGroups.isActive} = ${value === "true"}` + case "createdAt": + return sql`${codeGroups.createdAt}::text ILIKE ${`%${value}%`}` + default: + return null + } + }).filter(Boolean) + + if (filterConditions.length > 0) { + const operator = joinOperator === "or" ? sql` OR ` : sql` AND ` + const combinedFilters = filterConditions.reduce((acc, condition, index) => { + if (index === 0) return condition + return sql`${acc}${operator}${condition}` + }) + + whereConditions = sql`${whereConditions} AND (${combinedFilters})` + } + } + + // 정렬 (안전한 필드 체크 적용) let orderBy = sql`${codeGroups.createdAt} DESC` - if (sort && sort.length > 0) { - const sortField = sort[0] - const direction = sortField.desc ? sql`DESC` : sql`ASC` - - switch (sortField.id) { - case "groupId": - orderBy = sql`${codeGroups.groupId} ${direction}` - break - case "description": - orderBy = sql`${codeGroups.description} ${direction}` - break - case "codeFormat": - orderBy = sql`${codeGroups.codeFormat} ${direction}` - break - case "controlType": - orderBy = sql`${codeGroups.controlType} ${direction}` - break - case "isActive": - orderBy = sql`${codeGroups.isActive} ${direction}` - break - case "createdAt": - orderBy = sql`${codeGroups.createdAt} ${direction}` - break - default: - orderBy = sql`${codeGroups.createdAt} DESC` + if (input.sort && input.sort.length > 0) { + const sortField = input.sort[0] + // 안전성 체크: 필드가 실제 테이블에 존재하는지 확인 + if (sortField && sortField.id && typeof sortField.id === "string" && sortField.id in codeGroups) { + const direction = sortField.desc ? sql`DESC` : sql`ASC` + const col = codeGroups[sortField.id as keyof typeof codeGroups] + orderBy = sql`${col} ${direction}` } } |
