import type { PoolClient } from "pg"; /** * value_label 로 category_values 를 조회해 value_code 를 반환한다. * 매칭되는 카테고리가 없으면 입력 label 을 그대로 돌려준다. * * company_code 조건은 걸지 않는다 — 같은 label 은 전사에서 동일한 value_code 로 * 관리되는 것을 전제로, 업체 간 데이터 복사 시에도 값이 깨지지 않게 하기 위함. */ export async function resolveCategoryCode( client: PoolClient, tableName: string, columnName: string, label: string | null | undefined, ): Promise { if (!label) return label ?? null; const result = await client.query( `SELECT DISTINCT value_code FROM category_values WHERE table_name = $1 AND column_name = $2 AND value_label = $3 AND is_active = true LIMIT 1`, [tableName, columnName, label], ); return result.rows[0]?.value_code ?? label; }