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)
})
}
|