// db/seed/vendorSeed.ts import { faker } from "@faker-js/faker"; import db from "@/db/db"; import { vendors } from "../schema/vendors"; import { desc } from "drizzle-orm"; export async function generateVendor(index: number) { const vendorTypes = ["MANUFACTURER", "DISTRIBUTOR", "SERVICE_PROVIDER"]; const countries = ["KR", "US", "CN", "JP", "DE", "FR", "GB", "SG", "VN", "TH"]; const businessTypes = ["Corporation", "LLC", "Partnership", "Sole Proprietorship"]; const country = faker.helpers.arrayElement(countries); const businessType = faker.helpers.arrayElement(businessTypes); const vendorType = faker.helpers.arrayElement(vendorTypes); // 회사 이름 생성 패턴 다양화 let vendorName; const namePattern = Math.random(); if (namePattern < 0.3) { // 일반적인 회사명 vendorName = faker.company.name(); } else if (namePattern < 0.6) { // 산업 특화된 이름 const industry = faker.helpers.arrayElement([ "Tech", "Manufacturing", "Electronics", "Chemical", "Automotive", "Energy", "Construction" ]); vendorName = `${faker.company.name()} ${industry}`; } else { // 지역성이 있는 이름 const region = faker.location.state(); vendorName = `${region} ${faker.company.name()}`; } return { vendorCode: `VEN${String(index + 1).padStart(5, "0")}`, vendorName, businessNumber: faker.helpers.replaceSymbols("##-###-#####"), representativeName: faker.person.fullName(), businessType, vendorType, status: faker.helpers.arrayElement(["ACTIVE", "INACTIVE", "BLACKLISTED"]), country, address: faker.location.streetAddress(), city: faker.location.city(), state: faker.location.state(), zipCode: faker.location.zipCode(), contact1Name: faker.person.fullName(), contact1Email: faker.internet.email(), contact1Phone: faker.phone.number(), contact2Name: Math.random() > 0.5 ? faker.person.fullName() : null, contact2Email: Math.random() > 0.5 ? faker.internet.email() : null, contact2Phone: Math.random() > 0.5 ? faker.phone.number() : null, description: faker.company.catchPhrase(), website: Math.random() > 0.3 ? faker.internet.url() : null, foundedYear: faker.number.int({ min: 1950, max: 2023 }), employeeCount: faker.number.int({ min: 10, max: 10000 }), annualRevenue: faker.number.float({ min: 1000000, max: 1000000000, fractionDigits: 2 }), certifications: Math.random() > 0.5 ? faker.helpers.arrayElements( ["ISO 9001", "ISO 14001", "OHSAS 18001", "ISO/TS 16949", "AS9100"], faker.number.int({ min: 1, max: 3 }) ) : null, }; } export async function seedVendors(input: { count: number }) { try { const allVendors = []; // 현재 가장 큰 vendorCode 찾기 const [lastVendor] = await db.select() .from(vendors) .orderBy(desc(vendors.vendorCode)) .limit(1); const startIndex = lastVendor?.vendorCode ? parseInt(lastVendor.vendorCode.replace('VEN', '')) : 0; console.log("📝 Inserting vendors", { lastVendorCode: lastVendor?.vendorCode, startIndex }); for (let i = 0; i < input.count; i++) { allVendors.push(await generateVendor(startIndex + i)); } console.log("📝 Inserting vendors", allVendors.length); const result = await db.insert(vendors) .values(allVendors) .onConflictDoNothing() .returning(); console.log(`✅ Successfully added ${result.length} new vendors`); return result; } catch (err) { console.error("Failed to seed vendors:", err); throw err; } }