summaryrefslogtreecommitdiff
path: root/lib/contact-possible-items/service-add-mapping.ts
blob: 392155498a26002a201bb124b0ab1c601fe7a6ea (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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
"use server"

import { revalidatePath } from 'next/cache'
import { eq, and, or, ilike, inArray } from 'drizzle-orm'
import db from '@/db/db'
import { techSalesContactPossibleItems } from "@/db/schema/techSales"
import { techVendors, techVendorContacts, techVendorPossibleItems } from "@/db/schema/techVendors"
import { itemShipbuilding, itemOffshoreTop, itemOffshoreHull } from "@/db/schema/items"

/**
 * 조선 아이템 검색
 */
export async function searchShipbuildingItems(search: string = "") {
  try {
    const where = search
      ? or(
          ilike(itemShipbuilding.itemCode, `%${search}%`),
          ilike(itemShipbuilding.itemList, `%${search}%`),
          ilike(itemShipbuilding.workType, `%${search}%`),
          ilike(itemShipbuilding.shipTypes, `%${search}%`)
        )
      : undefined

    const items = await db
      .select({
        id: itemShipbuilding.id,
        itemCode: itemShipbuilding.itemCode,
        itemList: itemShipbuilding.itemList,
        workType: itemShipbuilding.workType,
        shipTypes: itemShipbuilding.shipTypes,
      })
      .from(itemShipbuilding)
      .where(where)
      .limit(100)

    return { data: items, error: null }
  } catch (err) {
    console.error("조선 아이템 검색 오류:", err)
    return { data: [], error: "조선 아이템 검색에 실패했습니다." }
  }
}

/**
 * 해양 TOP 아이템 검색
 */
export async function searchOffshoreTopItems(search: string = "") {
  try {
    const where = search
      ? or(
          ilike(itemOffshoreTop.itemCode, `%${search}%`),
          ilike(itemOffshoreTop.itemList, `%${search}%`),
          ilike(itemOffshoreTop.workType, `%${search}%`)
        )
      : undefined

    const items = await db
      .select({
        id: itemOffshoreTop.id,
        itemCode: itemOffshoreTop.itemCode,
        itemList: itemOffshoreTop.itemList,
        workType: itemOffshoreTop.workType,
        subItemList: itemOffshoreTop.subItemList,
      })
      .from(itemOffshoreTop)
      .where(where)
      .limit(100)

    return { data: items, error: null }
  } catch (err) {
    console.error("해양 TOP 아이템 검색 오류:", err)
    return { data: [], error: "해양 TOP 아이템 검색에 실패했습니다." }
  }
}

/**
 * 해양 HULL 아이템 검색
 */
export async function searchOffshoreHullItems(search: string = "") {
  try {
    const where = search
      ? or(
          ilike(itemOffshoreHull.itemCode, `%${search}%`),
          ilike(itemOffshoreHull.itemList, `%${search}%`),
          ilike(itemOffshoreHull.workType, `%${search}%`)
        )
      : undefined

    const items = await db
      .select({
        id: itemOffshoreHull.id,
        itemCode: itemOffshoreHull.itemCode,
        itemList: itemOffshoreHull.itemList,
        workType: itemOffshoreHull.workType,
        subItemList: itemOffshoreHull.subItemList,
      })
      .from(itemOffshoreHull)
      .where(where)
      .limit(100)

    return { data: items, error: null }
  } catch (err) {
    console.error("해양 HULL 아이템 검색 오류:", err)
    return { data: [], error: "해양 HULL 아이템 검색에 실패했습니다." }
  }
}

/**
 * 기술영업 벤더 검색
 */
export async function searchTechVendors(search: string = "") {
  try {
    const where = search
      ? or(
          ilike(techVendors.vendorCode, `%${search}%`),
          ilike(techVendors.vendorName, `%${search}%`),
          ilike(techVendors.email, `%${search}%`)
        )
      : undefined

    const vendors = await db
      .select({
        id: techVendors.id,
        vendorCode: techVendors.vendorCode,
        vendorName: techVendors.vendorName,
        email: techVendors.email,
        techVendorType: techVendors.techVendorType,
        status: techVendors.status,
      })
      .from(techVendors)
      .where(where)
      .limit(100)

    return { data: vendors, error: null }
  } catch (err) {
    console.error("벤더 검색 오류:", err)
    return { data: [], error: "벤더 검색에 실패했습니다." }
  }
}

/**
 * 여러 벤더의 담당자 조회
 */
export async function getTechVendorsContactsForMapping(vendorIds: number[]) {
  try {
    if (vendorIds.length === 0) {
      return { data: {}, error: null }
    }

    const contactsWithVendor = await db
      .select({
        contactId: techVendorContacts.id,
        contactName: techVendorContacts.contactName,
        contactPosition: techVendorContacts.contactPosition,
        contactTitle: techVendorContacts.contactTitle,
        contactEmail: techVendorContacts.contactEmail,
        contactPhone: techVendorContacts.contactPhone,
        isPrimary: techVendorContacts.isPrimary,
        vendorId: techVendorContacts.vendorId,
        vendorName: techVendors.vendorName,
        vendorCode: techVendors.vendorCode
      })
      .from(techVendorContacts)
      .leftJoin(techVendors, eq(techVendorContacts.vendorId, techVendors.id))
      .where(inArray(techVendorContacts.vendorId, vendorIds))

    // 벤더별로 그룹화
    const contactsByVendor = contactsWithVendor.reduce((acc, row) => {
      const vendorId = row.vendorId
      if (!acc[vendorId]) {
        acc[vendorId] = {
          vendor: {
            id: vendorId,
            vendorName: row.vendorName || '',
            vendorCode: row.vendorCode || ''
          },
          contacts: []
        }
      }
      acc[vendorId].contacts.push({
        id: row.contactId,
        contactName: row.contactName,
        contactPosition: row.contactPosition,
        contactTitle: row.contactTitle,
        contactEmail: row.contactEmail,
        contactPhone: row.contactPhone,
        isPrimary: row.isPrimary
      })
      return acc
    }, {} as Record<number, {
      vendor: {
        id: number
        vendorName: string
        vendorCode: string | null
      }
      contacts: Array<{
        id: number
        contactName: string
        contactPosition: string | null
        contactTitle: string | null
        contactEmail: string
        contactPhone: string | null
        isPrimary: boolean
      }>
    }>)

    return { data: contactsByVendor, error: null }
  } catch (err) {
    console.error("벤더 담당자 조회 오류:", err)
    return { data: {}, error: "벤더 담당자 조회에 실패했습니다." }
  }
}

/**
 * 담당자-아이템 매핑 추가
 * 선택된 아이템과 벤더를 기준으로 possible-items 테이블에 정보가 있으면 해당 id 사용,
 * 없으면 possible-items 테이블에 insert 후, insert한 id를 가지고 담당자와 연결
 */
interface AddContactItemMappingInput {
  items: {
    id: number
    type: 'SHIP' | 'TOP' | 'HULL'
  }[]
  vendors: number[]
  contactsByVendor: Record<number, number[]> // vendorId -> contactIds[]
}

export async function addContactItemMapping(input: AddContactItemMappingInput) {
  try {
    return await db.transaction(async (tx) => {
      const insertedMappings: Array<typeof techSalesContactPossibleItems.$inferSelect> = []

      // 각 아이템 x 벤더 조합에 대해 처리
      for (const item of input.items) {
        for (const vendorId of input.vendors) {
          const contactIds = input.contactsByVendor[vendorId] || []
          if (contactIds.length === 0) continue

          // techVendorPossibleItems에서 기존 항목 찾기
          let vendorPossibleItemId: number | null = null

          const whereCondition = and(
            eq(techVendorPossibleItems.vendorId, vendorId),
            item.type === 'SHIP' 
              ? eq(techVendorPossibleItems.shipbuildingItemId, item.id)
              : item.type === 'TOP'
              ? eq(techVendorPossibleItems.offshoreTopItemId, item.id)
              : eq(techVendorPossibleItems.offshoreHullItemId, item.id)
          )

          const existingPossibleItem = await tx
            .select({ id: techVendorPossibleItems.id })
            .from(techVendorPossibleItems)
            .where(whereCondition)
            .limit(1)

          if (existingPossibleItem.length > 0) {
            vendorPossibleItemId = existingPossibleItem[0].id
          } else {
            // 없으면 새로 생성
            const newPossibleItem = await tx
              .insert(techVendorPossibleItems)
              .values({
                vendorId,
                shipbuildingItemId: item.type === 'SHIP' ? item.id : null,
                offshoreTopItemId: item.type === 'TOP' ? item.id : null,
                offshoreHullItemId: item.type === 'HULL' ? item.id : null,
              })
              .returning({ id: techVendorPossibleItems.id })

            vendorPossibleItemId = newPossibleItem[0].id
          }

          // 각 담당자에 대해 techSalesContactPossibleItems에 추가
          for (const contactId of contactIds) {
            // 중복 체크
            const existing = await tx
              .select({ id: techSalesContactPossibleItems.id })
              .from(techSalesContactPossibleItems)
              .where(
                and(
                  eq(techSalesContactPossibleItems.contactId, contactId),
                  eq(techSalesContactPossibleItems.vendorPossibleItemId, vendorPossibleItemId)
                )
              )
              .limit(1)

            if (existing.length === 0) {
              const inserted = await tx
                .insert(techSalesContactPossibleItems)
                .values({
                  contactId,
                  vendorPossibleItemId,
                })
                .returning()

              if (inserted.length > 0) {
                insertedMappings.push(inserted[0])
              }
            }
          }
        }
      }

      revalidatePath("/evcp/contact-possible-items")
      return { data: insertedMappings, error: null }
    })
  } catch (err) {
    console.error("담당자-아이템 매핑 추가 오류:", err)
    return { data: null, error: "담당자-아이템 매핑 추가에 실패했습니다." }
  }
}