summaryrefslogtreecommitdiff
path: root/app/api/vendors/route.ts
blob: 27b79385b9e005b1082eaa507836cae8d55ff0d5 (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
// app/api/vendors/route.ts
import { NextRequest, NextResponse } from 'next/server'
import { unstable_noStore } from 'next/cache'
import { revalidateTag } from 'next/cache'
import { randomUUID } from 'crypto'
import * as fs from 'fs/promises'
import * as path from 'path'
import { eq } from 'drizzle-orm'
import { PgTransaction } from 'drizzle-orm/pg-core'

import  db  from '@/db/db'
import { users, vendors, vendorContacts, vendorAttachments } from '@/db/schema'
import { insertVendor } from '@/lib/vendors/repository'
import { getErrorMessage } from '@/lib/handle-error'

// Types
interface CreateVendorData {
  vendorName: string
  vendorCode?: string
  address?: string
  country?: string
  phone?: string
  email: string
  website?: string
  status?: string
  taxId: string
  vendorTypeId: number
  items?: string
  representativeName?: string
  representativeBirth?: string
  representativeEmail?: string
  representativePhone?: string
  corporateRegistrationNumber?: string
  representativeWorkExpirence?: boolean
}

interface ContactData {
  contactName: string
  contactPosition?: string
  contactDepartment?: string
  contactTask?: string
  contactEmail: string
  contactPhone?: string
  isPrimary?: boolean
}

// File attachment types
const FILE_TYPES = {
  BUSINESS_REGISTRATION: 'BUSINESS_REGISTRATION',
  ISO_CERTIFICATION: 'ISO_CERTIFICATION', 
  CREDIT_REPORT: 'CREDIT_REPORT',
  BANK_ACCOUNT_COPY: 'BANK_ACCOUNT_COPY'
} as const

type FileType = typeof FILE_TYPES[keyof typeof FILE_TYPES]

async function storeVendorFiles(
  tx: PgTransaction<any, any, any>,
  vendorId: number,
  files: File[],
  attachmentType: FileType
) {
  const vendorDir = path.join(
    process.cwd(),
    "public",
    "vendors",
    String(vendorId)
  )
  await fs.mkdir(vendorDir, { recursive: true })

  for (const file of files) {
    // Convert file to buffer
    const ab = await file.arrayBuffer()
    const buffer = Buffer.from(ab)

    // Generate a unique filename
    const uniqueName = `${randomUUID()}-${file.name}`
    const relativePath = path.join("vendors", String(vendorId), uniqueName)
    const absolutePath = path.join(process.cwd(), "public", relativePath)

    // Write to disk
    await fs.writeFile(absolutePath, buffer)

    // Insert attachment record
    await tx.insert(vendorAttachments).values({
      vendorId,
      fileName: file.name,
      filePath: "/" + relativePath.replace(/\\/g, "/"),
      attachmentType,
    })
  }
}

export async function POST(request: NextRequest) {
  unstable_noStore()

  try {
    const formData = await request.formData()

    // Parse vendor data and contacts from JSON strings
    const vendorDataString = formData.get('vendorData') as string
    const contactsString = formData.get('contacts') as string

    if (!vendorDataString || !contactsString) {
      return NextResponse.json(
        { error: 'Missing vendor data or contacts' },
        { status: 400 }
      )
    }

    const vendorData: CreateVendorData = JSON.parse(vendorDataString)
    const contacts: ContactData[] = JSON.parse(contactsString)

    // Extract files by type
    const businessRegistrationFiles = formData.getAll('businessRegistration') as File[]
    const isoCertificationFiles = formData.getAll('isoCertification') as File[]
    const creditReportFiles = formData.getAll('creditReport') as File[]
    const bankAccountFiles = formData.getAll('bankAccount') as File[]

    // Validate required files
    if (businessRegistrationFiles.length === 0) {
      return NextResponse.json(
        { error: '사업자등록증을 업로드해주세요.' },
        { status: 400 }
      )
    }

    if (isoCertificationFiles.length === 0) {
      return NextResponse.json(
        { error: 'ISO 인증서를 업로드해주세요.' },
        { status: 400 }
      )
    }

    if (creditReportFiles.length === 0) {
      return NextResponse.json(
        { error: '신용평가보고서를 업로드해주세요.' },
        { status: 400 }
      )
    }

    if (vendorData.country !== "KR" && bankAccountFiles.length === 0) {
      return NextResponse.json(
        { error: '대금지급 통장사본을 업로드해주세요.' },
        { status: 400 }
      )
    }

    // Check for existing email
    const existingUser = await db
      .select({ id: users.id })
      .from(users)
      .where(eq(users.email, vendorData.email))
      .limit(1)

    if (existingUser.length > 0) {
      return NextResponse.json(
        {
          error: `이미 등록된 이메일입니다. 다른 이메일을 사용해주세요. (Email ${vendorData.email} already exists in the system)`
        },
        { status: 400 }
      )
    }

    // Check for existing taxId (only if taxId is provided)
    if (vendorData.taxId && vendorData.taxId.trim()) {
      const existingVendor = await db
        .select({ id: vendors.id })
        .from(vendors)
        .where(eq(vendors.taxId, vendorData.taxId))
        .limit(1)

      if (existingVendor.length > 0) {
        return NextResponse.json(
          {
            error: `이미 등록된 사업자등록번호입니다. (Tax ID ${vendorData.taxId} already exists in the system)`
          },
          { status: 400 }
        )
      }
    }

    // Create vendor and handle files in transaction
    await db.transaction(async (tx) => {
      // Insert the vendor
      const [newVendor] = await insertVendor(tx, {
        vendorName: vendorData.vendorName,
        vendorCode: vendorData.vendorCode || null,
        address: vendorData.address || null,
        country: vendorData.country || null,
        phone: vendorData.phone || null,
        email: vendorData.email,
        website: vendorData.website || null,
        status: vendorData.status ?? "PENDING_REVIEW",
        taxId: vendorData.taxId,
        vendorTypeId: vendorData.vendorTypeId,
        items: vendorData.items || null,

        // Representative info
        representativeName: vendorData.representativeName || null,
        representativeBirth: vendorData.representativeBirth || null,
        representativeEmail: vendorData.representativeEmail || null,
        representativePhone: vendorData.representativePhone || null,
        corporateRegistrationNumber: vendorData.corporateRegistrationNumber || null,
        representativeWorkExpirence: vendorData.representativeWorkExpirence || false,

      })

      // Store files by type
      if (businessRegistrationFiles.length > 0) {
        await storeVendorFiles(tx, newVendor.id, businessRegistrationFiles, FILE_TYPES.BUSINESS_REGISTRATION)
      }

      if (isoCertificationFiles.length > 0) {
        await storeVendorFiles(tx, newVendor.id, isoCertificationFiles, FILE_TYPES.ISO_CERTIFICATION)
      }

      if (creditReportFiles.length > 0) {
        await storeVendorFiles(tx, newVendor.id, creditReportFiles, FILE_TYPES.CREDIT_REPORT)
      }

      if (bankAccountFiles.length > 0) {
        await storeVendorFiles(tx, newVendor.id, bankAccountFiles, FILE_TYPES.BANK_ACCOUNT_COPY)
      }

      // Insert contacts with new fields
      for (const contact of contacts) {
        await tx.insert(vendorContacts).values({
          vendorId: newVendor.id,
          contactName: contact.contactName,
          contactPosition: contact.contactPosition || null,
          contactDepartment: contact.contactDepartment || null,
          contactTask: contact.contactTask || null,
          contactEmail: contact.contactEmail,
          contactPhone: contact.contactPhone || null,
          isPrimary: contact.isPrimary ?? false,
        })
      }
    })

    revalidateTag("vendors")

    return NextResponse.json(
      { message: '벤더 등록이 완료되었습니다.' },
      { status: 201 }
    )

  } catch (error) {
    console.error('Vendor creation error:', error)
    return NextResponse.json(
      { error: getErrorMessage(error) },
      { status: 500 }
    )
  }
}