diff options
Diffstat (limited to 'db/seeds_2')
| -rw-r--r-- | db/seeds_2/poaSeed.ts | 109 |
1 files changed, 109 insertions, 0 deletions
diff --git a/db/seeds_2/poaSeed.ts b/db/seeds_2/poaSeed.ts new file mode 100644 index 00000000..d93cde14 --- /dev/null +++ b/db/seeds_2/poaSeed.ts @@ -0,0 +1,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) + }) +}
\ No newline at end of file |
