summaryrefslogtreecommitdiff
path: root/db/seeds_2/poaSeed.ts
blob: d93cde1475ca1f3f509a63bf7f80c0c87b17eb18 (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
import { faker } from "@faker-js/faker"
import db from "../db"
import { contracts, poa } from "../schema/contract"
import { sql } from "drizzle-orm"

export async function seedPOA({ count = 10 } = {}) {
    try {
        console.log(`๐Ÿ“ Inserting POA ${count}`)
        
        // ๊ธฐ์กด POA ๋ฐ์ดํ„ฐ ์‚ญ์ œ ๋ฐ ID ์‹œํ€€์Šค ์ดˆ๊ธฐํ™”
        await db.delete(poa)
        await db.execute(sql`ALTER SEQUENCE poa_id_seq RESTART WITH 1;`)
        console.log("โœ… ๊ธฐ์กด POA ๋ฐ์ดํ„ฐ ์‚ญ์ œ ๋ฐ ID ์ดˆ๊ธฐํ™” ์™„๋ฃŒ")
        
        // ์กฐ์„ ์—… ๋งฅ๋ฝ์— ๋งž๋Š” ์˜ˆ์‹œ ๋ฌธ๊ตฌ๋“ค
        const deliveryTermsExamples = [
            "FOB ๋ถ€์‚ฐํ•ญ",
            "CIF ์ƒํ•˜์ดํ•ญ",
            "DAP ์šธ์‚ฐ์กฐ์„ ์†Œ",
            "DDP ๊ฑฐ์ œ ์˜ฅํฌ์กฐ์„ ์†Œ",
        ]
        const deliveryLocations = [
            "๋ถ€์‚ฐ ์˜๋„์กฐ์„ ์†Œ",
            "์šธ์‚ฐ ๋ณธ์‚ฌ ๋„ํฌ #3",
            "๊ฑฐ์ œ ์˜ฅํฌ์กฐ์„ ์†Œ ํ•ด์–‘๊ณต์žฅ",
            "๋ชฉํฌ์‹ ํ•ญ ๋ถ€๋‘",
        ]
        const changeReasonExamples = [
            "๋‚ฉํ’ˆ ์ผ์ • ์กฐ์ • ํ•„์š”",
            "์ž์žฌ ์‚ฌ์–‘ ๋ณ€๊ฒฝ",
            "์„ ๋ฐ• ์„ค๊ณ„ ๋ณ€๊ฒฝ์— ๋”ฐ๋ฅธ ์ˆ˜์ •",
            "์ถ”๊ฐ€ ๋ถ€ํ’ˆ ์š”์ฒญ",
            "๋‚ฉํ’ˆ ์žฅ์†Œ ๋ณ€๊ฒฝ",
            "๊ณ„์•ฝ ์กฐ๊ฑด ์žฌํ˜‘์ƒ"
        ]
        
        // 1. ๊ธฐ์กด ๊ณ„์•ฝ(PO) ๋ชฉ๋ก ๊ฐ€์ ธ์˜ค๊ธฐ
        const existingContracts = await db.select().from(contracts)
        console.log(`Found ${existingContracts.length} existing contracts`)
        
        if (existingContracts.length === 0) {
            throw new Error("๊ณ„์•ฝ(PO) ๋ฐ์ดํ„ฐ๊ฐ€ ์—†์Šต๋‹ˆ๋‹ค. ๋จผ์ € ๊ณ„์•ฝ ๋ฐ์ดํ„ฐ๋ฅผ ์ƒ์„ฑํ•ด์ฃผ์„ธ์š”.")
        }

        // 2. POA ์ƒ์„ฑ
        for (let i = 0; i < count; i++) {
            try {
                // ๋žœ๋ค์œผ๋กœ ์›๋ณธ ๊ณ„์•ฝ ์„ ํƒ
                const originalContract = faker.helpers.arrayElement(existingContracts)
                console.log(`Selected original contract: ${originalContract.contractNo}`)

                // POA ์ƒ์„ฑ
                const totalAmount = faker.number.float({ min: 5000000, max: 500000000 })
                const discount = faker.helpers.maybe(() => faker.number.float({ min: 0, max: 500000 }), { probability: 0.3 })
                const tax = faker.helpers.maybe(() => faker.number.float({ min: 0, max: 1000000 }), { probability: 0.8 })
                const shippingFee = faker.helpers.maybe(() => faker.number.float({ min: 0, max: 300000 }), { probability: 0.5 })
                const netTotal = totalAmount - (discount || 0) + (tax || 0) + (shippingFee || 0)

                const poaData = {
                    // Form code๋Š” ์›๋ณธ๊ณผ ๋™์ผํ•˜๊ฒŒ ์œ ์ง€
                    contractNo: originalContract.contractNo,
                    originalContractNo: originalContract.contractNo,
                    projectId: originalContract.projectId,
                    vendorId: originalContract.vendorId,
                    originalContractName: originalContract.contractName,
                    originalStatus: originalContract.status,

                    // ๋ณ€๊ฒฝ ๊ฐ€๋Šฅํ•œ ์ •๋ณด๋“ค
                    deliveryTerms: faker.helpers.arrayElement(deliveryTermsExamples),
                    deliveryDate: faker.helpers.maybe(() => faker.date.future().toISOString(), { probability: 0.7 }),
                    deliveryLocation: faker.helpers.arrayElement(deliveryLocations),
                    currency: "KRW",
                    totalAmount: totalAmount.toString(),
                    discount: discount?.toString(),
                    tax: tax?.toString(),
                    shippingFee: shippingFee?.toString(),
                    netTotal: netTotal.toString(),
                    changeReason: faker.helpers.arrayElement(changeReasonExamples),
                    approvalStatus: faker.helpers.arrayElement(["PENDING", "APPROVED", "REJECTED"]),
                    createdAt: new Date(),
                    updatedAt: new Date(),
                }
                
                console.log("POA data:", poaData)
                
                await db.insert(poa).values(poaData)
                console.log(`Created POA for contract: ${originalContract.contractNo}`)
            } catch (error) {
                console.error(`Error creating POA ${i + 1}:`, error)
                throw error
            }
        }

        console.log(`โœ… Successfully added ${count} new POAs`)
    } catch (error) {
        console.error("Error in seedPOA:", error)
        throw error
    }
}

// ์‹คํ–‰
if (require.main === module) {
    seedPOA({ count: 5 })
        .then(() => process.exit(0))
        .catch((error) => {
            console.error(error)
            process.exit(1)
        })
}