summaryrefslogtreecommitdiff
path: root/lib/contact-possible-items/service.ts
blob: f4b8936853c637c6b80ec74f4f7fa5fed1e8d4c9 (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
"use server"

import db from "@/db/db"
import { techSalesContactPossibleItems } from "@/db/schema/techSales"
import { techVendors, techVendorContacts, techVendorPossibleItems } from "@/db/schema/techVendors"
import { eq, desc, ilike, count, or } from "drizzle-orm"
import { revalidatePath } from "next/cache"
import { unstable_noStore } from "next/cache"
import { GetContactPossibleItemsSchema } from "./validations"

// 담당자별 아이템 상세 타입 정의 (뷰 기반)
export interface ContactPossibleItemDetail {
  id: number
  contactId: number
  vendorPossibleItemId: number
  createdAt: Date
  updatedAt: Date
  
  // 벤더 정보
  vendorId: number
  vendorName: string | null
  vendorCode: string | null
  vendorEmail: string | null
  vendorPhone: string | null
  vendorCountry: string | null
  vendorStatus: string | null
  techVendorType: string | null
  
  // 연락처 정보
  contactName: string | null
  contactPosition: string | null
  contactEmail: string | null
  contactPhone: string | null
  contactCountry: string | null
  isPrimary: boolean | null
  
  // 아이템 정보
  itemCode: string | null
  workType: string | null
  shipTypes: string | null
  itemList: string | null
  subItemList: string | null
}

/**
 * 담당자별 아이템 목록 조회 (뷰 사용)
 */
export async function getContactPossibleItems(input: GetContactPossibleItemsSchema) {
  unstable_noStore()

  try {
    const offset = (input.page - 1) * input.per_page
    
    console.log("=== getContactPossibleItems DEBUG ===")
    console.log("Input:", input)
    console.log("Offset:", offset)
    
    // 검색 조건
    let whereCondition
    if (input.search) {
      const searchTerm = `%${input.search}%`
      whereCondition = or(
        ilike(techVendorPossibleItems.itemCode, searchTerm),
        ilike(techVendorPossibleItems.itemList, searchTerm),
        ilike(techVendors.vendorName, searchTerm),
        ilike(techVendorContacts.contactName, searchTerm)
      )
      console.log("Search term:", searchTerm)
    } else {
      console.log("No search condition")
    }

    // 원본 테이블들을 직접 조인해서 데이터 조회
    console.log("Executing data query...")
    const items = await db
      .select({
        // 기본 매핑 정보
        id: techSalesContactPossibleItems.id,
        contactId: techSalesContactPossibleItems.contactId,
        vendorPossibleItemId: techSalesContactPossibleItems.vendorPossibleItemId,
        createdAt: techSalesContactPossibleItems.createdAt,
        updatedAt: techSalesContactPossibleItems.updatedAt,
        
        // 벤더 정보
        vendorId: techVendors.id,
        vendorName: techVendors.vendorName,
        vendorCode: techVendors.vendorCode,
        vendorEmail: techVendors.email,
        vendorPhone: techVendors.phone,
        vendorCountry: techVendors.country,
        vendorStatus: techVendors.status,
        techVendorType: techVendors.techVendorType,
        
        // 연락처 정보
        contactName: techVendorContacts.contactName,
        contactPosition: techVendorContacts.contactPosition,
        contactEmail: techVendorContacts.contactEmail,
        contactPhone: techVendorContacts.contactPhone,
        contactCountry: techVendorContacts.contactCountry,
        isPrimary: techVendorContacts.isPrimary,
        
        // 벤더 가능 아이템 정보
        itemCode: techVendorPossibleItems.itemCode,
        workType: techVendorPossibleItems.workType,
        shipTypes: techVendorPossibleItems.shipTypes,
        itemList: techVendorPossibleItems.itemList,
        subItemList: techVendorPossibleItems.subItemList,
      })
      .from(techSalesContactPossibleItems)
      .leftJoin(techVendorContacts, eq(techSalesContactPossibleItems.contactId, techVendorContacts.id))
      .leftJoin(techVendorPossibleItems, eq(techSalesContactPossibleItems.vendorPossibleItemId, techVendorPossibleItems.id))
      .leftJoin(techVendors, eq(techVendorContacts.vendorId, techVendors.id))
      .where(whereCondition)
      .orderBy(desc(techSalesContactPossibleItems.createdAt))
      .offset(offset)
      .limit(input.per_page)

    console.log("Items found:", items.length)
    console.log("First 3 items:", items.slice(0, 3))

    // 전체 개수 조회 (동일한 조인과 검색 조건 적용)
    console.log("Executing count query...")
    const [{ count: total }] = await db
      .select({ count: count() })
      .from(techSalesContactPossibleItems)
      .leftJoin(techVendorContacts, eq(techSalesContactPossibleItems.contactId, techVendorContacts.id))
      .leftJoin(techVendorPossibleItems, eq(techSalesContactPossibleItems.vendorPossibleItemId, techVendorPossibleItems.id))
      .leftJoin(techVendors, eq(techVendorContacts.vendorId, techVendors.id))
      .where(whereCondition)

    console.log("Total count:", total)

    const pageCount = Math.ceil(total / input.per_page)

    console.log("Final result:", { dataLength: items.length, pageCount, total })
    console.log("=== END DEBUG ===")

    return {
      data: items as ContactPossibleItemDetail[],
      pageCount,
      total,
    }
  } catch (err) {
    console.error("=== ERROR in getContactPossibleItems ===")
    console.error("Error fetching contact possible items:", err)
    console.error("Input was:", input)
    console.error("=== END ERROR ===")
    return {
      data: [],
      pageCount: 0,
      total: 0,
    }
  }
}

/**
 * 담당자별 아이템 삭제
 */
export async function deleteContactPossibleItem(id: number) {
  try {
    await db
      .delete(techSalesContactPossibleItems)
      .where(eq(techSalesContactPossibleItems.id, id))

    revalidatePath("/evcp/contact-possible-items")
    return { success: true }
  } catch (error) {
    console.error("담당자별 아이템 삭제 오류:", error)
    return { success: false, error: "담당자별 아이템 삭제에 실패했습니다." }
  }
}

/**
 * 여러 담당자별 아이템 삭제
 */
export async function deleteContactPossibleItems(ids: number[]) {
  try {
    await db
      .delete(techSalesContactPossibleItems)
      .where(
        or(...ids.map(id => eq(techSalesContactPossibleItems.id, id)))
      )

    revalidatePath("/evcp/contact-possible-items")
    return { success: true }
  } catch (error) {
    console.error("담당자별 아이템 일괄 삭제 오류:", error)
    return { success: false, error: "담당자별 아이템 삭제에 실패했습니다." }
  }
}