summaryrefslogtreecommitdiff
path: root/lib/vendor-registration-status/repository.ts
blob: f9c3d63f99fe0e093f04d59d4eb2be556932a90b (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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
import { db } from "@/db"
import { 
  vendorBusinessContacts, 
  vendorAdditionalInfo,
  vendors
} from "@/db/schema"
import { eq, and, inArray } from "drizzle-orm"

// 업무담당자 정보 타입
export interface VendorBusinessContact {
  id: number
  vendorId: number
  contactType: "sales" | "design" | "delivery" | "quality" | "tax_invoice"
  contactName: string
  position: string
  department: string
  responsibility: string
  email: string
  createdAt: Date
  updatedAt: Date
}

// 추가정보 타입
export interface VendorAdditionalInfo {
  id: number
  vendorId: number
  businessType?: string
  industryType?: string
  companySize?: string
  revenue?: string
  factoryEstablishedDate?: Date
  preferredContractTerms?: string
  createdAt: Date
  updatedAt: Date
}

// 업무담당자 정보 조회
export async function getBusinessContactsByVendorId(vendorId: number): Promise<VendorBusinessContact[]> {
  try {
    return await db
      .select()
      .from(vendorBusinessContacts)
      .where(eq(vendorBusinessContacts.vendorId, vendorId))
      .orderBy(vendorBusinessContacts.contactType)
  } catch (error) {
    console.error("Error fetching business contacts:", error)
    throw new Error("업무담당자 정보를 가져오는 중 오류가 발생했습니다.")
  }
}

// 업무담당자 정보 저장/업데이트
export async function upsertBusinessContacts(
  vendorId: number, 
  contacts: Omit<VendorBusinessContact, "id" | "vendorId" | "createdAt" | "updatedAt">[]
): Promise<void> {
  try {
    // 기존 데이터 삭제
    await db
      .delete(vendorBusinessContacts)
      .where(eq(vendorBusinessContacts.vendorId, vendorId))

    // 새 데이터 삽입
    if (contacts.length > 0) {
      await db
        .insert(vendorBusinessContacts)
        .values(contacts.map(contact => ({
          ...contact,
          vendorId,
        })))
    }
  } catch (error) {
    console.error("Error upserting business contacts:", error)
    throw new Error("업무담당자 정보 저장 중 오류가 발생했습니다.")
  }
}

// 추가정보 조회
export async function getAdditionalInfoByVendorId(vendorId: number): Promise<VendorAdditionalInfo | null> {
  try {
    const result = await db
      .select()
      .from(vendorAdditionalInfo)
      .where(eq(vendorAdditionalInfo.vendorId, vendorId))
      .limit(1)

    return result[0] || null
  } catch (error) {
    console.error("Error fetching additional info:", error)
    throw new Error("추가정보를 가져오는 중 오류가 발생했습니다.")
  }
}

// 추가정보 저장/업데이트
export async function upsertAdditionalInfo(
  vendorId: number,
  info: Omit<VendorAdditionalInfo, "id" | "vendorId" | "createdAt" | "updatedAt">
): Promise<void> {
  try {
    const existing = await getAdditionalInfoByVendorId(vendorId)
    
    if (existing) {
      // 업데이트
      await db
        .update(vendorAdditionalInfo)
        .set({
          ...info,
          updatedAt: new Date(),
        })
        .where(eq(vendorAdditionalInfo.vendorId, vendorId))
    } else {
      // 신규 삽입
      await db
        .insert(vendorAdditionalInfo)
        .values({
          ...info,
          vendorId,
        })
    }
  } catch (error) {
    console.error("Error upserting additional info:", error)
    throw new Error("추가정보 저장 중 오류가 발생했습니다.")
  }
}

// 특정 벤더의 모든 추가정보 조회 (업무담당자 + 추가정보)
export async function getVendorAllAdditionalData(vendorId: number) {
  try {
    const [businessContacts, additionalInfo] = await Promise.all([
      getBusinessContactsByVendorId(vendorId),
      getAdditionalInfoByVendorId(vendorId)
    ])

    return {
      businessContacts,
      additionalInfo
    }
  } catch (error) {
    console.error("Error fetching vendor additional data:", error)
    throw new Error("벤더 추가정보를 가져오는 중 오류가 발생했습니다.")
  }
}

// 업무담당자 정보 삭제
export async function deleteBusinessContactsByVendorId(vendorId: number): Promise<void> {
  try {
    await db
      .delete(vendorBusinessContacts)
      .where(eq(vendorBusinessContacts.vendorId, vendorId))
  } catch (error) {
    console.error("Error deleting business contacts:", error)
    throw new Error("업무담당자 정보 삭제 중 오류가 발생했습니다.")
  }
}

// 추가정보 삭제
export async function deleteAdditionalInfoByVendorId(vendorId: number): Promise<void> {
  try {
    await db
      .delete(vendorAdditionalInfo)
      .where(eq(vendorAdditionalInfo.vendorId, vendorId))
  } catch (error) {
    console.error("Error deleting additional info:", error)
    throw new Error("추가정보 삭제 중 오류가 발생했습니다.")
  }
}