summaryrefslogtreecommitdiff
path: root/lib/tags/form-mapping-service.ts
blob: 6de0e2441c891f0aeebfe68b6ff1d7f4f3da9728 (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
93
94
95
96
97
98
99
100
101
"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<FormMapping[]> {

  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<FormMapping[]> {

    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 [];
}