diff options
| author | 0-Zz-ang <s1998319@gmail.com> | 2025-08-07 18:02:54 +0900 |
|---|---|---|
| committer | 0-Zz-ang <s1998319@gmail.com> | 2025-08-07 18:02:54 +0900 |
| commit | 67bb1ad7d7e001e19c8d1dd9153a5f663e2afa03 (patch) | |
| tree | 9aab25663d6c180fd3a315840a3035b491ac0b7d /lib/docu-list-rule/combo-box-settings/service.ts | |
| parent | e270e477f362dd68249bb4a013c66eab293bba82 (diff) | |
(박서영)docu-list-rule Project_code적용
Diffstat (limited to 'lib/docu-list-rule/combo-box-settings/service.ts')
| -rw-r--r-- | lib/docu-list-rule/combo-box-settings/service.ts | 75 |
1 files changed, 49 insertions, 26 deletions
diff --git a/lib/docu-list-rule/combo-box-settings/service.ts b/lib/docu-list-rule/combo-box-settings/service.ts index 80c1942d..96daefe4 100644 --- a/lib/docu-list-rule/combo-box-settings/service.ts +++ b/lib/docu-list-rule/combo-box-settings/service.ts @@ -3,6 +3,7 @@ import { revalidatePath } from "next/cache" import db from "@/db/db" import { codeGroups, comboBoxSettings } from "@/db/schema/docu-list-rule" +import { projects } from "@/db/schema/projects" import { eq, sql, count } from "drizzle-orm" import { unstable_noStore } from "next/cache" @@ -34,7 +35,8 @@ export async function getComboBoxCodeGroups(input: { whereConditions = sql`${whereConditions} AND ( ${codeGroups.groupId} ILIKE ${searchTerm} OR ${codeGroups.description} ILIKE ${searchTerm} OR - ${codeGroups.codeFormat} ILIKE ${searchTerm} + ${codeGroups.codeFormat} ILIKE ${searchTerm} OR + ${projects.code} ILIKE ${searchTerm} )` } @@ -78,14 +80,20 @@ export async function getComboBoxCodeGroups(input: { if (sort && sort.length > 0) { const sortField = sort[0] // 안전성 체크: 필드가 실제 테이블에 존재하는지 확인 - if (sortField && sortField.id && typeof sortField.id === "string" && sortField.id in codeGroups) { + if (sortField && sortField.id && typeof sortField.id === "string") { const direction = sortField.desc ? sql`DESC` : sql`ASC` - const col = codeGroups[sortField.id as keyof typeof codeGroups] - orderBy = sql`${col} ${direction}` + + // 프로젝트 코드 정렬 처리 + if (sortField.id === "projectCode") { + orderBy = sql`${projects.code} ${direction}` + } else if (sortField.id in codeGroups) { + const col = codeGroups[sortField.id as keyof typeof codeGroups] + orderBy = sql`${col} ${direction}` + } } } - // 데이터 조회 + // 데이터 조회 (프로젝트 정보 포함) const data = await db .select({ id: codeGroups.id, @@ -97,32 +105,36 @@ export async function getComboBoxCodeGroups(input: { isActive: codeGroups.isActive, createdAt: codeGroups.createdAt, updatedAt: codeGroups.updatedAt, + projectId: codeGroups.projectId, + projectCode: projects.code, + projectName: projects.name, }) .from(codeGroups) + .leftJoin(projects, eq(codeGroups.projectId, projects.id)) .where(whereConditions) .orderBy(orderBy) .limit(perPage) .offset(offset) - // 총 개수 조회 - const [{ count: total }] = await db - .select({ count: count() }) + // 총 개수 조회 (프로젝트 정보 포함) + const totalCountResult = await db + .select({ count: sql<number>`count(*)` }) .from(codeGroups) + .leftJoin(projects, eq(codeGroups.projectId, projects.id)) .where(whereConditions) - const pageCount = Math.ceil(total / perPage) + const totalCount = totalCountResult[0]?.count || 0 return { - success: true, data, - pageCount, + totalCount, + pageCount: Math.ceil(totalCount / perPage), } } catch (error) { console.error("Error fetching combo box code groups:", error) return { - success: false, - error: "Failed to fetch combo box code groups", data: [], + totalCount: 0, pageCount: 0, } } @@ -150,7 +162,8 @@ export async function getComboBoxOptions(codeGroupId: number, input?: { whereConditions = sql`${whereConditions} AND ( ${comboBoxSettings.code} ILIKE ${searchTerm} OR ${comboBoxSettings.description} ILIKE ${searchTerm} OR - ${comboBoxSettings.remark} ILIKE ${searchTerm} + ${comboBoxSettings.remark} ILIKE ${searchTerm} OR + ${projects.code} ILIKE ${searchTerm} )` } @@ -159,14 +172,20 @@ export async function getComboBoxOptions(codeGroupId: number, input?: { if (sort && sort.length > 0) { const sortField = sort[0] // 안전성 체크: 필드가 실제 테이블에 존재하는지 확인 - if (sortField && sortField.id && typeof sortField.id === "string" && sortField.id in comboBoxSettings) { + if (sortField && sortField.id && typeof sortField.id === "string") { const direction = sortField.desc ? sql`DESC` : sql`ASC` - const col = comboBoxSettings[sortField.id as keyof typeof comboBoxSettings] - orderBy = sql`${col} ${direction}` + + // 프로젝트 코드 정렬 처리 + if (sortField.id === "projectCode") { + orderBy = sql`${projects.code} ${direction}` + } else if (sortField.id in comboBoxSettings) { + const col = comboBoxSettings[sortField.id as keyof typeof comboBoxSettings] + orderBy = sql`${col} ${direction}` + } } } - // 데이터 조회 + // 데이터 조회 (프로젝트 정보 포함) const data = await db .select({ id: comboBoxSettings.id, @@ -176,32 +195,36 @@ export async function getComboBoxOptions(codeGroupId: number, input?: { remark: comboBoxSettings.remark, createdAt: comboBoxSettings.createdAt, updatedAt: comboBoxSettings.updatedAt, + projectId: comboBoxSettings.projectId, + projectCode: projects.code, + projectName: projects.name, }) .from(comboBoxSettings) + .leftJoin(projects, eq(comboBoxSettings.projectId, projects.id)) .where(whereConditions) .orderBy(orderBy) .limit(perPage) .offset(offset) - // 총 개수 조회 - const [{ count: total }] = await db - .select({ count: count() }) + // 총 개수 조회 (프로젝트 정보 포함) + const totalCountResult = await db + .select({ count: sql<number>`count(*)` }) .from(comboBoxSettings) + .leftJoin(projects, eq(comboBoxSettings.projectId, projects.id)) .where(whereConditions) - const pageCount = Math.ceil(total / perPage) + const totalCount = totalCountResult[0]?.count || 0 return { - success: true, data, - pageCount, + totalCount, + pageCount: Math.ceil(totalCount / perPage), } } catch (error) { console.error("Error fetching combo box options:", error) return { - success: false, - error: "Failed to fetch combo box options", data: [], + totalCount: 0, pageCount: 0, } } |
