"use server" import db from "@/db/db" import { tagTypeClassFormMappings } from "@/db/schema/vendorData"; import { eq, and } from "drizzle-orm" // 폼 정보 인터페이스 (동일) export interface FormMapping { formCode: string; formName: string; ep: string; remark: string; } /** * 주어진 tagType, classCode로 DB를 조회하여 * 1) 특정 classCode 매핑 => 존재하면 반환 * 2) 없으면 DEFAULT 매핑 => 없으면 빈 배열 */ export async function getFormMappingsByTagType( tagType: string, projectId: number, classCode?: string ): Promise { console.log(`DB-based getFormMappingsByTagType => tagType="${tagType}", class="${classCode ?? "NONE"}"`); // 1) classCode가 있으면 시도 if (classCode) { const specificRows = await db .select({ formCode: tagTypeClassFormMappings.formCode, formName: tagTypeClassFormMappings.formName, ep: tagTypeClassFormMappings.ep, remark: tagTypeClassFormMappings.remark }) .from(tagTypeClassFormMappings) .where(and( eq(tagTypeClassFormMappings.tagTypeLabel, tagType), eq(tagTypeClassFormMappings.projectId, projectId), eq(tagTypeClassFormMappings.classLabel, classCode) )) if (specificRows.length > 0) { console.log("Found specific mapping rows:", specificRows.length); return specificRows; } } // 2) fallback => DEFAULT console.log(`Falling back to DEFAULT for tagType="${tagType}"`); const defaultRows = await db .select({ formCode: tagTypeClassFormMappings.formCode, formName: tagTypeClassFormMappings.formName, ep: tagTypeClassFormMappings.ep }) .from(tagTypeClassFormMappings) .where(and( eq(tagTypeClassFormMappings.tagTypeLabel, tagType), eq(tagTypeClassFormMappings.projectId, projectId), eq(tagTypeClassFormMappings.classLabel, "DEFAULT") )) if (defaultRows.length > 0) { console.log("Using DEFAULT mapping rows:", defaultRows.length); return defaultRows; } // 3) 아무것도 없으면 빈 배열 console.log(`No mappings found at all for tagType="${tagType}"`); return []; } export async function getFormMappingsByTagTypebyProeject( projectId: number, ): Promise { const specificRows = await db .select({ formCode: tagTypeClassFormMappings.formCode, formName: tagTypeClassFormMappings.formName, ep: tagTypeClassFormMappings.ep, remark: tagTypeClassFormMappings.remark }) .from(tagTypeClassFormMappings) .where(and( eq(tagTypeClassFormMappings.projectId, projectId), )) if (specificRows.length > 0) { console.log("Found specific mapping rows:", specificRows.length); return specificRows; } return []; }