summaryrefslogtreecommitdiff
path: root/components/common/selectors/wbs-code/wbs-code-service.ts
blob: 7d9c17b14259a91c4e134f00b92be35719768b74 (plain)
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
"use server"

import { oracleKnex } from '@/lib/oracle-db/db'

// WBS 코드 타입 정의
export interface WbsCode {
  PROJ_NO: string // 프로젝트 번호
  WBS_ELMT: string // WBS 요소
  WBS_ELMT_NM: string // WBS 요소명
  WBS_LVL: string // WBS 레벨
}

// 테스트 환경용 폴백 데이터
const FALLBACK_TEST_DATA: WbsCode[] = [
  { PROJ_NO: 'SN2661', WBS_ELMT: 'WBS001', WBS_ELMT_NM: 'WBS 항목 1(테스트데이터 - 오라클 페칭 실패시)', WBS_LVL: '1' },
  { PROJ_NO: 'SN2661', WBS_ELMT: 'WBS002', WBS_ELMT_NM: 'WBS 항목 2(테스트데이터 - 오라클 페칭 실패시)', WBS_LVL: '2' },
  { PROJ_NO: 'SN2661', WBS_ELMT: 'WBS003', WBS_ELMT_NM: 'WBS 항목 3(테스트데이터 - 오라클 페칭 실패시)', WBS_LVL: '1' },
  { PROJ_NO: 'SN2661', WBS_ELMT: 'WBS004', WBS_ELMT_NM: 'WBS 항목 4(테스트데이터 - 오라클 페칭 실패시)', WBS_LVL: '2' },
  { PROJ_NO: 'SN2661', WBS_ELMT: 'WBS005', WBS_ELMT_NM: 'WBS 항목 5(테스트데이터 - 오라클 페칭 실패시)', WBS_LVL: '3' },
]

/**
 * WBS 코드 목록 조회 (Oracle에서 전체 조회, 실패 시 폴백 데이터 사용)
 * CMCTB_PROJ_WBS 테이블에서 조회
 * @param projNo - 프로젝트 번호 (선택적, 없으면 전체 조회)
 */
export async function getWbsCodes(projNo?: string): Promise<{
  success: boolean
  data: WbsCode[]
  error?: string
  isUsingFallback?: boolean
}> {
  try {
    console.log('📋 [getWbsCodes] Oracle 쿼리 시작...', projNo ? `프로젝트: ${projNo}` : '전체')

    let query = `
      SELECT 
        PROJ_NO,
        WBS_ELMT,
        WBS_ELMT_NM,
        WBS_LVL
      FROM CMCTB_PROJ_WBS
      WHERE ROWNUM < 100
    `

    if (projNo) {
      query += ` AND PROJ_NO = :projNo`
    }

    query += ` ORDER BY PROJ_NO, WBS_ELMT`

    const result = projNo 
      ? await oracleKnex.raw(query, { projNo })
      : await oracleKnex.raw(query)

    // Oracle raw query의 결과는 rows 배열에 들어있음
    const rows = (result.rows || result) as Array<Record<string, unknown>>
    
    console.log(`✅ [getWbsCodes] Oracle 쿼리 성공 - ${rows.length}건 조회`)
    
    // null 값 필터링
    const cleanedResult = rows
      .filter((item) => 
        item.PROJ_NO && 
        item.WBS_ELMT && 
        item.WBS_ELMT_NM
      )
      .map((item) => ({
        PROJ_NO: String(item.PROJ_NO),
        WBS_ELMT: String(item.WBS_ELMT),
        WBS_ELMT_NM: String(item.WBS_ELMT_NM),
        WBS_LVL: String(item.WBS_LVL || '')
      }))

    console.log(`✅ [getWbsCodes] 필터링 후 ${cleanedResult.length}건`)

    return {
      success: true,
      data: cleanedResult,
      isUsingFallback: false
    }
  } catch (error) {
    console.error('❌ [getWbsCodes] Oracle 오류:', error)
    console.log('🔄 [getWbsCodes] 폴백 테스트 데이터 사용 (' + FALLBACK_TEST_DATA.length + '건)')
    return {
      success: true,
      data: FALLBACK_TEST_DATA,
      isUsingFallback: true
    }
  }
}