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