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
|
'use server'
import db from '@/db/db'
import { projects } from '@/db/schema'
import { eq, and, inArray } from 'drizzle-orm'
/**
* 프로젝트 ID로 프로젝트 코드 조회
*/
export async function getProjectCodeById(projectId: number): Promise<string | null> {
try {
const result = await db
.select({ code: projects.code })
.from(projects)
.where(eq(projects.id, projectId))
.limit(1)
return result[0]?.code || null
} catch (error) {
console.error('Failed to get project code by id:', error)
return null
}
}
/**
* 프로젝트 코드와 이름으로 프로젝트 ID 조회
*/
export async function getProjectIdByCodeAndName(
projectCode: string,
projectName: string
): Promise<number | null> {
try {
if (!projectCode || !projectName) {
return null
}
const result = await db
.select({ id: projects.id })
.from(projects)
.where(
and(
eq(projects.code, projectCode.trim()),
eq(projects.name, projectName.trim())
)
)
.limit(1)
return result[0]?.id || null
} catch (error) {
console.error('Failed to get project id by code and name:', error)
return null
}
}
/**
* 여러 프로젝트 ID로 프로젝트 코드 맵 조회 (성능 최적화)
*/
export async function getProjectCodesByIds(
projectIds: number[]
): Promise<Map<number, string>> {
try {
if (projectIds.length === 0) {
return new Map()
}
const uniqueIds = [...new Set(projectIds.filter(id => id != null))]
if (uniqueIds.length === 0) {
return new Map()
}
const result = await db
.select({ id: projects.id, code: projects.code })
.from(projects)
.where(inArray(projects.id, uniqueIds))
const map = new Map<number, string>()
result.forEach((project) => {
map.set(project.id, project.code)
})
return map
} catch (error) {
console.error('Failed to get project codes by ids:', error)
return new Map()
}
}
|