summaryrefslogtreecommitdiff
path: root/db/seeds_2/vendorSeed.ts
blob: 98e0a98b7ba85092bf70149e16a19c1c49cb5361 (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
// 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;
  }
}