summaryrefslogtreecommitdiff
path: root/lib/docu-list-rule/combo-box-settings/service.ts
diff options
context:
space:
mode:
author0-Zz-ang <s1998319@gmail.com>2025-08-25 09:23:30 +0900
committer0-Zz-ang <s1998319@gmail.com>2025-08-25 09:23:30 +0900
commitb12a06766e32e3c76544b1d12bec91653e1fe9db (patch)
tree57ca1ddff3342677d132e07b78fc03873a960255 /lib/docu-list-rule/combo-box-settings/service.ts
parentd38877eef87917087a4a217bea32ae84d6738a7d (diff)
docu-list-rule페이지 수정
Diffstat (limited to 'lib/docu-list-rule/combo-box-settings/service.ts')
-rw-r--r--lib/docu-list-rule/combo-box-settings/service.ts119
1 files changed, 116 insertions, 3 deletions
diff --git a/lib/docu-list-rule/combo-box-settings/service.ts b/lib/docu-list-rule/combo-box-settings/service.ts
index c733f978..d2f7d0f7 100644
--- a/lib/docu-list-rule/combo-box-settings/service.ts
+++ b/lib/docu-list-rule/combo-box-settings/service.ts
@@ -19,6 +19,7 @@ export async function getComboBoxCodeGroups(input: {
groupId?: string
description?: string
isActive?: string
+ projectId?: string
}) {
unstable_noStore()
@@ -29,10 +30,15 @@ export async function getComboBoxCodeGroups(input: {
// Control Type이 combobox이고 plant 타입 프로젝트인 조건
let whereConditions = sql`${codeGroups.controlType} = 'combobox' AND ${projects.type} = 'plant'`
+ // 프로젝트 ID 필터링
+ if (input.projectId) {
+ whereConditions = sql`${whereConditions} AND ${codeGroups.projectId} = ${parseInt(input.projectId)}`
+ }
+
// 검색 조건
if (search) {
const searchTerm = `%${search}%`
- whereConditions = sql`${codeGroups.controlType} = 'combobox' AND ${projects.type} = 'plant' AND (
+ whereConditions = sql`${whereConditions} AND (
${codeGroups.groupId} ILIKE ${searchTerm} OR
${codeGroups.description} ILIKE ${searchTerm} OR
${codeGroups.codeFormat} ILIKE ${searchTerm} OR
@@ -168,7 +174,7 @@ export async function getComboBoxOptions(codeGroupId: number, input?: {
}
// 정렬 (안전한 필드 체크 적용)
- let orderBy = sql`${comboBoxSettings.code} ASC`
+ let orderBy = sql`${comboBoxSettings.sdq} ASC`
if (sort && sort.length > 0) {
const sortField = sort[0]
// 안전성 체크: 필드가 실제 테이블에 존재하는지 확인
@@ -193,6 +199,7 @@ export async function getComboBoxOptions(codeGroupId: number, input?: {
code: comboBoxSettings.code,
description: comboBoxSettings.description,
remark: comboBoxSettings.remark,
+ sdq: comboBoxSettings.sdq,
createdAt: comboBoxSettings.createdAt,
updatedAt: comboBoxSettings.updatedAt,
projectCode: projects.code,
@@ -274,6 +281,14 @@ export async function createComboBoxOption(input: {
}
}
+ // 다음 순서 번호 계산
+ const maxSdqResult = await db
+ .select({ maxSdq: sql<number>`COALESCE(MAX(sdq), 0)` })
+ .from(comboBoxSettings)
+ .where(eq(comboBoxSettings.codeGroupId, input.codeGroupId))
+
+ const nextSdq = (maxSdqResult[0]?.maxSdq || 0) + 1
+
const [newOption] = await db
.insert(comboBoxSettings)
.values({
@@ -281,6 +296,7 @@ export async function createComboBoxOption(input: {
code: input.code,
description: input.description || "-",
remark: input.remark,
+ sdq: nextSdq,
})
.returning({ id: comboBoxSettings.id })
@@ -308,6 +324,7 @@ export async function updateComboBoxOption(input: {
code: string
description: string
remark?: string
+ sdq?: number
}) {
try {
// 현재 수정 중인 항목의 codeGroupId 가져오기
@@ -346,6 +363,7 @@ export async function updateComboBoxOption(input: {
code: input.code,
description: input.description,
remark: input.remark,
+ ...(input.sdq !== undefined && { sdq: input.sdq }),
updatedAt: new Date(),
})
.where(eq(comboBoxSettings.id, input.id))
@@ -421,4 +439,99 @@ export async function clearComboBoxOptions(codeGroupId: number) {
}
}
- \ No newline at end of file
+// Combo Box 옵션 순서 업데이트 (드래그 앤 드롭)
+export async function updateComboBoxOptionOrder(codeGroupId: number, reorderedOptions: { id: number; sdq: number }[]) {
+ try {
+ console.log("Updating combo box option order:", { codeGroupId, reorderedOptions })
+
+ // 유니크 제약조건 때문에 임시로 큰 값으로 업데이트 후 실제 값으로 업데이트
+ await db.transaction(async (tx) => {
+ // 1단계: 모든 옵션을 임시 큰 값으로 업데이트
+ for (const option of reorderedOptions) {
+ console.log("Step 1 - Setting temporary value for option:", option.id)
+ await tx
+ .update(comboBoxSettings)
+ .set({
+ sdq: option.sdq + 1000, // 임시로 큰 값 설정
+ updatedAt: new Date(),
+ })
+ .where(eq(comboBoxSettings.id, option.id))
+ }
+
+ // 2단계: 실제 값으로 업데이트
+ for (const option of reorderedOptions) {
+ console.log("Step 2 - Setting final value for option:", option.id, "sdq:", option.sdq)
+ const result = await tx
+ .update(comboBoxSettings)
+ .set({
+ sdq: option.sdq,
+ updatedAt: new Date(),
+ })
+ .where(eq(comboBoxSettings.id, option.id))
+ .returning({ id: comboBoxSettings.id, sdq: comboBoxSettings.sdq })
+
+ console.log("Update result:", result)
+ }
+ })
+
+ revalidatePath("/evcp/docu-list-rule/combo-box-settings")
+
+ return {
+ success: true,
+ message: "Combo Box options reordered successfully"
+ }
+ } catch (error) {
+ console.error("Error updating combo box option order:", error)
+ return {
+ success: false,
+ error: "Failed to update combo box option order"
+ }
+ }
+}
+
+// 기존 데이터에 기본 순서값 설정 (마이그레이션 후 한 번만 실행)
+export async function initializeComboBoxOptionOrder() {
+ try {
+ // sdq가 null인 모든 옵션들을 찾아서 순서대로 업데이트
+ const codeGroupsWithOptions = await db
+ .select({
+ codeGroupId: comboBoxSettings.codeGroupId,
+ id: comboBoxSettings.id,
+ })
+ .from(comboBoxSettings)
+ .orderBy(comboBoxSettings.codeGroupId, comboBoxSettings.createdAt)
+
+ // codeGroupId별로 그룹화하여 순서 설정
+ const groupedOptions = codeGroupsWithOptions.reduce((acc, option) => {
+ if (!acc[option.codeGroupId]) {
+ acc[option.codeGroupId] = []
+ }
+ acc[option.codeGroupId].push(option.id)
+ return acc
+ }, {} as Record<number, number[]>)
+
+ // 각 그룹별로 순서 업데이트
+ for (const [codeGroupId, optionIds] of Object.entries(groupedOptions)) {
+ for (let i = 0; i < optionIds.length; i++) {
+ await db
+ .update(comboBoxSettings)
+ .set({
+ sdq: i + 1,
+ updatedAt: new Date(),
+ })
+ .where(eq(comboBoxSettings.id, optionIds[i]))
+ }
+ }
+
+ return {
+ success: true,
+ message: "Combo Box option order initialized successfully"
+ }
+ } catch (error) {
+ console.error("Error initializing combo box option order:", error)
+ return {
+ success: false,
+ error: "Failed to initialize combo box option order"
+ }
+ }
+} \ No newline at end of file